Skip to content

Commit

Permalink
Precedence parsing (#1362)
Browse files Browse the repository at this point in the history
* Initial prototype

* Update docs
Remove unused code

* More doc updates

* Add feature flags for Vec

* Add basic tests

* Fix formatting

* Add precedence to choosing_a_combinator.md

* Fix typo

* Minor refractoring

* Update docs

* Change parameter order

* Add alloc feature to the entire precedence module

The parser really cant work without it and the helpers dont make much sense without the parser.

* Use fail parser to express "no operators of this type"

* Document evaluation order

* Better documentation for parameters

* Fix precedence in documentation

* Fix doc formatting

* Fix typos

* Use map_res when parsing integers

* Example test for expressions with function calls and AST generation

* Typo

* Make evaluation a bit easier to read

* Update expression_ast

* Update expression_ast doc

* Implement ternary operator in expression_ast

* Shorten ast nodes

* Implement some tests for parser failures

* Update feature flags for docs

* Properly append errors

* Properly bubble up non Error errors

* Split operators into 3 distinct types to help with exhaustiveness checks.

---------

Co-authored-by: Geoffroy Couprie <[email protected]>
  • Loading branch information
cenodis and Geal authored Dec 8, 2024
1 parent c8c0313 commit 6c12469
Show file tree
Hide file tree
Showing 7 changed files with 622 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ name = "css"
[[test]]
name = "custom_errors"

[[test]]
name = "expression_ast"
required-features = ["alloc"]

[[test]]
name = "float"

Expand Down
1 change: 1 addition & 0 deletions doc/choosing_a_combinator.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The following parsers could be found on [docs.rs number section](https://docs.rs

- [`escaped`](https://docs.rs/nom/latest/nom/bytes/complete/fn.escaped.html): Matches a byte string with escaped characters
- [`escaped_transform`](https://docs.rs/nom/latest/nom/bytes/complete/fn.escaped_transform.html): Matches a byte string with escaped characters, and returns a new string with the escaped characters replaced
- [`precedence`](https://docs.rs/nom/latest/nom/precedence/fn.precedence.html): Parses an expression with regards to operator precedence

## Binary format parsing

Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ pub enum ErrorKind {
Fail,
Many,
Fold,
Precedence,
}

#[rustfmt::skip]
Expand Down Expand Up @@ -373,6 +374,7 @@ pub fn error_to_u32(e: &ErrorKind) -> u32 {
ErrorKind::Many => 76,
ErrorKind::Fold => 77,
ErrorKind::BinDigit => 78,
ErrorKind::Precedence => 79,
}
}

Expand Down Expand Up @@ -438,6 +440,7 @@ impl ErrorKind {
ErrorKind::Fail => "Fail",
ErrorKind::Many => "Many",
ErrorKind::Fold => "Fold",
ErrorKind::Precedence => "Precedence",
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ pub mod bytes;

pub mod character;

pub mod precedence;

mod str;

pub mod number;
Expand Down
Loading

0 comments on commit 6c12469

Please sign in to comment.