forked from progbits/spacebar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
141 lines (133 loc) · 3.05 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
{
open Parser
let keywords = [
("alignof", ALIGNOF);
("auto", AUTO);
("break", BREAK);
("case", CASE);
("char", CHAR);
("const", CONST);
("continue", CONTINUE);
("default", DEFAULT);
("do", DO);
("double", DOUBLE);
("else", ELSE);
("enum", ENUM);
("extern", EXTERN);
("float", FLOAT);
("for", FOR);
("goto", GOTO);
("if", IF);
("inline", INLINE);
("int", INT);
("long", LONG);
("register", REGISTER);
("restrict", RESTRICT);
("return", RETURN);
("short", SHORT);
("signed", SIGNED);
("sizeof", SIZEOF);
("static", STATIC);
("struct", STRUCT);
("switch", SWITCH);
("typedef", TYPEDEF);
("union", UNION);
("unsigned", UNSIGNED);
("void", VOID);
("volatile", VOLATILE);
("while", WHILE);
("_alignas", ALIGNAS);
("_atomic", ATOMIC);
("_bool", BOOL);
("_complex", COMPLEX);
("_generic", GENERIC);
("_imaginary", IMAGINARY);
("_noreturn", NORETURN);
("_static_assert", STATIC_ASSERT);
("_thread_local", THREAD_LOCAL);
]
let is_keyword word =
let found = List.find_opt (fun keyword -> (fst keyword) = word) keywords in
match found with
| Some keyword -> Some (snd keyword)
| None -> None
let buffer = Buffer.create 2048
}
(* 6.4.2 *)
let identifier = ['_' 'a'-'z' 'A'-'Z'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']*
(* 6.4.4 *)
let integer_constant = ['0'-'9']+
rule token = parse
| [' ' '\t' '\n'] { token lexbuf }
| integer_constant as i
{ CONSTANT (int_of_string i) }
| identifier as id {
match is_keyword id with
| Some keyword -> keyword
| None -> IDENTIFIER id
}
| "\"" {
Buffer.clear buffer;
string lexbuf;
STRING_LITERAL (Buffer.contents buffer)
}
| "[" { LBRACKET }
| "]" { RBRACKET }
| "(" { LPAREN }
| ")" { RPAREN }
| "{" { LBRACE }
| "}" { RBRACE }
| "." { DOT }
(* | "->" ->
| "++" ->
| "--" -> *)
| "&" { AMPERSAND }
| "*" { STAR }
| "+" { PLUS }
| "-" { MINUS }
| "~" { TILDE }
| "!" { BANG }
| "/" { SLASH }
| "%" { MOD }
| "++" { INC }
| "--" { DEC }
(* | "<<" ->
| ">>" -> *)
| "<" { LT }
| ">" { GT }
| "<=" { LTE }
| ">=" { GTE }
| "==" { EQ }
| "!=" { NEQ }
| "^" { HAT }
| "|" { PIPE }
| "&&" { AND }
| "||" { OR }
| "?" { CONDITIONAL }
| ":" { COLON }
| ";" { SEMICOLON }
| "..." { ELLIPSIS }
| "=" { ASSIGN }
(* | "*= " ->
| "/= " ->
| "%= " ->
| "+= " ->
| "-= " ->
| "<<= " ->
| ">>= " ->
| "&= " ->
| "^= " ->
| "|=" -> *)
| "," { COMMA }
(* | "#" ->
| "##" -> *)
| eof { EOF }
and string = parse
| "\"" {
()
}
| _ as c {
Buffer.add_char buffer c;
string lexbuf
}
{}