-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.lex
60 lines (57 loc) · 3.09 KB
/
lexer.lex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
structure Tokens = Tokens
exception LexError
type pos = int
(* Position in file *)
type svalue = Tokens.svalue
type ('a,'b) token = ('a,'b) Tokens.token
type lexresult = (svalue,pos) token
type lexarg = string
type arg = lexarg
val line = ref 1;
val col = ref 0;
val eolpos = ref 0;
val eof = fn _ => Tokens.EOF(!line, !col)
val error = fn (e, line, col) => TextIO.output(TextIO.stdErr, "Unknown token:" ^ (Int.toString line) ^ ":" ^ (Int.toString col) ^ ":" ^ e ^ "\n")
%%
%header (functor A3LexFun(structure Tokens:A3_TOKENS));
alpha=[A-Za-z];
num=[0-9];
ws = [\ \t];
%%
"\n"|"\r\n" => (line := (!line) + 1; eolpos := yypos + size yytext; lex());
{ws}+ => (lex());
";" => (col := yypos - (!eolpos); Tokens.TERM(!line, !col));
"if" => (col := yypos - (!eolpos); Tokens.IF(!line, !col));
"then" => (col := yypos - (!eolpos); Tokens.THEN(!line, !col));
"else" => (col := yypos - (!eolpos); Tokens.ELSE(!line, !col));
"fi" => (col := yypos - (!eolpos); Tokens.FI(!line, !col));
"IMPLIES" => (col := yypos - (!eolpos); Tokens.IMPLIES(!line, !col));
"NOT" => (col := yypos - (!eolpos); Tokens.NOT(!line, !col));
"(" => (col := yypos - (!eolpos); Tokens.LPAREN(!line, !col));
")" => (col := yypos - (!eolpos); Tokens.RPAREN(!line, !col));
"AND" => (col := yypos - (!eolpos); Tokens.AND(!line, !col));
"OR" => (col := yypos - (!eolpos); Tokens.OR(!line, !col));
"XOR" => (col := yypos - (!eolpos); Tokens.XOR(!line, !col));
"EQUALS" => (col := yypos - (!eolpos); Tokens.EQUALS(!line, !col));
"TRUE" => (col := yypos - (!eolpos); Tokens.BOOL(yytext, !line, !col));
"FALSE" => (col := yypos - (!eolpos); Tokens.BOOL(yytext, !line, !col));
"PLUS" => (col := yypos - (!eolpos); Tokens.PLUS(!line, !col));
"MINUS" => (col := yypos - (!eolpos); Tokens.MINUS(!line, !col));
"TIMES" => (col := yypos - (!eolpos); Tokens.TIMES(!line, !col));
"NEGATE" => (col := yypos - (!eolpos); Tokens.NEGATE(!line, !col));
"LESSTHAN" => (col := yypos - (!eolpos); Tokens.LESSTHAN(!line, !col));
"GREATERTHAN" => (col := yypos - (!eolpos); Tokens.GREATERTHAN(!line, !col));
"let" => (col := yypos - (!eolpos); Tokens.LET(!line, !col));
"=" => (col := yypos - (!eolpos); Tokens.ASSIGN(!line, !col));
"in" => (col := yypos - (!eolpos); Tokens.IN(!line, !col));
"end" => (col := yypos - (!eolpos); Tokens.END(!line, !col));
"fn" => (col := yypos - (!eolpos); Tokens.LAMBDA(!line, !col));
"fun" => (col := yypos - (!eolpos); Tokens.FUNC(!line, !col));
"->" => (col := yypos - (!eolpos); Tokens.MAP(!line, !col));
"=>" => (col := yypos - (!eolpos); Tokens.DEF(!line, !col));
":" => (col := yypos - (!eolpos); Tokens.TYPDEF(!line, !col));
"int" => (col := yypos - (!eolpos); Tokens.TYPE(yytext, !line, !col));
"bool" => (col := yypos - (!eolpos); Tokens.TYPE(yytext, !line, !col));
{alpha}({alpha}|{num})* => (col := yypos - (!eolpos); Tokens.ID(yytext, !line, !col));
{num}+ => (col := yypos - (!eolpos); Tokens.NUM(valOf (Int.fromString yytext), !line, !col));
. => (col := yypos - (!eolpos); error(yytext, !line, !col); raise LexError);