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

Hex and binary literals with capital base specifiers aren't parsed correctly #226

Open
idavis opened this issue Aug 30, 2024 · 0 comments · May be fixed by #232
Open

Hex and binary literals with capital base specifiers aren't parsed correctly #226

idavis opened this issue Aug 30, 2024 · 0 comments · May be fixed by #232

Comments

@idavis
Copy link

idavis commented Aug 30, 2024

From the spec for hex and binary literals, using x|X and b|B should be allowed. The values are lexed without error, but aren't handled correctly.

Here you can see the result of parsing int x = 0xFa_1F; and int x = 0XFa_1F; into an asg::Program

Program { version: None, stmts: [DeclareClassical(DeclareClassical { name: Ok(SymbolId(7)), initializer: Some(TExpr { expression: Cast(Cast { operand: TExpr { expression: Literal(Int(IntLiteral { value: 64031, sign: true })), ty: Int(Some(128), True) }, typ: Int(None, False) }), ty: Int(None, False) }) })] }
Program { version: None, stmts: [DeclareClassical(DeclareClassical { name: Ok(SymbolId(7)), initializer: Some(TExpr { expression: Cast(Cast { operand: TExpr { expression: Literal(Int(IntLiteral { value: 0, sign: true })), ty: Int(Some(128), True) }, typ: Int(None, False) }), ty: Int(None, False) }) })] }

And the result of parsing int x = 0b1010_1100; and int x = 0B1010_1100; into an asg::Program

Program { version: None, stmts: [DeclareClassical(DeclareClassical { name: Ok(SymbolId(7)), initializer: Some(TExpr { expression: Cast(Cast { operand: TExpr { expression: Literal(Int(IntLiteral { value: 172, sign: true })), ty: Int(Some(128), True) }, typ: Int(None, False) }), ty: Int(None, False) }) })] }
Program { version: None, stmts: [DeclareClassical(DeclareClassical { name: Ok(SymbolId(7)), initializer: Some(TExpr { expression: Cast(Cast { operand: TExpr { expression: Literal(Int(IntLiteral { value: 0, sign: true })), ty: Int(Some(128), True) }, typ: Int(None, False) }), ty: Int(None, False) }) })] }

In both cases the IntLiteral value is 0 instead of the correct value when the base specifier is a capital.

jlapeyre added a commit to jlapeyre/openqasm3_parser that referenced this issue Oct 29, 2024
Closes Qiskit#226

This fix also uncovers a bug that will be opened and fixed separately: Illegal (nonsense)
integer literals, with prefixes such as 0Q will be analyzed as valid, decimal,
literals. Instead they should cause an error to be logged. A lex, or possibly parse,
error.
jlapeyre added a commit to jlapeyre/openqasm3_parser that referenced this issue Oct 29, 2024
Closes Qiskit#226

Previously, literals with prefix `0x` were parsed and analyzed correctly. But literals
with prefix `0X` were analyzed as value `0`. This is now fixed. Prefixes `0B` and `0O`
were broken in the same way and are now fixed.

This fix also uncovers a bug that will be opened and fixed separately: Illegal (nonsense)
integer literals, with prefixes such as 0Q will be analyzed as valid, decimal,
literals. Instead they should cause an error to be logged. A lex, or possibly parse,
error.
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

Successfully merging a pull request may close this issue.

1 participant