-(* L2 Compiler
- * Lexer
- * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
- * Modified: Frank Pfenning <fp@cs.cmu.edu>
- * Modified: Chris Lu <czl@andrew.cmu.edu>
- * Modified: Joshua Wise <jwise@andrew.cmu.edu>
- *)
-
-structure A = Ast
-structure S = Symbol
-
-type pos = int
-type svalue = Tokens.svalue
-type ('a,'b) token = ('a,'b) Tokens.token
-type lexresult = (svalue,pos) Tokens.token
-
-local
- val commentLevel = ref 0
- val commentPos = ref 0
-in
- fun enterComment yypos =
- ( commentLevel := !commentLevel + 1 ;
- commentPos := yypos )
-
- fun exitComment () =
- ( commentLevel := !commentLevel - 1 ;
- !commentLevel = 0 )
-
- fun number (yyt, yyp) =
- let
- val ext = ParseState.ext (yyp, yyp + size yyt)
- val numOpt = Word32Signed.fromString yyt
- handle Overflow =>
- ( ErrorMsg.error ext
- ("integral constant `" ^ yyt ^ "' too large") ;
- NONE )
- in
- case numOpt
- of NONE => ( ErrorMsg.error ext
- ("cannot parse integral constant `" ^ yyt ^ "'");
- Tokens.INTNUM (Word32Signed.ZERO, yyp, yyp + size yyt) )
- | SOME n => Tokens.INTNUM (n,yyp,yyp + size yyt)
- end
-
- fun eof () =
- ( if (!commentLevel > 0)
- then (ErrorMsg.error (ParseState.ext (!commentPos,!commentPos)) "unterminated comment")
- else ();
- Tokens.EOF (0,0) ) (* bogus position information; unused *)
-
-end
-
-%%
-%header (functor L2LexFn(structure Tokens : L2_TOKENS));
-%full
-%s COMMENT COMMENT_LINE;
-
-id = [A-Za-z_][A-Za-z0-9_]*;
-decnum = [0-9][0-9]*;
-
-ws = [\ \t\012];
-
-%%
-
-<INITIAL> {ws}+ => (lex ());
-<INITIAL> \n => (ParseState.newline(yypos); lex());
-
-<INITIAL> "{" => (Tokens.LBRACE (yypos, yypos + size yytext));
-<INITIAL> "}" => (Tokens.RBRACE (yypos, yypos + size yytext));
-<INITIAL> "(" => (Tokens.LPAREN (yypos, yypos + size yytext));
-<INITIAL> ")" => (Tokens.RPAREN (yypos, yypos + size yytext));
-
-<INITIAL> ";" => (Tokens.SEMI (yypos, yypos + size yytext));
-
-<INITIAL> "=" => (Tokens.ASSIGN (yypos, yypos + size yytext));
-<INITIAL> "+=" => (Tokens.PLUSEQ (yypos, yypos + size yytext));
-<INITIAL> "-=" => (Tokens.MINUSEQ (yypos, yypos + size yytext));
-<INITIAL> "*=" => (Tokens.STAREQ (yypos, yypos + size yytext));
-<INITIAL> "/=" => (Tokens.SLASHEQ (yypos, yypos + size yytext));
-<INITIAL> "%=" => (Tokens.PERCENTEQ (yypos, yypos + size yytext));
-<INITIAL> "<<=" => (Tokens.LSHEQ (yypos, yypos + size yytext));
-<INITIAL> ">>=" => (Tokens.RSHEQ (yypos, yypos + size yytext));
-<INITIAL> "&=" => (Tokens.BITANDEQ (yypos, yypos + size yytext));
-<INITIAL> "^=" => (Tokens.BITXOREQ (yypos, yypos + size yytext));
-<INITIAL> "|=" => (Tokens.BITOREQ (yypos, yypos + size yytext));
-
-<INITIAL> "+" => (Tokens.PLUS (yypos, yypos + size yytext));
-<INITIAL> "-" => (Tokens.MINUS (yypos, yypos + size yytext));
-<INITIAL> "!" => (Tokens.BANG (yypos, yypos + size yytext));
-<INITIAL> "*" => (Tokens.STAR (yypos, yypos + size yytext));
-<INITIAL> "/" => (Tokens.SLASH (yypos, yypos + size yytext));
-<INITIAL> "%" => (Tokens.PERCENT (yypos, yypos + size yytext));
-<INITIAL> "<<" => (Tokens.LSH (yypos, yypos + size yytext));
-<INITIAL> ">>" => (Tokens.RSH (yypos, yypos + size yytext));
-<INITIAL> "||" => (Tokens.LOGOR (yypos, yypos + size yytext));
-<INITIAL> "&&" => (Tokens.LOGAND (yypos, yypos + size yytext));
-<INITIAL> "&" => (Tokens.BITAND (yypos, yypos + size yytext));
-<INITIAL> "^" => (Tokens.BITXOR (yypos, yypos + size yytext));
-<INITIAL> "|" => (Tokens.BITOR (yypos, yypos + size yytext));
-<INITIAL> "~" => (Tokens.BITNOT (yypos, yypos + size yytext));
-<INITIAL> "==" => (Tokens.EQ (yypos, yypos + size yytext));
-<INITIAL> "!=" => (Tokens.NEQ (yypos, yypos + size yytext));
-<INITIAL> "<" => (Tokens.LT (yypos, yypos + size yytext));
-<INITIAL> "<=" => (Tokens.LE (yypos, yypos + size yytext));
-<INITIAL> ">=" => (Tokens.GE (yypos, yypos + size yytext));
-<INITIAL> ">" => (Tokens.GT (yypos, yypos + size yytext));
-
-<INITIAL> "return" => (Tokens.RETURN (yypos, yypos + size yytext));
-<INITIAL> "if" => (Tokens.IF (yypos, yypos + size yytext));
-<INITIAL> "while" => (Tokens.WHILE (yypos, yypos + size yytext));
-<INITIAL> "for" => (Tokens.FOR (yypos, yypos + size yytext));
-<INITIAL> "continue" => (Tokens.CONTINUE (yypos, yypos + size yytext));
-<INITIAL> "break" => (Tokens.BREAK (yypos, yypos + size yytext));
-<INITIAL> "else" => (Tokens.ELSE (yypos, yypos + size yytext));
-
-<INITIAL> {decnum} => (number (yytext, yypos));
-
-<INITIAL> {id} => (let
- val id = Symbol.symbol yytext
- in
- Tokens.IDENT (id, yypos, yypos + size yytext)
- end);
-
-<INITIAL> "/*" => (YYBEGIN COMMENT; enterComment yypos; lex());
-<INITIAL> "*/" => (ErrorMsg.error (ParseState.ext (yypos, yypos)) "unbalanced comments";
- lex());
-
-<INITIAL> "//" => (YYBEGIN COMMENT_LINE; lex());
-<INITIAL> "#" => (YYBEGIN COMMENT_LINE; lex());
-<INITIAL> . => (ErrorMsg.error (ParseState.ext (yypos,yypos))
- ("illegal character: \"" ^ yytext ^ "\"");
- lex ());
-
-<COMMENT> "/*" => (enterComment yypos; lex());
-<COMMENT> "*/" => (if exitComment () then YYBEGIN INITIAL else (); lex());
-<COMMENT> \n => (ParseState.newline yypos; lex ());
-<COMMENT> . => (lex());
-
-<COMMENT_LINE> \n => (ParseState.newline yypos; YYBEGIN INITIAL; lex());
-<COMMENT_LINE> . => (lex());