-
Notifications
You must be signed in to change notification settings - Fork 9
/
input_lexer.mll
135 lines (130 loc) · 3.51 KB
/
input_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
{
open Input_parser
open Lexing
exception SyntaxError of string
let resolve_id =
let table = Hashtbl.create 100 in
let () = List.iter (fun (k, v) -> Hashtbl.add table k v) [
"Constants", CONSTANTS;
"constants", CONSTANTS;
"Variables", VARIABLES;
"variables", VARIABLES;
"Definitions", DEFINITIONS;
"definitions", DEFINITIONS;
"Constraints", CONSTRAINTS;
"constraints", CONSTRAINTS;
"Expressions", EXPRESSIONS;
"expressions", EXPRESSIONS;
"IN", IN;
"in", IN;
"int", INT;
"real", REAL;
"float16", FLOAT(16);
"float32", FLOAT(32);
"float64", FLOAT(64);
"float128", FLOAT(128);
"rnd", RND;
"no_rnd", NO_RND;
"rnd16_ne", RND_PAR(16, "ne");
"rnd16", RND_PAR(16, "ne");
"rnd16_0", RND_PAR(16, "zero");
"rnd16_down", RND_PAR(16, "down");
"rnd16_up", RND_PAR(16, "up");
"rnd32_ne", RND_PAR(32, "ne");
"rnd32", RND_PAR(32, "ne");
"rnd32_0", RND_PAR(32, "zero");
"rnd32_down", RND_PAR(32, "down");
"rnd32_up", RND_PAR(32, "up");
"rnd64_ne", RND_PAR(64, "ne");
"rnd64", RND_PAR(64, "ne");
"rnd64_0", RND_PAR(64, "zero");
"rnd64_down", RND_PAR(64, "down");
"rnd64_up", RND_PAR(64, "up");
"rnd128_ne", RND_PAR(128, "ne");
"rnd128", RND_PAR(128, "ne");
"rnd128_0", RND_PAR(128, "zero");
"rnd128_down", RND_PAR(128, "down");
"rnd128_up", RND_PAR(128, "up");
"inv", INV;
"abs", ABS;
"fma", FMA;
"sqrt", SQRT;
"min", MIN;
"max", MAX;
"exp", EXP;
"log", LOG;
"cos", COS;
"sin", SIN;
"tan", TAN;
"cosh", COSH;
"sinh", SINH;
"tanh", TANH;
"acos", ACOS;
"asin", ASIN;
"atan", ATAN;
"atan2", ATAN2;
"arccos", ACOS;
"arcsin", ASIN;
"arctan", ATAN;
"acosh", ACOSH;
"asinh", ASINH;
"atanh", ATANH;
"arsinh", ASINH;
"arcosh", ACOSH;
"artanh", ATANH;
"arcsinh", ASINH;
"arccosh", ACOSH;
"arctanh", ATANH;
"argsinh", ASINH;
"argcosh", ACOSH;
"argtanh", ATANH;
"sub2", SUB2;
"floor_power2", FLOOR_POWER2;
"interval", INTERVAL;
] in
fun str ->
try Hashtbl.find table str
with Not_found -> ID str
}
let ws = [' ' '\t']+
let newline = '\n' | '\r' | "\r\n"
let digit = ['0'-'9']
let hex_digit = ['0'-'9' 'a'-'f' 'A'-'F']
let alpha = ['a'-'z' 'A'-'Z' '_' '$']
let id = alpha (alpha | digit)*
let numeral = (("0x" (hex_digit+ ('.' hex_digit*)?)) |
(digit+ ('.' digit*)?))
(['e' 'p'] ['-' '+']? digit+)?
let double_numeral = numeral 'd'
let single_numeral = numeral 'f'
rule token = parse
| "//" [^ '\n']*
| ws { token lexbuf }
| newline { Lexing.new_line lexbuf; token lexbuf }
| single_numeral as str { SINGLE_NUMERAL (String.sub str 0 (String.length str - 1)) }
| double_numeral as str { DOUBLE_NUMERAL (String.sub str 0 (String.length str - 1)) }
| numeral as str { NUMBER str }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LBRACKET }
| ']' { RBRACKET }
| '{' { LBRACE }
| '}' { RBRACE }
| ';' { SEMICOLON }
| ':' { COLON }
| ',' { COMMA }
| '+' { PLUS }
| '-' { MINUS }
| '*' { MULT }
| '/' { DIVIDE }
| '^' { POW }
| '=' { EQ }
| "<=" { LE }
| ">=" { GE }
| "<" { LT }
| ">" { GT }
| "+/-" { PLUS_MINUS }
| "%e" { E_CONST }
| id as str { resolve_id str }
| _ { raise (SyntaxError ("Unexpected char: " ^ Lexing.lexeme lexbuf)) }
| eof { EOF }