Skip to content

Commit

Permalink
add parser implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
UnsignedByte committed Feb 2, 2025
1 parent 0f2a034 commit 16760e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ impl FilamentParser {
.map_err(|_| input.error("Expected valid bitwidth"))
}

fn float(input: Node) -> ParseResult<f64> {
input
.as_str()
.parse::<f64>()
.map_err(|_| input.error("Expected valid float"))
}

// ================ Intervals =====================
fn time(input: Node) -> ParseResult<Loc<ast::Time>> {
let sp = Self::get_span(&input);
Expand Down Expand Up @@ -833,12 +840,13 @@ impl FilamentParser {
Ok(())
}

fn attributes<Bool, Num>(
fn attributes<Bool, Num, Float>(
input: Node,
) -> ParseResult<utils::Attributes<Bool, Num>>
) -> ParseResult<utils::Attributes<Bool, Num, Float>>
where
Bool: FromStr + Hash + Eq + Copy,
Num: FromStr + Hash + Eq + Copy,
Float: FromStr + Hash + Eq + Copy,
{
let mut attrs = utils::Attributes::default();
for attr in input.into_children() {
Expand All @@ -853,6 +861,9 @@ impl FilamentParser {
[identifier(name), bitwidth(val)] => Num::from_str(name.as_ref()).map(
|attr| attrs.set(attr, val, name.pos())).map_err(
|_| attr.error(format!("Found unknown numeric attribute \"{name}\""))),
[identifier(name), float(val)] => Float::from_str(name.as_ref()).map(
|attr| attrs.set(attr, val, name.pos())).map_err(
|_| attr.error(format!("Found unknown float attribute \"{name}\""))),
)?;
}

Expand Down
4 changes: 4 additions & 0 deletions crates/ast/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ identifier = @{ ("_" | ASCII_ALPHA)+ ~ ("_" | ASCII_ALPHA | ASCII_DIGIT)* }
// Positive numbers
bitwidth = @{ ASCII_DIGIT+ }

// Float
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }

char = { !"\"" ~ ANY }
string_lit = ${ "\"" ~ char* ~ "\"" }
import = _{
Expand Down Expand Up @@ -74,6 +77,7 @@ not = { "not" }
attr_bind = {
not ~ "(" ~ identifier ~ ")"
| identifier ~ "=" ~ bitwidth
| identifier ~ "=" ~ float
| identifier
}

Expand Down

0 comments on commit 16760e7

Please sign in to comment.