Skip to content

Commit

Permalink
feat: Initial working parser
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Apr 27, 2023
0 parents commit 4f8fc20
Show file tree
Hide file tree
Showing 32 changed files with 28,275 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,20 @@
module.exports = {
'env': {
'commonjs': true,
'es2021': true,
},
'extends': 'google',
'overrides': [
],
'parserOptions': {
'ecmaVersion': 'latest',
'sourceType': 'module',
},
'rules': {
'indent': ['error', 2, {'SwitchCase': 1}],
'max-len': [
'error',
{'code': 120, 'ignoreComments': true, 'ignoreUrls': true, 'ignoreStrings': true},
],
},
};
6 changes: 6 additions & 0 deletions .gitattributes
@@ -0,0 +1,6 @@
/src/** linguist-vendored
/examples/* linguist-vendored

src/grammar.json -diff
src/node-types.json -diff
src/parser.c -diff
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,34 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- "**"

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm test

test_windows:
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm run-script test-windows
19 changes: 19 additions & 0 deletions .github/workflows/lint.yml
@@ -0,0 +1,19 @@
name: Lint

on:
push:
branches:
- master
pull_request:
branches:
- "**"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install modules
run: npm install
- name: Run ESLint
run: npm run lint
51 changes: 51 additions & 0 deletions .github/workflows/publish.yml
@@ -0,0 +1,51 @@
name: Publish

on:
push:
branches:
- master
tags:
- "v*"
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm install
- run: npm test

publish-npm:
if: startsWith(github.ref, 'refs/tags/v')
needs: build
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

publish-crates:
if: startsWith(github.ref, 'refs/tags/v')
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: katyo/publish-crates@v2
with:
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
Cargo.lock
package-lock.json
/build
/node_modules
/examples/*/
/target
5 changes: 5 additions & 0 deletions .npmignore
@@ -0,0 +1,5 @@
/test
/examples
/build
/script
/target
21 changes: 21 additions & 0 deletions Cargo.toml
@@ -0,0 +1,21 @@
[package]
name = "tree-sitter-luau"
description = "luau grammar for the tree-sitter parsing library"
version = "0.0.1"
keywords = ["incremental", "parsing", "luau"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-luau"
edition = "2021"
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.10"

[build-dependencies]
cc = "1.0"
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Amaan Qureshi <amaanq12@gmail.com>

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.
38 changes: 38 additions & 0 deletions Package.swift
@@ -0,0 +1,38 @@
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "TreeSitterPuppet",
platforms: [.macOS(.v10_13), .iOS(.v11)],
products: [
.library(name: "TreeSitterPuppet", targets: ["TreeSitterPuppet"]),
],
dependencies: [],
targets: [
.target(name: "TreeSitterPuppet",
path: ".",
exclude: [
"binding.gyp",
"bindings",
"Cargo.toml",
"examples",
"test",
"grammar.js",
"LICENSE",
"package.json",
"README.md",
"script",
"src/grammar.json",
"src/node-types.json",
],
sources: [
"src/parser.c",
"src/scanner.c",
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("src")])
]
)
8 changes: 8 additions & 0 deletions README.md
@@ -0,0 +1,8 @@
# tree-sitter-luau

[![Build Status](https://github.com/amaanq/tree-sitter-luau/actions/workflows/ci.yml/badge.svg)](https://github.com/amaanq/tree-sitter-luau/actions/workflows/ci.yml)
[![Discord](https://img.shields.io/discord/1063097320771698699?logo=discord)](https://discord.gg/w7nTvsVJhm)

Luau grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).

Adapted from [the syntax spec](https://luau-lang.org/syntax)
19 changes: 19 additions & 0 deletions binding.gyp
@@ -0,0 +1,19 @@
{
"targets": [
{
"target_name": "tree_sitter_luau_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
"src/scanner.c",
],
"cflags_c": [
"-std=c99",
]
}
]
}
30 changes: 30 additions & 0 deletions bindings/node/binding.cc
@@ -0,0 +1,30 @@
#include "nan.h"
#include "tree_sitter/parser.h"
#include <node.h>

using namespace v8;

extern "C" TSLanguage *tree_sitter_luau();

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_luau());

Nan::Set(instance, Nan::New("name").ToLocalChecked(),
Nan::New("luau").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
}

NODE_MODULE(tree_sitter_luau_binding, Init)

} // namespace
19 changes: 19 additions & 0 deletions bindings/node/index.js
@@ -0,0 +1,19 @@
try {
module.exports = require('../../build/Release/tree_sitter_luau_binding');
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require('../../build/Debug/tree_sitter_luau_binding');
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1;
}
}

try {
module.exports.nodeTypeInfo = require('../../src/node-types.json');
} catch (_) {}
48 changes: 48 additions & 0 deletions bindings/rust/README.md
@@ -0,0 +1,48 @@
# tree-sitter-luau

This crate provides a Luau grammar for the [tree-sitter][] parsing library. To
use this crate, add it to the `[dependencies]` section of your `Cargo.toml`
file. (Note that you will probably also need to depend on the
[`tree-sitter`][tree-sitter crate] crate to use the parsed result in any useful
way.)

```toml
[dependencies]
tree-sitter = "~0.20.3"
tree-sitter-luau = "0.0.1"
```

Typically, you will use the [language][language func] function to add this
grammar to a tree-sitter [Parser][], and then use the parser to parse some code:

```rust
let code = r#"
function foo(x: number, y: string): boolean
local k = (y :: string):rep(x)
local foo: (number, string) -> boolean
local no_returns: (number, string) -> ()
local returns_boolean_and_string: (number, string) -> (boolean, string)
local a = "hi"
local b = `bye {no_returns}`
end
export type Point = {
x: number,
y: number,
}
"#;
let mut parser = Parser::new();
parser.set_language(tree_sitter_luau::language()).expect("Error loading Luau grammar");
let parsed = parser.parse(code, None);
```

If you have any questions, please reach out to us in the [tree-sitter
discussions] page.

[language func]: https://docs.rs/tree-sitter-luau/*/tree_sitter_luau/fn.language.html
[parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
[tree-sitter]: https://tree-sitter.github.io/
[tree-sitter crate]: https://crates.io/crates/tree-sitter
[tree-sitter discussions]: https://github.com/tree-sitter/tree-sitter/discussions
19 changes: 19 additions & 0 deletions bindings/rust/build.rs
@@ -0,0 +1,19 @@
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());

c_config.compile("parser");
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
}

0 comments on commit 4f8fc20

Please sign in to comment.