]> Joshua Wise's Git repositories - snipe.git/blobdiff - parse/l5.lex
Add carriage return to string lexer.
[snipe.git] / parse / l5.lex
index e10f8b7da384d198bb047cce7e2c8f06a21fb1c4..667638cbd5e07f122c5bedc60546982404f200be 100644 (file)
@@ -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];
 <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));
@@ -164,10 +175,12 @@ ws = [\ \t\012];
 
 <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 ());
@@ -175,3 +188,17 @@ ws = [\ \t\012];
 
 <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() );
This page took 0.024606 seconds and 4 git commands to generate.