-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
84 lines (80 loc) · 1.8 KB
/
lexer.mll
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
open Parser;;
exception InvalidToken of string;;
}
let alpha_num = ['A'-'Z' 'a'-'z' '0'-'9' '_' '\'']
let var = ['A'-'Z'](alpha_num*)
let cons = ['a'-'z'](alpha_num*) | ("\"" [^ '\"']+ "\"" )
(* the latter part matches a string [^'\"'] matches one or more characters that are not double quotes *)
let skip = [' ' '\t' '\n' '\r']+
let number = '0'|['1'-'9']['0'-'9']*
rule token = parse
| skip
{ token lexbuf }
| ['+']
{ PLUS }
| ['-']
{ MINUS }
| ['*']
{ ASTERISK }
| ['/']
{ SLASH }
| var as lxm
{ IDENT(lxm) }
| cons as lxm
{ CONS(lxm) }
| number as lxm
{ INTEGER(int_of_string lxm) }
| ['(']
{ LEFTBRACKET }
| [')']
{ RIGHTBRACKET }
| ['[']
{ LEFTSQUARE }
| [']']
{ RIGHTSQUARE }
| [',']
{ COMMA }
| ['=']
{ ASSIGN }
| ['<']
{ LESSTHAN }
| ['>']
{ GREATERTHAN }
| "!="
{ NOTEQUAL }
| ['|']
{ PIPE }
| ['!']
{ BANG }
| ['.']
{ STOP }
| ":-"
{ COND }
| ['%']
{ oneLineComment lexbuf }
| ['_']
{ UNDERSCORE }
| "/*"
{multiLineComment 0 lexbuf }
| _ as lxm
{raise (InvalidToken (String.make 1 lxm))}
| eof
{ EOF }
and oneLineComment = parse
eof
{ EOF }
| ['\n']
{token lexbuf}
| _
{oneLineComment lexbuf}
and multiLineComment depth = parse
eof
{ failwith "Syntax error" }
| "*/"
{ if depth = 0 then token lexbuf
else multiLineComment (depth-1) lexbuf }
| "/*"
{ multiLineComment (depth+1) lexbuf }
| _
{ multiLineComment depth lexbuf }