Socket
Socket
Sign inDemoInstall

tree-sitter-c-sharp

Package Overview
Dependencies
13
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.4 to 0.13.0

753

grammar.js

@@ -21,10 +21,2 @@ const PREC = {

const COMMON_MODIFIERS = [
'new',
'public',
'protected',
'internal',
'private'
]
const BYTE_ORDER_MARK = '\xEF\xBB\xBF';

@@ -41,6 +33,14 @@

conflicts: $ => [
[$._field_modifiers, $.method_modifiers],
[$.return_type, $.variable_declaration]
[$.overloadable_unary_operator, $.overloadable_binary_operator],
[$.generic_name, $._expression],
[$.if_statement]
],
inline: $ => [
$.class_type,
$.return_type
],
word: $ => $.identifier_name,
rules: {

@@ -58,2 +58,4 @@ compilation_unit: $ => seq(

// types
_type_declaration: $ => choice(

@@ -67,11 +69,55 @@ $.class_declaration,

// extern
extern_alias_directive: $ => seq(
'extern',
'alias',
_type: $ => choice(
$.predefined_type,
$.identifier_name,
';'
$.generic_name
),
predefined_type: $ => choice(
'bool',
'byte',
'char',
'decimal',
'double',
'float',
'int',
'long',
'object',
'sbyte',
'short',
'string',
'uint',
'ulong',
'ushort'
),
type_parameter_list: $ => seq('<', commaSep1($.type_parameter), '>'),
type_parameter: $ => $._type,
// modifiers
modifiers: $ => repeat1(
choice(
'abstract',
'async',
'extern',
'internal',
'new',
'override',
'private',
'protected',
'public',
'readonly',
'sealed',
'static',
'unsafe',
'virtual',
'volatile'
)
),
// extern
extern_alias_directive: $ => seq('extern', 'alias', $.identifier_name, ';'),
// using

@@ -82,3 +128,3 @@

optional(choice(
$.static,
'static',
$.name_equals

@@ -93,2 +139,4 @@ )),

name_equals: $ => seq($.identifier_name, '='),
// namespace

@@ -108,5 +156,50 @@

)),
'}'
'}',
optional(';')
),
// properties
property_declaration: $ => seq(
optional($._attributes),
optional($.modifiers),
$._type,
$.identifier_name,
$._property_body
),
_property_body: $ => choice(
seq('{', $._accessor_declarations, '}', optional($._property_initializer)),
seq('=>', $._expression, ';')
),
_property_initializer: $ => seq('=', $.variable_initializer, ';'),
_accessor_declarations: $ => choice(
seq($.get_accessor_declaration, optional($.set_accessor_declaration)),
seq($.set_accessor_declaration, optional($.get_accessor_declaration))
),
get_accessor_declaration: $ => seq(
optional($._attributes),
optional($.accessor_modifier),
'get',
choice($.statement_block, ';')
),
set_accessor_declaration: $ => seq(
optional($._attributes),
optional($.accessor_modifier),
'set',
choice($.statement_block, ';')
),
accessor_modifier: $ => choice(
'protected',
'internal',
'private',
seq('protected', 'internal'),
seq('internal', 'protected')
),
// class

@@ -116,28 +209,185 @@

optional($._attributes),
optional($.class_modifiers),
optional($.modifiers),
optional('partial'),
'class',
$.identifier_name,
optional($.type_parameter_list),
optional($.class_base),
repeat($.type_parameter_constraints_clause),
'{',
repeat(choice(
$._type_declaration,
$.field_declaration,
$.constructor_declaration,
$.method_declaration
)),
'}'
repeat(
choice(
$.constant_declaration,
$.field_declaration,
$.method_declaration,
$.property_declaration,
$.event_declaration,
$.indexer_declaration,
$.operator_declaration,
$.constructor_declaration,
$.destructor_declaration,
$._type_declaration
),
),
'}',
optional(';')
),
class_modifiers: $ => $._class_modifiers,
_class_modifiers: $ => seq(
class_base: $ => seq(
':',
$.class_type,
optional(seq(', ', commaSep1($.identifier_name)))
),
class_type: $ => choice(
$.identifier_name,
'object',
'dynamic',
'string'
),
type_parameter_constraints_clause: $ => seq(
'where', $.identifier_name, ':', $.type_parameter_constraints
),
type_parameter_constraints: $ => choice(
$.constructor_constraint,
seq(
choice(
$.class_type,
'class',
'struct'
),
optional(seq(',', commaSep1($.identifier_name))),
optional(seq(',', $.constructor_constraint))
)
),
constructor_constraint: $ => seq('new', '(', ')'),
// indexers
indexer_declaration: $ => seq(
optional($._attributes),
optional($.modifiers),
$._indexer_declarator,
$._indexer_body
),
_indexer_declarator: $ => choice(
seq($._type, 'this', '[', $._formal_parameter_list, ']'),
seq(
$._type,
$.identifier_name,
'.',
'this',
'[',
$._formal_parameter_list,
']'
),
),
_indexer_body: $ => choice(
seq('{', $._accessor_declarations, '}'),
seq('=>', $._expression, ';'),
),
// events
event_declaration: $ => seq(
optional($._attributes),
optional($.modifiers),
'event',
$._type,
$.identifier_name,
'{',
choice(
'unsafe',
'abstract',
'sealed',
'static',
...COMMON_MODIFIERS
seq($.add_accessor_declaration, $.remove_accessor_declaration),
seq($.remove_accessor_declaration, $.add_accessor_declaration)
),
optional($._class_modifiers)
'}'
),
add_accessor_declaration: $ => seq(optional($._attributes), 'add', $.statement_block),
remove_accessor_declaration: $ => seq(optional($._attributes), 'remove', $.statement_block),
// operator declarations
operator_declaration: $ => seq(
optional($._attributes),
optional($.modifiers),
$._operator_declarator,
choice(
$.statement_block,
seq('=>', $._expression, ';'),
';'
)
),
_operator_declarator: $ => choice(
$._unary_operator_declarator,
$._binary_operator_declarator,
$._conversion_operator_declarator
),
_unary_operator_declarator: $ => seq(
$._type,
'operator',
$.overloadable_unary_operator,
'(',
$._type,
$.identifier_name,
')'
),
overloadable_unary_operator: $ => choice(
'+',
'-',
'!',
'~',
'++',
'--',
'true',
'false'
),
_binary_operator_declarator: $ => seq(
$._type,
'operator',
$.overloadable_binary_operator,
'(',
$._type,
$.identifier_name,
',',
$._type,
$.identifier_name,
')'
),
overloadable_binary_operator: $=> choice(
'+', '-',
'*', '/',
'%', '^',
'|', '&',
'<<', '>>',
'==', '!=',
'>', '<',
'>=', '<='
),
_conversion_operator_declarator: $=> seq(
$.overloadable_conversion_operator,
'operator',
$._type,
'(',
$._type,
$.identifier_name,
')'
),
overloadable_conversion_operator: $ => choice(
'implicit',
'explicit'
),
// interface

@@ -147,17 +397,71 @@

optional($._attributes),
optional($.interface_modifiers),
optional($.modifiers),
optional('partial'),
'interface',
$.identifier_name,
optional($.type_parameter_list),
optional($.interface_base),
repeat($.type_parameter_constraints_clause),
'{',
repeat(choice(
$.field_declaration
)),
repeat(
choice(
$.interface_method_declaration,
$.interface_event_declaration,
$.interface_property_declaration,
$.interface_indexer_declaration
)
),
'}',
optional(';')
),
interface_base: $ => seq(
':',
$.identifier_name,
optional(seq(',', commaSep1($.identifier_name)))
),
interface_method_declaration: $ => seq(
optional($._attributes),
optional('new'),
$.return_type,
$.identifier_name,
optional($.type_parameter_list),
optional($.parameter_list),
repeat($.type_parameter_constraints_clause),
';'
),
interface_event_declaration: $ => seq(
optional($._attributes),
optional('new'),
'event',
$._type,
$.identifier_name,
';'
),
interface_property_declaration: $ => seq(
optional($._attributes),
optional('new'),
$._type,
$.identifier_name,
'{',
repeat1($.interface_accessor),
'}'
),
interface_modifiers: $ => $._interface_modifiers,
_interface_modifiers: $ => seq(
choice(...COMMON_MODIFIERS),
optional($._interface_modifiers)
interface_accessor: $ => seq(optional($._attributes), choice('get', 'set'), ';'),
interface_indexer_declaration: $ => seq(
optional($._attributes),
optional('new'),
$._type,
'this',
'[',
$._formal_parameter_list,
']',
'{',
repeat1($.interface_accessor),
'}'
),

@@ -169,19 +473,28 @@

optional($._attributes),
optional($.struct_modifiers),
optional($.modifiers),
optional('partial'),
'struct',
$.identifier_name,
optional($.type_parameter_list),
optional($.struct_interfaces),
repeat($.type_parameter_constraints_clause),
'{',
repeat(choice(
$._type_declaration,
$.field_declaration
)),
'}'
repeat(
choice(
$.constant_declaration,
$.field_declaration,
$.method_declaration,
$.property_declaration,
$.event_declaration,
$.indexer_declaration,
$.operator_declaration,
$.constructor_declaration,
$._type_declaration
)
),
'}',
optional(';')
),
struct_modifiers: $ => $._struct_modifiers,
_struct_modifiers: $ => seq(
choice('unsafe', ...COMMON_MODIFIERS),
optional($._struct_modifiers)
),
struct_interfaces: $ => seq(':', commaSep1($.identifier_name)),

@@ -192,3 +505,3 @@ // enum

optional($._attributes),
optional($.enum_modifiers),
optional($.modifiers),
'enum',

@@ -206,11 +519,5 @@ $.identifier_name,

$.identifier_name,
optional($.equals_value_clause)
optional(seq('=', $.constant_expression))
),
enum_modifiers: $ => choice(...COMMON_MODIFIERS),
_enum_modifiers: $ => seq(
choice(...COMMON_MODIFIERS),
optional($._enum_modifiers)
),
_integral_type: $ => choice(

@@ -232,3 +539,3 @@ 'sbyte',

optional($._attributes),
optional($.delegate_modifier),
optional($.modifiers),
'delegate',

@@ -242,4 +549,2 @@ $.return_type,

delegate_modifier: $ => choice('unsafe', ...COMMON_MODIFIERS),
return_type: $ => choice($._type, $.void_keyword),

@@ -267,5 +572,6 @@ void_keyword: $ => 'void',

$.identifier_name,
optional($.equals_value_clause)
optional($.default_argument)
),
default_argument: $ => seq('=', $._expression),
parameter_modifier: $ => choice('ref', 'out', 'this'),

@@ -275,3 +581,3 @@

optional($._attributes),
$.params_keyword,
'params',
$.array_type,

@@ -281,32 +587,14 @@ $.identifier_name

params_keyword: $ => 'params',
// arrays
array_type: $ => seq(
$._type,
$.array_rank_specifier
),
array_type: $ => seq($._type, $.array_rank_specifier),
array_rank_specifier: $ => seq('[', repeat(','), ']'),
array_rank_specifier: $ => seq(
'[',
repeat(','),
']'
),
// attributes
_attributes: $ => repeat1($._attribute_section),
_attribute_section: $ => seq(
'[',
$.attribute_list,
']'
),
_attribute_section: $ => seq('[', $.attribute_list, ']'),
attribute_list: $ => commaSep1($.attribute),
attribute: $ => seq($.identifier_name, optional($.attribute_argument_list)),
attribute: $ => seq(
$.identifier_name,
optional($.attribute_argument_list)
),
attribute_argument_list: $ => seq(

@@ -326,4 +614,2 @@ '(',

const_keyword: $ => 'const',
// fields

@@ -333,4 +619,3 @@

optional($._attributes),
optional($.field_modifiers),
optional($.const_keyword),
optional($.modifiers),
$.variable_declaration,

@@ -340,39 +625,30 @@ ';'

field_modifiers: $ => $._field_modifiers,
_field_modifiers: $ => seq(
choice(
'unsafe',
'readonly',
'volatile',
'static',
...COMMON_MODIFIERS
),
optional($._field_modifiers)
variable_declaration: $ => seq($._type, commaSep1($.variable_declarator)),
generic_name: $ => seq($.identifier_name, $.type_parameter_list),
variable_declarator: $ => seq($.identifier_name, optional($.equals_value_clause)),
variable_initializer: $ => choice(
$._expression,
$.array_initalizer
),
variable_declaration: $ => seq(
$._type,
commaSep1($.variable_declarator)
),
array_initalizer: $ => seq('{', commaSep1($.variable_initializer), '}'),
equals_value_clause: $ => seq('=', $._expression),
_type: $ => choice(
$.predefined_type,
$.identifier_name,
$.generic_name
),
// constants
generic_name: $ => seq(
$.identifier_name,
$.type_parameter_list
constant_declaration: $ => seq(
optional($._attributes),
optional($.modifiers),
'const',
$._type,
$._constant_declarators,
';'
),
variable_declarator: $ => seq(
$.identifier_name,
optional($.equals_value_clause)
_constant_declarators: $ => seq(
$.constant_declarator,
repeat(seq(',', $.constant_declarator))
),
equals_value_clause: $ => seq(
'=',
$._expression
),
constant_declarator: $ => seq($.identifier_name, '=', $.constant_expression),

@@ -390,2 +666,4 @@ // expressions

boolean_expression: $ => $._expression,
constant_expression: $ => $._expression,
parenthesized_expression: $ => seq('(', $._expression, ')'),

@@ -554,28 +832,4 @@

predefined_type: $ => choice(
'bool',
'byte',
'char',
'decimal',
'double',
'float',
'int',
'long',
'object',
'sbyte',
'short',
'string',
'uint',
'ulong',
'ushort'
),
// names
type_parameter_list: $ => seq(
'<',
commaSep1($.type_parameter),
'>'
),
type_parameter: $ => $._type,
qualified_name: $ => seq(

@@ -591,19 +845,5 @@ choice(

alias_qualified_name: $ => seq(
$.global,
'::',
$.identifier_name
),
alias_qualified_name: $ => seq('global', '::', $.identifier_name),
identifier_name: $ => /[a-zA-Z_][a-zA-Z_0-9]*/,
name_equals: $ => seq(
$.identifier_name,
'='
),
global: $ => 'global',
static: $ => 'static',
identifier_name: $ => (/[a-zA-Z_][a-zA-Z_0-9]*/),
// commments

@@ -624,5 +864,6 @@

// methods
constructor_declaration: $ => seq(
optional($._attributes),
optional($.method_modifiers),
optional($.modifiers),
$.identifier_name,

@@ -634,6 +875,15 @@ optional($.type_parameter_list),

destructor_declaration: $ => seq(
optional($._attributes),
optional('extern'),
'~',
$.identifier_name,
$.parameter_list,
$.statement_block
),
method_declaration: $ => seq(
optional($._attributes),
optional($.method_modifiers),
optional('async'),
optional($.modifiers),
optional('partial'),
$.return_type,

@@ -643,45 +893,176 @@ $.identifier_name,

$.parameter_list,
repeat($.type_parameter_constraints_clause),
$.statement_block
),
method_modifiers: $ => choice(...COMMON_MODIFIERS),
// Statements
statement_block: $ => seq(
'{',
repeat($._statement),
'}'
_statement: $ => choice(
$._labeled_statement,
$._embedded_statement,
$._declaration_statement,
$.variable_assignment_statement // TODO: Remove
),
_statement: $ => choice(
variable_assignment_statement: $ => seq($.identifier_name, $.equals_value_clause, ';'),
statement_block: $ => seq('{', optional($._statement_list), '}'),
_statement_list: $ => repeat1($._statement),
_labeled_statement: $ => seq(
alias($.identifier_name, $.label_name),
':',
$._statement
),
// Embedded statements
_embedded_statement: $ => choice(
$.statement_block,
$.empty_statement,
$.expression_statement,
$._selection_statement,
$._iteration_statement,
$._jump_statement,
$.try_statement,
$.checked_statement,
$.unchecked_statement,
$.lock_statement,
$.yield_statement
),
empty_statement: $ => ';',
expression_statement: $ => seq($._expression, ';'),
_selection_statement: $ => choice(
$.if_statement,
$.switch_statement
),
_iteration_statement: $ => choice(
$.while_statement,
$.do_statement
),
_jump_statement: $ => choice(
$.break_statement,
$.continue_statement,
$.goto_statement,
$.return_statement,
$.empty_statement,
// $.variable_declaration_statement,
$.variable_assignment_statement
$.throw_statement
),
expression_statement: $ => seq(
$._expression,
try_statement: $ => seq(
'try',
$.statement_block,
repeat($.catch_clause),
optional($.finally_clause),
),
catch_clause: $ => seq(
'catch',
optional($._exception_specifier),
optional($._exception_filter),
$.statement_block
),
_exception_specifier: $ => seq('(', $._type, optional($.identifier_name), ')'),
_exception_filter: $ => seq('when', '(', $._expression, ')'),
finally_clause: $ => seq('finally', $.statement_block),
checked_statement: $ => seq('checked', $.statement_block),
unchecked_statement: $ => seq('unchecked', $.statement_block),
lock_statement: $ => seq('lock', '(', $._expression, ')', $._embedded_statement),
yield_statement: $ => seq(
'yield',
choice(
seq('return', $._expression),
'break'
),
';'
),
return_statement: $ => seq(
'return',
if_statement: $ => seq(
'if',
'(',
$.boolean_expression,
')',
$._embedded_statement,
optional(
seq(
'else',
$._embedded_statement,
)
)
),
switch_statement: $ => seq(
'switch',
'(',
$._expression,
')',
'{',
repeat($.switch_section),
'}'
),
switch_section: $ => seq(repeat1($.switch_label), $._statement_list),
switch_label: $ => choice(
seq('case', $.constant_expression, ':'),
seq('default', ':')
),
while_statement: $ => seq('while', '(', $.boolean_expression, ')', $._embedded_statement),
do_statement: $ => seq('do', $._embedded_statement, 'while', '(', $.boolean_expression, ')', ';'),
break_statement: $ => seq('break', ';'),
continue_statement: $ => seq('continue', ';'),
return_statement: $ => seq('return', optional($._expression), ';'),
throw_statement: $ => seq('throw', optional($._expression), ';'),
goto_statement: $ => seq(
'goto',
choice(
alias($.identifier_name, $.label_name),
seq('case', $.constant_expression),
'default'
),
';'
),
variable_declaration_statement: $ => seq(
optional($.const_keyword),
$.variable_declaration,
// declaration statements
_declaration_statement: $ => seq(
choice(
$.local_variable_declaration,
$.local_constant_declaration
),
';'
),
variable_assignment_statement: $ => seq(
local_variable_declaration: $ => seq(
$._local_variable_type,
$._local_variable_declarators
),
_local_variable_type: $ => choice(
'var',
$._type
),
_local_variable_declarators: $ => choice(
$.local_variable_declarator,
seq($._local_variable_declarators, ',', $.local_variable_declarator)
),
local_variable_declarator: $ => seq(
$.identifier_name,
$.equals_value_clause,
';'
optional(seq('=', $.local_variable_initializer))
),
empty_statement: $ => ';',
local_variable_initializer: $ => choice(
$._expression,
$.array_initalizer
),
local_constant_declaration: $ => seq('const', $._type, $._constant_declarators)
}

@@ -688,0 +1069,0 @@ })

module.exports = require("./build/Release/ts_language_c_sharp_binding");
{
"name": "tree-sitter-c-sharp",
"version": "0.0.4",
"version": "0.13.0",
"description": "C# grammar for tree-sitter",

@@ -13,6 +13,6 @@ "main": "index.js",

"dependencies": {
"nan": "^2.4.0"
"nan": "^2.10.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.8.2"
"tree-sitter-cli": "^0.13.1"
},

@@ -19,0 +19,0 @@ "scripts": {

@@ -6,4 +6,10 @@ tree-sitter-c-sharp

C# grammar for [tree-sitter][].
C# grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter)
[tree-sitter]: https://github.com/tree-sitter/tree-sitter
### Status
Currently incomplete, based on the C# 6.0 draft proposal.
### References
* [Official C# Language Spec](https://github.com/dotnet/csharplang/blob/master/spec/) It provides chapters that formally define the language grammar.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc