git @ Cat's Eye Technologies Fountain / 8feef68
Introduce a syntax for comments. Chris Pressey 1 year, 10 months ago
4 changed file(s) with 25 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
6565
6666 ### Syntax
6767
68 * A syntax for comments.
6968 * A syntax for terminals so that `"` can be given as a terminal.
7069 Probably any unicode code point by its hex.
7170
99 -------------------
1010
1111 This grammar is written in EBNF. Any amount of whitespace may occur
12 between tokens (and for this purpose, comments count as whitespace).
12 between tokens (and for this purpose, comments, which are introduced
13 by `//` and extend until the end of the line, count as whitespace).
1314 Some whitespace must appear between tokens if the tokens would otherwise
1415 be interpreted as a single token. The bottommost productions in the
1516 grammar describe the concrete structure of tokens.
1919 Goal ::= "f" "o" "o";
2020 ===> Grammar [("Goal",[],Alt [Seq [Terminal 'f',Terminal 'o',Terminal 'o']])]
2121
22 Multi-character terminal.
22 Multi-character terminal and whitespace and comments.
2323
24 Goal ::= "foo";
24 // This is my grammar.
25
26 Goal ::= "foo"; // You see
27 // how it is
2528 ===> Grammar [("Goal",[],Alt [Seq [Seq [Terminal 'f',Terminal 'o',Terminal 'o']]])]
2629
2730 Alternation and recursion.
77
88
99 fountain = do
10 fspaces
1011 ps <- many prod
1112 return (Grammar ps)
1213
117118
118119 keyword s = do
119120 try (string s)
120 spaces
121 fspaces
121122
122123 capWord = do
123124 c <- upper
124125 s <- many (alphaNum)
125 spaces
126 fspaces
126127 return (c:s)
127128
128129 lowWord = do
129130 c <- lower
130131 s <- many (alphaNum)
131 spaces
132 fspaces
132133 return (c:s)
133134
134135 intlit = do
135136 c <- digit
136137 cs <- many digit
137138 num <- return (read (c:cs) :: Integer)
138 spaces
139 fspaces
139140 return num
140141
141142 quotedString = do
142143 c1 <- char '"'
143144 s <- many $ satisfy (\x -> x /= '"')
144145 c2 <- char '"'
146 fspaces
147 return s
148
149 fspaces = do
145150 spaces
146 return s
151 many comment
152 return ()
153
154 comment = do
155 keyword "//"
156 many $ satisfy (\x -> x /= '\n')
157 (do { char '\n'; return ()} <|> eof)
158 fspaces
159 return ()
147160
148161 --
149162 -- Driver