Skip to content

Commit

Permalink
[j8] Fix parse error with ( ) tokens in JSON/JSON8
Browse files Browse the repository at this point in the history
These are valid in J8 Notation (TYG8), but not JSON or JSON8.
  • Loading branch information
Andy C committed Feb 2, 2024
1 parent 20a2d04 commit 2ecc7a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
9 changes: 1 addition & 8 deletions data_lang/j8.py
Expand Up @@ -562,7 +562,6 @@ class Parser(object):
def __init__(self, s, is_j8):
# type: (str, bool) -> None
self.s = s
self.is_j8 = is_j8
self.lang_str = "J8" if is_j8 else "JSON"

self.lexer = LexerDecoder(s, is_j8)
Expand Down Expand Up @@ -713,16 +712,10 @@ def _ParseValue(self):
raise self._Error('Unexpected EOF while parsing %s' %
self.lang_str)

elif self.tok_id == Id.Unknown_Tok:
else: # Id.Unknown_Tok, Id.J8_{LParen,RParen}
raise self._Error('Invalid token while parsing %s: %s' %
(self.lang_str, Id_str(self.tok_id)))

else:
# This should never happen
part = self.s[self.start_pos:self.end_pos]
raise AssertionError('Unexpected token %s %r' %
(Id_str(self.tok_id), part))

def ParseValue(self):
# type: () -> value_t
""" Raises error.Decode. """
Expand Down
3 changes: 3 additions & 0 deletions data_lang/json-errors.sh
Expand Up @@ -41,6 +41,9 @@ test-parse-errors() {

# Invalid token
_error-case-X 1 'echo + | json read'

# TYG8 token, not JSON8 token
_error-case-X 1 'echo "(" | json read'
}

test-lex-errors() {
Expand Down
11 changes: 11 additions & 0 deletions spec/ysh-json.test.sh
Expand Up @@ -874,3 +874,14 @@ declare -- y=$'\xbc'
b'\yf0\y9f\ya4\yff'
declare -- z=$'\xf0\x9f\xa4\xff'
## END

#### TYG8 token in JSON / JSON8

echo "(" | json read
echo status=$?

echo ")" | json8 read
echo status=$?

## STDOUT:
## END

0 comments on commit 2ecc7a3

Please sign in to comment.