-
Notifications
You must be signed in to change notification settings - Fork 0
/
objlanglexer.mll
89 lines (83 loc) · 1.69 KB
/
objlanglexer.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
{
open Lexing
open Objlangparser
let keyword_or_ident =
let h = Hashtbl.create 17 in
List.iter (fun (s, k) -> Hashtbl.add h s k)
[ "putchar", PUTCHAR;
"if", IF;
"else", ELSE;
"while", WHILE;
"true", BOOL true;
"false", BOOL false;
"var", VAR;
"function", FUNCTION;
"class", CLASS;
"attribute", ATTRIBUTE;
"method", METHOD;
"this", THIS;
"extends", EXTENDS;
"new", NEW;
"return", RETURN;
"int", TYP_INT;
"bool", TYP_BOOL;
"void", TYP_VOID;
] ;
fun s ->
try Hashtbl.find h s
with Not_found -> IDENT(s)
}
let digit = ['0'-'9']
let number = ['-']? digit+
let alpha = ['a'-'z' 'A'-'Z']
let ident = ['a'-'z' '_'] (alpha | '_' | digit)*
rule token = parse
| ['\n']
{ new_line lexbuf; token lexbuf }
| [' ' '\t' '\r']+
{ token lexbuf }
| "//" [^ '\n']* "\n"
{ new_line lexbuf; token lexbuf }
| "/*"
{ comment lexbuf; token lexbuf }
| number as n
{ CST(int_of_string n) }
| ident as id
{ keyword_or_ident id }
| ";"
{ SEMI }
| "="
{ SET }
| "+"
{ PLUS }
| "*"
{ STAR }
| "<"
{ LT }
| "("
{ LPAR }
| ")"
{ RPAR }
| "{"
{ BEGIN }
| "}"
{ END }
| "["
{ LBRACKET }
| "]"
{ RBRACKET }
| "."
{ DOT }
| ","
{ COMMA }
| _
{ failwith ("Unknown character : " ^ (lexeme lexbuf)) }
| eof
{ EOF }
and comment = parse
| "*/"
{ () }
| _
{ comment lexbuf }
| eof
{ failwith "unfinished comment" }