tree-sitter-hack
Hack grammar for tree-sitter. Very much 🚧 work in progress 🚧.
$ git clone https://github.com/antoniodejesusochoasolano/tree-sitter-hack
$ cd tree-sitter-hack
$ npm install
$ echo 'function main(): void { print "wyd, world\\n"; }' > script.hack
$ npx tree-sitter generate
$ npx tree-sitter parse script.hack
(script [0, 0] - [3, 0]
(function_declaration [0, 0] - [2, 1]
name: (identifier [0, 9] - [0, 13])
(parameters [0, 13] - [0, 15])
return_type: (primitive_type [0, 17] - [0, 21])
body: (compound_statement [0, 22] - [2, 1]
(expression_statement [1, 2] - [1, 23]
(print_expression [1, 2] - [1, 22]
(string [1, 8] - [1, 22]))))))
Note
npm doesn't allow packages with the word "hack" in their registry which is why the repo name does not match the package name.
Unfortunately, the word "hack" triggers our spam detection and can't be used in package names. We recommend choosing other keywords that highlight your package's functionality.
Questions
Inline vs hidden
$._rule
vs inline: [$._rule]
vs inline: [$.rule]
- What's the difference between hidden rules and inline rules?
- If a rule is inline, does it really have to be hidden?
Inline vs helper function
- What's the difference between an inline rule and using a helper function?
- Is there performance benefits from naming rules even though they're removed from the grammar?
- What does it mean for the rule to be removed from the grammar?
For example, I see $._variablish
is present in src/grammar.json
. What does this mean?
Alias vs inline vs hidden
alias($._rule, $.alias)
- Is$.alias
hidden?alias($.rule, $._alias)
- I'm assuming$._alias
is hidden?inline: [$._rule]
- Is alias inline?
Scripts
bin/generate-parser
Wrapper around tree-sitter generate
that skips parser generation if grammar.js
hasn't changed since last run.
bin/generate-corpus
Unlike most other Tree-sitter projects, we breakout test cases into separate files (see test/cases
). This is done so editors have an easier time syntax highlighting test cases. But also I find individual files easier to navigate than the corpus.txt
files used by Tree-sitter.
We use bin/generate-corpus
to generate the test/corpus/case1.txt
from individual test/cases
files so we can still use tree-sitter test
.
bin/test-corpus
Run bin/generate-corpus
and bin/generate-parser
before running tree-sitter test
.
References
There's no published official Hacklang language spec so we have to make do.
- HHVM Blog - Good source of language deprecations and changes.
- Hack Documentation - Source at hhvm/user-documentation.
- Hack Parser Source
- tree-sitter-javascript and tree-sitter-php - Used to guide writing
grammar.js
- Deprecated Hack Language Specification
- PHP Language Specification - Does not apply 100% to Hacklang, but still a good reference.