Unvendor tree-sitter-racket

pull/900/head
Antonin Delpeuch 2025-10-09 15:50:02 +07:00 committed by Wilfred Hughes
parent 7cc43998a7
commit 85bda2f11b
54 changed files with 18 additions and 27912 deletions

@ -2,7 +2,7 @@
### Parsing
Added support for protocol buffer files. Updated CMake, Solidity, HCL, Dart, Elm, Devicetree, Gleam, Newick and R parsers.
Added support for protocol buffer files. Updated CMake, Solidity, HCL, Dart, Elm, Devicetree, Gleam, Newick, Racket and R parsers.
### Display

11
Cargo.lock generated

@ -309,6 +309,7 @@ dependencies = [
"tree-sitter-proto",
"tree-sitter-python",
"tree-sitter-r",
"tree-sitter-racket",
"tree-sitter-ruby",
"tree-sitter-rust-orchard",
"tree-sitter-scala",
@ -1370,6 +1371,16 @@ dependencies = [
"tree-sitter-language",
]
[[package]]
name = "tree-sitter-racket"
version = "0.24.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8395b6a054e6264c67e1ef915f239c4f86575b7d7c69638bdbf3c336c58f128"
dependencies = [
"cc",
"tree-sitter-language",
]
[[package]]
name = "tree-sitter-ruby"
version = "0.23.1"

@ -109,6 +109,7 @@ tree-sitter-php = "0.23.11"
tree-sitter-proto = "0.2.0"
tree-sitter-python = "0.23.5"
tree-sitter-r = "1.2.0"
tree-sitter-racket = "0.24.7"
tree-sitter-ruby = "0.23.1"
tree-sitter-rust-orchard = "0.13.0"
tree-sitter-scala = "0.23.3"

@ -127,11 +127,6 @@ fn main() {
src_dir: "vendored_parsers/tree-sitter-qmljs-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-racket",
src_dir: "vendored_parsers/tree-sitter-racket-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-scheme",
src_dir: "vendored_parsers/tree-sitter-scheme-src",

@ -218,7 +218,7 @@ sample_files/r_1.R sample_files/r_2.R
f9ee6c271d614bfcd510633bddc9e49d -
sample_files/racket_1.rkt sample_files/racket_2.rkt
b017e169d9fc79336fd7ef82140fe8a7 -
ba1164af47f67d0865a4ba7706096bbc -
sample_files/repeated_line_no_eol_1.txt sample_files/repeated_line_no_eol_2.txt
b63c743f2133480de3ba42ecdec5eb93 -

@ -81,7 +81,6 @@ extern "C" {
fn tree_sitter_latex() -> ts::Language;
fn tree_sitter_perl() -> ts::Language;
fn tree_sitter_qmljs() -> ts::Language;
fn tree_sitter_racket() -> ts::Language;
fn tree_sitter_scheme() -> ts::Language;
fn tree_sitter_smali() -> ts::Language;
fn tree_sitter_scss() -> ts::Language;
@ -889,18 +888,16 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
}
}
Racket => {
let language = unsafe { tree_sitter_racket() };
let language_fn = tree_sitter_racket::LANGUAGE;
let language = tree_sitter::Language::new(language_fn);
TreeSitterConfig {
language: language.clone(),
atom_nodes: ["string", "byte_string", "regex", "here_string"]
.into_iter()
.collect(),
delimiter_tokens: vec![("{", "}"), ("(", ")"), ("[", "]")],
highlight_query: ts::Query::new(
&language,
include_str!("../../vendored_parsers/highlights/racket.scm"),
)
.unwrap(),
highlight_query: ts::Query::new(&language, tree_sitter_racket::HIGHLIGHTS_QUERY)
.unwrap(),
sub_languages: vec![],
}
}

@ -1 +0,0 @@
../tree-sitter-racket/queries/highlights.scm

@ -1 +0,0 @@
tree-sitter-racket/src

@ -1,7 +0,0 @@
BasedOnStyle: Google
IndentWidth: 4
PointerAlignment: Left
AllowShortIfStatementsOnASingleLine: Never
AllowShortFunctionsOnASingleLine: InlineOnly
BinPackArguments: false
BinPackParameters: false

@ -1,3 +0,0 @@
Checks: '*, -modernize-use-trailing-return-type, -llvmlibc-*'
HeaderFilterRegex: ''

@ -1,11 +0,0 @@
root = true
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
[{*.js,*.rkt,package.json}]
indent_style = space
indent_size = 2

@ -1,29 +0,0 @@
name: Test
on:
push:
branches:
- main
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm test
racket:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: git clone https://github.com/racket/racket --depth 1
- run: $(npm bin)/tree-sitter parse $(find racket -name '*.rkt' -type f -not -exec grep -q '#lang at-exp' {} \; -print) -qt

@ -1,9 +0,0 @@
node_modules/
build/
target/
package-lock.json
Cargo.lock
test.rkt
compile_commands.json
.cache/
.vscode/

@ -1,22 +0,0 @@
# Contributing
Thank you to contribute `tree-sitter-racket`.
## Workflow
It's recommended to use [nix](https://nixos.org/) package manager, and run
```shell
nix-shell
npm install # if you haven't install node modules
```
Then you can use `tree-sitter` command:
```shell
tree-sitter generate
tree-sitter test
```
If you don't use nix, you should follow the [official setup](https://tree-sitter.github.io/tree-sitter/creating-parsers) to configure the dev environment.

@ -1,26 +0,0 @@
[package]
name = "tree-sitter-racket"
description = "racket grammar for the tree-sitter parsing library"
version = "0.3.0"
keywords = ["incremental", "parsing", "racket"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-racket"
edition = "2018"
license = "MIT"
build = "bindings/rust/build.rs"
include = [
"bindings/rust/*",
"grammar.js",
"queries/*",
"src/*",
]
[lib]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = "~0.20"
[build-dependencies]
cc = "1.0"

@ -1,22 +0,0 @@
MIT License
Copyright (c) 2022 6cdh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -1,42 +0,0 @@
# tree-sitter-racket
[![Test](https://github.com/6cdh/tree-sitter-racket/actions/workflows/test.yml/badge.svg)](https://github.com/6cdh/tree-sitter-racket/actions/workflows/test.yml)
Racket grammar for tree-sitter.
This grammar only implements the Racket language with the default readtable.
## Status
It should be complete and compatible with Racket 8.9.
There are no plans to add support for new language currently.
## News
Starting from June 24, 2023, ([commit](https://github.com/6cdh/tree-sitter-racket/commit/989c3e631a7f2d87bb6a66a5394870aaeb6c56e7)) or release 0.3.0, the external scanner was written in C.
## Build and Try
You need
* nodejs
* a C compiler
then run
```shell
npm install
npx tree-sitter parse [filename]
```
## Usage
See [nodes.md](./nodes.md) for all visible nodes.
## Reference
Racket
- [The Racket Reader](https://docs.racket-lang.org/reference/reader.html)

@ -1,19 +0,0 @@
{
"targets": [
{
"target_name": "tree_sitter_racket_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
"src/scanner.c",
],
"cflags_c": [
"-std=c99",
],
}
]
}

@ -1,28 +0,0 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_racket();
namespace {
NAN_METHOD(New) {}
void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_racket());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("racket").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_racket_binding, Init)
} // namespace

@ -1,19 +0,0 @@
try {
module.exports = require("../../build/Release/tree_sitter_racket_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_racket_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}

@ -1,19 +0,0 @@
fn main() {
let src_dir = std::path::Path::new("src");
let mut c_config = cc::Build::new();
c_config.include(src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);
let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
c_config.compile("parser");
}

@ -1,52 +0,0 @@
//! This crate provides racket language support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_racket::language()).expect("Error loading racket grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/
use tree_sitter::Language;
extern "C" {
fn tree_sitter_racket() -> Language;
}
/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_racket() }
}
/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
// Uncomment these to include any queries that this grammar contains
pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.expect("Error loading racket language");
}
}

@ -1,17 +0,0 @@
===
box
===
#&17
#&"str"
#& ()
---
(program
(box
(number))
(box
(string))
(box
(list)))

@ -1,23 +0,0 @@
===
comment
===
;1
; comment
#| a |#
#| #| a |# 1 |#
#;1
#!/bin/sh
#! /bin/sh
---
(program
(comment)
(comment)
(block_comment)
(block_comment
(block_comment))
(sexp_comment
(number))
(comment)
(comment))

@ -1,20 +0,0 @@
===
racket/racket
===
|#%mzc:embed-me25| "10|" |abc|
|abc|
abc|abc| |abc|
#:|abc |
---
(program
(symbol)
(string)
(symbol)
(symbol)
(symbol)
(symbol)
(keyword))

@ -1,22 +0,0 @@
===
extension
===
#lang racket
#lang typed/racket
#lang racket1001001
#reader 123
#!r6rs
---
(program
(extension
(lang_name))
(extension
(lang_name))
(extension
(lang_name))
(extension
(number))
(extension
(lang_name)))

@ -1,25 +0,0 @@
===
graph
===
(#1=100 #1# #1#)
#0= (1 . #0#)
---
(program
(list
(graph
(decimal)
(number))
(graph
(decimal))
(graph
(decimal)))
(graph
(decimal)
(list
(number)
(dot)
(graph
(decimal)))))

@ -1,39 +0,0 @@
===
hash table
===
#hash()
#hasheq()
#hash(("a" . 5))
#hasheq(("a" . 5) ("b" . 7))
#hasheqv(("a" . 5))
---
(program
(hash
(list))
(hash
(list))
(hash
(list
(list
(string)
(dot)
(number))))
(hash
(list
(list
(string)
(dot)
(number))
(list
(string)
(dot)
(number))))
(hash
(list
(list
(string)
(dot)
(number)))))

@ -1,9 +0,0 @@
===
test1
===
#<<
---
(program
(here_string))

@ -1,10 +0,0 @@
===
test1
===
#<<
#<<
---
(program
(here_string))

@ -1,13 +0,0 @@
===
test1
===
;; NOTE: this file use CRLF
#<<
#<<
---
(program
(comment)
(here_string))

@ -1,12 +0,0 @@
===
test1
===
;; NOTE: this file use CRLF
#<<
---
(program
(comment)
(here_string))

@ -1,74 +0,0 @@
===
list
===
()
(1 2 3)
{1 2 3}
[1 2 3]
(1 (2) 3)
(1 . 3)
(1 . (3))
(1 . 2 . 3)
---
(program
(list)
(list
(number)
(number)
(number))
(list
(number)
(number)
(number))
(list
(number)
(number)
(number))
(list
(number)
(list
(number))
(number))
(list
(number)
(dot)
(number))
(list
(number)
(dot)
(list
(number)))
(list
(number)
(dot)
(number)
(dot)
(number)))
===
vector
===
#(1 "apple" 3)
#3("apple" "banana")
#3()
---
(program
(vector
(list
(number)
(string)
(number)))
(vector
(decimal)
(list
(string)
(string)))
(vector
(decimal)
(list)))

@ -1,38 +0,0 @@
===
quote
===
'123
`(1 ,2)
#'123
#`123
---
(program
(quote
(number))
(quasiquote
(list
(number)
(unquote
(number))))
(syntax (number))
(quasisyntax (number)))
===
unquote
===
,1
,@1
#,1
#,@1
---
(program
(unquote (number))
(unquote_splicing (number))
(unsyntax (number))
(unsyntax_splicing
(number)))

@ -1,118 +0,0 @@
===
boolean
===
#t
#f
#true
#false
---
(program
(boolean)
(boolean)
(boolean)
(boolean))
===
number
===
-1
1/2
1.0
1+2i
1/2+3/4i
1.0+3.0e7i
2e5
#i5
#e2e5
#x2e5
#b101
+inf.t
-nan.t
+i
0##+i
---
(program
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number)
(number))
===
character
===
#\newline
#\n
#\u3BB
#\λ
---
(program
(character)
(character)
(character)
(character))
===
symbol
===
Apple
Ap#ple
Ap ple
Ap| |ple
Ap\ ple
#%Apple
123app
app123app123
123app123
中文
a
fec
---
(program
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol)
(symbol))
===
keyword
===
#:apple
#:###abc##123123
#:123123
---
(program
(keyword)
(keyword)
(keyword))

@ -1,51 +0,0 @@
===
string
===
"Apple"
"\x41pple"
"\"Apple\""
"\\"
#"Apple"
#<<EOF
(define (fib n)
(if (< n 2) 1
(+ (fib (- n 1))
(fib (- n 2)))))
EOF
#<<one two three
one two three
---
(program
(string)
(string
(escape_sequence))
(string
(escape_sequence)
(escape_sequence))
(string
(escape_sequence))
(byte_string)
(here_string)
(here_string))
===
regex
===
#rx".*"
#px"[\\s]*"
#rx#".*"
#px#"[\\s]*"
---
(program
(regex)
(regex
(escape_sequence))
(regex)
(regex
(escape_sequence)))

@ -1,18 +0,0 @@
==========
structure
==========
#s(1 2)
#s{"abc" 2}
---
(program
(structure
(list
(number)
(number)))
(structure
(list
(string)
(number))))

@ -1,34 +0,0 @@
# fuzz test
The directory contains the scripts that test the implementation to avoid the problem that a symbol is parsed as a number or vice versa.
## Resource
* ~6 minutes to run
* ~300M generated files
## Run
```shell
$ cd fuzztest
# 30s
$ racket gen_cases.rkt
cpu time: 26531 real time: 26835 gc time: 1069
5114173 cases generated
$ cd ..
$ tree-sitter generate
# 3 minutes
$ tree-sitter parse fuzztest/case.txt > fuzztest/res1.txt
# 2 minutes
$ cd fuzztest && racket postprocess.rkt
# should show nothing
$ sdiff -s <(cat -n expect.txt) <(cat -n res.txt)
# If there is some error, run
$ sdiff -s <(cat -n expect.txt) <(cat -n res.txt) | less
# then get the first error case at `N`-th line
$ cat case.txt | sed -n 'Np'
```
You can edit `gen_cases.rkt` to generate less cases during development.

@ -1,57 +0,0 @@
#lang racket
(require racket/extflonum)
;; all characters that can appear in a valid number/exflonum
;; remove some insignificant parts to improve performance
;; (define alphabet-char "abdefilnost")
(define alphabet-char "abdefilnostx")
(define special-char "#./@+-")
;; (define numeric-char "0123456789")
(define numeric-char "0179")
(define all-char
(string-append alphabet-char
special-char
numeric-char))
(define cnt 0)
(define max-len 5)
(define case-port (open-output-file "case.txt" #:exists 'replace))
(define expect-port (open-output-file "expect.txt" #:exists 'replace))
(define (gen i case)
(with-handlers ([exn:fail? (lambda _ (void))])
(when (> i 0)
(define case-str (list->string case))
;; ".0@.0" should be a number according the document,
;; but it's actually a symbol.
;; It's a bug of Racket reader, and will fix in new Racket release.
;; we skip these cases.
(when (not (string-contains? case-str "@."))
(with-handlers ([exn:fail? void])
(with-input-from-string case-str
(lambda ()
(define fst (read))
(define snd (read))
(when (eof-object? snd)
(cond [(symbol? fst)
(set! cnt (add1 cnt))
(displayln case-str case-port)
(displayln "symbol" expect-port)]
[(number? fst)
(set! cnt (add1 cnt))
(displayln case-str case-port)
(displayln "number" expect-port)]
;; it's here for possible future change that
;; split extflonum from number
[(extflonum? fst)
(set! cnt (add1 cnt))
(displayln case-str case-port)
(displayln "number" expect-port)]))))))))
(when (< i max-len)
(for ([c all-char])
(gen (add1 i) (cons c case)))))
(time (gen 0 '()))
(displayln (format "~a cases generated" cnt))

@ -1,26 +0,0 @@
#lang racket
(define port (open-input-file "res1.txt"))
(define all-result (drop (read port) 4))
(define all-result-line
(for/list ([r all-result])
(cons (car r) (caadr r))))
(with-output-to-file "res.txt"
#:exists 'replace
(lambda ()
(let loop ([line 0]
[firstline? #t]
[lst all-result-line])
(match lst
['() (void)]
[(cons fst rem)
#:when (= (cdr fst) line)
(when (not firstline?)
(display " "))
(display (car fst))
(loop line (if firstline? #t #f) rem)]
[(cons fst rem)
(newline)
(display (car fst))
(loop (add1 line) #f rem)]))
(newline)))

@ -1,523 +0,0 @@
const PREC = {
first: $ => prec(100, $),
last: $ => prec(-1, $),
};
const LEAF = {
// https://en.wikipedia.org/wiki/Unicode_character_property#Whitespace
whitespace: /[ \t\n\v\f\r\u{0085}\u{00A0}\u{1680}\u{2000}-\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}]+/u,
newline: /[\r\n\u{85}\u{2028}\u{2029}]/,
delimiter: /[ \t\n\v\f\r\u{0085}\u{00A0}\u{1680}\u{2000}-\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{FEFF}(){}",'`;\[\]]/u,
non_delimiter: /[^ \t\n\v\f\r\u{0085}\u{00A0}\u{1680}\u{2000}-\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{FEFF}(){}",'`;\[\]]/u,
// first character of symbol
symbol_start:
choice(
/[^# \t\n\v\f\r\u{0085}\u{00A0}\u{1680}\u{2000}-\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{FEFF}(){}",'`;\[\]\|\\]/u,
"#%",
/\|[^|]*\|/,
/\\./),
symbol_remain:
choice(
/[^ \t\n\v\f\r\u{0085}\u{00A0}\u{1680}\u{2000}-\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{FEFF}(){}",'`;\[\]\|\\]/u,
/\|[^|]*\|/,
/\\./),
any_char: /.|[\r\n\u{85}\u{2028}\u{2029}]/,
};
function paren(tok) {
return choice(
seq("(", tok, ")"),
seq("[", tok, "]"),
seq("{", tok, "}"));
}
module.exports = grammar({
name: 'racket',
extras: _ => [],
externals: $ => [
$._here_string_body,
],
rules: {
program: $ => repeat($._token),
_token: $ =>
choice(
$._skip,
$.extension,
$._datum),
_skip: $ =>
choice(
LEAF.whitespace,
$.comment,
$.sexp_comment,
$.block_comment),
dot: _ => ".",
// comment {{{
comment: $ =>
choice(
token(
seq(/;.*/)),
$._line_comment),
block_comment: $ =>
seq("#|",
repeat(
choice(
PREC.first($.block_comment),
LEAF.any_char)),
PREC.first("|#")),
sexp_comment: $ =>
seq("#;",
repeat($._skip),
$._datum),
_line_comment: _ =>
token(
seq(
choice("#! ", "#!/"),
repeat(seq(/.*/, "\\", LEAF.newline)),
/.*/)),
// comment }}}
_datum: $ =>
choice(
$.boolean,
$.string,
$.here_string,
$.byte_string,
$.character,
// number/symbol precedence
// for same length token, prefer number
// otherwise, prefer symbol which is also longer
$.number,
$.symbol,
$.keyword,
$.regex,
$.box,
$.graph,
$.structure,
$.hash,
$.quote,
$.quasiquote,
$.syntax,
$.quasisyntax,
$.unquote,
$.unquote_splicing,
$.unsyntax,
$.unsyntax_splicing,
$.list,
$.vector),
boolean: _ => token(choice("#true", "#t", "#T", "#false", "#f", "#F")),
// string {{{
string: $ => $._real_string,
byte_string: $ =>
seq("#", $._real_string),
here_string: $ =>
seq("#<<", $._here_string_body),
regex: $ =>
seq(
token(choice("#rx", "#px", "#rx#", "#px#")),
$._real_string),
_real_string: $ =>
seq(
'"',
repeat(
choice(
$.escape_sequence,
/[^"\\]+/)),
'"'),
escape_sequence: _ =>
token(
choice(
"\\a", "\\b", "\\t", "\\n", "\\v", "\\f", "\\r", "\\e",
'\\"', "\\'", "\\\\",
seq("\\", /[0-7]{1,3}/),
seq("\\x", /[0-9a-fA-F]{1,2}/),
seq("\\u", /[0-9a-fA-F]{1,4}/),
seq("\\u", /[0-9a-fA-F]{4,4}/),
seq("\\U", /[0-9a-fA-F]{1,8}/),
seq("\\", /[\r\n]|(\r\n)/))),
// string }}}
number: _ =>
token(
choice(
extflonum(2),
extflonum(8),
extflonum(10),
extflonum(16),
number_base(2),
number_base(8),
number_base(10),
number_base(16))),
decimal: _ => /[0-9]+/,
character: _ =>
token(
seq(
"#\\",
choice(
"nul", "null", "backspace",
"tab", "newline", "linefeed",
"vtab", "page", "return",
"space", "rubout",
/[0-7]{3,3}/, /u[0-9a-fA-F]{1,4}/, /U[0-9a-fA-F]{1,8}/,
/./))),
symbol: _ =>
token(
choice(
/#[cC][iIsS]/, // read-case-sensitive parameter
seq(
LEAF.symbol_start,
repeat(LEAF.symbol_remain)))),
keyword: _ =>
token(
seq(
"#:",
repeat1(LEAF.symbol_remain))),
box: $ =>
seq(
"#&",
repeat($._skip),
$._datum),
list: $ =>
paren(
repeat(
choice(
$._token,
$.dot))),
vector: $ =>
seq(
choice("#", "#fl", "#fx"),
optional($.decimal),
$.list),
structure: $ =>
seq(
"#s",
$.list),
hash: $ =>
seq(
choice("#hash", "#hasheq", "#hasheqv"),
$.list),
graph: $ =>
seq(
"#",
$.decimal,
choice(
"#",
seq(
"=",
repeat($._skip),
$._datum))),
quote: $ =>
seq(
"'",
repeat($._skip),
$._datum),
quasiquote: $ =>
seq(
"`",
repeat($._skip),
$._datum),
syntax: $ =>
seq(
"#'",
repeat($._skip),
$._datum),
quasisyntax: $ =>
seq(
"#`",
repeat($._skip),
$._datum),
unquote: $ =>
seq(
",",
repeat($._skip),
$._datum),
unquote_splicing: $ =>
seq(
",@",
repeat($._skip),
$._datum),
unsyntax: $ =>
seq(
"#,",
repeat($._skip),
$._datum),
unsyntax_splicing: $ =>
seq(
"#,@",
repeat($._skip),
$._datum),
extension: $ =>
choice(
seq(
"#reader",
repeat($._skip),
$._datum),
seq(
choice("#lang ", "#!"),
$.lang_name)),
lang_name: _ => /[a-zA-Z0-9+_/-]+/,
}
})
// number {{{
function number_base(n) {
const digit = {
2: /[01]/,
8: /[0-7]/,
10: /[0-9]/,
16: /[0-9a-fA-F]/,
}[n];
const exp_mark = {
2: /[sldefSLDEF]/,
8: /[sldefSLDEF]/,
10: /[sldefSLDEF]/,
16: /[slSL]/,
}[n];
const prefix = {
2: /#[bB]/,
8: /#[oO]/,
10: optional(/#[dD]/),
16: /#[xX]/,
}[n];
const exactness =
/#[eiEI]/;
const sign = /[+-]/;
const digits_hash =
seq(
repeat1(digit),
repeat("#"));
const unsigned_integer =
repeat1(digit);
// exact
const exact_integer =
seq(
optional(sign),
unsigned_integer);
const unsigned_rational =
choice(
unsigned_integer,
seq(unsigned_integer, "/", unsigned_integer));
const exact_rational =
seq(
optional(sign),
unsigned_rational);
const exact_complex =
seq(
optional(exact_rational),
sign,
optional(unsigned_rational),
/[iI]/);
const exact =
choice(exact_rational, exact_complex);
// inexact
const inexact_special =
choice(
/[iI][nN][fF]\.[0fF]/,
/[nN][aA][nN]\.[0fF]/);
const inexact_simple =
choice(
seq(
digits_hash,
optional("."),
repeat("#")),
seq(
optional(unsigned_integer),
".",
digits_hash),
seq(
digits_hash,
"/",
digits_hash));
const inexact_normal =
seq(
inexact_simple,
optional(
seq(
exp_mark,
exact_integer)));
const inexact_unsigned =
choice(inexact_normal, inexact_special);
const inexact_real =
choice(
seq(
optional(sign),
inexact_normal),
seq(
sign,
inexact_special));
const inexact_complex =
choice(
seq(
optional(inexact_real),
sign,
optional(inexact_unsigned),
/[iI]/),
seq(
inexact_real,
"@",
inexact_real));
const inexact =
choice(inexact_real, inexact_complex);
const number =
choice(exact, inexact);
const general_number =
seq(
choice(
seq(
optional(exactness),
prefix),
seq(
prefix,
optional(exactness))),
number);
return general_number;
}
function extflonum(n) {
const digit = {
2: /[01]/,
8: /[0-7]/,
10: /[0-9]/,
16: /[0-9a-fA-F]/,
}[n];
const exp_mark = /[tT]/;
const prefix = {
2: /#[bB]/,
8: /#[oO]/,
10: optional(/#[dD]/),
16: /#[xX]/,
}[n];
const sign = /[+-]/;
const digits_hash =
seq(
repeat1(digit),
repeat("#"));
const unsigned_integer =
repeat1(digit);
// exact
const exact_integer =
seq(
optional(sign),
unsigned_integer);
// inexact
const inexact_special =
choice(
/[iI][nN][fF]\.[0fFtT]/,
/[nN][aA][nN]\.[0fFtT]/);
const inexact_simple =
choice(
seq(
digits_hash,
optional("."),
repeat("#")),
seq(
optional(unsigned_integer),
".",
digits_hash),
seq(
digits_hash,
"/",
digits_hash));
const inexact_normal =
seq(
inexact_simple,
optional(
seq(
exp_mark,
exact_integer)));
const inexact_real =
choice(
seq(
optional(sign),
inexact_normal),
seq(
sign,
inexact_special));
return seq(prefix, inexact_real);
}
// number }}}

@ -1,41 +0,0 @@
## Nodes
This page contains all visible nodes in yaml format.
```yml
- comment # comments
- block_comment
- sexp_comment
- extension # #lang or #reader
- lang_name # language name in extension
- dot # .
- boolean
- string
- escape_sequence # escape sequence in string
- here_string
- byte_string
- character
- number
- symbol
- decimal # [0-9]+ used for graph reference or vector length in literal
- keyword # for example, #:abc
- regex
- box
- graph
- structure
- hash # hash table
- quote # '
- quasiquote # `
- syntax # #'
- quasisyntax # #`
- unquote # ,
- unquote_splicing # ,@
- unsyntax # #,
- unsyntax_splicing # #,@
- list
- vector
```

@ -1,28 +0,0 @@
{
"name": "tree-sitter-racket",
"version": "0.3.0",
"description": "Tree-sitter grammar for Racket",
"main": "bindings/node",
"scripts": {
"build": "npx tree-sitter generate",
"test": "npx tree-sitter generate && npx tree-sitter test"
},
"author": "6cdh",
"license": "MIT",
"dependencies": {
"nan": "^2.15.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.20.6",
"typescript": "^4.8.4",
"typescript-language-server": "^2.1.0"
},
"tree-sitter": [
{
"scope": "source.racket",
"file-types": [
"rkt"
]
}
]
}

File diff suppressed because one or more lines are too long

@ -1,31 +0,0 @@
(symbol) @local.reference
(list
.
(symbol) @reference._let
.
(list
.
(list
.
(symbol) @local.definition)+)
(#match? @reference._let "^(let|let\\*|for|for\\*)$")) @local.scope
(list
.
(symbol) @reference._def
.
(list
.
(symbol)
.
(symbol)+ @local.definition)
(#match? @reference._def "^(define|define/contract|define-syntax-rule)$")) @local.scope
(list
.
(symbol) @reference._def
.
(symbol) @local.definition
(#match? @reference._def "^(lambda|λ)$")) @local.scope

@ -1,12 +0,0 @@
(list
.
(symbol) @reference._define
(#match? @reference._define "^(define|define/contract)$")
.
(list
.
(symbol) @name) @definition.function)
(list
.
(symbol) @reference.call)

@ -1,11 +0,0 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
nodejs
gcc
];
shellHook = ''
PATH=./node_modules/.bin:$PATH
'';
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,153 +0,0 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <tree_sitter/parser.h>
enum TokenType {
HERE_STRING_BODY,
};
// a hand written string implmentation
// data[0], data[1], ..., data[len-1] is the content of string
// data[len] is `\0` for typical string `char*` compatibility
// So 0 <= len < cap
typedef struct {
size_t len;
size_t cap;
char *data;
} String;
static void check_alloc(void *ptr) {
if (ptr == NULL) {
fprintf(stderr, "Scanner: Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
}
static String string_new(void) {
size_t init_len = 16;
// (init_len + 1) for null terminator
size_t cap = init_len + 1;
void *tmp = calloc(1, sizeof(char) * cap);
check_alloc(tmp);
return (String){.cap = cap, .len = 0, .data = tmp};
}
static void string_resize(String *str, size_t new_cap) {
void *block = realloc(str->data, new_cap * sizeof(char));
check_alloc(block);
str->data = block;
str->cap = new_cap;
memset(str->data + str->len, 0, (new_cap - str->len) * sizeof(char));
}
static void string_push(String *str, int32_t elem) {
if (str->len + sizeof(elem) >= str->cap) {
// str->cap * 2 + 1 > str->len + sizeof(elem) always holds
// as str->cap > 16
string_resize(str, str->cap * 2 + 1);
}
// NOTE: we don't consider little-endian/big-endian here
// the character in string is only for compare.
// They only need to be store in consistent way
memcpy(str->data + str->len, &elem, sizeof(elem));
str->len += sizeof(elem);
}
static void string_free(String *str) {
if (str->data != NULL) {
free(str->data);
str->data = NULL;
str->len = 0;
str->cap = 0;
}
}
static void string_clear(String *str) {
memset(str->data, 0, str->len * sizeof(char));
str->len = 0;
}
static void advance(TSLexer *lexer) {
lexer->advance(lexer, false);
}
static void skip(TSLexer *lexer) {
lexer->advance(lexer, true);
}
// NOTE: only "\n" is allowed as newline here,
// It implies that "\r" can also be terminator.
static bool isnewline(int32_t chr) {
return chr == '\n';
}
// `read_line` read strings until a newline or EOF
static void read_line(String *str, TSLexer *lexer) {
while (!isnewline(lexer->lookahead) && !lexer->eof(lexer)) {
string_push(str, lexer->lookahead);
advance(lexer);
}
}
// Suppose terminator is `T`, newline (\n) is `$`,
// It should accept "#<<T$T" or "#<<T$...$T", where `...`
// is the string content.
static bool scan(TSLexer *lexer, const bool *valid_symbols) {
if (!valid_symbols[HERE_STRING_BODY]) {
return false;
}
String terminator = string_new();
read_line(&terminator, lexer);
if (lexer->eof(lexer)) {
string_free(&terminator);
return false;
}
// skip `\n`
skip(lexer);
String current_line = string_new();
while (true) {
read_line(&current_line, lexer);
if (strcmp(terminator.data, current_line.data) == 0) {
lexer->result_symbol = HERE_STRING_BODY;
string_free(&terminator);
string_free(&current_line);
return true;
}
if (lexer->eof(lexer)) {
string_free(&terminator);
string_free(&current_line);
return false;
}
string_clear(&current_line);
// skip `\n`
skip(lexer);
}
}
void *tree_sitter_racket_external_scanner_create() {
return NULL;
}
unsigned tree_sitter_racket_external_scanner_serialize(void *payload,
char *buffer) {
return 0;
}
void tree_sitter_racket_external_scanner_deserialize(void *payload,
const char *buffer,
unsigned length) {
}
bool tree_sitter_racket_external_scanner_scan(void *payload,
TSLexer *lexer,
const bool *valid_symbols) {
return scan(lexer, valid_symbols);
}
void tree_sitter_racket_external_scanner_destroy(void *payload) {
}

@ -1,224 +0,0 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
};
/*
* Lexer Macros
*/
#define START_LEXER() \
bool result = false; \
bool skip = false; \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value, \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_