Commit fd6e3e79 authored by Michael Hanus's avatar Michael Hanus
Browse files

Format parser produces code for library Data.Format

parent fd88de19
...@@ -150,14 +150,15 @@ one can specify the desired format with foreign code marked by ...@@ -150,14 +150,15 @@ one can specify the desired format with foreign code marked by
similarly to the \code{printf} statement of C, similarly to the \code{printf} statement of C,
followed by a comma-separated list of arguments. followed by a comma-separated list of arguments.
This format specification is transformed into operations This format specification is transformed into operations
of the \CYS library \code{Format} so that it must be imported. of the library \code{Data.Format} (of package \code{printf})
so that it must be imported.
For instance, the following program defines an operation For instance, the following program defines an operation
that formats a string, an integer (with leading sign and zeros), that formats a string, an integer (with leading sign and zeros),
and a float with leading sign and precision 3: and a float with leading sign and precision 3:
\begin{currynomath} \begin{currynomath}
{-# OPTIONS_CYMAKE -F --pgmF=currypp --optF=foreigncode #-} {-# OPTIONS_CYMAKE -F --pgmF=currypp --optF=foreigncode #-}
import Format import Data.Format
showSIF :: String -> Int -> Float -> String showSIF :: String -> Int -> Float -> String
showSIF s i f = ``format "Name: %s | %+.5i | %+6.3f",s,i,f'' showSIF s i f = ``format "Name: %s | %+.5i | %+6.3f",s,i,f''
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
"currypath" : ">= 0.0.1", "currypath" : ">= 0.0.1",
"fl-parser" : ">= 1.0.0", "fl-parser" : ">= 1.0.0",
"html" : ">= 2.0.0", "html" : ">= 2.0.0",
"printf" : ">= 0.0.1",
"regexp" : ">= 1.1.0", "regexp" : ">= 1.1.0",
"wl-pprint" : ">= 0.0.1", "wl-pprint" : ">= 0.0.1",
"xml" : ">= 2.0.0" "xml" : ">= 2.0.0"
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
-- Example for using integrated code, default rules, and contracts in one -- Example for using integrated code, default rules, and contracts in one
-- module -- module
import Format import Data.Format
import Test.Prop import Test.Prop
showInt i = ``format "%+.3d",i'' showInt i = ``format "%+.3d",i''
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
--- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html> --- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
import Format -- required in the pre-processed program import Data.Format -- required in the pre-processed program
-- Format a string and an integer: -- Format a string and an integer:
ex1 :: String -> Int -> IO () ex1 :: String -> Int -> IO ()
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
import RegExp import RegExp
import Format import Data.Format
isEmail :: String -> Bool isEmail :: String -> Bool
isEmail s = s ``regex isEmail s = s ``regex
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
--- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html> --- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
import Format -- required in the pre-processed program import qualified Data.Format -- required in the pre-processed program
import Test.EasyCheck import Test.EasyCheck
-- Format a string and an integer: -- Format a string and an integer:
......
...@@ -20,13 +20,13 @@ ...@@ -20,13 +20,13 @@
--- For explanation on semantics see --- For explanation on semantics see
--- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html> --- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>
--- ---
--- __For further informations see the Format library. --- __For further informations see the library `Data.Format` of package
--- Not all parsable expressions are usable.__ --- `printf`. Not all parsable expressions are usable.__
--- ---
--- @author Jasper Sikorra (with changes by Michael Hanus) --- @author Jasper Sikorra (with changes by Michael Hanus)
--- @version November 2014 --- @version December 2018
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
module FormatParser(parse) where module FormatParser ( parse ) where
import Parser import Parser
import Char import Char
...@@ -68,9 +68,9 @@ isVarStartLetter c = c /= ',' ...@@ -68,9 +68,9 @@ isVarStartLetter c = c /= ','
isVarInnerLetter :: Char -> Bool isVarInnerLetter :: Char -> Bool
isVarInnerLetter c = c /= ',' isVarInnerLetter c = c /= ','
--- Map each type on a function in the Format library --- Map each type to a function in the `Data.Format` library
mapTypes :: Char -> String mapTypes :: Char -> String
mapTypes c = "Format." ++ case c of mapTypes c = "Data.Format." ++ case c of
'c' -> "showChar" 'c' -> "showChar"
'd' -> "showInt" 'd' -> "showInt"
'i' -> "showInt" 'i' -> "showInt"
...@@ -86,12 +86,12 @@ mapTypes c = "Format." ++ case c of ...@@ -86,12 +86,12 @@ mapTypes c = "Format." ++ case c of
_ -> error "mapTypes: unknown character" _ -> error "mapTypes: unknown character"
--- The function parses and converts a String that is in the format of a C-like --- The function parses and converts a String that is in the format of a C-like
--- printf expression into a Curry Expression that makes use of the Format --- printf expression into a Curry Expression that makes use of the
--- library. --- `Data.Format` library.
--- @param showfun - The operation to be applied to the formatted string result --- @param showfun - The operation to be applied to the formatted string result
--- @param pos - The position of the expression in the original file --- @param pos - The position of the expression in the original file
--- @param exp - The expression which should be converted --- @param exp - The expression which should be converted
--- @return A String in Curry Syntax matching exp using the Format --- @return A String in Curry Syntax matching exp using the `Data.Format`
--- library --- library
parse :: String -> LangParser parse :: String -> LangParser
parse showfun p s = do parse showfun p s = do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment