-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
167 lines (154 loc) · 4.76 KB
/
grammar.txt
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
167
class json::C_Parser
lexid Spaces
//
// Output Options
//
%ERROR_TOKEN
%CONTEXT [[bux::C_ParserOStreamCount]]
%ON_ERROR [[
$c.log(LL_ERROR, $pos, $message);
]]
%HEADERS_FOR_CPP [[
#include "JsonObjs.h" // JSON object types
]]
%SCANNEROPTION [[
%NAMEPREFIX JSON
float_or_int = ('.' dec_digit+ | dec_num {'.' {dec_digit+}}) {[eE] {'+'|'-'} dec_digit+} >>createFloatOrInt<CHAR_TYPE>
]]
%HEADERS_FOR_SCANNER_CPP [[
#include "JsonObjs.h" // json::jint, json::jfloat
#include <charconv> // std::from_chars()
]]
%LOCALS_FOR_SCANNER_CPP [[
template<class T_LexCh>
auto createFloatOrInt(const T_LexCh *c, size_t n)
{
const auto s = bux::toString(c,0,n);
const auto send = s.data() + s.size();
jint i;
if (std::from_chars(s.data(), send, i).ptr == send)
return bux::C_ActionRet{TID_LEX_Num, bux::createLex(i)};
#if 1
char *end;
jfloat f = strtod(s.c_str(), &end);
if (end != send)
RUNTIME_ERROR("Not a float: {}", s);
return bux::C_ActionRet{TID_LEX_Float, bux::createLex(f)};
#else
jfloat f;
auto t = std::from_chars(s.data(), send, f); // not supported by mainstream compilers yet
if (t.ptr == send)
return bux::C_ActionRet{TID_LEX_Float, bux::createLex(f)};
if (t.ptr == s.data())
{
const auto ecode = make_error_code(t.ec);
RUNTIME_ERROR("Error: {}:{}", ecode.category().name(), ecode.value());
}
else
RUNTIME_ERROR("Not a float: {}", s);
#endif
}
]]
%EXTRA_TOKENS [[spaces|c_str|float_or_int]]
//%SHOW_UNDEFINED
//
// Grammar with Reduction Code
//
// 1. Taken from https://www.json.org/
// 2. The following nonterminals are shortcutted:
// - <element> is value
// - <object> is list of members
// - <array> is list of elements
<@> ::= <value> [[
$r = $1;
]]
<value> ::= { } [[
$r = bux::createLex<json::value>(json::object());
]]
<value> ::= { <members> } [[
$r = bux::createLex<json::value>(bux::unlex<json::object>($2));
]]
<value> ::= [ ] [[
$r = bux::createLex<json::value>(json::array());
]]
<value> ::= [ <elements> ] [[
$r = bux::createLex<json::value>(bux::unlex<json::array>($2));
]]
<value> ::= $String [[
$r = bux::createLex<json::value>(bux::unlex<std::string>($1));
]]
<value> ::= $Num [[
$r = bux::createLex<json::value>(bux::unlex<jint>($1));
]]
<value> ::= - $Num [[
auto &t = bux::unlex<jint>($2);
t = -t;
$r = bux::createLex<json::value>(t);
]]
<value> ::= $Float [[
$r = bux::createLex<json::value>(bux::unlex<jfloat>($1));
]]
<value> ::= - $Float [[
auto &t = bux::unlex<jfloat>($2);
t = -t;
$r = bux::createLex<json::value>(t);
]]
<value> ::= true [[
$r = bux::createLex<json::value>(true);
]]
<value> ::= false [[
$r = bux::createLex<json::value>(false);
]]
<value> ::= null [[
$r = bux::createLex<json::value>();
]]
<members> ::= <member> [[
json::object t;
auto &src = bux::unlex<std::pair<std::string,json::value>>($1);
t.emplace_back(std::move(src));
$r = bux::createLex(std::move(t));
]]
<members> ::= <members> , <member> [[
auto &src = bux::unlex<std::pair<std::string,json::value>>($3);
bux::unlex<json::object>($1).emplace_back(std::move(src));
$r = $1;
]]
<members> ::= <members> , [[
$c.log(LL_WARNING, $2, "Superfluous ','");
$r = $1;
]]
<member> ::= $String : <value> [[
$r = bux::createLex(std::pair{bux::unlex<std::string>($1), bux::unlex<json::value>($3)});
]]
<member> ::= $String : $Error [[
$p.onError($3, "Expect <value>");
$r = bux::createLex(std::pair{bux::unlex<std::string>($1), json::value{}});
]]
<member> ::= $String $Error [[
$p.onError($2, "Expect ':'");
$r = bux::createLex(std::pair{bux::unlex<std::string>($1), json::value{}});
]]
<member> ::= $Error <value> : <value> [[
$p.onError($1, "Only string key allowed");
$r = bux::createLex(std::pair{std::string{"NonStrKey__"}, bux::unlex<json::value>($4)});
]]
<elements> ::= <value> [[
json::array t;
t.emplace_back(bux::unlex<json::value>($1));
$r = bux::createLex(std::move(t));
]]
<elements> ::= <elements> , <value> [[
bux::unlex<json::array>($1).emplace_back(bux::unlex<json::value>($3));
$r = $1;
]]
<elements> ::= <elements> , $Error [[
$c.log(LL_WARNING, $2, "Superfluous ','");
$r = $1;
]]
//%ENABLE_TUPLE_
#ifdef ENABLE_TUPLE_
<value> ::= ( <elements> ) [[
$c.log(LL_INFO, $1, "Load tuple as array");
$r = bux::createLex<json::value>(bux::unlex<json::array>($2));
]]
#endif