Skip to content

Commit

Permalink
fix(grit): patch more parser incompatibilities for complex patterns (#…
Browse files Browse the repository at this point in the history
…5037)

Co-authored-by: ematipico <[email protected]>
  • Loading branch information
morgante and ematipico authored Feb 5, 2025
1 parent 48eab5b commit a17ca7d
Show file tree
Hide file tree
Showing 10 changed files with 2,205 additions and 3 deletions.
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: GRIT_NAME@0..76 "function" [Comments("// The \"function\" AST ..."), Newline("\n")] [],
},
l_paren_token: L_PAREN@76..77 "(" [] [],
named_args: GritNamedArgList [
GritVariable {
value_token: GRIT_VARIABLE@77..88 "$parameters" [] [],
},
],
r_paren_token: R_PAREN@88..89 ")" [] [],
},
],
eof_token: EOF@89..90 "" [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: ENGINE_KW@0..7 "engine" [] [Whitespace(" ")],
engine_name: GritEngineName {
engine_kind: MARZANO_KW@7..14 "marzano" [] [],
},
l_paren_token: L_PAREN@14..15 "(" [] [],
version: GritDoubleLiteral {
value_token: GRIT_DOUBLE@15..18 "0.1" [] [],
},
r_paren_token: R_PAREN@18..19 ")" [] [],
},
language: GritLanguageDeclaration {
language_token: LANGUAGE_KW@19..29 "language" [Newline("\n")] [Whitespace(" ")],
name: GritLanguageName {
language_kind: JS_KW@29..31 "js" [] [],
},
flavor: missing (optional),
semicolon_token: missing (optional),
},
definitions: GritDefinitionList [
GritPatternWhere {
pattern: GritNodeLike {
name: GritName {
value_token: GRIT_NAME@31..37 "file" [Newline("\n"), Newline("\n")] [],
},
l_paren_token: L_PAREN@37..38 "(" [] [],
named_args: GritNamedArgList [
GritVariable {
value_token: GRIT_VARIABLE@38..40 "$x" [] [],
},
],
r_paren_token: R_PAREN@40..42 ")" [] [Whitespace(" ")],
},
where_token: WHERE_KW@42..48 "where" [] [Whitespace(" ")],
side_condition: GritPredicateAnd {
and_token: missing (optional),
l_curly_token: L_CURLY@48..49 "{" [] [],
predicates: GritPredicateList [
GritPredicateMatch {
left: GritVariable {
value_token: GRIT_VARIABLE@49..54 "$x" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")],
},
match_token: MATCH@54..57 "<:" [] [Whitespace(" ")],
right: GritPatternContains {
contains_token: CONTAINS_KW@57..66 "contains" [] [Whitespace(" ")],
contains: GritBubble {
bubble_token: BUBBLE_KW@66..73 "bubble" [] [Whitespace(" ")],
scope: missing (optional),
pattern: GritCurlyPattern {
l_curly_token: L_CURLY@73..75 "{" [] [Whitespace(" ")],
pattern: GritCodeSnippet {
source: GritBacktickSnippetLiteral {
value_token: GRIT_BACKTICK_SNIPPET@75..81 "`foo`" [] [Whitespace(" ")],
},
},
r_curly_token: R_CURLY@81..82 "}" [] [],
},
},
until_clause: missing (optional),
},
},
],
r_curly_token: R_CURLY@82..84 "}" [Newline("\n")] [],
},
},
],
eof_token: EOF@84..85 "" [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: GRIT_PREDICATE_LIST@49..82
0: GRIT_PREDICATE_MATCH@49..82
0: GRIT_VARIABLE@49..54
0: GRIT_VARIABLE@49..54 "$x" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")]
1: MATCH@54..57 "<:" [] [Whitespace(" ")]
2: GRIT_PATTERN_CONTAINS@57..82
0: CONTAINS_KW@57..66 "contains" [] [Whitespace(" ")]
1: GRIT_BUBBLE@66..82
0: BUBBLE_KW@66..73 "bubble" [] [Whitespace(" ")]
1: (empty)
2: GRIT_CURLY_PATTERN@73..82
0: L_CURLY@73..75 "{" [] [Whitespace(" ")]
1: GRIT_CODE_SNIPPET@75..81
0: GRIT_BACKTICK_SNIPPET_LITERAL@75..81
0: GRIT_BACKTICK_SNIPPET@75..81 "`foo`" [] [Whitespace(" ")]
2: R_CURLY@81..82 "}" [] []
2: (empty)
3: R_CURLY@82..84 "}" [Newline("\n")] []
4: EOF@84..85 "" [Newline("\n")] []
```

0 comments on commit a17ca7d

Please sign in to comment.