Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/FrontEnds/ml-antlr-based/spec.lex

raw
1 (* spec.lex
2 *
3 * COPYRIGHT (c) 2009 The Manticore Project (http://manticore.cs.uchicago.edu)
4 * All rights reserved.
5 *
6 * (With some code borrowed from ml-yacc)
7 *)
8
9 %defs (
10
11 structure Tok = SpecTokens
12
13 val comLvl : int ref = ref 0 (* nesting depth of comments *)
14 val comStart : int ref = ref 0 (* start line of current comment *)
15
16 type lex_result = Tok.token
17
18 fun err (lineNo, colNo, msg) = Err.errMsg [
19 "Lexical error [", Int.toString lineNo, ".", Int.toString colNo, "]: ", msg
20 ]
21
22 fun eof () = (
23 if (!comLvl > 0)
24 then ()
25 (* err(~1, "unclosed comment starting at line " ^ Int.toString(!comStart)) *)
26 else ();
27 Tok.EOF)
28
29 val text : string list ref = ref []
30 fun addText s = (text := s::(!text))
31 fun clrText () = (text := [])
32 fun getText () = concat (rev (!text))
33
34 val pcount = ref 0 (* nesting depth of parentheses in CODE *)
35 fun inc (ri as ref i) = (ri := i+1)
36 fun dec (ri as ref i) = (ri := i-1)
37 );
38
39 %let eol=("\n"|"\013\n"|"\013");
40 %let ws=("\009"|"\011"|"\012"|" "|{eol});
41 %let lc=[a-z];
42 %let uc=[A-Z];
43 %let alpha=({lc}|{uc});
44 %let digit=[0-9];
45 %let int=digit*;
46 %let idchars=({alpha}|{digit}|"_"|"'");
47 %let id={alpha}{idchars}*;
48 %let qualid ={id}".";
49 %let tyvar="'"{idchars}*;
50
51 (* the PRECODE state is used for scanning the whitespace between the precursor
52 * of a parenthesized SML code fragment and the opening "(". On most tokens it
53 * behaves like INITIAL so that the parser's error correction can respond.
54 *)
55 %states STRING COM PRECODE CODE PRECONSTR CONSTR;
56
57 %name SpecLex;
58
59 <INITIAL,PRECODE>"of" => (YYBEGIN CONSTR; Tok.OF);
60
61 <INITIAL,PRECODE>{ws}+ => (skip());
62 <INITIAL,PRECODE>{id} => (Tok.ID yytext);
63
64 <INITIAL,PRECODE>"%change" => (Tok.KW_change);
65 <INITIAL,PRECODE>"%defs" => (YYBEGIN PRECODE; Tok.KW_defs);
66 <INITIAL,PRECODE>"%dropping" => (Tok.KW_dropping);
67 <INITIAL,PRECODE>"%entry" => (Tok.KW_entry);
68 <INITIAL,PRECODE>"%header" => (YYBEGIN PRECODE; Tok.KW_header);
69 <INITIAL,PRECODE>"%import" => (Tok.KW_import);
70 <INITIAL,PRECODE>"%keyword"("s")? => (Tok.KW_keywords);
71 <INITIAL,PRECODE>"%name" => (Tok.KW_name);
72 <INITIAL,PRECODE>"%nonterm"("s")? => (Tok.KW_nonterms);
73 <INITIAL,PRECODE>"%prefer" => (Tok.KW_prefer);
74 <INITIAL,PRECODE>"%refcell" => (YYBEGIN CONSTR; Tok.KW_refcell);
75 <INITIAL,PRECODE>"%start" => (Tok.KW_start);
76 <INITIAL,PRECODE>"%token"("s")? => (YYBEGIN CONSTR; Tok.KW_tokens);
77 <INITIAL,PRECODE>"%tokentype" => (YYBEGIN CONSTR; Tok.KW_tokentype);
78 <INITIAL,PRECODE>"%try" => (Tok.KW_try);
79 <INITIAL,PRECODE>"%value" => (YYBEGIN PRECODE; Tok.KW_value);
80 <INITIAL,PRECODE>"%where" => (YYBEGIN PRECODE; Tok.KW_where);
81
82 <INITIAL,PRECODE>"|" => (Tok.BAR);
83 <INITIAL,PRECODE>"@" => (YYBEGIN PRECODE; Tok.AT);
84 <INITIAL,PRECODE>"$" => (Tok.DOLLAR);
85 <INITIAL,PRECODE>"+" => (Tok.PLUS);
86 <INITIAL,PRECODE>"*" => (Tok.STAR);
87 <INITIAL,PRECODE>"?" => (Tok.QUERY);
88 <INITIAL,PRECODE>":" => (Tok.COLON);
89 <INITIAL,PRECODE>";" => (Tok.SEMI);
90 <INITIAL,PRECODE>"," => (Tok.COMMA);
91 <INITIAL>"(" => (Tok.LP);
92 <INITIAL>")" => (Tok.RP);
93 <INITIAL,PRECODE>"[" => (Tok.LSB);
94 <INITIAL,PRECODE>"]" => (Tok.RSB);
95 <INITIAL,PRECODE>"/" => (Tok.SLASH);
96 <INITIAL,PRECODE>"=" => (Tok.EQ);
97 <INITIAL,PRECODE>"->" => (Tok.ARROW);
98 <INITIAL,PRECODE>"=>" => (YYBEGIN PRECODE; Tok.DARROW);
99 <INITIAL>"\"" => (YYBEGIN STRING; clrText(); addText yytext;
100 ignore(continue() before YYBEGIN INITIAL);
101 Tok.STRING (getText()));
102
103 <INITIAL>"(*"
104 => (comLvl := 1; comStart := !yylineno; YYBEGIN COM;
105 ignore(continue() before YYBEGIN INITIAL);
106 continue());
107 <CONSTR>"(*"
108 => (comLvl := 1; comStart := !yylineno; YYBEGIN COM;
109 ignore(continue() before YYBEGIN CONSTR);
110 continue());
111 <PRECODE>"(*"
112 => (comLvl := 1; comStart := !yylineno; YYBEGIN COM;
113 ignore(continue() before YYBEGIN PRECODE);
114 continue());
115 <CODE>"(*"
116 => (comLvl := 1; comStart := !yylineno; YYBEGIN COM;
117 ignore(continue() before YYBEGIN CODE);
118 continue());
119
120 <COM>"(*"
121 => (comLvl := !comLvl+1; continue());
122 <COM>"*)"
123 => (comLvl := !comLvl-1;
124 if (!comLvl = 0)
125 then (Tok.BOGUS)
126 else continue());
127 <COM>.|{eol}
128 => (continue());
129
130 <PRECODE>"(" => (pcount := 1; YYBEGIN CODE; clrText(); continue());
131 <PRECODE>"\"" => (YYBEGIN STRING; clrText(); addText yytext;
132 ignore(continue() before YYBEGIN PRECODE);
133 Tok.STRING (getText()));
134
135 <CODE>"(" => (addText yytext; (* NOTE: the initial "(" is consumed in the PRECODE state *)
136 inc pcount; continue());
137 <CODE>")" => (dec pcount;
138 if !pcount = 0
139 then (YYBEGIN INITIAL; Tok.CODE (getText()))
140 else (addText yytext; continue()));
141 <CODE>"\"" => (addText yytext; YYBEGIN STRING;
142 ignore(continue() before YYBEGIN CODE);
143 continue());
144 <CODE>[^()"]+ => (addText yytext; continue());
145
146 <STRING>"\"" => (addText yytext; Tok.BOGUS);
147 <STRING>{eol} => (addText yytext; err (!yylineno, !yycolno, "unclosed string");
148 Tok.BOGUS);
149 <STRING>\\ => (addText yytext; continue());
150 <STRING>\\\\ => (addText yytext; continue());
151 (* we apply toString to yytext below to ensure that UTF8 characters are
152 * printed in a format that is legal SML.
153 *)
154 <STRING>[^"\\\n\013]+
155 => (addText(String.toString yytext); continue());
156 <STRING>\\\" => (addText yytext; continue());
157
158 <CONSTR>{ws} => (continue());
159 <CONSTR>"of" => (Tok.OF);
160 <CONSTR>{id} => (Tok.ID yytext);
161 <CONSTR>{tyvar} => (Tok.TYVAR yytext);
162 <CONSTR>{qualid}=> (Tok.IDDOT yytext);
163 <CONSTR>{int} => (Tok.INT yytext);
164 <CONSTR>"|" => (Tok.BAR);
165 <CONSTR>"*" => (Tok.STAR);
166 <CONSTR>":" => (Tok.COLON);
167 <CONSTR>"," => (Tok.COMMA);
168 <CONSTR>";" => (YYBEGIN INITIAL; Tok.SEMI);
169 <CONSTR>"(" => (Tok.LP);
170 <CONSTR>")" => (Tok.RP);
171 <CONSTR>"{" => (Tok.LCB);
172 <CONSTR>"}" => (Tok.RCB);
173 <CONSTR>"->" => (Tok.ARROW);
174 <CONSTR>"\"" => (YYBEGIN STRING; clrText(); addText yytext;
175 ignore(continue() before YYBEGIN CONSTR);
176 Tok.STRING (getText()));
177 <CONSTR>"=" => (YYBEGIN PRECODE; Tok.EQ);
178
179 . => (err (!yylineno, !yycolno,
180 concat["illegal character '",
181 String.toCString yytext, "'"]);
182 continue());