| 1 |
(* spec.lex |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2006 |
| 4 |
* John Reppy (http://www.cs.uchicago.edu/~jhr) |
| 5 |
* Aaron Turon (http://www.cs.uchicago.edu/~adrassi) |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* (With some code borrowed from ml-yacc) |
| 9 |
*) |
| 10 |
|
| 11 |
val comLvl : int ref = ref 0 (* nesting depth of comments *) |
| 12 |
val comStart : int ref = ref 0 (* start line of current comment *) |
| 13 |
|
| 14 |
type pos = int |
| 15 |
type svalue = Tok.svalue |
| 16 |
type lexresult = (svalue,pos) Tok.token |
| 17 |
type lexarg = (int * string) -> unit (* error reporting function *) |
| 18 |
type arg = lexarg |
| 19 |
type ('a, 'b) token = ('a,'b) Tok.token |
| 20 |
|
| 21 |
fun eof (err) = ( |
| 22 |
if (!comLvl > 0) |
| 23 |
then err(~1, "unclosed comment starting at line " ^ Int.toString(!comStart)) |
| 24 |
else (); |
| 25 |
Tok.EOF(~1, ~1)) |
| 26 |
|
| 27 |
val text : string list ref = ref [] |
| 28 |
fun addText s = (text := s::(!text)) |
| 29 |
fun clrText () = (text := []) |
| 30 |
fun getText () = concat (rev (!text)) |
| 31 |
|
| 32 |
val pcount = ref 0 |
| 33 |
fun inc (ri as ref i) = (ri := i+1) |
| 34 |
fun dec (ri as ref i) = (ri := i-1) |
| 35 |
|
| 36 |
%% |
| 37 |
|
| 38 |
%header (functor MLYLexFn (structure Tok : MLY_TOKENS)); |
| 39 |
%arg (err); |
| 40 |
%count |
| 41 |
|
| 42 |
eol=("\n"|"\013\n"|"\013"); |
| 43 |
ws=("\009"|"\011"|"\012"|" "|{eol}); |
| 44 |
lc=[a-z]; |
| 45 |
uc=[A-Z]; |
| 46 |
alpha=({lc}|{uc}); |
| 47 |
digit=[0-9]; |
| 48 |
int=digit*; |
| 49 |
idchars=({alpha}|{digit}|"_"|"'"); |
| 50 |
id={alpha}{idchars}*; |
| 51 |
qualid ={id}"."; |
| 52 |
tyvar="'"{idchars}*; |
| 53 |
|
| 54 |
%s STRING COM CODE CONSTR; |
| 55 |
|
| 56 |
%% |
| 57 |
|
| 58 |
<INITIAL>{ws}+ => (continue()); |
| 59 |
<INITIAL>{id} => (Tok.ID (yytext, !yylineno, !yylineno)); |
| 60 |
|
| 61 |
<INITIAL>"%tokens" => (YYBEGIN CONSTR; |
| 62 |
Tok.KW_tokens (!yylineno, !yylineno)); |
| 63 |
<INITIAL>"%defs" => (YYBEGIN CODE; clrText(); |
| 64 |
Tok.KW_defs (!yylineno, !yylineno)); |
| 65 |
<INITIAL>"%keywords" => (Tok.KW_keywords (!yylineno, !yylineno)); |
| 66 |
<INITIAL>"%import" => (Tok.KW_import (!yylineno, !yylineno)); |
| 67 |
<INITIAL>"%name" => (Tok.KW_name (!yylineno, !yylineno)); |
| 68 |
<INITIAL>"%header" => (Tok.KW_header (!yylineno, !yylineno)); |
| 69 |
<INITIAL>"%start" => (Tok.KW_start (!yylineno, !yylineno)); |
| 70 |
<INITIAL>"%entry" => (Tok.KW_entry (!yylineno, !yylineno)); |
| 71 |
<INITIAL>"%try" => (Tok.KW_try (!yylineno, !yylineno)); |
| 72 |
<INITIAL>"%where" => (YYBEGIN CODE; clrText(); |
| 73 |
Tok.KW_where (!yylineno, !yylineno)); |
| 74 |
<INITIAL>"%debugactions"=> (Tok.KW_debugact (!yylineno, !yylineno)); |
| 75 |
<INITIAL>"%unitactions" => (Tok.KW_unitact (!yylineno, !yylineno)); |
| 76 |
<INITIAL>"%drop" => (Tok.KW_drop (!yylineno, !yylineno)); |
| 77 |
<INITIAL>"%extend" => (Tok.KW_extend (!yylineno, !yylineno)); |
| 78 |
<INITIAL>"%replace" => (Tok.KW_replace (!yylineno, !yylineno)); |
| 79 |
<INITIAL>"%refcell" => (YYBEGIN CONSTR; |
| 80 |
Tok.KW_refcell (!yylineno, !yylineno)); |
| 81 |
|
| 82 |
<INITIAL>"|" => (Tok.BAR (!yylineno, !yylineno)); |
| 83 |
<INITIAL>"@" => (YYBEGIN CODE; clrText(); |
| 84 |
Tok.AT (!yylineno, !yylineno)); |
| 85 |
<INITIAL>"$" => (Tok.DOLLAR (!yylineno, !yylineno)); |
| 86 |
<INITIAL>"+" => (Tok.PLUS (!yylineno, !yylineno)); |
| 87 |
<INITIAL>"*" => (Tok.STAR (!yylineno, !yylineno)); |
| 88 |
<INITIAL>"?" => (Tok.QUERY (!yylineno, !yylineno)); |
| 89 |
<INITIAL>":" => (Tok.COLON (!yylineno, !yylineno)); |
| 90 |
<INITIAL>";" => (Tok.SEMI (!yylineno, !yylineno)); |
| 91 |
<INITIAL>"," => (Tok.COMMA (!yylineno, !yylineno)); |
| 92 |
<INITIAL>"(" => (Tok.LP (!yylineno, !yylineno)); |
| 93 |
<INITIAL>")" => (Tok.RP (!yylineno, !yylineno)); |
| 94 |
<INITIAL>"[" => (Tok.LSB (!yylineno, !yylineno)); |
| 95 |
<INITIAL>"]" => (Tok.RSB (!yylineno, !yylineno)); |
| 96 |
<INITIAL>"/" => (Tok.SLASH (!yylineno, !yylineno)); |
| 97 |
<INITIAL>"=" => (Tok.EQ (!yylineno, !yylineno)); |
| 98 |
<INITIAL>"->" => (Tok.ARROW (!yylineno, !yylineno)); |
| 99 |
<INITIAL>"=>" => (YYBEGIN CODE; clrText(); |
| 100 |
Tok.DARROW (!yylineno, !yylineno)); |
| 101 |
<INITIAL>"\"" |
| 102 |
=> (YYBEGIN STRING; clrText(); addText yytext; |
| 103 |
ignore(continue() before YYBEGIN INITIAL); |
| 104 |
Tok.STRING (getText(), !yylineno, !yylineno)); |
| 105 |
|
| 106 |
<INITIAL>"(*" |
| 107 |
=> (comLvl := 1; comStart := !yylineno; YYBEGIN COM; |
| 108 |
ignore(continue() before YYBEGIN INITIAL); |
| 109 |
continue()); |
| 110 |
<CONSTR>"(*" |
| 111 |
=> (comLvl := 1; comStart := !yylineno; YYBEGIN COM; |
| 112 |
ignore(continue() before YYBEGIN CONSTR); |
| 113 |
continue()); |
| 114 |
<CODE>"(*" |
| 115 |
=> (comLvl := 1; comStart := !yylineno; YYBEGIN COM; |
| 116 |
ignore(continue() before YYBEGIN CODE); |
| 117 |
continue()); |
| 118 |
|
| 119 |
<COM>"(*" |
| 120 |
=> (comLvl := !comLvl+1; continue()); |
| 121 |
<COM>"*)" |
| 122 |
=> (comLvl := !comLvl-1; |
| 123 |
if (!comLvl = 0) |
| 124 |
then (Tok.BOGUS(!yylineno, !yylineno)) |
| 125 |
else continue()); |
| 126 |
<COM>.|{eol} |
| 127 |
=> (continue()); |
| 128 |
|
| 129 |
<CODE>"(" => (if !pcount = 0 then () else addText yytext; |
| 130 |
inc pcount; continue()); |
| 131 |
<CODE>")" => (dec pcount; |
| 132 |
if !pcount = 0 then |
| 133 |
(YYBEGIN INITIAL; |
| 134 |
Tok.CODE (getText(), !yylineno, !yylineno)) |
| 135 |
else (addText yytext; continue())); |
| 136 |
<CODE>"\"" => (addText yytext; YYBEGIN STRING; |
| 137 |
ignore(continue() before YYBEGIN CODE); |
| 138 |
continue()); |
| 139 |
<CODE>[^()"]+ => (addText yytext; continue()); |
| 140 |
|
| 141 |
<STRING>"\"" => (addText yytext; Tok.BOGUS(!yylineno, !yylineno)); |
| 142 |
<STRING>{eol} => (addText yytext; err (!yylineno, "unclosed string"); |
| 143 |
Tok.BOGUS(!yylineno, !yylineno)); |
| 144 |
<STRING>\\ => (addText yytext; continue()); |
| 145 |
<STRING>\\\\ => (addText yytext; continue()); |
| 146 |
<STRING>[^"\\\n\013]+ |
| 147 |
=> (addText yytext; continue()); |
| 148 |
<STRING>\\\" => (addText yytext; continue()); |
| 149 |
|
| 150 |
<CONSTR>{ws} => (continue()); |
| 151 |
<CONSTR>"of" => (Tok.OF (!yylineno, !yylineno)); |
| 152 |
<CONSTR>{id} => (Tok.ID (yytext, !yylineno, !yylineno)); |
| 153 |
<CONSTR>{tyvar} => (Tok.TYVAR (yytext, !yylineno, !yylineno)); |
| 154 |
<CONSTR>{qualid}=> (Tok.IDDOT (yytext, !yylineno, !yylineno)); |
| 155 |
<CONSTR>{int} => (Tok.INT (yytext, !yylineno, !yylineno)); |
| 156 |
<CONSTR>"|" => (Tok.BAR (!yylineno, !yylineno)); |
| 157 |
<CONSTR>"*" => (Tok.STAR (!yylineno, !yylineno)); |
| 158 |
<CONSTR>":" => (Tok.COLON (!yylineno, !yylineno)); |
| 159 |
<CONSTR>";" => (YYBEGIN INITIAL; |
| 160 |
Tok.SEMI (!yylineno, !yylineno)); |
| 161 |
<CONSTR>"(" => (Tok.LP (!yylineno, !yylineno)); |
| 162 |
<CONSTR>")" => (Tok.RP (!yylineno, !yylineno)); |
| 163 |
<CONSTR>"{" => (Tok.LCB (!yylineno, !yylineno)); |
| 164 |
<CONSTR>"}" => (Tok.RCB (!yylineno, !yylineno)); |
| 165 |
<CONSTR>"->" => (Tok.ARROW (!yylineno, !yylineno)); |
| 166 |
<CONSTR>"\"" => (YYBEGIN STRING; clrText(); addText yytext; |
| 167 |
ignore(continue() before YYBEGIN CONSTR); |
| 168 |
Tok.STRING (getText(), !yylineno, !yylineno)); |
| 169 |
<CONSTR>":==" => (YYBEGIN CODE; clrText(); |
| 170 |
Tok.REFSET (!yylineno, !yylineno)); |
| 171 |
|
| 172 |
. => (err (!yylineno, |
| 173 |
concat["illegal character '", |
| 174 |
String.toCString yytext, "'"]); |
| 175 |
continue()); |