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

syntax: valid yaml [\?] raises syntax error #594

Closed
silverwind opened this issue Sep 7, 2023 · 4 comments
Closed

syntax: valid yaml [\?] raises syntax error #594

silverwind opened this issue Sep 7, 2023 · 4 comments

Comments

@silverwind
Copy link

silverwind commented Sep 7, 2023

[\?] is valid according to various other parsers, but this one rejects it:

1:3       error    syntax error: expected ',' or ']', but got '?' (syntax)

It should decode to JSON ["\\?"].

@andrewimeson
Copy link
Contributor

PyYAML doesn't view it as valid

$ cat << 'EOF' | python3 -c 'import sys, yaml, json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))'
[\?]
EOF
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/__init__.py", line 125, in safe_load
    return load(stream, SafeLoader)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/__init__.py", line 81, in load
    return loader.get_single_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/constructor.py", line 49, in get_single_data
    node = self.get_single_node()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 36, in get_single_node
    document = self.compose_document()
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 55, in compose_document
    node = self.compose_node(None, None)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 82, in compose_node
    node = self.compose_sequence_node(anchor)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/composer.py", line 110, in compose_sequence_node
    while not self.check_event(SequenceEndEvent):
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
                         ^^^^^^^^^^^^
  File "/Users/andrewimeson/Documents/Code/personal/yamllint/issue_594/.venv/lib/python3.11/site-packages/yaml/parser.py", line 483, in parse_flow_sequence_entry
    raise ParserError("while parsing a flow sequence", self.marks[-1],
yaml.parser.ParserError: while parsing a flow sequence
  in "<unicode string>", line 1, column 1:
    [\?]
    ^
expected ',' or ']', but got '?'
  in "<unicode string>", line 1, column 3:
    [\?]
      ^

Nor does yq (Golang).

YAML is a superset of JSON, so the output you produced is valid JSON and YAML

This is equivalent to your JSON as well, and yamllint and other tools can parse it without issue.

- \?

@silverwind
Copy link
Author

silverwind commented Sep 8, 2023

Hmm, it does work for me in pyyaml:

$ python3
Python 3.11.5 (main, Aug 24 2023, 15:18:16) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from yaml import load, CLoader
>>> load("[\?]", CLoader)
['\\?']

Edit: It works with CLoader but not with Loader. Looks like a bug in Loader.

@silverwind
Copy link
Author

Opened yaml/pyyaml#753

@silverwind
Copy link
Author

Bug is not in this repo, so let's close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants