comparison src/lacweb.lex @ 14:f1c36df29ed7

Primitive type constants
author Adam Chlipala <adamc@hcoop.net>
date Sun, 08 Jun 2008 12:27:08 -0400
parents a455a9f85cc3
children 9a578171de9e
comparison
equal deleted inserted replaced
13:6049e2193bf2 14:f1c36df29ed7
57 (); 57 ();
58 Tokens.EOF (pos, pos) 58 Tokens.EOF (pos, pos)
59 end 59 end
60 end 60 end
61 61
62 val str = ref ([] : char list)
63 val strStart = ref 0
64
62 %% 65 %%
63 %header (functor LacwebLexFn(structure Tokens : Lacweb_TOKENS)); 66 %header (functor LacwebLexFn(structure Tokens : Lacweb_TOKENS));
64 %full 67 %full
65 %s COMMENT; 68 %s COMMENT STRING;
66 69
67 id = [a-z_][A-Za-z0-9_]*; 70 id = [a-z_][A-Za-z0-9_]*;
68 cid = [A-Z][A-Za-z0-9_]*; 71 cid = [A-Z][A-Za-z0-9_]*;
69 ws = [\ \t\012]; 72 ws = [\ \t\012];
73 intconst = [0-9]+;
74 realconst = [0-9]+\.[0-9]*;
70 75
71 %% 76 %%
72 77
73 <INITIAL> \n => (ErrorMsg.newline yypos; 78 <INITIAL> \n => (ErrorMsg.newline yypos;
74 continue ()); 79 continue ());
85 90
86 <COMMENT> "(*" => (enterComment yypos; 91 <COMMENT> "(*" => (enterComment yypos;
87 continue ()); 92 continue ());
88 <COMMENT> "*)" => (if exitComment () then YYBEGIN INITIAL else (); 93 <COMMENT> "*)" => (if exitComment () then YYBEGIN INITIAL else ();
89 continue ()); 94 continue ());
95
96 <INITIAL> "\"" => (YYBEGIN STRING; strStart := yypos; str := []; continue());
97 <STRING> "\\\"" => (str := #"\"" :: !str; continue());
98 <STRING> "\"" => (YYBEGIN INITIAL;
99 Tokens.STRING (String.implode (List.rev (!str)), !strStart, yypos + 1));
100 <STRING> "\n" => (ErrorMsg.newline yypos;
101 str := #"\n" :: !str; continue());
102 <STRING> . => (str := String.sub (yytext, 0) :: !str; continue());
90 103
91 <INITIAL> "(" => (Tokens.LPAREN (yypos, yypos + size yytext)); 104 <INITIAL> "(" => (Tokens.LPAREN (yypos, yypos + size yytext));
92 <INITIAL> ")" => (Tokens.RPAREN (yypos, yypos + size yytext)); 105 <INITIAL> ")" => (Tokens.RPAREN (yypos, yypos + size yytext));
93 <INITIAL> "[" => (Tokens.LBRACK (yypos, yypos + size yytext)); 106 <INITIAL> "[" => (Tokens.LBRACK (yypos, yypos + size yytext));
94 <INITIAL> "]" => (Tokens.RBRACK (yypos, yypos + size yytext)); 107 <INITIAL> "]" => (Tokens.RBRACK (yypos, yypos + size yytext));
117 <INITIAL> "Name" => (Tokens.NAME (yypos, yypos + size yytext)); 130 <INITIAL> "Name" => (Tokens.NAME (yypos, yypos + size yytext));
118 131
119 <INITIAL> {id} => (Tokens.SYMBOL (yytext, yypos, yypos + size yytext)); 132 <INITIAL> {id} => (Tokens.SYMBOL (yytext, yypos, yypos + size yytext));
120 <INITIAL> {cid} => (Tokens.CSYMBOL (yytext, yypos, yypos + size yytext)); 133 <INITIAL> {cid} => (Tokens.CSYMBOL (yytext, yypos, yypos + size yytext));
121 134
135 <INITIAL> {intconst} => (case Int64.fromString yytext of
136 SOME x => Tokens.INT (x, yypos, yypos + size yytext)
137 | NONE => (ErrorMsg.errorAt' (yypos, yypos)
138 ("Expected int, received: " ^ yytext);
139 continue ()));
140 <INITIAL> {realconst} => (case Real64.fromString yytext of
141 SOME x => Tokens.FLOAT (x, yypos, yypos + size yytext)
142 | NONE => (ErrorMsg.errorAt' (yypos, yypos)
143 ("Expected float, received: " ^ yytext);
144 continue ()));
145
122 <COMMENT> . => (continue()); 146 <COMMENT> . => (continue());
123 147
124 <INITIAL> . => (ErrorMsg.errorAt' (yypos, yypos) 148 <INITIAL> . => (ErrorMsg.errorAt' (yypos, yypos)
125 ("illegal character: \"" ^ yytext ^ "\""); 149 ("illegal character: \"" ^ yytext ^ "\"");
126 continue ()); 150 continue ());