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

Make the semicolon queryable #135

Merged
merged 4 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 2 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ module.exports = grammar({
// Simple statements

_simple_statements: $ => seq(
$._simple_statement,
optional(repeat(seq(
$._semicolon,
$._simple_statement
))),
optional($._semicolon),
resolritter marked this conversation as resolved.
Show resolved Hide resolved
sep1($._simple_statement, ';'),
optional(';'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't exactly the same as the before, it doesn't cover the first case, only the second, and it allows the third.

a = 'a'; b = 'b''
a = 'a'; b = 'b';
a = 'a'; b = 'b';;  # this is wrong

Would be great to add a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests in 29b828e. They are passing locally.

Copy link
Contributor

@stsewd stsewd Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the last expression shouldn't be valid

~/github/tree-sitter-python on  semicolon! ⌚ 13:45:27
$ cat test.py
   1   a = 'a'; b = 'b'

~/github/tree-sitter-python on  semicolon! ⌚ 13:45:29
$ npm run parse -- test.py

> [email protected] parse
> tree-sitter parse "test.py"

(module [0, 0] - [1, 0]
  (expression_statement [0, 0] - [0, 7]
    (assignment [0, 0] - [0, 7]
      left: (identifier [0, 0] - [0, 1])
      right: (string [0, 4] - [0, 7])))
  (expression_statement [0, 9] - [0, 16]
    (assignment [0, 9] - [0, 16]
      left: (identifier [0, 9] - [0, 10])
      right: (string [0, 13] - [0, 16]))))


~/github/tree-sitter-python on  semicolon! ⌚ 13:45:32
$ cat test.py
   1   a = 'a'; b = 'b';

~/github/tree-sitter-python on  semicolon! ⌚ 13:45:40
$ npm run parse -- test.py

> [email protected] parse
> tree-sitter parse "test.py"

(module [0, 0] - [1, 0]
  (expression_statement [0, 0] - [0, 7]
    (assignment [0, 0] - [0, 7]
      left: (identifier [0, 0] - [0, 1])
      right: (string [0, 4] - [0, 7])))
  (expression_statement [0, 9] - [0, 16]
    (assignment [0, 9] - [0, 16]
      left: (identifier [0, 9] - [0, 10])
      right: (string [0, 13] - [0, 16]))))

~/github/tree-sitter-python on  semicolon! ⌚ 13:45:42
$ cat test.py
   1   a = 'a'; b = 'b';;  # this is wrong

~/github/tree-sitter-python on  semicolon! ⌚ 13:45:51
$ npm run parse -- test.py

> [email protected] parse
> tree-sitter parse "test.py"

(module [0, 0] - [1, 0]
  (expression_statement [0, 0] - [0, 7]
    (assignment [0, 0] - [0, 7]
      left: (identifier [0, 0] - [0, 1])
      right: (string [0, 4] - [0, 7])))
  (expression_statement [0, 9] - [0, 16]
    (assignment [0, 9] - [0, 16]
      left: (identifier [0, 9] - [0, 10])
      right: (string [0, 13] - [0, 16])))
  (expression_statement [0, 17] - [0, 17]
    (identifier [0, 17] - [0, 17]))
  (comment [0, 20] - [0, 35]))
test.py 0 ms    (MISSING identifier [0, 17] - [0, 17])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With master, the last case results in

(module [0, 0] - [1, 0]
  (expression_statement [0, 0] - [0, 7]
    (assignment [0, 0] - [0, 7]
      left: (identifier [0, 0] - [0, 1])
      right: (string [0, 4] - [0, 7])))
  (ERROR [0, 7] - [0, 17]
    (expression_statement [0, 9] - [0, 16]
      (assignment [0, 9] - [0, 16]
        left: (identifier [0, 9] - [0, 10])
        right: (string [0, 13] - [0, 16])))))
test.py 0 ms    (ERROR [0, 7] - [0, 17])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried reverting to how the rule is in master but it did not result in an error. Might have something to do with the semicolon now being a literal instead of a $ => * rule.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I think this is fine (it generates a missing node)

$._newline
),

Expand Down Expand Up @@ -968,8 +964,6 @@ module.exports = grammar({
)),

comment: $ => token(seq('#', /.*/)),

_semicolon: $ => ';'
}
})

Expand Down
25 changes: 9 additions & 16 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,36 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_simple_statement"
},
{
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_simple_statement"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_semicolon"
"type": "STRING",
"value": ";"
},
{
"type": "SYMBOL",
"name": "_simple_statement"
}
]
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_semicolon"
"type": "STRING",
"value": ";"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -4885,10 +4882,6 @@
}
]
}
},
"_semicolon": {
"type": "STRING",
"value": ";"
}
},
"extras": [
Expand Down
4 changes: 4 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,10 @@
"type": ":=",
"named": false
},
{
"type": ";",
"named": false
},
{
"type": "<",
"named": false
Expand Down
Loading