-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathop-grammar.rl
163 lines (131 loc) · 3.83 KB
/
op-grammar.rl
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
%%{
machine FRAME;
include UUID "./uuid-grammar.rl";
action spec_start {
ps.omitted = 15;
}
action redef_uuid {
if (atm>0) {
atoms[atm] = atoms[atm-1];
}
}
action spec_uuid_start {
n = (int)(ABC[fc]);
hlf, dgt = VALUE, 0;
if (n < atm) {
// parse #op1#op2#op3 without Ragel state explosion
fnext *RON_start;
frame.position ++
p--;
fbreak;
} else {
// next UUID
atm = n;
ps.omitted -= 1<<uint(n);
}
}
action spec_uuid_end {
atm++;
}
action atom_start {
hlf, dgt = VALUE, 0;
atoms = append(atoms, Atom{})
}
action atom_end {
atm++;
}
action int_atom_start {
atoms[atm].setType(ATOM_INT)
atoms[atm].setFrom(p)
}
action float_atom_start {
atoms[atm].setType(ATOM_FLOAT)
atoms[atm].setFrom(p)
}
action string_atom_start {
atoms[atm].setType(ATOM_STRING)
atoms[atm].setFrom(p)
}
action scalar_atom_end {
atoms[atm].setTill(p)
atoms[atm].parseValue(frame.Body)
}
action uuid_atom_start {
if (atm==4) {
atoms[atm] = atoms[SPEC_OBJECT];
} else if (atoms[atm-1].Type()==ATOM_UUID) {
atoms[atm] = atoms[atm-1];
}
}
action uuid_atom_end {
atoms[atm][1] |= ((uint64)(ATOM_UUID))<<62;
}
action atoms_start {
atm = 4;
hlf = VALUE;
dgt = 0;
}
action atoms {
}
action opterm {
frame.term = int(ABC[fc]);
}
action op_start {
hlf = VALUE;
if (p>frame.Parser.off && frame.position!=-1) {
// one op is done, so stop parsing for now
// make sure the parser restarts with the next op
p--;
fnext *RON_start;
fbreak;
} else {
//op_idx++;
if (frame.term!=TERM_RAW) {
frame.term = TERM_REDUCED;
}
}
}
action op_end {
frame.position ++
}
action spec_end {
}
action frame_end {
fnext *RON_FULL_STOP;
fbreak;
}
# one of op spec UUIDs: type, object, event id or a reference
REDEF = "`" @redef_uuid;
QUANT = [*#@:] @spec_uuid_start ;
SPEC_UUID = QUANT space* REDEF? (UUID space*)? %spec_uuid_end ;
# 64-bit signed integer
INT_ATOM = ([\-+]? digit+ ) >int_atom_start %scalar_atom_end;
# 64-bit (double) float
FLOAT_ATOM = ( [\-+]? digit+ ("." digit+ ([eE] [\-+]? digit+)? | [eE] [\-+]? digit+ ) ) >float_atom_start %scalar_atom_end;
UUID_ATOM = UUID >uuid_atom_start %uuid_atom_end;
# JSON-escaped string
UNIESC = "\\u" [0-9a-fA-F]{4};
ESC = "\\" [^\n\r];
CHAR = [^"'\n\r\\];
STRING_ATOM = (UNIESC|ESC|CHAR)* %scalar_atom_end >string_atom_start;
# an atom (int, float, string or UUID)
ATOM = (
"=" space* INT_ATOM |
"^" space* FLOAT_ATOM |
['] STRING_ATOM ['] |
">" space* UUID_ATOM
) >atom_start %atom_end space*;
# op value - an atom, an atom tuple, or empty
ATOMS = ATOM+ %atoms >atoms_start;
# an optional op terminator (raw, reduced, header, query)
OPTERM = [,;!?] @opterm space*;
# a RON op "specifier" (four UUIDs for its type, object, event, and ref)
SPEC = SPEC_UUID+ >spec_start %spec_end ;
# a RON op
# op types: (0) raw op (1) reduced op (2) frame header (3) query header
OP = space* ( SPEC ATOMS? OPTERM? | ATOMS OPTERM? | OPTERM) $2 %1 >op_start %op_end;
# optional frame terminator; mandatory in the streaming mode
DOT = "." @frame_end;
# RON frame, including multiframes (those have more headers inside)
FRAME = OP* DOT? ;
}%%