Qiskit / openqasm3_parser

Parser and semantic analyzer for the OpenQASM3 language
Apache License 2.0
11 stars 12 forks source link

Parse error handling floats with leading DOT #225

Open idavis opened 2 months ago

idavis commented 2 months ago

Floats with leading DOT fail to parse. Example values that fail parsing:

From the spec:

fragment FloatLiteralExponent: [eE] (PLUS | MINUS)? DecimalIntegerLiteral;
FloatLiteral:
    // 1_123e-3, 123e+4 or 123E5 (needs the exponent or it's just an integer)
    DecimalIntegerLiteral FloatLiteralExponent
    // .1234_5678 or .1e3 (no digits before the dot)
    | DOT DecimalIntegerLiteral FloatLiteralExponent?
    // 123.456, 123. or 145.32e+1_00
    | DecimalIntegerLiteral DOT DecimalIntegerLiteral? FloatLiteralExponent?;

Example test:

#[test]
fn test_spec_types_float_leading_dot() {
    fn parse_source_string(code: &str) -> oq3_source_file::SourceString {
        oq3_source_file::parse_source_string(code, None, None::<&[&std::path::Path]>)
    }
    let code = r#"
   float my_machine_float = .1e3;
"#;
    let source_string = parse_source_string(code);

    if source_string.any_parse_errors() {
        source_string.print_syntax_errors();
        panic!("Errors found");
    }
}

Example output:

Error: atom_expr: expected expression
   ╭─[no file:2:29]
 2 │   float my_machine_float = .1e3;
   │                            ╰ Near this point
Error: expected SEMICOLON
   ╭─[no file:2:30]
 2 │   float my_machine_float = .1e3;
   │                             ╰ Near this point
test test_spec_types_float_leading_dot ... FAILED