Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Dec 11, 2022
0 parents commit 3e3740b
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/node_modules

# Generated files
/bindings
/target
/src
/Cargo.toml
/Cargo.lock
/binding.gyp
*.lib
*.exp
*.obj
*.so
*.wasm

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ecsact Tree-sitter Parser

50 changes: 50 additions & 0 deletions examples/example.ecsact
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package example;

import other.package;
import cool;

enum DamageType {
FIRE = 0;
ROCK = 1;
}

component Health {
f32 value;
}

component Attacking {
DamageType damage;
entity target;
}

component Poisoned {
f32 potency;
}

system PoisonDamage {
readwrite Health;
readonly Poisoned;
}

system AttackerDamageShortSyntax {
readonly Attacking with target {
readwrite Health;
}
}

system AttackerDamageLongSyntax {
readonly Attacking {
with target {
readwrite Health;
}
}
}

component Attacker;

action Attack {
entity target;
include Attacker;
adds Attacking;
}

199 changes: 199 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
module.exports = grammar({
name : 'ecsact',
rules: {
source_file: $ => seq(
// TODO(zaucy): Package statements are not optional in an Ecsact file
optional($.package_statement),
repeat($.import_statement),
repeat($._declaration_statement),
),

package_statement: $ => seq(
'package',
field('name', $.package_identifier),
choice(';', seq('{', '}')),
),

import_statement: $ => seq(
'import',
$.package_identifier,
choice(';', seq('{', '}')),
),

_declaration_statement: $ => choice(
$.component_statement,
$.transient_statement,
$.enum_statement,
$.system_statement,
$.action_statement,
),

component_statement: $ => seq(
'component',
field('name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat($.field_statement),
'}',
),
),
),

transient_statement: $ => seq(
'transient',
field('name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat($.field_statement),
'}',
),
),
),

enum_statement: $ => seq(
'enum',
field('name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat(
$.enum_value_statement
),
'}',
),
),
),

system_statement: $ => seq(
'system',
field('name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat($.system_capability_statement),
'}',
),
),
),

action_statement: $ => seq(
'action',
field('name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat(choice(
$.system_capability_statement,
$.field_statement,
)),
'}',
),
),
),

with_statement: $ => seq(
'with',
field('field_name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat($.system_capability_statement),
'}',
),
),
),

system_capability: $ => choice(
'readwrite',
'readonly',
'writeonly',
'adds',
'removes',
'exclude',
'incldue',
),

_system_capability_statement_with_shorthand: $ => seq(
$.system_capability,
field('component_name', $.declaration_identifier),
$.with_statement,
),

_system_capability_statement: $ => seq(
$.system_capability,
field('component_name', $.declaration_identifier),
choice(
';',
seq(
'{',
repeat($.with_statement),
'}',
),
),
),

system_capability_statement: $ => seq(
choice(
$._system_capability_statement_with_shorthand,
$._system_capability_statement,
),
),

number: () => /\d+/,

enum_value_statement: $ => seq(
field('name', $.declaration_identifier),
'=',
field('value', $.number),
';',
),

builtin_field_type: () => choice(
'i32',
'u32',
'f32',
'i16',
'u16',
'i8',
'u8',
'entity',
),

user_field_type: $ => $.declaration_identifier,

_field_type: $ => seq(
field(
'field_type',
choice(
$.builtin_field_type,
$.user_field_type,
),
),
optional(
seq(
'[',
field('length', $.number),
']',
),
),
),

field_statement: $ => seq(
$._field_type,
field('field_name', $.declaration_identifier),
choice(';', seq('{', '}')),
),

package_identifier: () => /[a-z][a-z.]+[a-z]/,
declaration_identifier: () => /[a-zA-Z][a-zA-Z0-9_]*/,
},
});

47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "tree-sitter-ecsact",
"version": "0.1.0",
"description": "",
"main": "bindings/node",
"scripts": {
"test": "tree-sitter test",
"generate": "tree-sitter generate",
"highlight": "tree-sitter highlight examples/example.ecsact"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ecsact-dev/tree-sitter-ecsact.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/ecsact-dev/tree-sitter-ecsact/issues"
},
"homepage": "https://github.com/ecsact-dev/tree-sitter-ecsact#readme",
"dependencies": {
"nan": "^2.17.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.20.7"
},
"tree-sitter": [
{
"scope": "source.ecsact",
"file-types": [
"ecsact"
]
}
]
}
36 changes: 36 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
";" @punctuation
"[" @punctuation.bracket
"]" @punctuation.bracket
"{" @punctuation.bracket
"}" @punctuation.bracket
"=" @operator

(package_identifier) @module

(builtin_field_type) @type.builtin
(user_field_type) @type
(number) @number

(enum_statement name: (declaration_identifier) @type)
(component_statement name: (declaration_identifier) @type)
(transient_statement name: (declaration_identifier) @type)
(system_statement name: (declaration_identifier) @type)
(action_statement name: (declaration_identifier) @type)
(system_capability_statement component_name: (declaration_identifier) @type)
(with_statement field_name: (declaration_identifier) @property)

(enum_value_statement name: (declaration_identifier) @property)
(field_statement field_name: (declaration_identifier) @property)

(system_capability) @function

"with" @function
"package" @keyword
"import" @keyword
"enum" @keyword
"component" @keyword
"transient" @keyword
"action" @keyword
"system" @keyword


Loading

0 comments on commit 3e3740b

Please sign in to comment.