Skip to content

Commit

Permalink
Merge pull request #47 from tobil4sk/fix/keywords
Browse files Browse the repository at this point in the history
Remove keyword node from syntax tree
  • Loading branch information
vantreeseba authored Sep 12, 2024
2 parents 68b5322 + b86dbbb commit e7a07ad
Show file tree
Hide file tree
Showing 20 changed files with 105,881 additions and 121,742 deletions.
28 changes: 15 additions & 13 deletions grammar-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@ module.exports = {
$.variable_declaration,
),
_access_identifier: ($) =>
alias(choice('default', 'null', 'get', 'set', 'dynamic', 'never'), $.keyword),
choice('default', 'null', 'get', 'set', 'dynamic', 'never'),
access_identifiers: ($) =>
seq('(', $._access_identifier, optional(seq(',', $._access_identifier)), ')'),
type_param: ($) => choice($._lhs_expression, seq($._lhs_expression, $.type_params)),
type_params: ($) => prec.right(1, seq('<', commaSep1($.type_param), '>')),

_modifier: ($) => choice('macro', 'abstract', 'static', 'public', 'private', 'extern', 'inline', 'overload', 'override', 'final'),

class_declaration: ($) =>
seq(
repeat($.metadata),
optional(choice(alias('final', $.keyword), alias('abstract', $.keyword))),
alias('class', $.keyword),
optional(choice('final', 'abstract')),
'class',
field('name', $._lhs_expression),
optional($.type_params),
optional(
seq(
alias('extends', $.keyword),
'extends',
field('super_class_name', $._lhs_expression),
optional($.type_params),
),
),
optional(
repeat(
seq(
alias('implements', $.keyword),
'implements',
field('interface_name', $._lhs_expression),
optional($.type_params),
),
Expand All @@ -45,14 +47,14 @@ module.exports = {

interface_declaration: ($) =>
seq(
optional(alias('final', $.keyword)),
alias('interface', $.keyword),
optional('final'),
'interface',
field('name', $._lhs_expression),
optional($.type_params),
optional(
repeat(
seq(
alias('extends', $.keyword),
'extends',
field('interface_name', $._lhs_expression),
optional($.type_params),
),
Expand All @@ -64,7 +66,7 @@ module.exports = {
typedef_declaration: ($) =>
seq(
repeat($.metadata),
alias('typedef', $.keyword),
'typedef',
field('name', $._lhs_expression),
optional($.type_params),
seq('=', choice($.block, $._lhs_expression, $.type)),
Expand All @@ -74,8 +76,8 @@ module.exports = {
function_declaration: ($) =>
seq(
repeat($.metadata),
repeat($.keyword),
alias('function', $.keyword),
repeat($._modifier),
'function',
choice(field('name', $._lhs_expression), field('name', alias('new', $.identifier))),
optional($.type_params),
$._function_arg_list,
Expand All @@ -99,8 +101,8 @@ module.exports = {
variable_declaration: ($) =>
seq(
repeat($.metadata),
repeat($.keyword),
choice(alias('var', $.keyword), alias('final', $.keyword)),
repeat($._modifier),
choice('var', 'final'),
field('name', $._lhs_expression),
optional($.access_identifiers),
optional(seq(':', optional(repeat('(')), field('type', $.type), optional(repeat(')')))),
Expand Down
49 changes: 0 additions & 49 deletions grammar-keywords.js

This file was deleted.

40 changes: 23 additions & 17 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const operators = require('./grammar-operators.js');
const literals = require('./grammar-literals.js');
const declarations = require('./grammar-declarations');
const keywords = require('./grammar-keywords');
const builtins = require('./grammar-builtins');

const { commaSep, commaSep1 } = require('./utils');
Expand Down Expand Up @@ -68,19 +67,19 @@ const haxe_grammar = {

package_statement: ($) =>
seq(
alias('package', $.keyword),
'package',
optional(field('name', $._lhs_expression)),
$._semicolon
),

import_statement: ($) =>
seq(alias('import', $.keyword), field('name', $._lhs_expression), $._semicolon),
seq('import', field('name', $._lhs_expression), $._semicolon),

using_statement: ($) =>
seq(alias('using', $.keyword), field('name', $._lhs_expression), $._semicolon),
seq('using', field('name', $._lhs_expression), $._semicolon),

throw_statement: ($) =>
prec.right(seq(alias('throw', $.keyword), $.expression, $._lookback_semicolon)),
prec.right(seq('throw', $.expression, $._lookback_semicolon)),

_rhs_expression: ($) =>
prec.right(choice($._literal, $.identifier, $.member_expression, $.call_expression)),
Expand All @@ -103,7 +102,7 @@ const haxe_grammar = {
switch_expression: ($) =>
prec.right(
seq(
alias('switch', $.keyword),
'switch',
choice($.identifier, $._parenthesized_expression),
alias($.switch_block, $.block),
),
Expand All @@ -117,28 +116,28 @@ const haxe_grammar = {
prec.right(
choice(
seq(
alias('case', $.keyword),
'case',
choice($._rhs_expression, alias('_', $._rhs_expression)),
':',
$.statement,
),
seq(alias('default', $.keyword), ':', $.statement),
seq('default', ':', $.statement),
),
),

cast_expression: ($) =>
choice(
seq(alias('cast', $.keyword), $._rhs_expression),
seq('cast', $._rhs_expression),
seq(
alias('cast', $.keyword),
'cast',
'(',
$._rhs_expression,
optional(seq(',', field('type', $.type))),
')',
),
),

type_trace_expression: ($) => seq(alias('$type', $.keyword), '(', $._rhs_expression, ')'),
type_trace_expression: ($) => seq('$type', '(', $._rhs_expression, ')'),

_parenthesized_expression: ($) => seq('(', repeat1(prec.left($.expression)), ')'),

Expand All @@ -147,7 +146,7 @@ const haxe_grammar = {
1,
seq(
$.identifier,
alias('in', $.keyword),
'in',
choice(seq($.integer, $._rangeOperator, $.integer), $.identifier),
),
),
Expand All @@ -164,6 +163,10 @@ const haxe_grammar = {
$.switch_expression,
// simple expression, or chained.
seq($._rhs_expression, repeat(seq($.operator, $._rhs_expression))),
seq('return', optional($._rhs_expression)),
seq('untyped', $._rhs_expression),
'break',
'continue',
),

subscript_expression: ($) =>
Expand All @@ -182,7 +185,7 @@ const haxe_grammar = {
prec.right(
seq(
choice(
field('object', choice(alias('this', $.keyword), $.identifier)),
field('object', choice('this', $.identifier)),
field('literal', $._literal),
),
choice(token('.'), seq(alias('?', $.operator), '.')),
Expand Down Expand Up @@ -236,10 +239,10 @@ const haxe_grammar = {
prec.right(
1,
seq(
field('name', alias('if', $.keyword)),
field('name', 'if'),
field('arguments_list', $._arg_list),
optional($.block),
optional(seq(alias(choice('else', 'else if'), $.keyword), $.block)),
optional(seq(choice('else', 'else if'), $.block)),
),
),

Expand All @@ -255,7 +258,7 @@ const haxe_grammar = {

_constructor_call: ($) =>
seq(
optional(alias('new', $.keyword)), // for constructor calls.
optional('new'), // for constructor calls.
$._call,
),

Expand All @@ -266,7 +269,10 @@ const haxe_grammar = {
...literals,

comment: ($) => token(choice(seq('//', /.*/), seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'))),
keyword: ($) => prec.right(choice(...keywords)),
// TODO: implement the structures that use these
keyword: ($) => choice('catch', 'do', 'enum', 'for', 'try', 'while'),
// keywords reserved by the haxe compiler that are not currently used
reserved_keyword: ($) => choice('operator'),
identifier: ($) => /[a-zA-Z_]+[a-zA-Z0-9]*/,
// Hidden Nodes in tree.
_semicolon: ($) => $._lookback_semicolon,
Expand Down
48 changes: 47 additions & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

; Literals
; --------
[(keyword) (null)] @keyword
(null) @keyword
; (type) @type
(type (identifier) !built_in) @type
(type built_in: (identifier)) @type.builtin
Expand All @@ -76,6 +76,52 @@
(operator) @operator
(escape_sequence) @punctuation

; Keywords
; --------
[
"abstract"
"break"
"case"
"cast"
"catch"
"class"
"continue"
"default"
"do"
"dynamic"
"else"
"enum"
"extends"
"extern"
"final"
"for"
"function"
"if"
"implements"
"import"
"in"
"inline"
"interface"
"macro"
"new"
"operator"
"overload"
"override"
"package"
"private"
"public"
"return"
"static"
"switch"
"this"
"throw"
"try"
"typedef"
"untyped"
"using"
"var"
"while"
] @keyword

; Tokens
; ------
Expand Down
Loading

0 comments on commit e7a07ad

Please sign in to comment.