mirror of https://github.com/Wilfred/difftastic/
Add 'vendored_parsers/tree-sitter-scheme/' from commit '85b6188fb77c03dfb01d13e58e2844450506860c'
git-subtree-dir: vendored_parsers/tree-sitter-scheme git-subtree-mainline:pull/640/head3476a68efbgit-subtree-split:85b6188fb7
commit
1dc45d84c6
@ -0,0 +1,11 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[{*.js,*.scm,*.ss,package.json}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
name: Build/test
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: 16
|
||||
- run: npm install
|
||||
- run: npm test
|
||||
|
||||
chez:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 14
|
||||
- run: npm install
|
||||
- run: git clone --depth 1 https://github.com/cisco/ChezScheme.git
|
||||
- run: npx tree-sitter parse $(find ./ChezScheme -type f -name "*.scm" -o -name "*.ss" -not -exec grep -q -e '<<' -e '(#' -e '#{' -e '#!eof' -e '(\\' {} \; -print) -qt
|
||||
|
||||
sicp:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 14
|
||||
- run: npm install
|
||||
- name: Download sicp code
|
||||
run: |
|
||||
mkdir -p sicp && cd sicp
|
||||
curl https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/code/allcode.tar.gz -O
|
||||
tar xvf allcode.tar.gz
|
||||
- run: npx tree-sitter parse sicp/*.scm -qt
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
node_modules/
|
||||
build/
|
||||
target/
|
||||
package-lock.json
|
||||
Cargo.lock
|
||||
test.scm
|
||||
unicode.js
|
||||
@ -0,0 +1,28 @@
|
||||
# Contributing
|
||||
|
||||
Thank you to contribute `tree-sitter-scheme`.
|
||||
|
||||
## 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 dont't use nix, you should follow the [official setup](https://tree-sitter.github.io/tree-sitter/creating-parsers) to configure the dev environment.
|
||||
|
||||
## Todo
|
||||
|
||||
* check the tracking issues
|
||||
* review the code to ensure it meets the standard
|
||||
* improve queries
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "tree-sitter-scheme"
|
||||
description = "scheme grammar for the tree-sitter parsing library"
|
||||
version = "0.6.0"
|
||||
keywords = ["incremental", "parsing", "scheme"]
|
||||
categories = ["parsing", "text-editors"]
|
||||
repository = "https://github.com/tree-sitter/tree-sitter-scheme"
|
||||
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"
|
||||
@ -0,0 +1,22 @@
|
||||
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.
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
# tree-sitter-scheme
|
||||
|
||||
[](https://github.com/6cdh/tree-sitter-scheme/actions/workflows/test.yml)
|
||||
|
||||
Scheme grammar for tree-sitter.
|
||||
|
||||
## Recent News
|
||||
|
||||
* With R7RS support, a single `|` can no longer appears in the middle of an identifier.
|
||||
* The nodes `directive` no longer contain sub nodes `symbol`. Now it's a single node.
|
||||
* some extensions added, see below
|
||||
|
||||
## Status
|
||||
|
||||
~~tree-sitter-scheme should work on a superset of Scheme.~~
|
||||
|
||||
Different Implementations might have conflict grammar. I am not sure if I should support
|
||||
them. If you need some implementation-specific features, please open an issue, then I will consider support it.
|
||||
|
||||
current status:
|
||||
|
||||
* [x] R5RS
|
||||
* [x] R6RS
|
||||
* [x] R7RS
|
||||
* [ ] Extensions
|
||||
* [x] `{}` as replacement for `()` or `[]`
|
||||
* [x] extend symbols which can start with digits, period, ... etc as long as it's not a number
|
||||
* [x] keyword `#:symbol`
|
||||
* [x] escape sequence in string `\` + any character
|
||||
* [x] characters `#\bel`, `#\ls`, `#\nel`, `#\rubout`, `#\vt`
|
||||
|
||||
Please open an issue to let me know it's really used if you want to add new extensions.
|
||||
|
||||
## Implementation
|
||||
|
||||
* [ ] Support for implementation
|
||||
* [ ] Chez Scheme ([#1](https://github.com/6cdh/tree-sitter-scheme/issues/1))
|
||||
* [ ] Chicken Scheme ([#3](https://github.com/6cdh/tree-sitter-scheme/issues/3))
|
||||
* [ ] Guile Scheme ([#7](https://github.com/6cdh/tree-sitter-scheme/issues/7))
|
||||
|
||||
## Usage
|
||||
|
||||
See [nodes.md](./nodes.md) for all visible nodes.
|
||||
|
||||
This parser don't parse language constructs. Instead, it parse code as lists.
|
||||
|
||||
If you want language constructs support, use custom queries (see [#5](https://github.com/6cdh/tree-sitter-scheme/issues/5)), also see [thchha/tree-sitter-scheme](https://gitlab.com/thchha/tree-sitter-scheme).
|
||||
|
||||
## Query
|
||||
|
||||
The queries here are too simple and not intended to be useful in an editor.
|
||||
Please open an issue if you have suggestions.
|
||||
|
||||
## Reference
|
||||
|
||||
Scheme
|
||||
|
||||
* [R5RS](https://schemers.org/Documents/Standards/R5RS/)
|
||||
* [R6RS](http://www.r6rs.org/)
|
||||
* [R7RS](https://small.r7rs.org/)
|
||||
* [The Scheme Programming Language](https://www.scheme.com/tspl4/)
|
||||
|
||||
Tree-sitter
|
||||
|
||||
* [official documents](https://tree-sitter.github.io/tree-sitter)
|
||||
* [Guide to your first Tree-sitter grammar](https://gist.github.com/Aerijo/df27228d70c633e088b0591b8857eeef)
|
||||
* [tree-sitter-clojure](https://github.com/sogaiu/tree-sitter-clojure)
|
||||
* [tree-sitter-commonlisp](https://github.com/theHamsta/tree-sitter-commonlisp)
|
||||
* [tree-sitter-fennel](https://github.com/TravonteD/tree-sitter-fennel)
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "tree_sitter_scheme_binding",
|
||||
"include_dirs": [
|
||||
"<!(node -e \"require('nan')\")",
|
||||
"src"
|
||||
],
|
||||
"sources": [
|
||||
"bindings/node/binding.cc",
|
||||
"src/parser.c",
|
||||
# If your language uses an external scanner, add it here.
|
||||
],
|
||||
"cflags_c": [
|
||||
"-std=c99",
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
#include "tree_sitter/parser.h"
|
||||
#include <node.h>
|
||||
#include "nan.h"
|
||||
|
||||
using namespace v8;
|
||||
|
||||
extern "C" TSLanguage * tree_sitter_scheme();
|
||||
|
||||
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_scheme());
|
||||
|
||||
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("scheme").ToLocalChecked());
|
||||
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
|
||||
}
|
||||
|
||||
NODE_MODULE(tree_sitter_scheme_binding, Init)
|
||||
|
||||
} // namespace
|
||||
@ -0,0 +1,19 @@
|
||||
try {
|
||||
module.exports = require("../../build/Release/tree_sitter_scheme_binding");
|
||||
} catch (error1) {
|
||||
if (error1.code !== 'MODULE_NOT_FOUND') {
|
||||
throw error1;
|
||||
}
|
||||
try {
|
||||
module.exports = require("../../build/Debug/tree_sitter_scheme_binding");
|
||||
} catch (error2) {
|
||||
if (error2.code !== 'MODULE_NOT_FOUND') {
|
||||
throw error2;
|
||||
}
|
||||
throw error1
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
module.exports.nodeTypeInfo = require("../../src/node-types.json");
|
||||
} catch (_) {}
|
||||
@ -0,0 +1,40 @@
|
||||
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);
|
||||
|
||||
// If your language uses an external scanner written in C,
|
||||
// then include this block of code:
|
||||
|
||||
/*
|
||||
let scanner_path = src_dir.join("scanner.c");
|
||||
c_config.file(&scanner_path);
|
||||
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
|
||||
*/
|
||||
|
||||
c_config.compile("parser");
|
||||
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
|
||||
|
||||
// If your language uses an external scanner written in C++,
|
||||
// then include this block of code:
|
||||
|
||||
/*
|
||||
let mut cpp_config = cc::Build::new();
|
||||
cpp_config.cpp(true);
|
||||
cpp_config.include(&src_dir);
|
||||
cpp_config
|
||||
.flag_if_supported("-Wno-unused-parameter")
|
||||
.flag_if_supported("-Wno-unused-but-set-variable");
|
||||
let scanner_path = src_dir.join("scanner.cc");
|
||||
cpp_config.file(&scanner_path);
|
||||
cpp_config.compile("scanner");
|
||||
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
|
||||
*/
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
//! This crate provides scheme 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_scheme::language()).expect("Error loading scheme 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_scheme() -> 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_scheme() }
|
||||
}
|
||||
|
||||
/// 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: &str = include_str!("../../src/node-types.json");
|
||||
|
||||
// Uncomment these to include any queries that this grammar contains
|
||||
|
||||
pub const HIGHLIGHTS_QUERY: &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 scheme language");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,159 @@
|
||||
===
|
||||
symbol
|
||||
===
|
||||
+
|
||||
-
|
||||
*
|
||||
/
|
||||
!
|
||||
even?
|
||||
odd?
|
||||
set!
|
||||
hello
|
||||
ar!$->2
|
||||
->
|
||||
variable
|
||||
123app123
|
||||
变量
|
||||
|
||||
---
|
||||
(program
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol))
|
||||
|
||||
===
|
||||
list
|
||||
===
|
||||
(quote)
|
||||
(sym)
|
||||
(define x 1)
|
||||
(if (= 1 1) 1 0)
|
||||
#(1 2)
|
||||
#vu8(0 1)
|
||||
|
||||
---
|
||||
(program
|
||||
(list (symbol))
|
||||
(list (symbol))
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(number))
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(number)
|
||||
(number))
|
||||
(number)
|
||||
(number))
|
||||
(vector
|
||||
(number)
|
||||
(number))
|
||||
(byte_vector
|
||||
(number)
|
||||
(number)))
|
||||
|
||||
===
|
||||
procedure
|
||||
===
|
||||
(lambda (x) (+ x 1))
|
||||
|
||||
(define (fib n)
|
||||
(if (< n 2) 1
|
||||
(+ (fib (- n 1))
|
||||
(fib (- n 2)))))
|
||||
|
||||
---
|
||||
(program
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol))
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(number)))
|
||||
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(symbol))
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(number))
|
||||
(number)
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(number)))
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(number)))))))
|
||||
|
||||
===
|
||||
macro
|
||||
===
|
||||
(define ~>
|
||||
(symtax-rules ()
|
||||
[(_ val)
|
||||
val]
|
||||
[(_ val (f args ...) body ...)
|
||||
(~> (f val args ...)
|
||||
body ...)]))
|
||||
|
||||
---
|
||||
(program
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(list)
|
||||
(list
|
||||
(list
|
||||
(symbol)
|
||||
(symbol))
|
||||
(symbol))
|
||||
(list
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol))
|
||||
(symbol)
|
||||
(symbol))
|
||||
(list
|
||||
(symbol)
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol))
|
||||
(symbol)
|
||||
(symbol))))))
|
||||
@ -0,0 +1,54 @@
|
||||
===
|
||||
list
|
||||
===
|
||||
|
||||
{define x 1}
|
||||
|
||||
---
|
||||
(program
|
||||
(list
|
||||
(symbol)
|
||||
(symbol)
|
||||
(number)))
|
||||
|
||||
===
|
||||
keyword
|
||||
===
|
||||
|
||||
#:abc
|
||||
#:1abc
|
||||
|
||||
---
|
||||
(program
|
||||
(keyword)
|
||||
(keyword))
|
||||
|
||||
===
|
||||
character
|
||||
===
|
||||
|
||||
#\bel
|
||||
#\ls
|
||||
#\nel
|
||||
#\rubout
|
||||
#\vt
|
||||
|
||||
---
|
||||
(program
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character))
|
||||
|
||||
===
|
||||
string
|
||||
===
|
||||
|
||||
"\. \'"
|
||||
|
||||
---
|
||||
(program
|
||||
(string
|
||||
(escape_sequence)
|
||||
(escape_sequence)))
|
||||
@ -0,0 +1,135 @@
|
||||
===
|
||||
boolean
|
||||
===
|
||||
|
||||
#t
|
||||
#f
|
||||
#T
|
||||
#F
|
||||
|
||||
---
|
||||
(program
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean))
|
||||
|
||||
===
|
||||
character
|
||||
===
|
||||
|
||||
#\SpAcE
|
||||
#\NewLiNe
|
||||
#\1
|
||||
#\x
|
||||
|
||||
---
|
||||
(program
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character))
|
||||
|
||||
===
|
||||
string
|
||||
===
|
||||
";;; abcd"
|
||||
"str\\\\"
|
||||
"str\\"
|
||||
"str\""
|
||||
" ; abc ; "
|
||||
" "
|
||||
"
|
||||
abc"
|
||||
|
||||
---
|
||||
(program
|
||||
(string)
|
||||
(string
|
||||
(escape_sequence)
|
||||
(escape_sequence))
|
||||
(string
|
||||
(escape_sequence))
|
||||
(string
|
||||
(escape_sequence))
|
||||
(string)
|
||||
(string)
|
||||
(string))
|
||||
|
||||
===
|
||||
comment
|
||||
===
|
||||
|
||||
; abc
|
||||
|
||||
---
|
||||
(program
|
||||
(comment))
|
||||
|
||||
===
|
||||
quote
|
||||
===
|
||||
|
||||
'abc
|
||||
`abc
|
||||
#'abc
|
||||
#`abc
|
||||
|
||||
---
|
||||
(program
|
||||
(quote (symbol))
|
||||
(quasiquote (symbol))
|
||||
(syntax (symbol))
|
||||
(quasisyntax (symbol)))
|
||||
|
||||
===
|
||||
unquote
|
||||
===
|
||||
|
||||
,abc
|
||||
,@abc
|
||||
#,abc
|
||||
#,@abc
|
||||
|
||||
---
|
||||
(program
|
||||
(unquote (symbol))
|
||||
(unquote_splicing (symbol))
|
||||
(unsyntax (symbol))
|
||||
(unsyntax_splicing (symbol)))
|
||||
|
||||
===
|
||||
number
|
||||
===
|
||||
|
||||
1
|
||||
1.0
|
||||
+1
|
||||
-1
|
||||
#d1
|
||||
#b010101011
|
||||
#B010101011
|
||||
#o7
|
||||
-i
|
||||
-1i
|
||||
1-2i
|
||||
1e12312
|
||||
#b#e-1######/1########-1#######/1########i
|
||||
#e-1010#######/1#########
|
||||
|
||||
---
|
||||
(program
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number))
|
||||
@ -0,0 +1,176 @@
|
||||
===
|
||||
boolean
|
||||
===
|
||||
|
||||
#t
|
||||
#f
|
||||
#T
|
||||
#F
|
||||
|
||||
---
|
||||
(program
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean))
|
||||
|
||||
===
|
||||
character
|
||||
===
|
||||
|
||||
#\1
|
||||
#\x
|
||||
#\nul #\alarm #\backspace
|
||||
#\tab #\linefeed #\newline
|
||||
#\vtab #\page #\return #\esc
|
||||
#\space #\delete
|
||||
#\x0
|
||||
#\xF
|
||||
#\xFFAA00
|
||||
|
||||
---
|
||||
(program
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character))
|
||||
|
||||
===
|
||||
string
|
||||
===
|
||||
";;; abcd"
|
||||
"str\\\\"
|
||||
"str\\"
|
||||
"str\""
|
||||
" ; abc ; "
|
||||
" "
|
||||
"
|
||||
abc"
|
||||
"\a\b\t\n\v\f\r\"\\\\
|
||||
\\"
|
||||
|
||||
---
|
||||
(program
|
||||
(string)
|
||||
(string
|
||||
(escape_sequence)
|
||||
(escape_sequence))
|
||||
(string
|
||||
(escape_sequence))
|
||||
(string
|
||||
(escape_sequence))
|
||||
(string)
|
||||
(string)
|
||||
(string)
|
||||
(string
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)))
|
||||
|
||||
===
|
||||
directive
|
||||
===
|
||||
|
||||
#!r6rs
|
||||
|
||||
---
|
||||
(program
|
||||
(directive))
|
||||
|
||||
===
|
||||
comment
|
||||
===
|
||||
; abc
|
||||
#; (list)
|
||||
#| comment |#
|
||||
#|
|
||||
|comment
|
||||
|#
|
||||
#| a #| b |# |#
|
||||
#|#|#||##|x|#|##||#|#
|
||||
|
||||
---
|
||||
(program
|
||||
(comment)
|
||||
(comment (list (symbol)))
|
||||
(block_comment)
|
||||
(block_comment)
|
||||
(block_comment
|
||||
(block_comment))
|
||||
(block_comment
|
||||
(block_comment
|
||||
(block_comment)
|
||||
(block_comment))
|
||||
(block_comment)))
|
||||
|
||||
===
|
||||
quote
|
||||
===
|
||||
|
||||
'abc
|
||||
`abc
|
||||
#'abc
|
||||
#`abc
|
||||
|
||||
---
|
||||
(program
|
||||
(quote (symbol))
|
||||
(quasiquote (symbol))
|
||||
(syntax (symbol))
|
||||
(quasisyntax (symbol)))
|
||||
|
||||
===
|
||||
unquote
|
||||
===
|
||||
|
||||
,abc
|
||||
,@abc
|
||||
#,abc
|
||||
#,@abc
|
||||
|
||||
---
|
||||
(program
|
||||
(unquote (symbol))
|
||||
(unquote_splicing (symbol))
|
||||
(unsyntax (symbol))
|
||||
(unsyntax_splicing (symbol)))
|
||||
|
||||
===
|
||||
number
|
||||
===
|
||||
|
||||
#X01AF
|
||||
100000|10
|
||||
+nan.0
|
||||
-inf.0
|
||||
#E#D+10000.1098|100-1000i
|
||||
|
||||
---
|
||||
(program
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number))
|
||||
|
||||
@ -0,0 +1,190 @@
|
||||
===
|
||||
boolean
|
||||
===
|
||||
|
||||
#t #T
|
||||
#f #F
|
||||
#TruE
|
||||
#FaLse
|
||||
|
||||
---
|
||||
(program
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean)
|
||||
(boolean))
|
||||
|
||||
===
|
||||
character
|
||||
===
|
||||
|
||||
#\.
|
||||
#\1
|
||||
#\alarm #\backspace #\delete #\escape
|
||||
#\newline #\null #\return #\space #\tab
|
||||
#\X09abcDEF
|
||||
|
||||
---
|
||||
(program
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character)
|
||||
(character))
|
||||
|
||||
===
|
||||
string
|
||||
===
|
||||
|
||||
" "
|
||||
"\
|
||||
123"
|
||||
"\X09abcDEF;"
|
||||
"\a\t \b \n \r"
|
||||
|
||||
---
|
||||
(program
|
||||
(string)
|
||||
(string
|
||||
(escape_sequence))
|
||||
(string
|
||||
(escape_sequence))
|
||||
(string
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)
|
||||
(escape_sequence)))
|
||||
|
||||
===
|
||||
directive
|
||||
===
|
||||
|
||||
#!fold-case
|
||||
#!no-fold-case
|
||||
|
||||
---
|
||||
(program
|
||||
(directive)
|
||||
(directive))
|
||||
|
||||
===
|
||||
comment
|
||||
===
|
||||
|
||||
; cmt
|
||||
#| #|
|
||||
|# |#
|
||||
|
||||
#; #;1 2
|
||||
|
||||
---
|
||||
(program
|
||||
(comment)
|
||||
(block_comment
|
||||
(block_comment))
|
||||
(comment
|
||||
(comment
|
||||
(number))
|
||||
(number)))
|
||||
|
||||
===
|
||||
quote
|
||||
===
|
||||
|
||||
'sym
|
||||
`(1 ,2 ,@3 . 4)
|
||||
|
||||
---
|
||||
(program
|
||||
(quote (symbol))
|
||||
(quasiquote
|
||||
(list
|
||||
(number)
|
||||
(unquote
|
||||
(number))
|
||||
(unquote_splicing
|
||||
(number))
|
||||
(symbol)
|
||||
(number))))
|
||||
|
||||
===
|
||||
number
|
||||
===
|
||||
|
||||
1
|
||||
0.0
|
||||
#I#d+inf.0
|
||||
#i#D10/99+99/1i
|
||||
#i#D10/99-0123.0E+1i
|
||||
|
||||
---
|
||||
(program
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number)
|
||||
(number))
|
||||
|
||||
===
|
||||
symbol
|
||||
===
|
||||
|
||||
a
|
||||
Z
|
||||
! $ % & * / : < = > ? ^ _ ~
|
||||
+ -
|
||||
++
|
||||
--
|
||||
+@
|
||||
+!
|
||||
+!.
|
||||
+..!$
|
||||
1+
|
||||
123abc123
|
||||
| abc |
|
||||
|
|
||||
#;abc
|
||||
|
|
||||
|\| \a\b\t\n\r\X0AF;|
|
||||
|
||||
---
|
||||
(program
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol)
|
||||
(symbol))
|
||||
@ -0,0 +1,532 @@
|
||||
// r5rs: case insensitive
|
||||
// comment: only line comment
|
||||
// r6rs: case sensitive except boolean, number, unicode hex literals
|
||||
// comment: all
|
||||
// r7rs: case insensitive except letters, character names, mnemonic escapes.
|
||||
// comment: all
|
||||
|
||||
const PREC = {
|
||||
first: $ => prec(100, $),
|
||||
last: $ => prec(-100, $),
|
||||
};
|
||||
|
||||
const common = {
|
||||
whitespace: /[ \r\n\t\f\v\p{Zs}\p{Zl}\p{Zp}]/,
|
||||
intra_whitespace: /[\t\p{Zs}]/,
|
||||
line_ending: /[\n\r\u{2028}\u{0085}]|(\r\n)|(\r\u{0085})/,
|
||||
any_char: /.|[\r\n\u{85}\u{2028}\u{2029}]/,
|
||||
|
||||
symbol_element: /[^ \r\n\t\f\v\p{Zs}\p{Zl}\p{Zp}#;"'`,(){}\[\]\\|]/,
|
||||
};
|
||||
|
||||
const r5rs = {
|
||||
boolean: seq("#", /[tTfF]/),
|
||||
number:
|
||||
choice(
|
||||
r5rs_number_base(2),
|
||||
r5rs_number_base(8),
|
||||
r5rs_number_base(10),
|
||||
r5rs_number_base(16)),
|
||||
character:
|
||||
seq(
|
||||
"#\\",
|
||||
choice(
|
||||
/[sS][pP][aA][cC][eE]/,
|
||||
/[nN][eE][wW][lL][iI][nN][eE]/,
|
||||
common.any_char)),
|
||||
escape_sequence:
|
||||
choice(
|
||||
"\\\"",
|
||||
"\\\\"),
|
||||
};
|
||||
|
||||
const r6rs = {
|
||||
boolean: seq("#", /[tTfF]/),
|
||||
number:
|
||||
choice(
|
||||
r6rs_number_base(2),
|
||||
r6rs_number_base(8),
|
||||
r6rs_number_base(10),
|
||||
r6rs_number_base(16)),
|
||||
character:
|
||||
seq(
|
||||
"#\\",
|
||||
choice(
|
||||
"nul", "alarm", "backspace", "tab",
|
||||
"linefeed", "newline", "vtab", "page",
|
||||
"return", "esc", "space", "delete",
|
||||
/x[0-9a-fA-F]+/,
|
||||
common.any_char)),
|
||||
escape_sequence:
|
||||
choice(
|
||||
/\\[abtnvfr"\\]/,
|
||||
/\\x[0-9a-fA-F]+;/,
|
||||
seq(
|
||||
"\\",
|
||||
common.intra_whitespace,
|
||||
common.line_ending,
|
||||
common.intra_whitespace)),
|
||||
};
|
||||
|
||||
const r7rs = {
|
||||
boolean:
|
||||
seq(
|
||||
"#",
|
||||
choice(
|
||||
/[tTfF]/,
|
||||
/[tT][rR][uU][eE]/,
|
||||
/[fF][aA][lL][sS][eE]/)),
|
||||
number:
|
||||
choice(
|
||||
r7rs_number_base(2),
|
||||
r7rs_number_base(8),
|
||||
r7rs_number_base(10),
|
||||
r7rs_number_base(16)),
|
||||
character:
|
||||
seq(
|
||||
"#\\",
|
||||
choice(
|
||||
"alarm", "backspace", "delete",
|
||||
"escape", "newline", "null",
|
||||
"return", "space", "tab",
|
||||
/[xX][0-9a-fA-F]+/,
|
||||
common.any_char)),
|
||||
escape_sequence:
|
||||
seq(
|
||||
"\\",
|
||||
choice(
|
||||
/[abtnr"\\]/,
|
||||
seq(
|
||||
repeat(common.intra_whitespace),
|
||||
common.line_ending,
|
||||
repeat(common.intra_whitespace)),
|
||||
/[xX][0-9a-fA-F]+;/)),
|
||||
symbol:
|
||||
seq(
|
||||
"|",
|
||||
repeat(
|
||||
choice(
|
||||
/[^|\\]+/,
|
||||
/\\[xX][0-9a-fA-F]+;/,
|
||||
/\\[abtnr]/,
|
||||
"\\|")),
|
||||
"|")
|
||||
};
|
||||
|
||||
const extension = {
|
||||
escape_sequence: /\\./,
|
||||
character:
|
||||
seq(
|
||||
"#\\",
|
||||
choice("bel", "ls", "nel", "rubout", "vt")),
|
||||
};
|
||||
|
||||
const hidden_node = {
|
||||
symbol:
|
||||
token(
|
||||
choice(
|
||||
repeat1(common.symbol_element),
|
||||
r7rs.symbol)),
|
||||
};
|
||||
|
||||
module.exports = grammar({
|
||||
name: "scheme",
|
||||
|
||||
extras: _ => [],
|
||||
|
||||
rules: {
|
||||
program: $ => repeat($._token),
|
||||
|
||||
_token: $ =>
|
||||
choice(
|
||||
$._intertoken,
|
||||
$._datum),
|
||||
|
||||
_intertoken: $ =>
|
||||
choice(
|
||||
// NOTE: `repeat1` here can significantly reduce code size than `repeat`
|
||||
token(repeat1(common.whitespace)),
|
||||
$.directive,
|
||||
$.comment,
|
||||
$.block_comment),
|
||||
|
||||
comment: $ =>
|
||||
choice(
|
||||
/;.*/,
|
||||
seq("#;", repeat($._intertoken), $._datum)),
|
||||
|
||||
directive: $ =>
|
||||
seq("#!", repeat($._intertoken), hidden_node.symbol),
|
||||
|
||||
block_comment: $ =>
|
||||
seq("#|",
|
||||
repeat(
|
||||
choice(
|
||||
PREC.first($.block_comment),
|
||||
common.any_char)),
|
||||
PREC.first("|#")),
|
||||
|
||||
_datum: $ => choice(
|
||||
$.boolean,
|
||||
$.character,
|
||||
$.string,
|
||||
|
||||
// number/symbol precedence
|
||||
// for same length token, prefer number
|
||||
// otherwise, prefer symbol which is also longer
|
||||
$.number,
|
||||
$.symbol,
|
||||
|
||||
$.vector,
|
||||
$.byte_vector,
|
||||
$.list,
|
||||
|
||||
$.quote,
|
||||
$.quasiquote,
|
||||
$.unquote,
|
||||
$.unquote_splicing,
|
||||
$.syntax,
|
||||
$.quasisyntax,
|
||||
$.unsyntax,
|
||||
$.unsyntax_splicing,
|
||||
|
||||
$.keyword),
|
||||
|
||||
// simple datum {{{
|
||||
|
||||
boolean: _ =>
|
||||
token(
|
||||
choice(
|
||||
r5rs.boolean,
|
||||
r6rs.boolean,
|
||||
r7rs.boolean)),
|
||||
|
||||
number: _ =>
|
||||
token(
|
||||
choice(
|
||||
r5rs.number,
|
||||
r6rs.number,
|
||||
r7rs.number)),
|
||||
|
||||
character: _ =>
|
||||
token(
|
||||
choice(
|
||||
r5rs.character,
|
||||
r6rs.character,
|
||||
r7rs.character,
|
||||
extension.character)),
|
||||
|
||||
string: $ =>
|
||||
seq(
|
||||
'"',
|
||||
repeat(
|
||||
choice(
|
||||
$.escape_sequence,
|
||||
/[^"\\]+/)),
|
||||
'"'),
|
||||
|
||||
escape_sequence: _ =>
|
||||
token(
|
||||
choice(
|
||||
r5rs.escape_sequence,
|
||||
r6rs.escape_sequence,
|
||||
r7rs.escape_sequence,
|
||||
extension.escape_sequence)),
|
||||
|
||||
symbol: _ => token(hidden_node.symbol),
|
||||
|
||||
keyword: _ =>
|
||||
token(
|
||||
seq(
|
||||
"#:",
|
||||
hidden_node.symbol)),
|
||||
|
||||
// simple datum }}}
|
||||
|
||||
// compound datum {{{
|
||||
|
||||
list: $ =>
|
||||
choice(
|
||||
seq("(", repeat($._token), ")"),
|
||||
seq("[", repeat($._token), "]"),
|
||||
seq("{", repeat($._token), "}")),
|
||||
|
||||
quote: $ =>
|
||||
seq(
|
||||
"'",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
quasiquote: $ =>
|
||||
seq(
|
||||
"`",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
syntax: $ =>
|
||||
seq(
|
||||
"#'",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
quasisyntax: $ =>
|
||||
seq(
|
||||
"#`",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
unquote: $ =>
|
||||
seq(
|
||||
",",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
unquote_splicing: $ =>
|
||||
seq(
|
||||
",@",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
unsyntax: $ =>
|
||||
seq(
|
||||
"#,",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
unsyntax_splicing: $ =>
|
||||
seq(
|
||||
"#,@",
|
||||
repeat($._intertoken),
|
||||
$._datum),
|
||||
|
||||
vector: $ => seq("#(", repeat($._token), ")"),
|
||||
|
||||
byte_vector: $ => seq("#vu8(", repeat($._token), ")"),
|
||||
// compound datum }}}
|
||||
},
|
||||
});
|
||||
|
||||
// number {{{
|
||||
|
||||
function r5rs_number_base(n) {
|
||||
const radixn = {
|
||||
2: choice("#b", "#B"),
|
||||
8: choice("#o", "#O"),
|
||||
10: optional(choice("#d", "#D")),
|
||||
16: choice("#x", "#X"),
|
||||
};
|
||||
const digitsn = {
|
||||
2: /[01]/,
|
||||
8: /[0-7]/,
|
||||
10: /[0-9]/,
|
||||
16: /[0-9a-fA-F]/,
|
||||
};
|
||||
|
||||
const exactness =
|
||||
optional(
|
||||
choice("#i", "#e", "#I", "#E"));
|
||||
const radix = radixn[n];
|
||||
const prefix =
|
||||
choice(
|
||||
seq(radix, exactness),
|
||||
seq(exactness, radix));
|
||||
|
||||
const sign = optional(/[+-]/);
|
||||
const digits = digitsn[n];
|
||||
|
||||
const exponent = /[eEsSfFdDlL]/;
|
||||
const suffix =
|
||||
optional(
|
||||
seq(
|
||||
exponent,
|
||||
sign,
|
||||
repeat1(digitsn[10])));
|
||||
|
||||
const uinteger =
|
||||
seq(
|
||||
repeat1(digits),
|
||||
repeat("#"));
|
||||
const decimal10 = choice(
|
||||
seq(uinteger, suffix),
|
||||
seq(".", repeat1(digits), repeat("#"), suffix),
|
||||
seq(repeat1(digits), ".", repeat(digits), repeat("#"), suffix),
|
||||
seq(repeat1(digits), repeat1("#"), ".", repeat("#"), suffix)
|
||||
);
|
||||
const decimal = {
|
||||
2: "",
|
||||
8: "",
|
||||
10: decimal10,
|
||||
16: "",
|
||||
}[n];
|
||||
|
||||
const ureal =
|
||||
choice(
|
||||
uinteger,
|
||||
seq(uinteger, "/", uinteger),
|
||||
decimal);
|
||||
const real = seq(sign, ureal);
|
||||
const complex = choice(
|
||||
real,
|
||||
seq(real, "@", real),
|
||||
seq(optional(real), /[+-]/, optional(ureal), "i")
|
||||
);
|
||||
|
||||
return seq(prefix, complex);
|
||||
}
|
||||
|
||||
function r6rs_number_base(n) {
|
||||
const radixn = {
|
||||
2: choice("#b", "#B"),
|
||||
8: choice("#o", "#O"),
|
||||
10: optional(choice("#d", "#D")),
|
||||
16: choice("#x", "#X"),
|
||||
};
|
||||
const digitsn = {
|
||||
2: /[01]/,
|
||||
8: /[0-7]/,
|
||||
10: /[0-9]/,
|
||||
16: /[0-9a-fA-F]/,
|
||||
};
|
||||
|
||||
const exactness =
|
||||
optional(
|
||||
choice("#i", "#e", "#I", "#E"));
|
||||
const radix = radixn[n];
|
||||
const prefix =
|
||||
choice(
|
||||
seq(radix, exactness),
|
||||
seq(exactness, radix));
|
||||
|
||||
const sign = optional(/[+-]/);
|
||||
const digits = digitsn[n];
|
||||
const digits10 = digitsn[10];
|
||||
|
||||
const exponent = /[eEsSfFdDlL]/;
|
||||
const suffix =
|
||||
optional(
|
||||
seq(
|
||||
exponent,
|
||||
sign,
|
||||
repeat1(digits10)));
|
||||
|
||||
const uinteger = repeat1(digits);
|
||||
const decimal10 =
|
||||
choice(
|
||||
seq(uinteger, suffix),
|
||||
seq(".", repeat1(digits), suffix),
|
||||
seq(repeat1(digits), ".", repeat(digits), suffix),
|
||||
seq(repeat1(digits), ".", suffix));
|
||||
const decimal = {
|
||||
2: "",
|
||||
8: "",
|
||||
10: decimal10,
|
||||
16: "",
|
||||
}[n];
|
||||
|
||||
const mantissa_width =
|
||||
optional(
|
||||
seq("|", repeat1(digits10)));
|
||||
|
||||
const naninf = choice("nan.0", "inf.0");
|
||||
|
||||
const ureal =
|
||||
seq(
|
||||
choice(
|
||||
uinteger,
|
||||
seq(uinteger, "/", uinteger),
|
||||
seq(decimal, mantissa_width)));
|
||||
const real =
|
||||
choice(
|
||||
seq(sign, ureal),
|
||||
seq(/[+-]/, naninf));
|
||||
const complex =
|
||||
choice(
|
||||
real,
|
||||
seq(real, "@", real),
|
||||
seq(
|
||||
optional(real),
|
||||
/[+-]/,
|
||||
optional(choice(ureal, naninf)),
|
||||
"i"));
|
||||
|
||||
return seq(prefix, complex);
|
||||
}
|
||||
|
||||
function r7rs_number_base(n) {
|
||||
const infnan =
|
||||
choice(
|
||||
/[+-][iI][nN][fF]\.0/,
|
||||
/[+-][nN][aA][nN]\.0/);
|
||||
|
||||
const exponent_marker = /[eE]/;
|
||||
const sign = optional(/[+-]/);
|
||||
const exactness = optional(/#[ieIE]/);
|
||||
const radix =
|
||||
{
|
||||
2: /#[bB]/,
|
||||
8: /#[oO]/,
|
||||
10: optional(/#[dD]/),
|
||||
16: /#[xX]/,
|
||||
}[n];
|
||||
const digit =
|
||||
{
|
||||
2: /[01]/,
|
||||
8: /[0-7]/,
|
||||
10: /[0-9]/,
|
||||
16: /[0-9a-fA-F]/,
|
||||
}[n];
|
||||
|
||||
const suffix =
|
||||
optional(
|
||||
seq(exponent_marker, sign, repeat1(digit)));
|
||||
|
||||
const prefix =
|
||||
choice(
|
||||
seq(radix, exactness),
|
||||
seq(exactness, radix));
|
||||
|
||||
const uinteger = repeat1(digit);
|
||||
|
||||
const decimal =
|
||||
{
|
||||
2: "",
|
||||
8: "",
|
||||
10:
|
||||
choice(
|
||||
seq(uinteger, suffix),
|
||||
seq(".", repeat1(digit), suffix),
|
||||
seq(repeat1(digit), ".", repeat(digit), suffix)),
|
||||
16: "",
|
||||
}[n];
|
||||
|
||||
const ureal =
|
||||
choice(
|
||||
uinteger,
|
||||
seq(uinteger, "/", uinteger),
|
||||
decimal);
|
||||
|
||||
const real =
|
||||
choice(
|
||||
seq(sign, ureal),
|
||||
infnan);
|
||||
|
||||
const complex =
|
||||
choice(
|
||||
real,
|
||||
seq(real, "@", real),
|
||||
seq(real, /[+-]/, ureal, "i"),
|
||||
seq(real, /[+-]/, "i"),
|
||||
seq(real, infnan, "i"),
|
||||
seq(/[+-]/, ureal, "i"),
|
||||
seq(infnan, "i"),
|
||||
seq(/[+-]/, "i"));
|
||||
|
||||
const num =
|
||||
seq(
|
||||
prefix,
|
||||
complex);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
// number }}}
|
||||
@ -0,0 +1,30 @@
|
||||
## Nodes
|
||||
|
||||
This page contains all visible nodes in yaml format.
|
||||
|
||||
```yaml
|
||||
- comment
|
||||
- block_comment # for example, #| something |#
|
||||
- directive # for example, #!r6rs
|
||||
- boolean
|
||||
- character
|
||||
- string
|
||||
- escape_sequence # escape sequence in string, for example, \n in "abc\n"
|
||||
- number
|
||||
- symbol # identifier
|
||||
- keyword # #:identifier
|
||||
|
||||
- list # things surrounded by () or [] or {}
|
||||
- quote # '
|
||||
- quasiquote # `
|
||||
- syntax # #'
|
||||
- quasisyntax #`
|
||||
- unquote # ,
|
||||
- unquote_splicing # ,@
|
||||
- unsyntax # #,
|
||||
- unsyntax_splicing # #,@
|
||||
|
||||
- vector
|
||||
- byte_vector
|
||||
```
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "tree-sitter-scheme",
|
||||
"version": "0.6.0",
|
||||
"description": "Tree-sitter grammar for Scheme",
|
||||
"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.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tree-sitter-cli": "^0.20.8",
|
||||
"typescript": "^4.9.5",
|
||||
"typescript-language-server": "^2.3.0"
|
||||
},
|
||||
"tree-sitter": [
|
||||
{
|
||||
"scope": "source.scheme",
|
||||
"file-types": [
|
||||
"scm",
|
||||
"ss"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
["(" ")" "[" "]" "{" "}"] @punctuation.bracket
|
||||
|
||||
(number) @number
|
||||
(character) @constant.builtin
|
||||
(boolean) @constant.builtin
|
||||
|
||||
(string) @string
|
||||
|
||||
(escape_sequence) @escape
|
||||
|
||||
[(comment)
|
||||
(block_comment)
|
||||
(directive)] @comment
|
||||
|
||||
(list
|
||||
.
|
||||
"["
|
||||
.
|
||||
(symbol)+ @variable
|
||||
.
|
||||
"]")
|
||||
|
||||
((symbol) @operator
|
||||
(#match? @operator "^(\\+|-|\\*|/|=|>|<|>=|<=)$"))
|
||||
|
||||
(list
|
||||
.
|
||||
(symbol) @keyword
|
||||
(#match? @keyword
|
||||
"^(define-syntax|let\\*|lambda|λ|case|=>|quote-splicing|unquote-splicing|set!|let|letrec|letrec-syntax|let-values|let\\*-values|do|else|define|cond|syntax-rules|unquote|begin|quote|let-syntax|and|if|quasiquote|letrec|delay|or|when|unless|identifier-syntax|assert|library|export|import|rename|only|except|prefix)$"
|
||||
))
|
||||
|
||||
(list
|
||||
.
|
||||
(symbol) @function.builtin
|
||||
(#match? @function.builtin
|
||||
"^(caar|cadr|call-with-input-file|call-with-output-file|cdar|cddr|list|open-input-file|open-output-file|with-input-from-file|with-output-to-file|\\*|\\+|-|/|<|<=|=|>|>=|abs|acos|angle|append|apply|asin|assoc|assq|assv|atan|boolean\\?|caaaar|caaadr|caaar|caadar|caaddr|caadr|cadaar|cadadr|cadar|caddar|cadddr|caddr|call-with-current-continuation|call-with-values|car|cdaaar|cdaadr|cdaar|cdadar|cdaddr|cdadr|cddaar|cddadr|cddar|cdddar|cddddr|cdddr|cdr|ceiling|char->integer|char-alphabetic\\?|char-ci<=\\?|char-ci<\\?|char-ci=\\?|char-ci>=\\?|char-ci>\\?|char-downcase|char-lower-case\\?|char-numeric\\?|char-ready\\?|char-upcase|char-upper-case\\?|char-whitespace\\?|char<=\\?|char<\\?|char=\\?|char>=\\?|char>\\?|char\\?|close-input-port|close-output-port|complex\\?|cons|cos|current-error-port|current-input-port|current-output-port|denominator|display|dynamic-wind|eof-object\\?|eq\\?|equal\\?|eqv\\?|eval|even\\?|exact->inexact|exact\\?|exp|expt|floor|flush-output|for-each|force|gcd|imag-part|inexact->exact|inexact\\?|input-port\\?|integer->char|integer\\?|interaction-environment|lcm|length|list->string|list->vector|list-ref|list-tail|list\\?|load|log|magnitude|make-polar|make-rectangular|make-string|make-vector|map|max|member|memq|memv|min|modulo|negative\\?|newline|not|null-environment|null\\?|number->string|number\\?|numerator|odd\\?|output-port\\?|pair\\?|peek-char|positive\\?|procedure\\?|quotient|rational\\?|rationalize|read|read-char|real-part|real\\?|remainder|reverse|round|scheme-report-environment|set-car!|set-cdr!|sin|sqrt|string|string->list|string->number|string->symbol|string-append|string-ci<=\\?|string-ci<\\?|string-ci=\\?|string-ci>=\\?|string-ci>\\?|string-copy|string-fill!|string-length|string-ref|string-set!|string<=\\?|string<\\?|string=\\?|string>=\\?|string>\\?|string\\?|substring|symbol->string|symbol\\?|tan|transcript-off|transcript-on|truncate|values|vector|vector->list|vector-fill!|vector-length|vector-ref|vector-set!|vector\\?|write|write-char|zero\\?)$"
|
||||
))
|
||||
|
||||
(list
|
||||
.
|
||||
(symbol) @function)
|
||||
|
||||
(symbol) @variable
|
||||
@ -0,0 +1,11 @@
|
||||
{ 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
@ -0,0 +1,224 @@
|
||||
#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_
|
||||
Loading…
Reference in New Issue