gdritter repos telml / 8257889
Clarified tag names in formal grammar and code Getty Ritter 9 years ago
2 changed file(s) with 13 addition(s) and 10 deletion(s). Collapse all Expand all
22
33 module Data.TeLML.Parser (Fragment(..), Document, parse) where
44
5 import Data.Char (isAlpha, isSpace)
5 import Data.Char (isAlpha, isAlphaNum, isSpace)
66 import Data.TeLML.Type
77
88 type Result a = Either String (String, a)
5252
5353 -- Parse a tag name of length >= 0.
5454 pTagName :: Parse String
55 pTagName s = go s `bind` ensureLen
55 pTagName s = go s `bind` ensureName
5656 where go i@(x:xs)
57 | isAlpha x = (x:) `over` go xs
58 | elem x "-_" = (x:) `over` go xs
59 | otherwise = return (i, "")
57 | isAlphaNum x = (x:) `over` go xs
58 | elem x "-_" = (x:) `over` go xs
59 | otherwise = return (i, "")
6060 go [] = throw "unexpected end-of-document while parsing tag"
61 ensureLen (xs, name)
62 | length name > 0 = return (xs, name)
63 | otherwise = throw $ "expected tag name after `\\': " ++ show (name, xs)
61 ensureName (xs, name)
62 | length name == 0 =
63 throw "expected tag name after `\\'"
64 | not (isAlpha (head name)) =
65 throw "tag names must begin with an alphabetic character"
66 | otherwise = return (xs, name)
6467
6568 -- Skip any space charaters, returning () for the first non-space
6669 -- character (including EOF).
5353 <document> ::= <fragment>*
5454 <fragment> ::= <tag> | <text> | "{" <document> "}"
5555
56 <text> ::= /[^\]|\\[\{}|]*/
56 <text> ::= /[^\]|[\][\{}|]*/
5757
5858 <tag> ::= "\" <tagname> <spaces> "{" <arglist> "}"
5959 <tagname> ::= /[A-Za-z][A-Za-z0-9_-]*/
60 <arglist> ::= document ("|" document)*
60 <arglist> ::= <document> ("|" <document>)*
6161 <spaces> ::= /[ \t\r\n]*/
6262 ~~~~
6363