Skip to content

Commit

Permalink
Merge pull request #154 from nikomatsakis/parser-cleanup
Browse files Browse the repository at this point in the history
Parser cleanup
  • Loading branch information
nikomatsakis authored Nov 9, 2023
2 parents 2002ced + fab9e5f commit b7fd961
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Run cargo test
- name: Run cargo test --all
uses: actions-rs/cargo@v1
with:
command: test
args: --all
- name: Run cargo test --all-targets
uses: actions-rs/cargo@v1
with:
command: test
args: --all-targets

6 changes: 3 additions & 3 deletions book/src/formality_core/parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ When parsing an enum there will be multiple possibilities. We will attempt to pa
We support left-recursive grammars like this one from the `parse-torture-tests`:

```rust
{{#include ../../../tests/parser-torture-tests/src/path.rs:path}}
{{#include ../../../tests/parser-torture-tests/path.rs:path}}
```

We also support ambiguous grammars. For example, you can code up arithmetic expressions like this:


```rust
{{#include ../../../tests/parser-torture-tests/src/left_associative.rs:Expr}}
{{#include ../../../tests/parser-torture-tests/left_associative.rs:Expr}}
```

When specifying the `#[precedence]` of a variant, the default is left-associativity, which can be written more explicitly as `#[precedence(L, left)]`. If you prefer, you can specify right-associativity (`#[precedence(L, right)]`) or non-associativity `#[precedence(L, none)]`. This affects how things of the same level are parsed:
Expand Down Expand Up @@ -71,7 +71,7 @@ A grammar consists of a series of *symbols*. Each symbol matches some text in th
* `$[?field]` -- parse `[E1, E2, E3]`, where `field: Vec<E>`, but accept empty string as empty vector
* `${field}` -- parse `{E1, E2, E3}`, where `field: Vec<E>`
* `${?field}` -- parse `{E1, E2, E3}`, where `field: Vec<E>`, but accept empty string as empty vector
* `$:guard <nonterminal>` -- parses `<nonterminal>` but only if the keyword `guard` is present. For example, `$:where $,where_clauses` would parse `where WhereClause1, WhereClause2, WhereClause3`
* `$:guard <nonterminal>` -- parses `<nonterminal>` but only if the keyword `guard` is present. For example, `$:where $,where_clauses` would parse `where WhereClause1, WhereClause2, WhereClause3` but would also accept nothing (in which case, you would get an empty vector).

### Greediness

Expand Down
4 changes: 2 additions & 2 deletions crates/formality-core/src/parse/parser/left_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl StackEntry {
}
}

pub fn enter<'s, 't, L, T>(
pub(super) fn enter<'s, 't, L, T>(
scope: &'s Scope<L>,
text: &'t str,
mut op: impl FnMut(usize) -> ParseResult<'t, T>,
Expand Down Expand Up @@ -439,7 +439,7 @@ where
}
}

pub fn recurse<'s, 't, R>(current_state: CurrentState, op: impl FnOnce() -> R) -> R {
pub(super) fn recurse<'s, 't, R>(current_state: CurrentState, op: impl FnOnce() -> R) -> R {
STACK.with_borrow_mut(|stack| {
let top = stack.last_mut().unwrap();
assert!(
Expand Down

0 comments on commit b7fd961

Please sign in to comment.