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

non-declaration statements are permitted at top level #63

Closed
adonovan opened this issue Mar 22, 2022 · 1 comment · Fixed by #67
Closed

non-declaration statements are permitted at top level #63

adonovan opened this issue Mar 22, 2022 · 1 comment · Fixed by #67

Comments

@adonovan
Copy link
Contributor

A Go source file always starts with a package declaration, but the Tree Sitter grammar for Go permits statements at top level:

x = 1

[source_file](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [1, 0]
  [assignment_statement](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 5]
    left: [expression_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 1]
      [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 1]
    right: [expression_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 4] - [0, 5]
      [int_literal](https://tree-sitter.github.io/tree-sitter/playground#) [0, 4] - [0, 5]

x := <-ch

[source_file](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [1, 0]
  [short_var_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 9]
    left: [expression_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 1]
      [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 1]
    right: [expression_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 5] - [0, 9]
      [unary_expression](https://tree-sitter.github.io/tree-sitter/playground#) [0, 5] - [0, 9]
        operand: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 7] - [0, 9]

The cause is this production:

tree-sitter-go/grammar.js

Lines 105 to 108 in 07d7228

source_file: $ => repeat(choice(
seq($._statement, terminator),
seq($._top_level_declaration, optional(terminator)),
)),

The fix is to remove the reference to _statement here and to add _declaration (var/const/type) to the choices for _top_level_declaration:

tree-sitter-go/grammar.js

Lines 110 to 115 in 07d7228

_top_level_declaration: $ => choice(
$.package_clause,
$.function_declaration,
$.method_declaration,
$.import_declaration
),

I wonder whether this was done intentionally because some tools want to process a chunk of statements instead of a complete file.

@maxbrunsfeld
Copy link
Contributor

This is intentional, so that Tree-sitter can parse things like code snippets inside of markdown files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants