Skip to content
Merged
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
8 changes: 8 additions & 0 deletions jsonquerylang/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def parse_operator(precedence_level: int = len(all_operators) - 1):
while True:
skip_whitespace()

if get_char() == '.' and 'pipe' in current_operators:
# an implicitly piped property like "fn().prop"
right = parse_property()
left = [*left, right] if left[0] == 'pipe' else ['pipe', left, right]
continue

start = i
name = parse_operator_name(current_operators)
if not name:
Expand Down Expand Up @@ -145,6 +151,8 @@ def parse_property():

props.append(prop)

skip_whitespace()

return ["get", *props]

return parse_function()
Expand Down
47 changes: 39 additions & 8 deletions tests/test-suite/parse.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@
{ "input": ".array.2", "output": ["get", "array", 2] }
]
},
{
"category": "property",
"description": "should parse an implicitly piped a property",
"tests": [
{
"input": "groupBy(.city).myCity",
"output": ["pipe", ["groupBy", ["get", "city"]], ["get", "myCity"]]
},
{
"input": "groupBy(.city).myCity | size()",
"output": ["pipe", ["groupBy", ["get", "city"]], ["get", "myCity"], ["size"]]
},
{
"input": "groupBy(.city).myCity.location | size()",
"output": ["pipe", ["groupBy", ["get", "city"]], ["get", "myCity", "location"], ["size"]]
},
{
"input": ".data | groupBy(.city).myCity",
"output": ["pipe", ["get", "data"], ["groupBy", ["get", "city"]], ["get", "myCity"]]
},
{
"input": "\"hello\".length",
"output": ["pipe", "hello", ["get", "length"]]
}
]
},
{
"category": "property",
"description": "should allow whitespace between multiple properties",
"tests": [
{ "input": ".\"address\" .\"city\"", "output": ["get", "address", "city"] },
{ "input": ".address .city", "output": ["get", "address", "city"] },
{ "input": ".address\t.city", "output": ["get", "address", "city"] },
{ "input": ".address\n.city", "output": ["get", "address", "city"] }
]
},
{
"category": "property",
"description": "should throw an error when a property misses an end quote",
Expand All @@ -54,11 +90,7 @@
{
"category": "property",
"description": "should throw an error when there is whitespace between the dot and the property name",
"tests": [
{ "input": ". \"name\"", "throws": "Property expected (pos: 1)" },
{ "input": ".\"address\" .\"city\"", "throws": "Unexpected part '.\"city\"' (pos: 11)" },
{ "input": ".address .city", "throws": "Unexpected part '.city' (pos: 9)" }
]
"tests": [{ "input": ". \"name\"", "throws": "Property expected (pos: 1)" }]
},
{
"category": "function",
Expand Down Expand Up @@ -453,11 +485,10 @@
"description": "should throw an error in case of an invalid number",
"tests": [
{ "input": "-", "throws": "Value expected (pos: 0)" },
{ "input": "2.", "throws": "Unexpected part '.' (pos: 1)" },
{ "input": "2.", "throws": "Property expected (pos: 2)" },
{ "input": "2.3e", "throws": "Unexpected part 'e' (pos: 3)" },
{ "input": "2.3e+", "throws": "Unexpected part 'e+' (pos: 3)" },
{ "input": "2.3e-", "throws": "Unexpected part 'e-' (pos: 3)" },
{ "input": "2.", "throws": "Unexpected part '.' (pos: 1)" }
{ "input": "2.3e-", "throws": "Unexpected part 'e-' (pos: 3)" }
]
},
{
Expand Down