@@ -8,11 +8,11 @@ discussed how to handle whitespace and comments.
9In some grammars whitespace and comments would not be considered significant and
9In some grammars whitespace and comments would not be considered significant and
10so we might be tempted not to generate any tokens for them. However, in the
10so we might be tempted not to generate any tokens for them. However, in the
11−running example, we may want to make spaces significant. For example, we might
11+running example, we may want to make spaces significant in the future. For
12−want to implement function application expressed by juxtaposition as in
12+example, we might want to implement function application expressed by
13−Haskell and Idris like this: ``f x``.
13+juxtaposition as in Haskell and Idris like this: ``f x``.
15−Also, I'm not sure if it is an problem for error messages and so on if some
15+Also, I'm not sure if it would be problem for error messages and so on if some
16text does not have corresponding tokens.
16text does not have corresponding tokens.
18So, on this page, we will add a token for whitespace and comments.
18So, on this page, we will add a token for whitespace and comments.
@@ -85,42 +85,68 @@ Now that there are ``Comment`` tokens the parser needs to be able to handle them
85So far we don't have any syntax that requires spaces to be significant so we
85So far we don't have any syntax that requires spaces to be significant so we
86need to define the grammar so that it will parse with, or without, spaces.
86need to define the grammar so that it will parse with, or without, spaces.
87This needs to be done in a systematic way, here I have defined the grammar so
87This needs to be done in a systematic way, here I have defined the grammar so
88−that there is an optional space to the right of every atom or operator:
88+that there is an optional space to the right of every atom or operator.
89+First we add versions of ``intLiteral`` , ``openParen`` , ``closeParen``
90+and ``op`` that allow optional spaces/comments to the right of them:
90.. code-block:: idris
92.. code-block:: idris
92− partial
94+ intLiteralC : Rule Integer
95+ intLiteralC = (intLiteral <* commentSpace) <|> intLiteral
93− exprAtom : Rule Integer
97+ openParenC : Rule Integer
94− exprAtom = (intLiteral <* commentSpace)
98+ openParenC = (openParen <* commentSpace) <|> openParen
95− <|> intLiteral <|> (paren expr)
99+
96−
100+ closeParenC : Rule Integer
97− partial
101+ closeParenC = (closeParen <* commentSpace) <|> closeParen
103+ ||| like op but followed by optional comment or space
104+ opC : String -> Rule Integer
105+ opC s = ((op s) <* commentSpace) <|> (op s)
107+Then we just use these functions instead of the original functions:
109+.. code-block:: idris
98− expr1 : Rule Integer
113+ factor : Rule Integer
114+ factor = intLiteralC <|> do
99− expr1 = map multInt exprAtom <*> (
121+ term = map multInt factor <*> (
100− (((op "*") <* commentSpace) <|> (op "*"))
122+ (opC "*")
101− *> exprAtom)
123+ *> factor)
104− exprMult : Rule Integer
105− exprMult = expr1 <|> exprAtom
108− expr2 : Rule Integer
109− expr2 = map addInt exprMult <*> (
126+ expr = map addInt term <*> (
110− (((op "+") <* commentSpace) <|> (op "+"))
127+ (opC "+")
111− *> exprMult)
128+ *> term)
114− exprAdd : Rule Integer
115− exprAdd = expr2 <|> exprMult
118− expr3 : Rule Integer
119− expr3 = map subInt exprAdd <*> (
129+ <|> map subInt term <*> (
120− (((op "-") <* commentSpace) <|> (op "-"))
130+ (opC "-")
121− *> expr)
131+ *> term)
123− expr = expr3 <|> exprAdd
133+
134+ calc : String -> Either (ParseError (TokenData ExpressionToken))
135+ (Integer, List (TokenData ExpressionToken))
136+ calc s = parse expr (fst (lex expressionTokens s))
138+ lft : (ParseError (TokenData ExpressionToken)) -> IO ()
139+ lft (Error s lst) = putStrLn ("error:"++s)
141+ rht : (Integer, List (TokenData ExpressionToken)) -> IO ()
142+ rht i = putStrLn ("right " ++ (show i))
148+ either lft rht (calc x) -- eliminator for Either
125Defining Block Structure using Indents
151Defining Block Structure using Indents
126--------------------------------------
152--------------------------------------