-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer0.mll
60 lines (52 loc) · 1.13 KB
/
lexer0.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
{ (* Emacs, use -*- tuareg -*- to open this file. *)
open Parser0
let enter_newline lexbuf =
Lexing.new_line lexbuf;
lexbuf
}
let newline = ('\010' | '\013' | "\013\010")
let blank = [' ' '\009' '\012']
let id = ['a'-'z''0'-'9''_']+
let decimal = ['0'-'9']+
let fn = decimal ("." decimal?)?
rule token = parse
| newline { enter_newline lexbuf |> token }
| blank { token lexbuf }
| "(*" { comment 0 lexbuf }
| "->" { ARROW }
| ":" { COLON }
| "," { COMMA }
| "=" { EQUAL }
| "fun" { FUN }
| "let" { LET }
| "in" { IN }
| "fst" { FST }
| "snd" { SND }
| "float" { FLOAT }
| "(" { LPAREN }
| ")" { RPAREN }
| "*" { TIMES }
| "-" { MINUS }
| "+" { PLUS }
| fn as f { LFLOAT (float_of_string f) }
| id as s { ID s }
| eof { EOF }
| _ as c {
Printf.eprintf "Lexing error at %d: Unexpected `%c'."
lexbuf.Lexing.lex_curr_pos c;
exit 1
}
and comment depth = parse
| "*)" {
if depth = 0 then token lexbuf else comment (depth - 1) lexbuf
}
| "(*" {
comment (depth + 1) lexbuf
}
| eof {
Printf.eprintf "Unexpected EOF inside comments.";
exit 1
}
| _ {
comment depth lexbuf
}