File: mdl.lex

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (338 lines) | stat: -rw-r--r-- 11,262 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
exception Error

type pos = int
type svalue = Tokens.svalue
type ('a,'b) token = ('a,'b) Tokens.token
type lexresult = (svalue,pos) token
type lexarg = {srcMap  : SourceMapping.sourcemap,
               err     : pos * pos * string -> unit,
               MDLmode : bool
              }
type arg = lexarg

open Tokens

val commentLevel = ref 0
val metaLevel = ref 0

val asmLQuote = ref "``"
val asmRQuote = ref "''"
val asmLMeta  = ref "<"
val asmRMeta  = ref ">"

exception Error

fun init() = (commentLevel := 0; metaLevel := 0;
	      asmLQuote := "``"; asmRQuote := "''";
	      asmLMeta := "<"; asmRMeta := ">"
	     )

fun eof{srcMap,err,MDLmode} = 
    let val pos = SourceMapping.currPos srcMap
    in  EOF(pos,pos) end
fun debug _ = ()

fun check(err,_,_,SOME w) = w
  | check(err,pos,s,NONE) = 
      (err(pos,pos+size s,"bad literal "^s); raise Error)

fun strip k s = String.substring(s,k,String.size s - k)
fun scan err fmt (s,s') tok pos = 
      tok(check(err,pos,s,StringCvt.scanString fmt s'),
                pos,pos + size s) 
      handle _ => ID(s,pos,pos)

fun wdecimal(err,s,pos) = 
      scan err (Word32.scan StringCvt.DEC) (s,strip 2 s) WORD pos
fun whex(err,s,pos) = 
      scan err (Word32.scan StringCvt.HEX) (s,strip 3 s) WORD pos
fun woctal(err,s,pos) = scan err (Word32.scan StringCvt.OCT) (s,strip 3 s) WORD pos
fun wbinary(err,s,pos) = scan err (Word32.scan StringCvt.BIN) (s,strip 3 s) WORD pos
fun decimal(err,s,pos) = scan err (Int.scan StringCvt.DEC) (s,s) INT pos
fun real(err,s,pos) = scan err (Real.scan) (s,s) 
                       (fn (x,y,z) => REAL(Real.toString x, y, z)) pos
fun hex(err,s,pos) = scan err (Int.scan StringCvt.HEX) (s,strip 2 s) INT pos
fun octal(err,s,pos) = scan err (Int.scan StringCvt.OCT) (s,strip 2 s) INT pos
fun binary(err,s,pos) = scan err (Int.scan StringCvt.BIN) (s,strip 2 s) INT pos

fun decimalinf(err,s,pos) = scan err (IntInf.scan StringCvt.DEC) (s,s) INTINF pos
fun hexinf(err,s,pos) = scan err (IntInf.scan StringCvt.HEX) (s,strip 2 s) INTINF pos
fun octalinf(err,s,pos) = scan err (IntInf.scan StringCvt.OCT) (s,strip 2 s) INTINF pos
fun binaryinf(err,s,pos) = scan err (IntInf.scan StringCvt.BIN) (s,strip 2 s) INTINF pos

fun string(err,s,pos) = 
  STRING(
    check(err,pos,s,String.fromString(String.substring(s,1,String.size s-2))),
    pos, pos + size s)
fun char(err,s,pos) = 
  CHAR(check(err,pos,s,Char.fromString(String.substring(s,2,String.size s-3))),
       pos,pos + size s)
fun transAsm s = 
let fun loop(#"\\" :: #"<" ::s) = #"<"::loop s
      | loop(#"\\" :: #">" ::s) = #">"::loop s
      | loop(c::s) = c::loop s
      | loop [] = []  
in  String.implode(loop(String.explode s))
end

fun asmtext(err,s,pos) = 
  ASMTEXT(check(err,pos,s,String.fromString(transAsm s)),pos,pos + size s)

infix $$ 
fun x $$ y = y :: x 

exception NotFound

val keywords = HashTable.mkTable (HashString.hashString,op =) (13,NotFound) 
               : (string,int * int -> (svalue,int) token) HashTable.hash_table
val MDLkeywords = HashTable.mkTable (HashString.hashString,op =) (13,NotFound) 
               : (string,int * int -> (svalue,int) token) HashTable.hash_table
val symbols  = HashTable.mkTable (HashString.hashString,op =) (13,NotFound)
               : (string,int * int -> (svalue,int) token) HashTable.hash_table

val _ = app (HashTable.insert keywords) 
( nil       $$
 ("_",WILD) $$
 ("datatype", DATATYPE) $$
 ("type", TYPE) $$
 ("end", END) $$
 ("fun", FUN) $$
 ("fn", FN) $$
 ("val", VAL) $$
 ("raise", RAISE) $$
 ("handle", HANDLE) $$
 ("let", LET) $$
 ("local", LOCAL) $$
 ("exception", EXCEPTION) $$
 ("structure", STRUCTURE) $$
 ("signature", SIGNATURE) $$
 ("functor", FUNCTOR) $$
 ("sig", SIG) $$
 ("struct", STRUCT) $$
 ("sharing", SHARING) $$
 ("where", WHERE) $$
 ("withtype", WITHTYPE) $$
 ("if", IF) $$
 ("then", THEN) $$
 ("else", ELSE) $$
 ("in", IN) $$
 ("true", TRUE) $$
 ("false", FALSE) $$
 ("and", AND) $$
 ("at", AT) $$
 ("of", OF) $$
 ("case", CASE) $$
 ("as", AS) $$
 ("open", OPEN) $$
 ("op", OP) $$
 ("include", INCLUDE) $$
 ("infix", INFIX) $$
 ("infixr", INFIXR) $$
 ("nonfix", NONFIX) $$
 ("not", NOT) 
)

val _ = app (HashTable.insert MDLkeywords) 
( nil $$
 ("architecture", ARCHITECTURE) $$
 ("assembly", ASSEMBLY) $$
 ("storage", STORAGE) $$
 ("locations", LOCATIONS) $$
 ("equation", EQUATION) $$
 ("at", AT) $$
 ("vliw", VLIW) $$
 ("field", FIELD) $$
 ("fields", FIELDS) $$
 ("signed", SIGNED) $$
 ("unsigned", UNSIGNED) $$
 ("superscalar", SUPERSCALAR) $$
 ("bits", BITS) $$
 ("ordering", ORDERING) $$
 ("little", LITTLE) $$
 ("big", BIG) $$
 ("endian", ENDIAN) $$
 ("register", REGISTER) $$
 ("as", AS) $$
 ("cell", CELL) $$
 ("cells", CELLS) $$
 ("cellset", CELLSET) $$
 ("pipeline", PIPELINE) $$
 ("cpu", CPU) $$
 ("resource", RESOURCE) $$
 ("reservation", RESERVATION) $$
 ("table", TABLE) $$
 ("latency", LATENCY) $$
 ("predicated", PREDICATED) $$
 ("instruction", INSTRUCTION) $$
 ("formats", FORMATS) $$
 ("uppercase", UPPERCASE) $$
 ("lowercase", LOWERCASE) $$
 ("verbatim", VERBATIM) $$
 ("span", SPAN) $$
 ("dependent", DEPENDENT) $$
 ("always", ALWAYS) $$
 ("never", NEVER) $$
 ("delayslot", DELAYSLOT) $$
 (* ("branching", BRANCHING) $$ *)
 ("candidate", CANDIDATE) $$
 ("rtl", RTL) $$
 ("debug", DEBUG) $$
 ("aliasing", ALIASING) $$
 ("aggregable",AGGREGABLE) 
)

val _ = app (HashTable.insert symbols) 
(
  nil $$
  ("=",	EQ) $$
  ("*",	TIMES) $$
  (":",	COLON) $$
  (":>",COLONGREATER) $$
  ("|", BAR) $$
  ("->", ARROW) $$
  ("=>", DARROW) $$
  ("#", HASH) $$
  ("!", DEREF) $$
  ("^^", CONCAT)
)

fun lookup(MDLmode,s,yypos) =
let val l = String.size s
    fun id() = ID(UniqueSymbol.toString
                    (UniqueSymbol.fromString s), yypos, yypos + l)
in  HashTable.lookup keywords s (yypos,yypos + l) 
      handle _ => 
        (if MDLmode then 
           (HashTable.lookup MDLkeywords s (yypos,yypos + l) handle _ => id())
         else id()
        )
end

fun lookupSym(s,yypos) =
let val l = String.size s
in  HashTable.lookup symbols s (yypos,yypos + l) 
      handle _ => SYMBOL(UniqueSymbol.toString
                     (UniqueSymbol.fromString s), yypos, yypos + l)
end

%%

%header (functor MDLLexFun(Tokens : MDL_TOKENS));
%arg ({srcMap,err,MDLmode});
%reject

alpha=[A-Za-z];
digit=[0-9];
id=[A-Za-z_][A-Za-z0-9_\']*;
tyvar=\'{id};
decimal={digit}+;
integer=~?{decimal};
real={integer}\.{decimal}(e{integer})?;
octal=0[0-7]+;
hex=0x[0-9a-fA-F]+;
binary=0b[0-1]+;
wdecimal=0w{digit}+;
woctal=0w0[0-7]+;
whex=0wx[0-9a-fA-F]+;
wbinary=0wb[0-1]+;
ws=[\ \t];
string=\"([^\\\n\t"]|\\.)*\";
char=#\"([^\\\n\t"]|\\.)*\";
sym1=(\-|[=\.+~/*:!@#$%^&*|?])+;
sym2=`+|'+|\<+|\>+|\=\>|~\>\>;
sym3=\\.;
asmsymbol={sym1}|{sym2}|{sym3};
symbol=(\-|[=+~/*:!@#$%^&*|?<>])+|``|'';
asmtext=([^\n\t<>']+|');
inf=i;

%s COMMENT ASM ASMQUOTE;

%%
<INITIAL,COMMENT,ASM>\n		=> (SourceMapping.newline srcMap yypos; continue());
<INITIAL,COMMENT,ASM>{ws}	=> (continue());
<ASMQUOTE>\n		=> (err(yypos,yypos+size yytext,
                                "newline in assembly text!"); continue());
<INITIAL>"(*"		=> (commentLevel := 1; YYBEGIN COMMENT; continue());
<INITIAL,ASM>{integer}	=> (decimal(err,yytext,yypos));
<INITIAL,ASM>{hex}	=> (hex(err,yytext,yypos));
<INITIAL,ASM>{octal}	=> (octal(err,yytext,yypos));
<INITIAL,ASM>{binary}	=> (binary(err,yytext,yypos));
<INITIAL,ASM>{integer}{inf}	=> (decimalinf(err,yytext,yypos));
<INITIAL,ASM>{hex}{inf}		=> (hexinf(err,yytext,yypos));
<INITIAL,ASM>{octal}{inf}	=> (octalinf(err,yytext,yypos));
<INITIAL,ASM>{binary}{inf}	=> (binaryinf(err,yytext,yypos));
<INITIAL,ASM>{wdecimal}	=> (wdecimal(err,yytext,yypos));
<INITIAL,ASM>{whex}	=> (whex(err,yytext,yypos));
<INITIAL,ASM>{woctal}	=> (woctal(err,yytext,yypos));
<INITIAL,ASM>{wbinary}	=> (wbinary(err,yytext,yypos));
<INITIAL,ASM>{string}	=> (string(err,yytext,yypos));
<INITIAL,ASM>{char}	=> (char(err,yytext,yypos));
<INITIAL,ASM>{real}	=> (real(err,yytext,yypos));
<INITIAL,ASM>"$"	=> (if MDLmode then DOLLAR(yypos,yypos+1)
                            else SYMBOL("$",yypos,yypos+1));
<INITIAL,ASM>"asm:"     => (if MDLmode then 
                              ASM_COLON(yypos,yypos+size yytext) else REJECT());
<INITIAL,ASM>"mc:"      => (if MDLmode then 
                               MC_COLON(yypos,yypos+size yytext) else REJECT());
<INITIAL,ASM>"rtl:"     => (if MDLmode then 
                               RTL_COLON(yypos,yypos+size yytext) else REJECT());
<INITIAL,ASM>"delayslot:" => (if MDLmode then
                               DELAYSLOT_COLON(yypos,size yytext) else REJECT());
<INITIAL,ASM>"padding:" => (if MDLmode then  
                               PADDING_COLON(yypos,size yytext) else REJECT());
<INITIAL,ASM>"nullified:" => (if MDLmode then
                                NULLIFIED_COLON(yypos,size yytext) else REJECT());
<INITIAL,ASM>"candidate:" => (if MDLmode then  
                                CANDIDATE_COLON(yypos,size yytext) else REJECT());
<INITIAL,ASM>{id}	=> (lookup(MDLmode,yytext,yypos));
<INITIAL,ASM>{tyvar}	=> (TYVAR(yytext,yypos,yypos + size yytext));
<INITIAL,ASM>"("	=> (LPAREN(yypos,yypos+1));
<INITIAL,ASM>")"	=> (RPAREN(yypos,yypos+1));
<INITIAL,ASM>"["	=> (LBRACKET(yypos,yypos+1));
<INITIAL,ASM>"#["	=> (LHASHBRACKET(yypos,yypos+1));
<INITIAL,ASM>"]"	=> (RBRACKET(yypos,yypos+1));
<INITIAL,ASM>"{"	=> (LBRACE(yypos,yypos+1));
<INITIAL,ASM>"}"	=> (RBRACE(yypos,yypos+1));
<INITIAL,ASM>","	=> (COMMA(yypos,yypos+1));
<INITIAL,ASM>";"	=> (SEMICOLON(yypos,yypos+1));
<INITIAL,ASM>"."	=> (DOT(yypos,yypos+1));
<INITIAL,ASM>".."	=> (DOTDOT(yypos,yypos+2));
<INITIAL,ASM>"..."	=> (DOTDOT(yypos,yypos+3));
<INITIAL>{symbol}	=> (if yytext = !asmLQuote then
				(debug("lquote "^yytext^"\n");
				 YYBEGIN ASMQUOTE; 
                                 LDQUOTE(yypos,yypos+size yytext))
			    else
			        lookupSym(yytext,yypos));
<ASMQUOTE>{asmsymbol}	=> (if yytext = !asmRQuote then
				(if !metaLevel <> 0 then
                                    err(yypos,yypos+size yytext,
                                       "Mismatch between "^(!asmLMeta)^
                                          " and "^(!asmRMeta)) else ();
				 debug("rquote "^yytext^"\n");
                                 YYBEGIN INITIAL; 
                                 RDQUOTE(yypos,yypos+size yytext))
			    else if yytext = !asmLMeta then
				(metaLevel := !metaLevel + 1;
				 debug("lmeta "^yytext^"\n");
				 YYBEGIN ASM; LMETA(yypos,yypos+size yytext))
			    else
			        asmtext(err,yytext,yypos));
<ASM>{asmsymbol}	=> (if yytext = !asmRMeta then
				(metaLevel := !metaLevel - 1;
				 debug("rmeta "^yytext^"("^Int.toString(!metaLevel)^")\n");
				 if !metaLevel = 0 then YYBEGIN ASMQUOTE
				 else (); RMETA(yypos,yypos+size yytext))
			    else
			        lookupSym(yytext,yypos));
<ASMQUOTE>{asmtext}	=> (debug("text="^yytext^"\n"); 
                            asmtext(err,yytext,yypos));
<COMMENT>"*)"		=> (commentLevel := !commentLevel - 1;
			    if !commentLevel = 0 then YYBEGIN INITIAL else (); 
			    continue());
<COMMENT>"(*"		=> (commentLevel := !commentLevel + 1; continue());
<COMMENT>.		=> (continue());
.			=> (err(yypos,yypos+size yytext,
                                "unknown character "^String.toString yytext);
                            continue());