-
Notifications
You must be signed in to change notification settings - Fork 28
/
lexer.mll
166 lines (157 loc) · 7.08 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(****************************************************************
* ASL lexer
*
* Copyright Arm Limited (c) 2017-2019
* SPDX-Licence-Identifier: BSD-3-Clause
****************************************************************)
{
open Asl_parser (* The type token is defined in parser.mli *)
open Asl_ast
exception Eof
let keywords : (string * Asl_parser.token) list = [
("AND", AND);
("CONSTRAINED_UNPREDICTABLE", CONSTRAINED_UNDERSCORE_UNPREDICTABLE);
("DIV", DIV);
("EOR", EOR);
("IMPLEMENTATION_DEFINED", IMPLEMENTATION_UNDERSCORE_DEFINED);
("IN", IN);
("IFF", IFF);
("IMPLIES", IMPLIES);
("MOD", MOD);
("NOT", NOT);
("OR", OR);
("QUOT", QUOT);
("REM", REM);
("SEE", SEE);
("UNDEFINED", UNDEFINED);
("UNKNOWN", UNKNOWN);
("UNPREDICTABLE", UNPREDICTABLE);
("__ExceptionTaken", UNDERSCORE_UNDERSCORE_EXCEPTIONTAKEN);
("__NOP", UNDERSCORE_UNDERSCORE_NOP);
("__UNALLOCATED", UNDERSCORE_UNDERSCORE_UNALLOCATED);
("__UNPREDICTABLE", UNDERSCORE_UNDERSCORE_UNPREDICTABLE);
("__array", UNDERSCORE_UNDERSCORE_ARRAY);
("__builtin", UNDERSCORE_UNDERSCORE_BUILTIN);
("__conditional", UNDERSCORE_UNDERSCORE_CONDITIONAL);
("__config", UNDERSCORE_UNDERSCORE_CONFIG);
("__decode", UNDERSCORE_UNDERSCORE_DECODE);
("__encoding", UNDERSCORE_UNDERSCORE_ENCODING);
("__event", UNDERSCORE_UNDERSCORE_EVENT);
("__execute", UNDERSCORE_UNDERSCORE_EXECUTE);
("__field", UNDERSCORE_UNDERSCORE_FIELD);
("__function", UNDERSCORE_UNDERSCORE_FUNCTION);
("__guard", UNDERSCORE_UNDERSCORE_GUARD);
("__instruction", UNDERSCORE_UNDERSCORE_INSTRUCTION);
("__instruction_set", UNDERSCORE_UNDERSCORE_INSTRUCTION_UNDERSCORE_SET);
("__map", UNDERSCORE_UNDERSCORE_MAP);
("__newmap", UNDERSCORE_UNDERSCORE_NEWMAP);
("__newevent", UNDERSCORE_UNDERSCORE_NEWEVENT);
("__operator1", UNDERSCORE_UNDERSCORE_OPERATOR_ONE);
("__operator2", UNDERSCORE_UNDERSCORE_OPERATOR_TWO);
("__opcode", UNDERSCORE_UNDERSCORE_OPCODE);
("__postdecode", UNDERSCORE_UNDERSCORE_POSTDECODE);
("__readwrite", UNDERSCORE_UNDERSCORE_READWRITE);
("__register", UNDERSCORE_UNDERSCORE_REGISTER);
("__unpredictable_unless", UNDERSCORE_UNDERSCORE_UNPREDICTABLE_UNDERSCORE_UNLESS);
("__write", UNDERSCORE_UNDERSCORE_WRITE);
("array", ARRAY);
("assert", ASSERT);
("bits", BITS);
("case", CASE);
("catch", CATCH);
("constant", CONSTANT);
("do", DO);
("downto", DOWNTO);
("else", ELSE);
("elsif", ELSIF);
("enumeration", ENUMERATION);
("for", FOR);
("if", IF);
("is", IS);
("of", OF);
("otherwise", OTHERWISE);
("record", RECORD);
("repeat", REPEAT);
("return", RETURN);
("then", THEN);
("throw", THROW);
("to", TO);
("try", TRY);
("type", TYPE);
("typeof", TYPEOF);
("until", UNTIL);
("when", WHEN);
("while", WHILE);
]
}
rule token = parse
(* whitespace and comments *)
| ['\n'] { Lexing.new_line lexbuf; EOL1 }
| [' ' '\t'] { token lexbuf }
| '/' '/' [^'\n']* { token lexbuf }
| '/' '*' { comment 1 lexbuf }
(* numbers, strings and identifiers *)
| '"' [^'"']* '"' as lxm { STRINGLIT(String.sub lxm 1 (String.length lxm - 2)) }
| '\'' ['0' '1' ' ']* '\'' as lxm { BITSLIT(String.sub lxm 1 (String.length lxm - 2)) }
| '\'' ['0' '1' 'x' ' ']* '\'' as lxm { MASKLIT(String.sub lxm 1 (String.length lxm - 2)) }
| '0''x'['0'-'9' 'A' - 'F' 'a'-'f' '_']+ as lxm { HEXLIT(String.sub lxm 2 (String.length lxm - 2)) }
| ['0'-'9']+ '.' ['0'-'9']+ as lxm { REALLIT(lxm) }
| ['0'-'9']+ as lxm { INTLIT(lxm) }
| ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_']* as lxm {
( match List.assoc_opt lxm keywords with
| Some x -> x
| None -> if isTypeIdent(lxm) then TYPEID(lxm)
else if String.equal lxm "AArch32" then QUALIFIER(lxm)
else if String.equal lxm "AArch64" then QUALIFIER(lxm)
else ID(lxm)
)
}
(* delimiters *)
| '!' { BANG }
| '!' '=' { BANG_EQ }
| '&' '&' { AMPERSAND_AMPERSAND }
| '&' { AMPERSAND }
| '(' { LPAREN }
| ')' { RPAREN }
| '*' { STAR }
| '+' '+' { PLUS_PLUS }
| '+' { PLUS }
| '+' ':' { PLUS_COLON }
| ',' { COMMA }
| '-' { MINUS }
| '.' { DOT }
| '.' '.' { DOT_DOT }
| '/' { SLASH }
| ':' { COLON }
| ';' { SEMICOLON }
| '<' { LT }
| '<' '<' { LT_LT }
| '<' '=' { LT_EQ }
| '=' { EQ }
| '=' '=' { EQ_EQ }
| '=' '>' { EQ_GT }
| '>' { GT }
| '>' '=' { GT_EQ }
| '>' '>' { GT_GT }
| '[' { LBRACK }
| ']' { RBRACK }
| '^' { CARET }
| '{' { LBRACE }
| '{' '{' { LBRACE_LBRACE }
| '|' '|' { BAR_BAR }
| '}' { RBRACE }
| '}' '}' { RBRACE_RBRACE }
| eof { raise Eof }
| _ as c { Printf.printf "%s:%d Unrecognized character '%c'\n"
lexbuf.lex_curr_p.pos_fname
lexbuf.lex_curr_p.pos_lnum
c;
exit 0 }
and comment depth = parse
'/' '*' { comment (depth+1) lexbuf }
| '*' '/' { if depth = 1 then token lexbuf else comment (depth-1) lexbuf }
| '\n' { Lexing.new_line lexbuf; comment depth lexbuf }
| _ { comment depth lexbuf }
(****************************************************************
* End
****************************************************************)