Skip to content

Commit 16760e7

Browse files
committed
add parser implementation
1 parent 0f2a034 commit 16760e7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

crates/ast/src/parser.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ impl FilamentParser {
239239
.map_err(|_| input.error("Expected valid bitwidth"))
240240
}
241241

242+
fn float(input: Node) -> ParseResult<f64> {
243+
input
244+
.as_str()
245+
.parse::<f64>()
246+
.map_err(|_| input.error("Expected valid float"))
247+
}
248+
242249
// ================ Intervals =====================
243250
fn time(input: Node) -> ParseResult<Loc<ast::Time>> {
244251
let sp = Self::get_span(&input);
@@ -833,12 +840,13 @@ impl FilamentParser {
833840
Ok(())
834841
}
835842

836-
fn attributes<Bool, Num>(
843+
fn attributes<Bool, Num, Float>(
837844
input: Node,
838-
) -> ParseResult<utils::Attributes<Bool, Num>>
845+
) -> ParseResult<utils::Attributes<Bool, Num, Float>>
839846
where
840847
Bool: FromStr + Hash + Eq + Copy,
841848
Num: FromStr + Hash + Eq + Copy,
849+
Float: FromStr + Hash + Eq + Copy,
842850
{
843851
let mut attrs = utils::Attributes::default();
844852
for attr in input.into_children() {
@@ -853,6 +861,9 @@ impl FilamentParser {
853861
[identifier(name), bitwidth(val)] => Num::from_str(name.as_ref()).map(
854862
|attr| attrs.set(attr, val, name.pos())).map_err(
855863
|_| attr.error(format!("Found unknown numeric attribute \"{name}\""))),
864+
[identifier(name), float(val)] => Float::from_str(name.as_ref()).map(
865+
|attr| attrs.set(attr, val, name.pos())).map_err(
866+
|_| attr.error(format!("Found unknown float attribute \"{name}\""))),
856867
)?;
857868
}
858869

crates/ast/src/syntax.pest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ identifier = @{ ("_" | ASCII_ALPHA)+ ~ ("_" | ASCII_ALPHA | ASCII_DIGIT)* }
99
// Positive numbers
1010
bitwidth = @{ ASCII_DIGIT+ }
1111

12+
// Float
13+
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
14+
1215
char = { !"\"" ~ ANY }
1316
string_lit = ${ "\"" ~ char* ~ "\"" }
1417
import = _{
@@ -74,6 +77,7 @@ not = { "not" }
7477
attr_bind = {
7578
not ~ "(" ~ identifier ~ ")"
7679
| identifier ~ "=" ~ bitwidth
80+
| identifier ~ "=" ~ float
7781
| identifier
7882
}
7983

0 commit comments

Comments
 (0)