]> Joshua Wise's Git repositories - snipe.git/blobdiff - parse/l5.lex
Add strings to type system and parser/lexer
[snipe.git] / parse / l5.lex
index e10f8b7da384d198bb047cce7e2c8f06a21fb1c4..3028a6c9407460d83bdf00a64e5c8fc146e9b2a6 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,14 +65,20 @@ 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]*;
@@ -143,6 +152,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 +174,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 +187,6 @@ ws = [\ \t\012];
 
 <COMMENT_LINE> \n     => (ParseState.newline yypos; YYBEGIN INITIAL; lex());
 <COMMENT_LINE> .      => (lex());
+
+<STRING> [^\"\\]*     => (addString yytext ; lex() );
+<STRING> "\""         => (YYBEGIN INITIAL; endString yypos );
This page took 0.053071 seconds and 4 git commands to generate.