-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
48 lines (44 loc) · 1.65 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
{
open Parser;;
exception InvalidToken of char ;;
}
let alpha_num = ['A'-'Z' 'a'-'z' '0'-'9' '_']
let var = ['A'-'Z'](alpha_num*)
let cons = ['a'-'z'](alpha_num*) | ("\"" [^ '\"']+ "\"")
let sp = [' ' '\t' '\n']+
let number = '0'|['1'-'9']['0'-'9']*
rule read = parse
eof {EOF}
| sp {read lexbuf}
| var as v {VAR(v)}
| cons as c {CONS(c)}
| number as n {NUM(int_of_string n)}
| '(' {LP}
| ')' {RP}
| '[' {LB}
| ']' {RB}
| ',' {COMMA}
| '=' {EQ}
| '+' {PLUS}
| '-' {MINUS}
| '*' {MULT}
| '/' {DIV}
| '>' {GT}
| '<' {LT}
| "\\=" {NOT_EQ}
| '|' {PIPE}
| '!' {CUT}
| '.' {ENDL}
| ":-" {COND}
| '%' {single_line_comment lexbuf}
| "/*" {multi_line_comment 0 lexbuf}
| _ as s {raise (InvalidToken s)}
and single_line_comment = parse
eof {EOF}
| '\n' {read lexbuf}
| _ {single_line_comment lexbuf}
and multi_line_comment depth = parse
eof {failwith "Syntax error: End of file in /* ... */ comment"}
| "*/" {if depth = 0 then read lexbuf else multi_line_comment (depth-1) lexbuf}
| "/*" {multi_line_comment (depth+1) lexbuf}
| _ {multi_line_comment depth lexbuf}