-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexer.mll
79 lines (63 loc) · 1.69 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
{ (* -*- tuareg -*- *)
open Lexing
open Error
open Position
open Parser
let next_line_and f lexbuf =
Lexing.new_line lexbuf;
f lexbuf
let error lexbuf =
error "during lexing" (lex_join lexbuf.lex_start_p lexbuf.lex_curr_p)
}
let newline = ('\010' | '\013' | "\013\010")
let blank = [' ' '\009' '\012']
let digit = ['0'-'9']
let identifier = ['a'-'z' '_' 'A'-'Z']+
rule token = parse
(** Layout *)
| newline { next_line_and token lexbuf }
| blank+ { token lexbuf }
| "(*" { comment 1 lexbuf }
| "var" { VAR }
| "def" { DEF }
| "if" { IF }
| "then" { THEN }
| "else" { ELSE }
| "return" { RETURN }
(** Identifiers *)
| identifier as i { ID i }
(** Literals *)
| (['-']? digit+) as d { LINT (int_of_string d) }
(** Infix operators *)
| "=" { EQ }
| "+" { PLUS }
| "*" { STAR }
(** Punctuation *)
| ";" { SEMICOLON }
| "(" { LPAREN }
| ")" { RPAREN }
| "{" { LBRACE }
| "}" { RBRACE }
| "," { COMMA }
| eof { EOF }
(** Lexing error. *)
| _ { error lexbuf "unexpected character." (fun _ -> EOF) }
and comment level = parse
| "*)" {
if level = 1 then
token lexbuf
else
comment (pred level) lexbuf
}
| "(*" {
comment (succ level) lexbuf
}
| eof {
error lexbuf "unterminated comment." (fun _ -> EOF)
}
| newline {
next_line_and (comment level) lexbuf
}
| _ {
comment level lexbuf
}