diff options
author | Kenn Knowles <kenn.knowles@gmail.com> | 2013-07-23 12:15:29 -0700 |
---|---|---|
committer | Kenn Knowles <kenn.knowles@gmail.com> | 2013-07-23 12:15:29 -0700 |
commit | 97c6823bbe1ac13a152478416e07b5beb5a3744d (patch) | |
tree | b09be71925e5994692a0719387cc101439d47dfa | |
parent | 7d5169c363a8b2366a3b08ae102f3246ac8b8a7b (diff) | |
parent | 3451d2e7a74eabb39497e3461bd8df055928b1bf (diff) | |
download | jsonpath-rw-97c6823bbe1ac13a152478416e07b5beb5a3744d.tar.gz |
Merge pull request #6 from gregglind/negativearray-bug6
Negative array slicing?
-rw-r--r-- | jsonpath_rw/lexer.py | 12 | ||||
-rw-r--r-- | tests/test_lexer.py | 4 | ||||
-rw-r--r-- | tests/test_parser.py | 4 |
3 files changed, 12 insertions, 8 deletions
diff --git a/jsonpath_rw/lexer.py b/jsonpath_rw/lexer.py index d9efa50..9a70934 100644 --- a/jsonpath_rw/lexer.py +++ b/jsonpath_rw/lexer.py @@ -11,9 +11,9 @@ class JsonPathLexerError(Exception): class JsonPathLexer(object): ''' - A Lexical analyzer for JsonPath. + A Lexical analyzer for JsonPath. ''' - + def __init__(self, debug=False): self.debug = debug if self.__doc__ == None: @@ -23,7 +23,7 @@ class JsonPathLexer(object): ''' Maps a string to an iterator over tokens. In other words: [char] -> [token] ''' - + new_lexer = ply.lex.lex(module=self, debug=self.debug, errorlog=logger) new_lexer.latest_newline = 0 new_lexer.input(string) @@ -43,7 +43,7 @@ class JsonPathLexer(object): # Anyhow, it is pythonic to give some rope to hang oneself with :-) literals = ['*', '.', '[', ']', '(', ')', '$', ',', ':', '|', '&'] - + reserved_words = { 'where': 'WHERE' } tokens = ['DOUBLEDOT', 'NUMBER', 'ID', 'NAMED_OPERATOR'] + list(reserved_words.values()) @@ -62,7 +62,7 @@ class JsonPathLexer(object): return t def t_NUMBER(self, t): - r'\d+' + r'-?\d+' t.value = int(t.value) return t @@ -101,7 +101,7 @@ class JsonPathLexer(object): def t_doublequote_error(self, t): raise JsonPathLexerError('Error on line %s, col %s while lexing doublequoted field: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0])) - + # Back-quoted "magic" operators t_backquote_ignore = '' def t_BACKQUOTE(self, t): diff --git a/tests/test_lexer.py b/tests/test_lexer.py index 115874f..64d26c9 100644 --- a/tests/test_lexer.py +++ b/tests/test_lexer.py @@ -15,7 +15,7 @@ class TestLexer(unittest.TestCase): t.lineno = -1 t.lexpos = -1 return t - + def assert_lex_equiv(self, s, stream2): # NOTE: lexer fails to reset after call? l = JsonPathLexer(debug=True) @@ -38,6 +38,8 @@ class TestLexer(unittest.TestCase): self.assert_lex_equiv('fuzz', [self.token('fuzz', 'ID')]) self.assert_lex_equiv('1', [self.token(1, 'NUMBER')]) self.assert_lex_equiv('45', [self.token(45, 'NUMBER')]) + self.assert_lex_equiv('-1', [self.token(-1, 'NUMBER')]) + self.assert_lex_equiv(' -13 ', [self.token(-13, 'NUMBER')]) self.assert_lex_equiv('"fuzz.bang"', [self.token('fuzz.bang', 'ID')]) self.assert_lex_equiv('fuzz.bang', [self.token('fuzz', 'ID'), self.token('.', '.'), self.token('bang', 'ID')]) self.assert_lex_equiv('fuzz.*', [self.token('fuzz', 'ID'), self.token('.', '.'), self.token('*', '*')]) diff --git a/tests/test_parser.py b/tests/test_parser.py index ed41966..fd1e121 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -28,7 +28,9 @@ class TestParser(unittest.TestCase): ('[:]', Slice()), ('[*]', Slice()), ('[:2]', Slice(end=2)), - ('[1:2]', Slice(start=1, end=2))]) + ('[1:2]', Slice(start=1, end=2)), + ('[5:-2]', Slice(start=5, end=-2)) + ]) def test_nested(self): self.check_parse_cases([('foo.baz', Child(Fields('foo'), Fields('baz'))), |