Skip to content

Commit fabf50b

Browse files
committed
allow C-style comments
examples: * single line comment: type:node and natural=tree // this selects all trees * inline comment highway in (unclassified, road /* outdated tag */, service)
1 parent 1b7b4ad commit fabf50b

8 files changed

+793
-636
lines changed

OFL.g4

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
grammar OFL;
22

33

4-
root: expression? EOF;
4+
root: WS* expression? WS* EOF;
55

66
expression
77
: po expression pc
@@ -90,19 +90,19 @@ OR: 'or';
9090
NOT: 'not';
9191
IN: 'in';
9292

93-
and: WS? AND WS?;
94-
or: WS? OR WS?;
95-
not: WS? NOT WS?;
96-
in : WS? IN WS?;
93+
and: WS* AND WS*;
94+
or: WS* OR WS*;
95+
not: WS* NOT WS*;
96+
in : WS* IN WS*;
9797

98-
eq: WS? '=' WS?;
99-
ne: WS? '!=' WS?;
100-
po: WS? '(' WS?;
101-
pc: WS? ')' WS?;
102-
co: WS? ',' WS?;
103-
dd: WS? '..' WS?;
104-
cn: WS? ':' WS?;
105-
tl: WS? '~' WS?;
98+
eq: WS* '=' WS*;
99+
ne: WS* '!=' WS*;
100+
po: WS* '(' WS*;
101+
pc: WS* ')' WS*;
102+
co: WS* ',' WS*;
103+
dd: WS* '..' WS*;
104+
cn: WS* ':' WS*;
105+
tl: WS* '~' WS*;
106106

107107
WILDCARD: '*';
108108

@@ -131,10 +131,11 @@ QUOTED: '"' CHARACTER+ '"';
131131
range_int: po (NUMBER dd NUMBER | dd NUMBER | NUMBER dd ) pc;
132132
range_dec: po ((NUMBER | DECIMAL) dd (NUMBER | DECIMAL) | dd (NUMBER | DECIMAL) | (NUMBER | DECIMAL) dd) pc;
133133

134-
WS: [ \t\r\n]+;
134+
COMMENT: '/*' .*? '*/' -> skip;
135+
LINE_COMMENT: '//' ~[\n\r]* ('\r'? '\n' | EOF) -> skip;
136+
WS: [ \t\r\n];
135137

136138
fragment NUMERAL: [0-9];
137139
fragment LETTER: [-_a-zA-Z0-9];
138-
fragment CHARACTER: ~["\\\r\n] | EscapeSequence;
139-
fragment EscapeSequence: '\\' ["rn\\];
140-
140+
fragment CHARACTER: ~["\\\r\n] | ESCAPE_SEQUENCE;
141+
fragment ESCAPE_SEQUENCE: '\\' ["rn\\];

ohsome_filter_to_sql/OFLLexer.py

Lines changed: 133 additions & 119 deletions
Large diffs are not rendered by default.

ohsome_filter_to_sql/OFLParser.py

Lines changed: 611 additions & 496 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
key ~ foo*bar
22

3-
line 1:10 extraneous input 'bar' expecting <EOF>
3+
line 1:10 extraneous input 'bar' expecting {<EOF>, WS}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
id:node/ 4540889804
22

3-
line 1:7 token recognition error at: '/'
3+
line 1:7 token recognition error at: '/ '
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
id:node /4540889804
22

3-
line 1:8 token recognition error at: '/'
3+
line 1:8 token recognition error at: '/4'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
id:node / 4540889804
22

3-
line 1:8 token recognition error at: '/'
3+
line 1:8 token recognition error at: '/ '

tests/test_main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ async def test_build_tree(filter):
6262
#
6363

6464

65+
@pytest.mark.parametrize(
66+
"filter",
67+
(
68+
"natural=tree // this is a tree",
69+
"highway in (unclassified, road /* outdated tag */, service)",
70+
"waterway=stream\n// full line comment\nor waterway=river",
71+
"waterway=stream /* inline\ncomment\nwith\nlinebreaks */ or waterway=river",
72+
),
73+
)
74+
async def test_comments(filter):
75+
sql = ohsome_filter_to_sql(filter)
76+
assert await validate_and_verify(sql, filter)
77+
78+
79+
@pytest.mark.parametrize(
80+
"filter",
81+
(
82+
" natural=tree ",
83+
" natural=tree",
84+
"natural=tree ",
85+
),
86+
)
87+
async def test_untrimmed_whitespace(filter):
88+
sql = ohsome_filter_to_sql(filter)
89+
assert await validate_and_verify(sql, filter)
90+
91+
6592
async def test_expression_and_expression():
6693
filter = "natural=tree and leaf_type=broadleaved"
6794
sql = ohsome_filter_to_sql(filter)

0 commit comments

Comments
 (0)