-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_lex.mll
More file actions
70 lines (64 loc) · 1.38 KB
/
ml_lex.mll
File metadata and controls
70 lines (64 loc) · 1.38 KB
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
(* header section *)
{
open Ml_parse
open Lexing
let incr_lineno lexbuf =
let pos = lexbuf.lex_curr_p in
lexbuf.lex_curr_p <- { pos with
pos_lnum = pos.pos_lnum + 1;
pos_bol = pos.pos_cnum;
}
let inc x = x := (!x) + 1
let dec x = x := (!x) - 1
let commentLevel = ref 0
let commentStart = ref 0
}
(* definition section *)
let cr='\013'
let nl='\010'
let eol=(cr nl|nl|cr)
let ws=('\012'|'\t'|' ')*
let digit=['0'-'9']
let id = ['a'-'z''_']['a'-'z''A'-'Z''0'-'9''_''\'']*
(* rules section *)
rule lexer = parse
| eol { incr_lineno lexbuf; lexer lexbuf }
| ws+ { lexer lexbuf }
| digit+ { INT(int_of_string(Lexing.lexeme lexbuf)) }
| "::" { CONS }
| '~' { TILDE }
| '+' { PLUS }
| '*' { TIMES }
| '-' { MINUS }
| '/' { DIV }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LBRACKET }
| ']' { RBRACKET }
| '<' { LT }
| "->" { DARROW }
| '=' { EQUALS }
| ',' { COMMA }
| ';' { SEMI }
| "else" { ELSE }
| "false" { FALSE }
| "fun" { FN }
| "fst" { FST }
| "hd" { HD }
| "if" { IF }
| "in" { IN }
| "isnil" { ISNIL }
| "let" { LET }
| "nil" { NIL }
| "snd" { SND }
| "then" { THEN }
| "tl" { TL }
| "true" { TRUE }
| id { ID(Lexing.lexeme lexbuf) }
| "(*" { comment lexbuf }
| eof { EOF }
and comment = parse
| "(*" { inc commentLevel; comment lexbuf }
| eol {incr_lineno lexbuf; comment lexbuf }
| "*)" { dec commentLevel; if (!commentLevel) = 0 then lexer lexbuf else comment lexbuf }
| _ { comment lexbuf }