Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(grit): patch more parser incompatibilities for complex patterns #5037

Merged
merged 7 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
655 changes: 655 additions & 0 deletions crates/biome_grit_formatter/tests/specs/grit/marzano/react_hooks.grit

Large diffs are not rendered by default.

1,289 changes: 1,289 additions & 0 deletions crates/biome_grit_formatter/tests/specs/grit/marzano/react_hooks.grit.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

pattern identifier_scope($name) {
or {
function($parameters) where {
$parameters <: contains $name
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: grit/marzano/shadow_scope.grit
---
# Input

```grit

pattern identifier_scope($name) {
or {
function($parameters) where {
$parameters <: contains $name
},
}
}

```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
-----

```grit
pattern identifier_scope($name) {
or { function($parameters) where { $parameters <: contains $name } }}
```
9 changes: 8 additions & 1 deletion crates/biome_grit_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,14 @@ impl<'src> GritLexer<'src> {
b"private" => PRIVATE_KW,
b"pattern" => PATTERN_KW,
b"predicate" => PREDICATE_KW,
b"function" => FUNCTION_KW,
b"function" => {
// Function is also an AST node name, so we need to check if the next token is a `(`
if self.current_byte() == Some(b'(') {
GRIT_NAME
} else {
FUNCTION_KW
}
}
b"true" => TRUE_KW,
b"false" => FALSE_KW,
b"undefined" => UNDEFINED_KW,
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_grit_parser/src/parser/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ fn parse_bubble(p: &mut GritParser) -> ParsedSyntax {
p.bump(BUBBLE_KW);

parse_bubble_scope(p).ok();

parse_expected_pattern_with_precedence(p, PRECEDENCE_PATTERN);
parse_maybe_curly_pattern(p).ok();

Present(m.complete(p, GRIT_BUBBLE))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// The "function" AST name can conflict with the "function" keyword
function($parameters)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: crates/biome_grit_parser/tests/spec_test.rs
expression: snapshot
---
## Input
```grit
// The "function" AST name can conflict with the "function" keyword
function($parameters)

```

## AST

```
GritRoot {
bom_token: missing (optional),
version: missing (optional),
language: missing (optional),
definitions: GritDefinitionList [
GritNodeLike {
name: GritName {
value_token: [email protected] "function" [Comments("// The \"function\" AST ..."), Newline("\n")] [],
},
l_paren_token: [email protected] "(" [] [],
named_args: GritNamedArgList [
GritVariable {
value_token: [email protected] "$parameters" [] [],
},
],
r_paren_token: [email protected] ")" [] [],
},
],
eof_token: [email protected] "" [Newline("\n")] [],
}
```

## CST

```
0: [email protected]
0: (empty)
1: (empty)
2: (empty)
3: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "function" [Comments("// The \"function\" AST ..."), Newline("\n")] []
1: [email protected] "(" [] []
2: [email protected]
0: [email protected]
0: [email protected] "$parameters" [] []
3: [email protected] ")" [] []
4: [email protected] "" [Newline("\n")] []

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
engine marzano(0.1)
language js

file($x) where {
$x <: contains bubble { `foo` }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
source: crates/biome_grit_parser/tests/spec_test.rs
expression: snapshot
---
## Input
```grit
engine marzano(0.1)
language js

file($x) where {
$x <: contains bubble { `foo` }
}

```

## AST

```
GritRoot {
bom_token: missing (optional),
version: GritVersion {
engine_token: [email protected] "engine" [] [Whitespace(" ")],
engine_name: GritEngineName {
engine_kind: [email protected] "marzano" [] [],
},
l_paren_token: [email protected] "(" [] [],
version: GritDoubleLiteral {
value_token: [email protected] "0.1" [] [],
},
r_paren_token: [email protected] ")" [] [],
},
language: GritLanguageDeclaration {
language_token: [email protected] "language" [Newline("\n")] [Whitespace(" ")],
name: GritLanguageName {
language_kind: [email protected] "js" [] [],
},
flavor: missing (optional),
semicolon_token: missing (optional),
},
definitions: GritDefinitionList [
GritPatternWhere {
pattern: GritNodeLike {
name: GritName {
value_token: [email protected] "file" [Newline("\n"), Newline("\n")] [],
},
l_paren_token: [email protected] "(" [] [],
named_args: GritNamedArgList [
GritVariable {
value_token: [email protected] "$x" [] [],
},
],
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
where_token: [email protected] "where" [] [Whitespace(" ")],
side_condition: GritPredicateAnd {
and_token: missing (optional),
l_curly_token: [email protected] "{" [] [],
predicates: GritPredicateList [
GritPredicateMatch {
left: GritVariable {
value_token: [email protected] "$x" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")],
},
match_token: [email protected] "<:" [] [Whitespace(" ")],
right: GritPatternContains {
contains_token: [email protected] "contains" [] [Whitespace(" ")],
contains: GritBubble {
bubble_token: [email protected] "bubble" [] [Whitespace(" ")],
scope: missing (optional),
pattern: GritCurlyPattern {
l_curly_token: [email protected] "{" [] [Whitespace(" ")],
pattern: GritCodeSnippet {
source: GritBacktickSnippetLiteral {
value_token: [email protected] "`foo`" [] [Whitespace(" ")],
},
},
r_curly_token: [email protected] "}" [] [],
},
},
until_clause: missing (optional),
},
},
],
r_curly_token: [email protected] "}" [Newline("\n")] [],
},
},
],
eof_token: [email protected] "" [Newline("\n")] [],
}
```

## CST

```
0: [email protected]
0: (empty)
1: [email protected]
0: [email protected] "engine" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "marzano" [] []
2: [email protected] "(" [] []
3: [email protected]
0: [email protected] "0.1" [] []
4: [email protected] ")" [] []
2: [email protected]
0: [email protected] "language" [Newline("\n")] [Whitespace(" ")]
1: [email protected]
0: [email protected] "js" [] []
2: (empty)
3: (empty)
3: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "file" [Newline("\n"), Newline("\n")] []
1: [email protected] "(" [] []
2: [email protected]
0: [email protected]
0: [email protected] "$x" [] []
3: [email protected] ")" [] [Whitespace(" ")]
1: [email protected] "where" [] [Whitespace(" ")]
2: [email protected]
0: (empty)
1: [email protected] "{" [] []
2: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "$x" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")]
1: [email protected] "<:" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "contains" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "bubble" [] [Whitespace(" ")]
1: (empty)
2: [email protected]
0: [email protected] "{" [] [Whitespace(" ")]
1: [email protected]
0: [email protected]
0: [email protected] "`foo`" [] [Whitespace(" ")]
2: [email protected] "}" [] []
2: (empty)
3: [email protected] "}" [Newline("\n")] []
4: [email protected] "" [Newline("\n")] []

```