3 * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
5 * Modified: Chris Lu <czl@andrew.cmu.edu>
6 * Modified: Joshua Wise <jwise@andrew.cmu.edu>
13 type svalue = Tokens.svalue
14 type ('a,'b) token = ('a,'b) Tokens.token
15 type lexresult = (svalue,pos) Tokens.token
18 val commentLevel = ref 0
19 val commentPos = ref 0
20 val inString = ref false
22 val stringAcc : string list ref = ref [] (* :( *)
24 fun enterComment yypos =
25 ( commentLevel := !commentLevel + 1 ;
29 ( commentLevel := !commentLevel - 1 ;
32 fun number (yyt, yyp) =
34 val ext = ParseState.ext (yyp, yyp + size yyt)
35 val numOpt = Word32Signed.fromString yyt
38 ("integral constant `" ^ yyt ^ "' too large") ;
42 of NONE => ( ErrorMsg.error ext
43 ("cannot parse integral constant `" ^ yyt ^ "'");
44 Tokens.INTNUM (Word32Signed.ZERO, yyp, yyp + size yyt) )
45 | SOME n => Tokens.INTNUM (n,yyp,yyp + size yyt)
47 fun hexnumber (yyt, yyp) =
49 val t = String.extract (yyt, 2, NONE)
50 val ext = ParseState.ext (yyp, yyp + size yyt)
51 val numOpt = StringCvt.scanString (Word32.scan StringCvt.HEX) t
54 ("integral constant `" ^ yyt ^ "' too large") ;
58 of NONE => ( ErrorMsg.error ext
59 ("cannot parse integral constant `" ^ yyt ^ "'");
60 Tokens.INTNUM (Word32Signed.ZERO, yyp, yyp + size yyt) )
61 | SOME n => Tokens.INTNUM (n,yyp,yyp + size yyt)
65 ( if (!commentLevel > 0)
66 then (ErrorMsg.error (ParseState.ext (!commentPos,!commentPos)) "unterminated comment")
69 then (ErrorMsg.error (ParseState.ext (!stringPos,!stringPos)) "unterminated string")
71 Tokens.EOF (0,0) ) (* bogus position information; unused *)
73 fun newString yyp = ( inString := true; stringPos := yyp; stringAcc := [] )
74 fun endString yyp = ( Tokens.STRING (concat (rev (!stringAcc)), !stringPos, yyp+1) )
75 fun addString yyt = ( inString := false; stringAcc := yyt :: (!stringAcc) )
79 %header (functor L5LexFn(structure Tokens : L5_TOKENS));
81 %s COMMENT COMMENT_LINE STRING;
83 id = [A-Za-z_][A-Za-z0-9_]*;
85 hexnum = 0x[0-9a-fA-F][0-9a-fA-F]*;
91 <INITIAL> {ws}+ => (lex ());
92 <INITIAL> \n => (ParseState.newline(yypos); lex());
94 <INITIAL> "{" => (Tokens.LBRACE (yypos, yypos + size yytext));
95 <INITIAL> "}" => (Tokens.RBRACE (yypos, yypos + size yytext));
96 <INITIAL> "(" => (Tokens.LPAREN (yypos, yypos + size yytext));
97 <INITIAL> ")" => (Tokens.RPAREN (yypos, yypos + size yytext));
99 <INITIAL> ";" => (Tokens.SEMI (yypos, yypos + size yytext));
101 <INITIAL> "=" => (Tokens.ASSIGN (yypos, yypos + size yytext));
102 <INITIAL> "+=" => (Tokens.PLUSEQ (yypos, yypos + size yytext));
103 <INITIAL> "-=" => (Tokens.MINUSEQ (yypos, yypos + size yytext));
104 <INITIAL> "*=" => (Tokens.STAREQ (yypos, yypos + size yytext));
105 <INITIAL> "/=" => (Tokens.SLASHEQ (yypos, yypos + size yytext));
106 <INITIAL> "%=" => (Tokens.PERCENTEQ (yypos, yypos + size yytext));
107 <INITIAL> "<<=" => (Tokens.LSHEQ (yypos, yypos + size yytext));
108 <INITIAL> ">>=" => (Tokens.RSHEQ (yypos, yypos + size yytext));
109 <INITIAL> "&=" => (Tokens.BITANDEQ (yypos, yypos + size yytext));
110 <INITIAL> "^=" => (Tokens.BITXOREQ (yypos, yypos + size yytext));
111 <INITIAL> "|=" => (Tokens.BITOREQ (yypos, yypos + size yytext));
113 <INITIAL> "++" => (Tokens.PLUSPLUS (yypos, yypos + size yytext));
114 <INITIAL> "--" => (Tokens.MINUSMINUS (yypos, yypos + size yytext));
116 <INITIAL> "+" => (Tokens.PLUS (yypos, yypos + size yytext));
117 <INITIAL> "-" => (Tokens.MINUS (yypos, yypos + size yytext));
118 <INITIAL> "!" => (Tokens.BANG (yypos, yypos + size yytext));
119 <INITIAL> "*" => (Tokens.STAR (yypos, yypos + size yytext));
120 <INITIAL> "/" => (Tokens.SLASH (yypos, yypos + size yytext));
121 <INITIAL> "%" => (Tokens.PERCENT (yypos, yypos + size yytext));
122 <INITIAL> "<<" => (Tokens.LSH (yypos, yypos + size yytext));
123 <INITIAL> ">>" => (Tokens.RSH (yypos, yypos + size yytext));
124 <INITIAL> "||" => (Tokens.LOGOR (yypos, yypos + size yytext));
125 <INITIAL> "&&" => (Tokens.LOGAND (yypos, yypos + size yytext));
126 <INITIAL> "&" => (Tokens.BITAND (yypos, yypos + size yytext));
127 <INITIAL> "^" => (Tokens.BITXOR (yypos, yypos + size yytext));
128 <INITIAL> "|" => (Tokens.BITOR (yypos, yypos + size yytext));
129 <INITIAL> "~" => (Tokens.BITNOT (yypos, yypos + size yytext));
130 <INITIAL> "==" => (Tokens.EQ (yypos, yypos + size yytext));
131 <INITIAL> "!=" => (Tokens.NEQ (yypos, yypos + size yytext));
132 <INITIAL> "<" => (Tokens.LT (yypos, yypos + size yytext));
133 <INITIAL> "<=" => (Tokens.LE (yypos, yypos + size yytext));
134 <INITIAL> ">=" => (Tokens.GE (yypos, yypos + size yytext));
135 <INITIAL> ">" => (Tokens.GT (yypos, yypos + size yytext));
137 <INITIAL> "?" => (Tokens.QUESTION (yypos, yypos + size yytext));
138 <INITIAL> ":" => (Tokens.COLON (yypos, yypos + size yytext));
139 <INITIAL> "," => (Tokens.COMMA (yypos, yypos + size yytext));
141 <INITIAL> "[" => (Tokens.LBRACKET (yypos, yypos + size yytext));
142 <INITIAL> "]" => (Tokens.RBRACKET (yypos, yypos + size yytext));
143 <INITIAL> "->" => (Tokens.ARROW (yypos, yypos + size yytext));
144 <INITIAL> "." => (Tokens.DOT (yypos, yypos + size yytext));
146 <INITIAL> "return" => (Tokens.RETURN (yypos, yypos + size yytext));
147 <INITIAL> "if" => (Tokens.IF (yypos, yypos + size yytext));
148 <INITIAL> "while" => (Tokens.WHILE (yypos, yypos + size yytext));
149 <INITIAL> "for" => (Tokens.FOR (yypos, yypos + size yytext));
150 <INITIAL> "continue" => (Tokens.CONTINUE (yypos, yypos + size yytext));
151 <INITIAL> "break" => (Tokens.BREAK (yypos, yypos + size yytext));
152 <INITIAL> "else" => (Tokens.ELSE (yypos, yypos + size yytext));
153 <INITIAL> "var" => (Tokens.VAR (yypos, yypos + size yytext));
154 <INITIAL> "int" => (Tokens.INT (yypos, yypos + size yytext));
155 <INITIAL> "string" => (Tokens.TSTRING (yypos, yypos + size yytext));
156 <INITIAL> "extern" => (Tokens.EXTERN (yypos, yypos + size yytext));
157 <INITIAL> "struct" => (Tokens.STRUCT (yypos, yypos + size yytext));
158 <INITIAL> "NULL" => (Tokens.NULL (yypos, yypos + size yytext));
159 <INITIAL> "new" => (Tokens.NEW (yypos, yypos + size yytext));
162 <INITIAL> {decnum} => (number (yytext, yypos));
163 <INITIAL> {hexnum} => (hexnumber (yytext, yypos));
165 <INITIAL> {id} => (let
166 val id = Symbol.symbol yytext
168 Tokens.IDENT (id, yypos, yypos + size yytext)
171 <INITIAL> "/*" => (YYBEGIN COMMENT; enterComment yypos; lex());
172 <INITIAL> "*/" => (ErrorMsg.error (ParseState.ext (yypos, yypos)) "unbalanced comments";
175 <INITIAL> "//" => (YYBEGIN COMMENT_LINE; lex());
176 <INITIAL> "#" => (YYBEGIN COMMENT_LINE; lex());
177 <INITIAL> "\"" => (YYBEGIN STRING; newString yypos ; lex () );
178 <INITIAL> . => (ErrorMsg.error (ParseState.ext (yypos,yypos))
179 ("illegal character: \"" ^ yytext ^ "\"");
183 <COMMENT> "/*" => (enterComment yypos; lex());
184 <COMMENT> "*/" => (if exitComment () then YYBEGIN INITIAL else (); lex());
185 <COMMENT> \n => (ParseState.newline yypos; lex ());
186 <COMMENT> . => (lex());
188 <COMMENT_LINE> \n => (ParseState.newline yypos; YYBEGIN INITIAL; lex());
189 <COMMENT_LINE> . => (lex());
191 <STRING> [^\"\\]* => (addString yytext ; lex() );
192 <STRING> "\"" => (YYBEGIN INITIAL; endString yypos );