X-Git-Url: http://git.joshuawise.com/snipe.git/blobdiff_plain/5c79bb689ab446551bc7ec4497e6c9b75582837e..a644da892dbd55a7be1aed029dafebe28d26d27e:/parse/l5.lex?ds=sidebyside diff --git a/parse/l5.lex b/parse/l5.lex index e10f8b7..667638c 100644 --- a/parse/l5.lex +++ b/parse/l5.lex @@ -17,6 +17,9 @@ type lexresult = (svalue,pos) Tokens.token local val commentLevel = ref 0 val commentPos = ref 0 + val inString = ref false + val stringPos = ref 0 + val stringAcc : string list ref = ref [] (* :( *) in fun enterComment yypos = ( commentLevel := !commentLevel + 1 ; @@ -62,20 +65,27 @@ in ( if (!commentLevel > 0) then (ErrorMsg.error (ParseState.ext (!commentPos,!commentPos)) "unterminated comment") else (); + if (!inString) + then (ErrorMsg.error (ParseState.ext (!stringPos,!stringPos)) "unterminated string") + else (); Tokens.EOF (0,0) ) (* bogus position information; unused *) + fun newString yyp = ( inString := true; stringPos := yyp; stringAcc := [] ) + fun endString yyp = ( Tokens.STRING (concat (rev (!stringAcc)), !stringPos, yyp+1) ) + fun addString yyt = ( inString := false; stringAcc := yyt :: (!stringAcc) ) end %% %header (functor L5LexFn(structure Tokens : L5_TOKENS)); %full -%s COMMENT COMMENT_LINE; +%s COMMENT COMMENT_LINE STRING; id = [A-Za-z_][A-Za-z0-9_]*; decnum = [0-9][0-9]*; hexnum = 0x[0-9a-fA-F][0-9a-fA-F]*; ws = [\ \t\012]; +quote = [\"]; %% @@ -143,6 +153,7 @@ ws = [\ \t\012]; "else" => (Tokens.ELSE (yypos, yypos + size yytext)); "var" => (Tokens.VAR (yypos, yypos + size yytext)); "int" => (Tokens.INT (yypos, yypos + size yytext)); + "string" => (Tokens.TSTRING (yypos, yypos + size yytext)); "extern" => (Tokens.EXTERN (yypos, yypos + size yytext)); "struct" => (Tokens.STRUCT (yypos, yypos + size yytext)); "NULL" => (Tokens.NULL (yypos, yypos + size yytext)); @@ -164,10 +175,12 @@ ws = [\ \t\012]; "//" => (YYBEGIN COMMENT_LINE; lex()); "#" => (YYBEGIN COMMENT_LINE; lex()); + "\"" => (YYBEGIN STRING; newString yypos ; lex () ); . => (ErrorMsg.error (ParseState.ext (yypos,yypos)) ("illegal character: \"" ^ yytext ^ "\""); lex ()); + "/*" => (enterComment yypos; lex()); "*/" => (if exitComment () then YYBEGIN INITIAL else (); lex()); \n => (ParseState.newline yypos; lex ()); @@ -175,3 +188,17 @@ ws = [\ \t\012]; \n => (ParseState.newline yypos; YYBEGIN INITIAL; lex()); . => (lex()); + + "\\\\" => (addString "\\" ; lex() ); + "\\n" => (addString "\n" ; lex() ); + "\\r" => (addString "\r" ; lex() ); + "\\t" => (addString "\t" ; lex() ); + "\\\"" => (addString "\t" ; lex() ); + "\\". => (ErrorMsg.error (ParseState.ext (yypos,yypos)) + ("illegal escape sequence: \"" ^ yytext ^ "\""); + lex ()); + "\n" => (ErrorMsg.error (ParseState.ext (yypos,yypos)) + ("illegal newline in the middle of the string, asshole"); + lex ()); + "\"" => (YYBEGIN INITIAL; endString yypos ); + . => (addString yytext ; lex() );