-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLexer.mll
151 lines (129 loc) · 2.88 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
open Lexing
open Error
open Parser
open RawLambda
let kw = [
"effect", EFFECT ;
"else", ELSE ;
"fun", FUN ;
"ifzero", IFZERO ;
"in", IN ;
"let", LET ;
"match", MATCH ;
"of", OF ;
"print", PRINT ;
"rec", REC ;
"then", THEN ;
"type", TYPE ;
"with", WITH ;
]
let keywords = Hashtbl.create (List.length kw)
let () = List.iter (fun (a,b) -> Hashtbl.add keywords a b) kw
let pragmas = Hashtbl.create 17
let add_pragma = Hashtbl.add pragmas
let run_pragma x place =
let f =
try Hashtbl.find pragmas x
with Not_found -> error place "Unknown pragma declaration: %s" x
in
f ()
}
(* -------------------------------------------------------------------------- *)
(* Regular expressions. *)
let newline =
('\010' | '\013' | "\013\010")
let whitespace =
[ ' ' '\t' ]
let lowercase =
['a'-'z' '\223'-'\246' '\248'-'\255' '_']
let uppercase =
['A'-'Z' '\192'-'\214' '\216'-'\222']
let identchar =
['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255' '0'-'9']
let digit =
['0'-'9']
(* -------------------------------------------------------------------------- *)
(* The lexer. *)
rule entry = parse
| "#pragma" whitespace+ ((identchar | '-')+ as x) whitespace* newline
{ run_pragma x (place lexbuf); new_line lexbuf; entry lexbuf }
| newline
{ new_line lexbuf; entry lexbuf }
| whitespace+
{ entry lexbuf }
| "(*"
{ ocamlcomment (place lexbuf) lexbuf; entry lexbuf }
| ""
{ token lexbuf }
and token = parse
| "->"
{ ARROW }
| "-["
{ ARROWOPEN }
| "]->"
{ ARROWCLOSE }
| "="
{ EQ }
| "("
{ LPAREN }
| ")"
{ RPAREN }
| "+"
{ ADDOP OpAdd }
| "-"
{ ADDOP OpSub }
| "*"
{ STAR }
| "/"
{ MULOP OpDiv }
| "|"
{ BAR }
| ","
{ COMMA }
| ";;"
{ SEMISEMI }
| ":"
{ COLON }
| "'"
{ QUOTE }
| "!"
{ BANG }
| "["
{ LBRACK }
| "]"
{ RBRACK }
| (lowercase identchar *) as x
{ try Hashtbl.find keywords x with Not_found -> IDENT x }
| (uppercase identchar *) as x
{ UIDENT x }
| digit+ as i
{ try
INTLITERAL (int_of_string i)
with Failure _ ->
error (place lexbuf) "invalid integer literal." }
| "(*"
{ ocamlcomment (place lexbuf) lexbuf; token lexbuf }
| newline
{ new_line lexbuf; token lexbuf }
| whitespace+
{ token lexbuf }
| eof
{ EOF }
| _ as c
{ error (place lexbuf) "unexpected character: '%c'." c }
(* ------------------------------------------------------------------------ *)
(* Skip OCaml-style comments. Comments can be nested. This sub-lexer is
parameterized with the place of the opening comment, so if an unterminated
comment is detected, we can show where it was opened. *)
and ocamlcomment p = parse
| "*)"
{ () }
| "(*"
{ ocamlcomment (place lexbuf) lexbuf; ocamlcomment p lexbuf }
| newline
{ new_line lexbuf; ocamlcomment p lexbuf }
| eof
{ error p "unterminated comment." }
| _
{ ocamlcomment p lexbuf }