Add simple rust library
This commit is contained in:
parent
b7cecd46a0
commit
72eab8826e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
build/
|
||||
node_modules/
|
||||
target/
|
||||
|
4974
Cargo.lock
generated
4974
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
11
Cargo.toml
@ -8,7 +8,7 @@ repository = "https://github.com/tree-sitter/tree-sitter-dust"
|
||||
edition = "2018"
|
||||
license = "MIT"
|
||||
|
||||
build = "bindings/rust/build.rs"
|
||||
build = "runtime/build.rs"
|
||||
include = [
|
||||
"bindings/rust/*",
|
||||
"grammar.js",
|
||||
@ -16,10 +16,17 @@ include = [
|
||||
"src/*",
|
||||
]
|
||||
|
||||
[[bin]]
|
||||
name = "main"
|
||||
path = "runtime/main.rs"
|
||||
|
||||
[lib]
|
||||
path = "bindings/rust/lib.rs"
|
||||
path = "runtime/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
clap = "4.4.4"
|
||||
dust-lang = "0.1.2"
|
||||
rustyline = "12.0.0"
|
||||
tree-sitter = "~0.20.10"
|
||||
|
||||
[build-dependencies]
|
||||
|
@ -1,28 +0,0 @@
|
||||
#include "tree_sitter/parser.h"
|
||||
#include <node.h>
|
||||
#include "nan.h"
|
||||
|
||||
using namespace v8;
|
||||
|
||||
extern "C" TSLanguage * tree_sitter_Dust();
|
||||
|
||||
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_Dust());
|
||||
|
||||
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("Dust").ToLocalChecked());
|
||||
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
|
||||
}
|
||||
|
||||
NODE_MODULE(tree_sitter_Dust_binding, Init)
|
||||
|
||||
} // namespace
|
@ -1,19 +0,0 @@
|
||||
try {
|
||||
module.exports = require("../../build/Release/tree_sitter_Dust_binding");
|
||||
} catch (error1) {
|
||||
if (error1.code !== 'MODULE_NOT_FOUND') {
|
||||
throw error1;
|
||||
}
|
||||
try {
|
||||
module.exports = require("../../build/Debug/tree_sitter_Dust_binding");
|
||||
} catch (error2) {
|
||||
if (error2.code !== 'MODULE_NOT_FOUND') {
|
||||
throw error2;
|
||||
}
|
||||
throw error1
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
module.exports.nodeTypeInfo = require("../../src/node-types.json");
|
||||
} catch (_) {}
|
@ -164,3 +164,19 @@ true false
|
||||
(expression
|
||||
(value
|
||||
(boolean))))
|
||||
|
||||
==================
|
||||
Function
|
||||
==================
|
||||
|
||||
{ output "hi" }
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(expression
|
||||
(value
|
||||
(boolean)))
|
||||
(expression
|
||||
(value
|
||||
(boolean))))
|
||||
|
@ -54,9 +54,13 @@ module.exports = grammar({
|
||||
|
||||
integer: $ => /\d+/,
|
||||
|
||||
string: $ => /"(.*?)\"/,
|
||||
string: $ => seq(
|
||||
'"',
|
||||
repeat(),
|
||||
'"'
|
||||
),
|
||||
|
||||
function: $ => /'(.*?)\'/,
|
||||
function: $ => /\{(.*?)\}/,
|
||||
|
||||
empty: $ => "()",
|
||||
|
||||
|
8
package-lock.json
generated
8
package-lock.json
generated
@ -9,16 +9,16 @@
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"nan": "^2.17.0"
|
||||
"nan": "^2.18.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tree-sitter-cli": "^0.20.8"
|
||||
}
|
||||
},
|
||||
"node_modules/nan": {
|
||||
"version": "2.17.0",
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz",
|
||||
"integrity": "sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ=="
|
||||
"version": "2.18.0",
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.18.0.tgz",
|
||||
"integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w=="
|
||||
},
|
||||
"node_modules/tree-sitter-cli": {
|
||||
"version": "0.20.8",
|
||||
|
@ -19,7 +19,7 @@
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"nan": "^2.17.0"
|
||||
"nan": "^2.18.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tree-sitter-cli": "^0.20.8"
|
||||
|
@ -15,23 +15,39 @@
|
||||
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
|
||||
//! [tree-sitter]: https://tree-sitter.github.io/
|
||||
|
||||
use tree_sitter::Language;
|
||||
use tree_sitter::{Language, Node};
|
||||
use dust_lib::Value;
|
||||
|
||||
extern "C" {
|
||||
fn tree_sitter_Dust() -> Language;
|
||||
fn tree_sitter_dust() -> 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_Dust() }
|
||||
unsafe { tree_sitter_dust() }
|
||||
}
|
||||
|
||||
/// 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");
|
||||
pub const NODE_TYPES: &'static str = include_str!("../src/node-types.json");
|
||||
|
||||
pub fn evaluate(root: Node) -> Value {
|
||||
let cursor = root.walk();
|
||||
|
||||
|
||||
if let Some(name) = cursor.field_name() {
|
||||
match name {
|
||||
"source" => Value::Empty,
|
||||
_ => Value::Empty,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Uncomment these to include any queries that this grammar contains
|
||||
|
38
runtime/main.rs
Normal file
38
runtime/main.rs
Normal file
@ -0,0 +1,38 @@
|
||||
//! Command line interface for the dust programming language.
|
||||
use clap::Parser;
|
||||
use std::fs::read_to_string;
|
||||
use tree_sitter::{Parser as TSParser, Language};
|
||||
|
||||
/// Command-line arguments to be parsed.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Whale source code to evaluate.
|
||||
#[arg(short, long)]
|
||||
command: Option<String>,
|
||||
|
||||
/// Location of the file to run.
|
||||
path: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
if args.path.is_none() && args.command.is_none() {
|
||||
todo!();
|
||||
}
|
||||
|
||||
if let Some(path) = args.path {
|
||||
let file_contents = read_to_string(path).unwrap();
|
||||
|
||||
let mut parser = TSParser::new();
|
||||
parser.set_language(tree_sitter_dust::language()).unwrap();
|
||||
let tree = parser.parse(&file_contents, None).unwrap();
|
||||
let root = tree.root_node();
|
||||
|
||||
tree_sitter_dust::evaluate(root);
|
||||
|
||||
println!("{tree:?}");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user