Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/Parser/mdl.lex

raw
1 exception Error
2
3 type pos = int
4 type svalue = Tokens.svalue
5 type ('a,'b) token = ('a,'b) Tokens.token
6 type lexresult = (svalue,pos) token
7 type lexarg = {srcMap : SourceMapping.sourcemap,
8 err : pos * pos * string -> unit,
9 MDLmode : bool
10 }
11 type arg = lexarg
12
13 open Tokens
14
15 val commentLevel = ref 0
16 val metaLevel = ref 0
17
18 val asmLQuote = ref "``"
19 val asmRQuote = ref "''"
20 val asmLMeta = ref "<"
21 val asmRMeta = ref ">"
22
23 exception Error
24
25 fun init() = (commentLevel := 0; metaLevel := 0;
26 asmLQuote := "``"; asmRQuote := "''";
27 asmLMeta := "<"; asmRMeta := ">"
28 )
29
30 fun eof{srcMap,err,MDLmode} =
31 let val pos = SourceMapping.currPos srcMap
32 in EOF(pos,pos) end
33 fun debug _ = ()
34
35 fun check(err,_,_,SOME w) = w
36 | check(err,pos,s,NONE) =
37 (err(pos,pos+size s,"bad literal "^s); raise Error)
38
39 fun strip k s = String.substring(s,k,String.size s - k)
40 fun scan err fmt (s,s') tok pos =
41 tok(check(err,pos,s,StringCvt.scanString fmt s'),
42 pos,pos + size s)
43 handle _ => ID(s,pos,pos)
44
45 fun wdecimal(err,s,pos) =
46 scan err (Word32.scan StringCvt.DEC) (s,strip 2 s) WORD pos
47 fun whex(err,s,pos) =
48 scan err (Word32.scan StringCvt.HEX) (s,strip 3 s) WORD pos
49 fun woctal(err,s,pos) = scan err (Word32.scan StringCvt.OCT) (s,strip 3 s) WORD pos
50 fun wbinary(err,s,pos) = scan err (Word32.scan StringCvt.BIN) (s,strip 3 s) WORD pos
51 fun decimal(err,s,pos) = scan err (Int.scan StringCvt.DEC) (s,s) INT pos
52 fun real(err,s,pos) = scan err (Real.scan) (s,s)
53 (fn (x,y,z) => REAL(Real.toString x, y, z)) pos
54 fun hex(err,s,pos) = scan err (Int.scan StringCvt.HEX) (s,strip 2 s) INT pos
55 fun octal(err,s,pos) = scan err (Int.scan StringCvt.OCT) (s,strip 2 s) INT pos
56 fun binary(err,s,pos) = scan err (Int.scan StringCvt.BIN) (s,strip 2 s) INT pos
57
58 fun decimalinf(err,s,pos) = scan err (IntInf.scan StringCvt.DEC) (s,s) INTINF pos
59 fun hexinf(err,s,pos) = scan err (IntInf.scan StringCvt.HEX) (s,strip 2 s) INTINF pos
60 fun octalinf(err,s,pos) = scan err (IntInf.scan StringCvt.OCT) (s,strip 2 s) INTINF pos
61 fun binaryinf(err,s,pos) = scan err (IntInf.scan StringCvt.BIN) (s,strip 2 s) INTINF pos
62
63 fun string(err,s,pos) =
64 STRING(
65 check(err,pos,s,String.fromString(String.substring(s,1,String.size s-2))),
66 pos, pos + size s)
67 fun char(err,s,pos) =
68 CHAR(check(err,pos,s,Char.fromString(String.substring(s,2,String.size s-3))),
69 pos,pos + size s)
70 fun transAsm s =
71 let fun loop(#"\\" :: #"<" ::s) = #"<"::loop s
72 | loop(#"\\" :: #">" ::s) = #">"::loop s
73 | loop(c::s) = c::loop s
74 | loop [] = []
75 in String.implode(loop(String.explode s))
76 end
77
78 fun asmtext(err,s,pos) =
79 ASMTEXT(check(err,pos,s,String.fromString(transAsm s)),pos,pos + size s)
80
81 infix $$
82 fun x $$ y = y :: x
83
84 exception NotFound
85
86 val keywords = HashTable.mkTable (HashString.hashString,op =) (13,NotFound)
87 : (string,int * int -> (svalue,int) token) HashTable.hash_table
88 val MDLkeywords = HashTable.mkTable (HashString.hashString,op =) (13,NotFound)
89 : (string,int * int -> (svalue,int) token) HashTable.hash_table
90 val symbols = HashTable.mkTable (HashString.hashString,op =) (13,NotFound)
91 : (string,int * int -> (svalue,int) token) HashTable.hash_table
92
93 val _ = app (HashTable.insert keywords)
94 ( nil $$
95 ("_",WILD) $$
96 ("datatype", DATATYPE) $$
97 ("type", TYPE) $$
98 ("end", END) $$
99 ("fun", FUN) $$
100 ("fn", FN) $$
101 ("val", VAL) $$
102 ("raise", RAISE) $$
103 ("handle", HANDLE) $$
104 ("let", LET) $$
105 ("local", LOCAL) $$
106 ("exception", EXCEPTION) $$
107 ("structure", STRUCTURE) $$
108 ("signature", SIGNATURE) $$
109 ("functor", FUNCTOR) $$
110 ("sig", SIG) $$
111 ("struct", STRUCT) $$
112 ("sharing", SHARING) $$
113 ("where", WHERE) $$
114 ("withtype", WITHTYPE) $$
115 ("if", IF) $$
116 ("then", THEN) $$
117 ("else", ELSE) $$
118 ("in", IN) $$
119 ("true", TRUE) $$
120 ("false", FALSE) $$
121 ("and", AND) $$
122 ("at", AT) $$
123 ("of", OF) $$
124 ("case", CASE) $$
125 ("as", AS) $$
126 ("open", OPEN) $$
127 ("op", OP) $$
128 ("include", INCLUDE) $$
129 ("infix", INFIX) $$
130 ("infixr", INFIXR) $$
131 ("nonfix", NONFIX) $$
132 ("not", NOT)
133 )
134
135 val _ = app (HashTable.insert MDLkeywords)
136 ( nil $$
137 ("architecture", ARCHITECTURE) $$
138 ("assembly", ASSEMBLY) $$
139 ("storage", STORAGE) $$
140 ("locations", LOCATIONS) $$
141 ("equation", EQUATION) $$
142 ("at", AT) $$
143 ("vliw", VLIW) $$
144 ("field", FIELD) $$
145 ("fields", FIELDS) $$
146 ("signed", SIGNED) $$
147 ("unsigned", UNSIGNED) $$
148 ("superscalar", SUPERSCALAR) $$
149 ("bits", BITS) $$
150 ("ordering", ORDERING) $$
151 ("little", LITTLE) $$
152 ("big", BIG) $$
153 ("endian", ENDIAN) $$
154 ("register", REGISTER) $$
155 ("as", AS) $$
156 ("cell", CELL) $$
157 ("cells", CELLS) $$
158 ("cellset", CELLSET) $$
159 ("pipeline", PIPELINE) $$
160 ("cpu", CPU) $$
161 ("resource", RESOURCE) $$
162 ("reservation", RESERVATION) $$
163 ("table", TABLE) $$
164 ("latency", LATENCY) $$
165 ("predicated", PREDICATED) $$
166 ("instruction", INSTRUCTION) $$
167 ("formats", FORMATS) $$
168 ("uppercase", UPPERCASE) $$
169 ("lowercase", LOWERCASE) $$
170 ("verbatim", VERBATIM) $$
171 ("span", SPAN) $$
172 ("dependent", DEPENDENT) $$
173 ("always", ALWAYS) $$
174 ("never", NEVER) $$
175 ("delayslot", DELAYSLOT) $$
176 (* ("branching", BRANCHING) $$ *)
177 ("candidate", CANDIDATE) $$
178 ("rtl", RTL) $$
179 ("debug", DEBUG) $$
180 ("aliasing", ALIASING) $$
181 ("aggregable",AGGREGABLE)
182 )
183
184 val _ = app (HashTable.insert symbols)
185 (
186 nil $$
187 ("=", EQ) $$
188 ("*", TIMES) $$
189 (":", COLON) $$
190 (":>",COLONGREATER) $$
191 ("|", BAR) $$
192 ("->", ARROW) $$
193 ("=>", DARROW) $$
194 ("#", HASH) $$
195 ("!", DEREF) $$
196 ("^^", CONCAT)
197 )
198
199 fun lookup(MDLmode,s,yypos) =
200 let val l = String.size s
201 fun id() = ID(UniqueSymbol.toString
202 (UniqueSymbol.fromString s), yypos, yypos + l)
203 in HashTable.lookup keywords s (yypos,yypos + l)
204 handle _ =>
205 (if MDLmode then
206 (HashTable.lookup MDLkeywords s (yypos,yypos + l) handle _ => id())
207 else id()
208 )
209 end
210
211 fun lookupSym(s,yypos) =
212 let val l = String.size s
213 in HashTable.lookup symbols s (yypos,yypos + l)
214 handle _ => SYMBOL(UniqueSymbol.toString
215 (UniqueSymbol.fromString s), yypos, yypos + l)
216 end
217
218 %%
219
220 %header (functor MDLLexFun(Tokens : MDL_TOKENS));
221 %arg ({srcMap,err,MDLmode});
222 %reject
223
224 alpha=[A-Za-z];
225 digit=[0-9];
226 id=[A-Za-z_][A-Za-z0-9_\']*;
227 tyvar=\'{id};
228 decimal={digit}+;
229 integer=~?{decimal};
230 real={integer}\.{decimal}(e{integer})?;
231 octal=0[0-7]+;
232 hex=0x[0-9a-fA-F]+;
233 binary=0b[0-1]+;
234 wdecimal=0w{digit}+;
235 woctal=0w0[0-7]+;
236 whex=0wx[0-9a-fA-F]+;
237 wbinary=0wb[0-1]+;
238 ws=[\ \t];
239 string=\"([^\\\n\t"]|\\.)*\";
240 char=#\"([^\\\n\t"]|\\.)*\";
241 sym1=(\-|[=\.+~/*:!@#$%^&*|?])+;
242 sym2=`+|'+|\<+|\>+|\=\>|~\>\>;
243 sym3=\\.;
244 asmsymbol={sym1}|{sym2}|{sym3};
245 symbol=(\-|[=+~/*:!@#$%^&*|?<>])+|``|'';
246 asmtext=([^\n\t<>']+|');
247 inf=i;
248
249 %s COMMENT ASM ASMQUOTE;
250
251 %%
252 <INITIAL,COMMENT,ASM>\n => (SourceMapping.newline srcMap yypos; continue());
253 <INITIAL,COMMENT,ASM>{ws} => (continue());
254 <ASMQUOTE>\n => (err(yypos,yypos+size yytext,
255 "newline in assembly text!"); continue());
256 <INITIAL>"(*" => (commentLevel := 1; YYBEGIN COMMENT; continue());
257 <INITIAL,ASM>{integer} => (decimal(err,yytext,yypos));
258 <INITIAL,ASM>{hex} => (hex(err,yytext,yypos));
259 <INITIAL,ASM>{octal} => (octal(err,yytext,yypos));
260 <INITIAL,ASM>{binary} => (binary(err,yytext,yypos));
261 <INITIAL,ASM>{integer}{inf} => (decimalinf(err,yytext,yypos));
262 <INITIAL,ASM>{hex}{inf} => (hexinf(err,yytext,yypos));
263 <INITIAL,ASM>{octal}{inf} => (octalinf(err,yytext,yypos));
264 <INITIAL,ASM>{binary}{inf} => (binaryinf(err,yytext,yypos));
265 <INITIAL,ASM>{wdecimal} => (wdecimal(err,yytext,yypos));
266 <INITIAL,ASM>{whex} => (whex(err,yytext,yypos));
267 <INITIAL,ASM>{woctal} => (woctal(err,yytext,yypos));
268 <INITIAL,ASM>{wbinary} => (wbinary(err,yytext,yypos));
269 <INITIAL,ASM>{string} => (string(err,yytext,yypos));
270 <INITIAL,ASM>{char} => (char(err,yytext,yypos));
271 <INITIAL,ASM>{real} => (real(err,yytext,yypos));
272 <INITIAL,ASM>"$" => (if MDLmode then DOLLAR(yypos,yypos+1)
273 else SYMBOL("$",yypos,yypos+1));
274 <INITIAL,ASM>"asm:" => (if MDLmode then
275 ASM_COLON(yypos,yypos+size yytext) else REJECT());
276 <INITIAL,ASM>"mc:" => (if MDLmode then
277 MC_COLON(yypos,yypos+size yytext) else REJECT());
278 <INITIAL,ASM>"rtl:" => (if MDLmode then
279 RTL_COLON(yypos,yypos+size yytext) else REJECT());
280 <INITIAL,ASM>"delayslot:" => (if MDLmode then
281 DELAYSLOT_COLON(yypos,size yytext) else REJECT());
282 <INITIAL,ASM>"padding:" => (if MDLmode then
283 PADDING_COLON(yypos,size yytext) else REJECT());
284 <INITIAL,ASM>"nullified:" => (if MDLmode then
285 NULLIFIED_COLON(yypos,size yytext) else REJECT());
286 <INITIAL,ASM>"candidate:" => (if MDLmode then
287 CANDIDATE_COLON(yypos,size yytext) else REJECT());
288 <INITIAL,ASM>{id} => (lookup(MDLmode,yytext,yypos));
289 <INITIAL,ASM>{tyvar} => (TYVAR(yytext,yypos,yypos + size yytext));
290 <INITIAL,ASM>"(" => (LPAREN(yypos,yypos+1));
291 <INITIAL,ASM>")" => (RPAREN(yypos,yypos+1));
292 <INITIAL,ASM>"[" => (LBRACKET(yypos,yypos+1));
293 <INITIAL,ASM>"#[" => (LHASHBRACKET(yypos,yypos+1));
294 <INITIAL,ASM>"]" => (RBRACKET(yypos,yypos+1));
295 <INITIAL,ASM>"{" => (LBRACE(yypos,yypos+1));
296 <INITIAL,ASM>"}" => (RBRACE(yypos,yypos+1));
297 <INITIAL,ASM>"," => (COMMA(yypos,yypos+1));
298 <INITIAL,ASM>";" => (SEMICOLON(yypos,yypos+1));
299 <INITIAL,ASM>"." => (DOT(yypos,yypos+1));
300 <INITIAL,ASM>".." => (DOTDOT(yypos,yypos+2));
301 <INITIAL,ASM>"..." => (DOTDOT(yypos,yypos+3));
302 <INITIAL>{symbol} => (if yytext = !asmLQuote then
303 (debug("lquote "^yytext^"\n");
304 YYBEGIN ASMQUOTE;
305 LDQUOTE(yypos,yypos+size yytext))
306 else
307 lookupSym(yytext,yypos));
308 <ASMQUOTE>{asmsymbol} => (if yytext = !asmRQuote then
309 (if !metaLevel <> 0 then
310 err(yypos,yypos+size yytext,
311 "Mismatch between "^(!asmLMeta)^
312 " and "^(!asmRMeta)) else ();
313 debug("rquote "^yytext^"\n");
314 YYBEGIN INITIAL;
315 RDQUOTE(yypos,yypos+size yytext))
316 else if yytext = !asmLMeta then
317 (metaLevel := !metaLevel + 1;
318 debug("lmeta "^yytext^"\n");
319 YYBEGIN ASM; LMETA(yypos,yypos+size yytext))
320 else
321 asmtext(err,yytext,yypos));
322 <ASM>{asmsymbol} => (if yytext = !asmRMeta then
323 (metaLevel := !metaLevel - 1;
324 debug("rmeta "^yytext^"("^Int.toString(!metaLevel)^")\n");
325 if !metaLevel = 0 then YYBEGIN ASMQUOTE
326 else (); RMETA(yypos,yypos+size yytext))
327 else
328 lookupSym(yytext,yypos));
329 <ASMQUOTE>{asmtext} => (debug("text="^yytext^"\n");
330 asmtext(err,yytext,yypos));
331 <COMMENT>"*)" => (commentLevel := !commentLevel - 1;
332 if !commentLevel = 0 then YYBEGIN INITIAL else ();
333 continue());
334 <COMMENT>"(*" => (commentLevel := !commentLevel + 1; continue());
335 <COMMENT>. => (continue());
336 . => (err(yypos,yypos+size yytext,
337 "unknown character "^String.toString yytext);
338 continue());