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 ;
( 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 = [\"];
%%
<INITIAL> "else" => (Tokens.ELSE (yypos, yypos + size yytext));
<INITIAL> "var" => (Tokens.VAR (yypos, yypos + size yytext));
<INITIAL> "int" => (Tokens.INT (yypos, yypos + size yytext));
+<INITIAL> "string" => (Tokens.TSTRING (yypos, yypos + size yytext));
<INITIAL> "extern" => (Tokens.EXTERN (yypos, yypos + size yytext));
<INITIAL> "struct" => (Tokens.STRUCT (yypos, yypos + size yytext));
<INITIAL> "NULL" => (Tokens.NULL (yypos, yypos + size yytext));
<INITIAL> "//" => (YYBEGIN COMMENT_LINE; lex());
<INITIAL> "#" => (YYBEGIN COMMENT_LINE; lex());
+<INITIAL> "\"" => (YYBEGIN STRING; newString yypos ; 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_LINE> \n => (ParseState.newline yypos; YYBEGIN INITIAL; lex());
<COMMENT_LINE> . => (lex());
+
+<STRING> "\\\\" => (addString "\\" ; lex() );
+<STRING> "\\n" => (addString "\n" ; lex() );
+<STRING> "\\r" => (addString "\r" ; lex() );
+<STRING> "\\t" => (addString "\t" ; lex() );
+<STRING> "\\\"" => (addString "\t" ; lex() );
+<STRING> "\\". => (ErrorMsg.error (ParseState.ext (yypos,yypos))
+ ("illegal escape sequence: \"" ^ yytext ^ "\"");
+ lex ());
+<STRING> "\n" => (ErrorMsg.error (ParseState.ext (yypos,yypos))
+ ("illegal newline in the middle of the string, asshole");
+ lex ());
+<STRING> "\"" => (YYBEGIN INITIAL; endString yypos );
+<STRING> . => (addString yytext ; lex() );