Skip to content

Commit

Permalink
adding and operator
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-sabbaghi committed Oct 9, 2017
1 parent b27688d commit 3ca9fd0
Showing 1 changed file with 29 additions and 2 deletions.
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 result or (t == 'string' and result ~= '') or (t == 'table' and next(result) ~= nil)
then
result = interpreter:visit(node.children[2], data)
end
return result
end,

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

0 comments on commit 3ca9fd0

Please sign in to comment.