Skip to content

Commit

Permalink
Fixed a bug in boolean / logical literal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nahuel-M committed Aug 15, 2023
1 parent d7f93bf commit 9bbc4f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
7 changes: 7 additions & 0 deletions espr/src/parser/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ pub fn not<'a>(f: impl EsprParser<'a, &'a str>) -> impl EsprParser<'a, &'a str>
}
}

pub fn peek<'a>(f: impl EsprParser<'a, &'a str>) -> impl EsprParser<'a, &'a str> {
move |input: &'a str| {
let (input, _) = nom::combinator::peek(f.clone())(input)?;
Ok((input, ("", Vec::new())))
}
}

pub fn char<'a>(c: char) -> impl EsprParser<'a, char> {
move |input| {
let (input, c) = nom::character::complete::char(c)(input)?;
Expand Down
12 changes: 8 additions & 4 deletions espr/src/parser/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ pub fn literal(input: &str) -> ParseResult<Literal> {

/// 255 logical_literal = `FALSE` | `TRUE` | `UNKNOWN` .
pub fn logical_literal(input: &str) -> ParseResult<Logical> {
alt((
value(Logical::True, tag("TRUE")),
value(Logical::False, tag("FALSE")),
value(Logical::Unknown, tag("UNKNOWN")),
tuple((
alt((
value(Logical::True, tag("TRUE")),
value(Logical::False, tag("FALSE")),
value(Logical::Unknown, tag("UNKNOWN")),
)),
peek(not(remarked(nom::character::complete::alpha1))),
))
.map(|(logical, _non_alpha)| logical)
.parse(input)
}

Expand Down
47 changes: 47 additions & 0 deletions espr/tests/logical_literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use espr::{ast::SyntaxTree, codegen::rust::*, ir::IR};

const EXPRESS: &str = r#"
SCHEMA IFC4X3_DEV_6a23ae8;
ENTITY IfcGeometricRepresentationContext;
TrueNorth : OPTIONAL BOOLEAN;
WHERE
North2D : NOT(EXISTS(TrueNorth)) OR (HIINDEX(TrueNorth.DirectionRatios) = 2);
END_ENTITY;
END_SCHEMA;
"#;

#[test]
fn logical_literal() {
let st = SyntaxTree::parse(EXPRESS).unwrap();
let ir = IR::from_syntax_tree(&st).unwrap();
let tt = ir.to_token_stream(CratePrefix::External).to_string();

let tt = rustfmt(tt);

insta::assert_snapshot!(tt, @r###"
pub mod IFC4X3_DEV_6a23ae8 {
use ruststep::{as_holder, derive_more::*, primitive::*, Holder, TableInit};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Default, TableInit)]
pub struct Tables {
IfcGeometricRepresentationContext:
HashMap<u64, as_holder!(IfcGeometricRepresentationContext)>,
}
impl Tables {
pub fn IfcGeometricRepresentationContext_holders(
&self,
) -> &HashMap<u64, as_holder!(IfcGeometricRepresentationContext)> {
&self.IfcGeometricRepresentationContext
}
}
#[derive(Debug, Clone, PartialEq, :: derive_new :: new, Holder)]
# [holder (table = Tables)]
# [holder (field = IfcGeometricRepresentationContext)]
#[holder(generate_deserialize)]
pub struct IfcGeometricRepresentationContext {
pub TrueNorth: Option<bool>,
}
}
"###);
}

0 comments on commit 9bbc4f4

Please sign in to comment.