Skip to content

Commit 18dd472

Browse files
authored
Improve errors message by improving the grammar definition (#267)
Signed-off-by: Christian Vetter <christian.vetter@here.com>
1 parent 77ca7b2 commit 18dd472

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

flatdata-generator/flatdata/generator/grammar.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@
99
Word, alphas, alphanums, nums, cppStyleComment,
1010
Keyword, Group, Optional, Or, OneOrMore, delimitedList, ZeroOrMore,
1111
hexnums, Combine, FollowedBy, ParseException as pyparsingParseException,
12-
ParseResults, QuotedString, Suppress, Literal
12+
ParseResults, QuotedString, Suppress, Literal, Regex
1313
)
1414

1515
ParseException = pyparsingParseException
1616

17-
identifier = Word(alphas + "_", alphas + alphanums + "_")
18-
qualified_identifier = Word(alphas + "_.", alphas + alphanums + "_.")
17+
identifier = Word(alphas + "_", alphas + alphanums + "_").set_name("identifier")
18+
19+
qualified_identifier = Regex(r'\.?[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*').set_name("qualified name")
1920

2021
BASIC_TYPES = ["bool", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64"]
21-
basic_type = Or([Keyword(t) for t in BASIC_TYPES])
22+
basic_type = Or([Keyword(t) for t in BASIC_TYPES]).set_name(
23+
"type (one of: " + ", ".join(BASIC_TYPES) + ")")
2224

23-
bit_width = Word(nums)
25+
bit_width = Word(nums).set_name("bit width")
2426

25-
dec_literal = Word(nums)
26-
hex_literal = Combine("0x" + Word(hexnums))
27-
signed_literal = Combine(Optional('-') + (dec_literal ^ hex_literal))
27+
dec_literal = Word(nums).set_name("decimal literal")
28+
hex_literal = Combine("0x" + Word(hexnums)).set_name("hex literal")
29+
signed_literal = Combine(Optional('-') + (dec_literal ^ hex_literal)).set_name("integer literal")
2830

2931
comment = cppStyleComment
3032

@@ -36,7 +38,7 @@
3638

3739
enum = Group(
3840
Optional(comment)("doc") +
39-
Keyword("enum") +
41+
Keyword("enum") -
4042
identifier("name") + ':' + basic_type("type") + Optional(':' + bit_width("width")) +
4143
'{' +
4244
delimitedList(enumValue, ",")("enum_values") +
@@ -179,7 +181,7 @@ def _combine_list(t: ParseResults) -> str:
179181

180182
constant = Group(
181183
Optional(comment)("doc") +
182-
Keyword("const") +
184+
Keyword("const") -
183185
basic_type("type") +
184186
identifier("name") + "=" +
185187
signed_literal("value") +
@@ -196,7 +198,7 @@ def _combine_list(t: ParseResults) -> str:
196198

197199
namespace = Group(
198200
Optional(comment)("doc") +
199-
Keyword("namespace") +
201+
Keyword("namespace") -
200202
qualified_identifier("name") +
201203
"{" +
202204
ZeroOrMore(flatdata_entry) + "}" +
@@ -205,7 +207,7 @@ def _combine_list(t: ParseResults) -> str:
205207

206208
import_statement = Group(
207209
Optional(comment)("doc") +
208-
Suppress(Keyword("import")) +
210+
Suppress(Keyword("import")) -
209211
QuotedString('"')("path") +
210212
Suppress(Literal(';'))
211213
)

0 commit comments

Comments
 (0)