Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions OFL.g4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
grammar OFL;


root: expression? EOF;
root: WS* expression? WS* EOF;

expression
: po expression pc
Expand Down Expand Up @@ -90,19 +90,19 @@ OR: 'or';
NOT: 'not';
IN: 'in';

and: WS? AND WS?;
or: WS? OR WS?;
not: WS? NOT WS?;
in : WS? IN WS?;
and: WS* AND WS*;
or: WS* OR WS*;
not: WS* NOT WS*;
in : WS* IN WS*;

eq: WS? '=' WS?;
ne: WS? '!=' WS?;
po: WS? '(' WS?;
pc: WS? ')' WS?;
co: WS? ',' WS?;
dd: WS? '..' WS?;
cn: WS? ':' WS?;
tl: WS? '~' WS?;
eq: WS* '=' WS*;
ne: WS* '!=' WS*;
po: WS* '(' WS*;
pc: WS* ')' WS*;
co: WS* ',' WS*;
dd: WS* '..' WS*;
cn: WS* ':' WS*;
tl: WS* '~' WS*;

WILDCARD: '*';

Expand Down Expand Up @@ -131,10 +131,11 @@ QUOTED: '"' CHARACTER+ '"';
range_int: po (NUMBER dd NUMBER | dd NUMBER | NUMBER dd ) pc;
range_dec: po ((NUMBER | DECIMAL) dd (NUMBER | DECIMAL) | dd (NUMBER | DECIMAL) | (NUMBER | DECIMAL) dd) pc;

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

fragment NUMERAL: [0-9];
fragment LETTER: [-_a-zA-Z0-9];
fragment CHARACTER: ~["\\\r\n] | EscapeSequence;
fragment EscapeSequence: '\\' ["rn\\];

fragment CHARACTER: ~["\\\r\n] | ESCAPE_SEQUENCE;
fragment ESCAPE_SEQUENCE: '\\' ["rn\\];
252 changes: 133 additions & 119 deletions ohsome_filter_to_sql/OFLLexer.py

Large diffs are not rendered by default.

1,107 changes: 611 additions & 496 deletions ohsome_filter_to_sql/OFLParser.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
waterway=stream /* inline
comment
with
linebreaks */ or waterway=river

tags @> '{"waterway": "stream"}' OR tags @> '{"waterway": "river"}'

[<Record count=22746>]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
highway in (unclassified, road /* outdated tag */, service)

tags -> 'highway' IN ('"unclassified"', '"road"', '"service"')

[<Record count=226328>]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
waterway=stream
// full line comment
or waterway=river

tags @> '{"waterway": "stream"}' OR tags @> '{"waterway": "river"}'

[<Record count=22746>]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
natural=tree // this is a tree

tags @> '{"natural": "tree"}'

[<Record count=98199>]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
key ~ foo*bar

line 1:10 extraneous input 'bar' expecting <EOF>
line 1:10 extraneous input 'bar' expecting {<EOF>, WS}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id:node/ 4540889804

line 1:7 token recognition error at: '/'
line 1:7 token recognition error at: '/ '
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id:node /4540889804

line 1:8 token recognition error at: '/'
line 1:8 token recognition error at: '/4'
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id:node / 4540889804

line 1:8 token recognition error at: '/'
line 1:8 token recognition error at: '/ '
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
natural=tree

tags @> '{"natural": "tree"}'

[<Record count=98199>]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
natural=tree

tags @> '{"natural": "tree"}'

[<Record count=98199>]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
natural=tree

tags @> '{"natural": "tree"}'

[<Record count=98199>]
1 change: 1 addition & 0 deletions tests/cassettes/test_main.py--test_comments.cassette.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"3915070601": {"results": [{"count": 22746}], "query": "SELECT COUNT(*) FROM contributions WHERE tags @> '{\"waterway\": \"stream\"}' OR tags @> '{\"waterway\": \"river\"}'", "args": [], "limit": 0, "timeout": null, "record_class": null}, "2406030824": {"results": [{"count": 226328}], "query": "SELECT COUNT(*) FROM contributions WHERE tags -> 'highway' IN ('\"unclassified\"', '\"road\"', '\"service\"')", "args": [], "limit": 0, "timeout": null, "record_class": null}, "1868929949": {"results": [{"count": 98199}], "query": "SELECT COUNT(*) FROM contributions WHERE tags @> '{\"natural\": \"tree\"}'", "args": [], "limit": 0, "timeout": null, "record_class": null}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"1868929949": {"results": [{"count": 98199}], "query": "SELECT COUNT(*) FROM contributions WHERE tags @> '{\"natural\": \"tree\"}'", "args": [], "limit": 0, "timeout": null, "record_class": null}, "550655163": {"results": [{"count": 98199}], "query": "SELECT COUNT(*) FROM contributions WHERE tags @> '{\"natural\": \"tree\"}'", "args": [], "limit": 0, "timeout": null, "record_class": null}}
30 changes: 30 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ async def test_build_tree(filter):
#


@pytest.mark.parametrize(
"filter",
(
"natural=tree // this is a tree",
"highway in (unclassified, road /* outdated tag */, service)",
"waterway=stream\n// full line comment\nor waterway=river",
"waterway=stream /* inline\ncomment\nwith\nlinebreaks */ or waterway=river",
# TODO: It would be nice to not have to quote urls
"website=https://heigit.org", # unquoted
"long_name=name//with//slashes", # unquoted
),
)
async def test_comments(filter):
sql = ohsome_filter_to_sql(filter)
assert await validate_and_verify(sql, filter)


@pytest.mark.parametrize(
"filter",
(
" natural=tree ",
" natural=tree",
"natural=tree ",
),
)
async def test_untrimmed_whitespace(filter):
sql = ohsome_filter_to_sql(filter)
assert await validate_and_verify(sql, filter)


async def test_expression_and_expression():
filter = "natural=tree and leaf_type=broadleaved"
sql = ohsome_filter_to_sql(filter)
Expand Down