Skip to content

Commit

Permalink
Allow exprs in the source_file grammar rule
Browse files Browse the repository at this point in the history
Summary:
Closes WhatsApp/tree-sitter-erlang#3.

Currently, only "top level" forms of a module are allowed at the beginning of the source_file rule, but this makes this tree-sitter not usable, for example, in Markdown code blocks.

Allowing any fragment of an expression makes this tree-sitter usable for other languages to inject Erlang code, for example, a new language in the Phoenix (Elixir) style, that embeeds Erlang code in HTML.

 ---

Notes:

1. The tree generated for fragmented code is `(source_file (...exprs))`. Maybe the tree as is today as `(source_file (...forms))` should be for a file/module structure and `(fragment (...exprs))` should be for fragmented parts of code
2. The term "fragment" is what comes to my mind, but could be any other
3. I'm not sure about the conflicts order introduced by this PR

 ---

## Before this PR

![tree-sitter-erlang-before](https://github.com/user-attachments/assets/b922e440-8ca0-432e-845d-82ad6c532299)

## After this PR

![tree-sitter-erlang-after](https://github.com/user-attachments/assets/0a835a76-1a90-4d7c-b171-9e46c9c47301)

X-link: WhatsApp/tree-sitter-erlang#9

Reviewed By: michalmuskala

Differential Revision: D69120163

Pulled By: alanz

fbshipit-source-id: 73f02ac6b61dbd0bec100d2dcfaa52b7694304cb
  • Loading branch information
williamthome authored and facebook-github-bot committed Feb 7, 2025
1 parent 01c58ce commit 8033c49
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
3 changes: 3 additions & 0 deletions crates/syntax/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5615,6 +5615,9 @@ pub struct SourceFile {
pub(crate) syntax: SyntaxNode,
}
impl SourceFile {
pub fn exprs(&self) -> AstChildren<Expr> {
support::children(&self.syntax)
}
pub fn forms_only(&self) -> AstChildren<Form> {
support::children(&self.syntax)
}
Expand Down
56 changes: 30 additions & 26 deletions crates/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,38 +459,42 @@ mod tests {

#[test]
fn error_nodes1() {
// Note: even though the FUN_DECL is in an ERROR node, we
// still lower it into something we can use. See test
// body::tests::lowering_with_error_nodes
check_node(
"f(1a) -> ok begin 1 end.",
expect![[r#"
[email protected]
FUN_DECL@0..11
FUNCTION_CLAUSE@0..11
ATOM@0..1
[email protected] "f"
[email protected]
ANON_LPAREN@1..2 "("
[email protected]
INTEGER@2..3
[email protected] "1"
[email protected]
[email protected] "a"
[email protected] ")"
[email protected] " "
[email protected]
ANON_DASH_GT@6..8 "->"
[email protected] " "
[email protected]
[email protected] "ok"
[email protected] " "
[email protected]
ERROR@0..17
FUN_DECL@0..11
FUNCTION_CLAUSE@0..11
[email protected]
[email protected] "f"
EXPR_ARGS@1..5
[email protected] "("
ERROR@2..3
[email protected]
[email protected] "1"
[email protected]
[email protected] "a"
[email protected] ")"
[email protected] " "
CLAUSE_BODY@6..11
[email protected] "->"
[email protected] " "
[email protected]
[email protected] "ok"
[email protected] " "
[email protected]
[email protected] "begin"
[email protected] " "
[email protected]
[email protected] "1"
[email protected] " "
[email protected] "end"
[email protected] ".""#]],
[email protected] " "
[email protected]
[email protected] "1"
[email protected] " "
[email protected]
[email protected] "end"
[email protected] ".""#]],
);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/syntax/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub enum SyntaxKind {
CATCH_EXPR = 202u16,
CHAR = 133u16,
CLAUSE_BODY = 200u16,
ANON_COLON = 3u16,
ANON_COLON = 4u16,
ANON_COLON_COLON = 62u16,
ANON_COLON_EQ = 95u16,
ANON_COMMA = 30u16,
Expand All @@ -78,7 +78,7 @@ pub enum SyntaxKind {
DEPRECATED_WILDCARD = 57u16,
DEPRECATION_DESC = 170u16,
ANON_DIV = 106u16,
ANON_DOT = 4u16,
ANON_DOT = 2u16,
ANON_DOT_DOT = 73u16,
DOTDOTDOT = 74u16,
ANON_ELIF = 26u16,
Expand Down Expand Up @@ -216,7 +216,7 @@ pub enum SyntaxKind {
SOURCE_FILE = 138u16,
SPEC = 180u16,
ANON_SPEC = 65u16,
ANON_SSR = 2u16,
ANON_SSR = 3u16,
SSR_DEFINITION = 140u16,
ANON_SSR_MATCH = 5u16,
SSR_REPLACEMENT = 141u16,
Expand Down

0 comments on commit 8033c49

Please sign in to comment.