diff --git a/.gitignore b/.gitignore index 285a11d..e9c2c8e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ node_modules build *.gyp +bindings/* + # see https://stackoverflow.com/a/5534865 !src/ src/* diff --git a/corpus/branch.txt b/corpus/branch.txt new file mode 100644 index 0000000..5ecd8ea --- /dev/null +++ b/corpus/branch.txt @@ -0,0 +1,81 @@ +=== +Simple branch 1 +=== + +if ( $true ) { } + +--- + +(program + (if_statement + (pipeline_statement + (variable + (simple_variable))))) + +=== +Simple branch 2 +=== + +if ( $true ) +{ +} + +--- + +(program + (if_statement + (pipeline_statement + (variable + (simple_variable))))) + +=== +Simple branch 3 +=== + +if($true){ +} + +--- + +(program + (if_statement + (pipeline_statement + (variable + (simple_variable))))) + +=== +Simple branch 4 +=== + +if ( $true ) { } else { } + +--- + +(program + (if_statement + (pipeline_statement + (variable + (simple_variable))) + (else_statement)) +) + +=== +Simple branch 5 +=== + +if ( $true ) { +} +elseif ( $true ) { +} +else { +} + +--- + +(program + (if_statement + (pipeline_statement + (variable + (simple_variable)))) +) + diff --git a/corpus/operators.txt b/corpus/operators.txt new file mode 100644 index 0000000..aa704ce --- /dev/null +++ b/corpus/operators.txt @@ -0,0 +1,337 @@ +=== +Simple binary operator 1 +=== + +$a -eQ $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + +=== +Simple binary operator 2 +=== + +$a -ne $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + +=== +Simple binary operator 3 +=== + +$a -ge $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 4 +=== + +$a -gt $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 5 +=== + +$a -lt $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 6 +=== + +$a -le $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 7 +=== + +$a -like $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 8 +=== + +$a -notlike $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 9 +=== + +$a -match $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 10 +=== + +$a -notmatch $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 11 +=== + +$a -replace $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 12 +=== + +$a -contains $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 13 +=== + +$a -notcontains $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 14 +=== + +$a -in $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 15 +=== + +$a -notin $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 16 +=== + +$a -split $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 17 +=== + +$a -join $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 18 +=== + +$a -is $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 19 +=== + +$a -isnot $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + + +=== +Simple binary operator 20 +=== + +$a -as $a + +--- + +(program + (binary_operator_statement + (variable (simple_variable)) + (binary_operator) + (variable (simple_variable)) + ) +) + diff --git a/corpus/type_definitions.txt b/corpus/type_definitions.txt index 2f9fc0f..908031a 100644 --- a/corpus/type_definitions.txt +++ b/corpus/type_definitions.txt @@ -38,7 +38,7 @@ enum Enum2 (bareword_string))) === -Basic class +Basic class 1 === class MyClass {} @@ -49,22 +49,35 @@ class MyClass {} (class_definition)) === -Class with property +Basic class 2 === -class Class2 +class MyClass { - [string]$Name + } --- (program - (class_definition + (class_definition)) + +=== +Class with property +=== +class Class2 +{ + [string]$Name +} +--- + + (program + (class_definition (class_property (type_expr) (simple_variable)))) + === Class with method === @@ -182,4 +195,4 @@ class Class2 (variable (simple_variable)) (variable - (simple_variable)))))))))) \ No newline at end of file + (simple_variable)))))))))) diff --git a/grammar.js b/grammar.js index d37d4d0..5de5b8f 100644 --- a/grammar.js +++ b/grammar.js @@ -125,6 +125,7 @@ module.exports = grammar({ _expression_statement: $ => choice( $.assignment_statement, + $.binary_operator_statement, $.pipeline_statement, $.if_statement, $.while_statement, @@ -160,7 +161,7 @@ module.exports = grammar({ ), elseif_statement: $ => seq( - caseInsensitive('elif'), + caseInsensitive('elseif'), '(', $.pipeline_statement, ')', @@ -319,6 +320,35 @@ module.exports = grammar({ $._expression ), + binary_operator: $ => choice( + caseInsensitive('-eq'), + caseInsensitive('-ne'), + caseInsensitive('-ge'), + caseInsensitive('-gt'), + caseInsensitive('-lt'), + caseInsensitive('-le'), + caseInsensitive('-like'), + caseInsensitive('-notlike'), + caseInsensitive('-match'), + caseInsensitive('-notmatch'), + caseInsensitive('-replace'), + caseInsensitive('-contains'), + caseInsensitive('-notcontains'), + caseInsensitive('-in'), + caseInsensitive('-notin'), + caseInsensitive('-split'), + caseInsensitive('-join'), + caseInsensitive('-is'), + caseInsensitive('-isnot'), + caseInsensitive('-as'), + ), + + binary_operator_statement: $ => seq( + $.variable, + $.binary_operator, + $._expression + ), + property_name: $ => choice( $._string_expr, $.bareword_string, @@ -338,6 +368,10 @@ module.exports = grammar({ ) ), + _newline: $ => choice( + '\n' + ), + simple_variable: $ => /\$[a-zA-Z0-9_:]+/i, splatted_variable: $ => /@[a-zA-Z0-9_:]+/i, @@ -457,6 +491,15 @@ function caseInsensitive (keyword) { ) } +function caseInsensitiveOperator(operator) { + re = new RegExp(operator + .split('') + .map(letter => `[${letter}${letter.toUpperCase()}]`) + .join('') + ); + return '-[Cc]?' + re; +} + function statement_sequence($) { return repeat( @@ -467,7 +510,7 @@ function statement_sequence($) ) } -function delimited_seq(rule, delimeter, oneOrMore, canFollowLast) +function delimited_seq(rule, delimiter, oneOrMore, canFollowLast) { if (canFollowLast) { @@ -476,7 +519,7 @@ function delimited_seq(rule, delimeter, oneOrMore, canFollowLast) return repeat1( seq( rule, - delimeter + delimiter ) ); } @@ -484,7 +527,7 @@ function delimited_seq(rule, delimeter, oneOrMore, canFollowLast) return repeat( seq( rule, - delimeter + delimiter ) ); } @@ -495,7 +538,7 @@ function delimited_seq(rule, delimeter, oneOrMore, canFollowLast) rule, repeat( seq( - delimeter, + delimiter, rule ) ) diff --git a/index.js b/index.js deleted file mode 100644 index 31eb304..0000000 --- a/index.js +++ /dev/null @@ -1,18 +0,0 @@ -//------------------------------------------------------------------------------------------------------- -// Copyright (C) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -//------------------------------------------------------------------------------------------------------- - -try { - module.exports = require("./build/Release/tree_sitter_PowerShell_binding"); -} catch (error) { - try { - module.exports = require("./build/Debug/tree_sitter_PowerShell_binding"); - } catch (_) { - throw error - } -} - -try { - module.exports.nodeTypeInfo = require("./src/node-types.json"); -} catch (_) {} diff --git a/package.json b/package.json index d96ac61..d732b1d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "tree-sitter-powershell", "version": "0.1.0", "description": "", - "main": "index.js", + "main": "bindings/node", "scripts": { "generate": "tree-sitter generate", "test": "tree-sitter test",