Skip to content

Parse failures raise ValidationError with an empty message; the ANTLR diagnostic is lost #436

Description

@ryanhill1

Summary

When pyqasm.loads() fails to parse, the ValidationError message is "Failed to parse OpenQASM string: " with nothing after the colon. The ANTLR diagnostic that says what is wrong (line 3:10 no viable alternative at input 'rx(pi/2q') goes to stderr only, so a service that surfaces str(exc) to a user gives them an empty reason.

Repro (pyqasm 1.2.1, openqasm3 1.0.1)

import pyqasm
try:
    pyqasm.loads("OPENQASM 3.0;\nqubit q;\nrx(pi / 2 q;\n")
except Exception as exc:
    print(repr(str(exc)))
# prints: 'Failed to parse OpenQASM string: '
# stderr: line 3:10 no viable alternative at input 'rx(pi/2q'

openqasm3.parser.QASM3ParsingError is raised with no message (raise QASM3ParsingError() from exc in openqasm3/parser.py:107), so {err} formats as empty.

Suggestion

Capture the ANTLR error listener output (line, column, message) and put it in the ValidationError message, e.g. Failed to parse OpenQASM string: line 3:10 no viable alternative at input 'rx(pi/2q'. Line and column would also let callers point at the offending statement.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions