Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding and operator #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 29 additions & 2 deletions jmespath.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ local Lexer = (function()
['('] = 'lparen',
[')'] = 'rparen',
['@'] = 'current',
['&'] = 'expref'
},
-- Tokens that can be numbers
numbers = {
Expand Down Expand Up @@ -178,6 +177,16 @@ local Lexer = (function()
return {type = 'or', value = '||', pos = lexer.pos - 2};
end

--- Consumes an and, '&&', and expref, '&' token
local function consume_amp(lexer)
consume(lexer)
if lexer.c ~= '&' then
return {type = 'expref', value = '&', pos = lexer.pos - 1};
end
consume(lexer)
return {type = 'and', value = '&&', pos = lexer.pos - 2};
end

--- Parse a string of tokens inside of a delimiter.
-- @param lexer Lexer instance
-- @param wrapper Wrapping character
Expand Down Expand Up @@ -265,6 +274,8 @@ local Lexer = (function()
tokens[#tokens + 1] = consume_operator(self)
elseif self.c == '|' then
tokens[#tokens + 1] = consume_pipe(self)
elseif self.c == '&' then
tokens[#tokens + 1] = consume_amp(self)
elseif self.c == '"' then
tokens[#tokens + 1] = consume_quoted_identifier(self)
elseif self.c == '`' then
Expand Down Expand Up @@ -341,7 +352,8 @@ local Parser = (function()
pipe = 1,
comparator = 2,
['or'] = 5,
flatten = 6,
['and'] = 6,
flatten = 7,
star = 20,
dot = 40,
lbrace = 50,
Expand Down Expand Up @@ -559,6 +571,11 @@ local Parser = (function()
return {type = 'or', children = {left, expr(parser, bp['or'])}}
end

parselets.led_and = function(parser, left)
parser:advance()
return {type = 'and', children = {left, expr(parser, bp['and'])}}
end

parselets.led_pipe = function(parser, left)
parser:advance()
return {type = 'pipe', children = {left, expr(parser, bp.pipe)}}
Expand Down Expand Up @@ -1170,6 +1187,16 @@ local Interpreter = (function()
return result
end,

["and"] = function(interpreter, node, data)
local result = interpreter:visit(node.children[1], data)
local t = type(result)
if not result or result == '' or (t == 'table' and next(result) == nil)
then
return result
end
return interpreter:visit(node.children[2], data)
end,

pipe = function(interpreter, node, data)
return interpreter:visit(
node.children[2],
Expand Down