Skip to content

Commit

Permalink
Merge pull request Wilfred#4 from JoranHonig/update_grammar
Browse files Browse the repository at this point in the history
Add contract body rule fixes and tests to tree-sitter-solidity
  • Loading branch information
JoranHonig authored Oct 4, 2020
2 parents 7a31b60 + 4c9ac84 commit 8e66b2c
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 29,776 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@


# Dependency directories
node_modules/
node_modules/
src/
.vscode/
binding.gyp
index.js
15 changes: 14 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run tree-sitter test",
"request": "launch",
"runtimeArgs": [
"test",
],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"type": "node",
"request": "launch",
Expand All @@ -13,6 +25,7 @@
],
"program": "${workspaceFolder}/node_modules/bin/tree-sitter",
"args": ["generate"],
}
},

]
}
18 changes: 0 additions & 18 deletions binding.gyp

This file was deleted.

65 changes: 42 additions & 23 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ module.exports = grammar({
repeat(choice(
$.function_definition,
$.modifier_definition,
$.field_definition,
$.state_variable_declaration,
$.struct_declaration,
$.enum_declaration,
$.event_definition,
$.using_directive,
$.constructor_definition,
$.fallback_definition,
$.fallback_receive_definition,
)),
"}",
),
Expand Down Expand Up @@ -207,11 +207,25 @@ module.exports = grammar({


event_definition: $ => seq(
'event', field('name', $.identifier), $._parameter_list, optional('anonymous'), $._semicolon
'event', field('name', $.identifier), $._event_parameter_list , optional('anonymous'), $._semicolon
),

_event_parameter_list: $ => seq(
"(",
commaSep(seq(
field("type", $.type_name),
optional("indexed"),
optional(field("name", $.identifier)),
)),
")"
),

using_directive: $ => seq(
'using', $._user_defined_type, 'for', choice('*', $.type_name), $._semicolon
'using',
field("alias", $._user_defined_type),
'for',
field("source", choice('*', $.type_name)),
$._semicolon
),

// -- [ Statements ] --
Expand Down Expand Up @@ -303,17 +317,21 @@ module.exports = grammar({

// -- [ Definitions ] --
// Definitions
field_definition: $ => seq(
$.type_name,
field('visibility', $.visibility), // TODO: add constant
optional($._immutable),
$.identifier,
state_variable_declaration: $ => seq(
field("type", $.type_name),
repeat(choice(
field('visibility', $.visibility), // FIXME: this also allows external
$.constant,
$.override_specifier,
$.immutable,
)),
field("name", $.identifier),
optional(seq(
'=', $._expression
'=', field("value", $._expression)
)),
$._semicolon
),

constant: $ => "constant",
visibility: $ => choice(
'public',
'internal',
Expand All @@ -327,7 +345,7 @@ module.exports = grammar({
'payable'
),

_immutable: $ => 'immutable',
immutable: $ => 'immutable',
_override: $ => 'override',

override_specifier: $ => seq(
Expand All @@ -341,13 +359,13 @@ module.exports = grammar({

modifier_definition: $ => seq(
"modifier",
$.identifier,
$._parameter_list,
field("name", $.identifier),
optional($._parameter_list),
repeat(choice(
'virtual',
'override',
$.virtual,
$.override_specifier,
)),
choice($._semicolon, $.function_body)
choice($._semicolon, field("body", $.function_body)),
),

constructor_definition: $ => seq(
Expand All @@ -361,17 +379,18 @@ module.exports = grammar({
field('body', $.function_body),
),

fallback_definition: $ => seq(
"function",
fallback_receive_definition: $ => seq(
optional("function"),
choice('fallback', 'receive'),
'(', ')',
// FIXME: We use repeat to allow for unorderedness. However, this means that the parser
// accepts more than just the solidity language. The same problem exists for other definition rules.
repeat(choice(
field('visibility', $.visibility),
$.visibility,
$.modifier_invocation,
'virtual',
'override',
$.state_mutability,
$.virtual,
$.override_specifier,
)),
choice($._semicolon, field('body', $.function_body))
),
Expand Down Expand Up @@ -634,7 +653,7 @@ module.exports = grammar({

_semicolon: $ => ';',

identifier: $ => /[a-zA-Z$_][a-zA-Z0-9$_]+/,
identifier: $ => /[a-zA-Z$_][a-zA-Z0-9$_]*/,

number: $ => /\d+/,
literal: $ => choice(
Expand Down
13 changes: 0 additions & 13 deletions index.js

This file was deleted.

28 changes: 0 additions & 28 deletions src/binding.cc

This file was deleted.

Loading

0 comments on commit 8e66b2c

Please sign in to comment.