3 * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
11 type svalue = Tokens.svalue
12 type ('a,'b) token = ('a,'b) Tokens.token
13 type lexresult = (svalue,pos) Tokens.token
16 val commentLevel = ref 0
17 val commentPos = ref 0
19 fun enterComment yypos =
20 ( commentLevel := !commentLevel + 1 ;
24 ( commentLevel := !commentLevel - 1 ;
27 fun number (yyt, yyp) =
29 val ext = ParseState.ext (yyp, yyp + size yyt)
30 val numOpt = Word32Signed.fromString yyt
33 ("integral constant `" ^ yyt ^ "' too large") ;
37 of NONE => ( ErrorMsg.error ext
38 ("cannot parse integral constant `" ^ yyt ^ "'");
39 Tokens.INTNUM (Word32Signed.ZERO, yyp, yyp + size yyt) )
40 | SOME n => Tokens.INTNUM (n,yyp,yyp + size yyt)
44 ( if (!commentLevel > 0)
45 then (ErrorMsg.error (ParseState.ext (!commentPos,!commentPos)) "unterminated comment")
47 Tokens.EOF (0,0) ) (* bogus position information; unused *)
52 %header (functor L1LexFn(structure Tokens : L1_TOKENS));
54 %s COMMENT COMMENT_LINE;
56 id = [A-Za-z_][A-Za-z0-9_]*;
63 <INITIAL> {ws}+ => (lex ());
64 <INITIAL> \n => (ParseState.newline(yypos); lex());
66 <INITIAL> "{" => (Tokens.LBRACE (yypos, yypos + size yytext));
67 <INITIAL> "}" => (Tokens.RBRACE (yypos, yypos + size yytext));
68 <INITIAL> "(" => (Tokens.LPAREN (yypos, yypos + size yytext));
69 <INITIAL> ")" => (Tokens.RPAREN (yypos, yypos + size yytext));
71 <INITIAL> ";" => (Tokens.SEMI (yypos, yypos + size yytext));
73 <INITIAL> "=" => (Tokens.ASSIGN (yypos, yypos + size yytext));
74 <INITIAL> "+=" => (Tokens.PLUSEQ (yypos, yypos + size yytext));
75 <INITIAL> "-=" => (Tokens.MINUSEQ (yypos, yypos + size yytext));
76 <INITIAL> "*=" => (Tokens.STAREQ (yypos, yypos + size yytext));
77 <INITIAL> "/=" => (Tokens.SLASHEQ (yypos, yypos + size yytext));
78 <INITIAL> "%=" => (Tokens.PERCENTEQ (yypos, yypos + size yytext));
80 <INITIAL> "+" => (Tokens.PLUS (yypos, yypos + size yytext));
81 <INITIAL> "-" => (Tokens.MINUS (yypos, yypos + size yytext));
82 <INITIAL> "*" => (Tokens.STAR (yypos, yypos + size yytext));
83 <INITIAL> "/" => (Tokens.SLASH (yypos, yypos + size yytext));
84 <INITIAL> "%" => (Tokens.PERCENT (yypos, yypos + size yytext));
86 <INITIAL> "return" => (Tokens.RETURN (yypos, yypos + size yytext));
88 <INITIAL> {decnum} => (number (yytext, yypos));
90 <INITIAL> {id} => (let
91 val id = Symbol.symbol yytext
93 Tokens.IDENT (id, yypos, yypos + size yytext)
96 <INITIAL> "/*" => (YYBEGIN COMMENT; enterComment yypos; lex());
97 <INITIAL> "*/" => (ErrorMsg.error (ParseState.ext (yypos, yypos)) "unbalanced comments";
100 <INITIAL> "//" => (YYBEGIN COMMENT_LINE; lex());
101 <INITIAL> "#" => (YYBEGIN COMMENT_LINE; lex());
102 <INITIAL> . => (ErrorMsg.error (ParseState.ext (yypos,yypos))
103 ("illegal character: \"" ^ yytext ^ "\"");
106 <COMMENT> "/*" => (enterComment yypos; lex());
107 <COMMENT> "*/" => (if exitComment () then YYBEGIN INITIAL else (); lex());
108 <COMMENT> \n => (ParseState.newline yypos; lex ());
109 <COMMENT> . => (lex());
111 <COMMENT_LINE> \n => (ParseState.newline yypos; YYBEGIN INITIAL; lex());
112 <COMMENT_LINE> . => (lex());