Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jsonnet support #676

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ fn main() {
src_dir: "vendored_parsers/tree-sitter-json-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-jsonnet",
src_dir: "vendored_parsers/tree-sitter-jsonnet-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-julia",
src_dir: "vendored_parsers/tree-sitter-julia-src",
Expand Down
3 changes: 3 additions & 0 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ b9f8e04d42f4c88b3514d31743e17297 -
sample_files/json_before.json sample_files/json_after.json
79d34d1bbb670bb702f6ea75ff759fa5 -

sample_files/jsonnet_before.jsonnet sample_files/jsonnet_after.jsonnet
ac7d694beebb1de40bf3c4455c15d894 -

sample_files/jsx_before.jsx sample_files/jsx_after.jsx
908a1637b237cd7763948b7abeb95325 -

Expand Down
6 changes: 6 additions & 0 deletions sample_files/jsonnet_after.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
local func(arg) = arg,
foo: [2, 3, 4, 5],
zab: 'testing',
woo: ['foobar'],
}
5 changes: 5 additions & 0 deletions sample_files/jsonnet_before.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
func(arg): arg,
foo: [1, 2, 3, 4],
bar: 'testing',
}
3 changes: 3 additions & 0 deletions src/parse/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub(crate) enum Language {
JavaScript,
JavascriptJsx,
Json,
Jsonnet,
Julia,
Kotlin,
LaTeX,
Expand Down Expand Up @@ -137,6 +138,7 @@ pub(crate) fn language_name(language: Language) -> &'static str {
JavaScript => "JavaScript",
JavascriptJsx => "JavaScript JSX",
Json => "JSON",
Jsonnet => "Jsonnet",
Julia => "Julia",
Kotlin => "Kotlin",
LaTeX => "LaTeX",
Expand Down Expand Up @@ -297,6 +299,7 @@ pub(crate) fn language_globs(language: Language) -> Vec<glob::Pattern> {
"composer.lock",
"mcmod.info",
],
Jsonnet => &["*.jsonnet", "*.libsonnet"],
JavascriptJsx => &["*.jsx"],
Julia => &["*.jl"],
Kotlin => &["*.kt", "*.ktm", "*.kts"],
Expand Down
15 changes: 15 additions & 0 deletions src/parse/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ extern "C" {
fn tree_sitter_java() -> ts::Language;
fn tree_sitter_javascript() -> ts::Language;
fn tree_sitter_json() -> ts::Language;
fn tree_sitter_jsonnet() -> ts::Language;
fn tree_sitter_julia() -> ts::Language;
fn tree_sitter_kotlin() -> ts::Language;
fn tree_sitter_latex() -> ts::Language;
Expand Down Expand Up @@ -622,6 +623,20 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
sub_languages: vec![],
}
}
Jsonnet => {
let language = unsafe { tree_sitter_jsonnet() };
TreeSitterConfig {
language,
atom_nodes: vec!["string"].into_iter().collect(),
delimiter_tokens: vec![("{", "}"), ("[", "]"), ("(", ")")],
highlight_query: ts::Query::new(
language,
include_str!("../../vendored_parsers/highlights/jsonnet.scm"),
)
.unwrap(),
sub_languages: vec![],
}
}
Julia => {
let language = unsafe { tree_sitter_julia() };
TreeSitterConfig {
Expand Down
1 change: 1 addition & 0 deletions vendored_parsers/highlights/jsonnet.scm
1 change: 1 addition & 0 deletions vendored_parsers/tree-sitter-jsonnet-src
5 changes: 5 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Cargo.lock
node_modules
build
package-lock.json
/target/
26 changes: 26 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "tree-sitter-jsonnet"
description = "jsonnet grammar for the tree-sitter parsing library"
version = "0.0.1"
keywords = ["incremental", "parsing", "jsonnet"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-jsonnet"
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"
21 changes: 21 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Sourcegraph

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.
10 changes: 10 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ts := tree-sitter

generate:
${ts} generate

test: generate
${ts} test

fmt:
npx prettier --tab-width 4 -w grammar.js
2 changes: 2 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# tree-sitter-jsonnet
tree-sitter grammar for JSONNET
19 changes: 19 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"targets": [
{
"target_name": "tree_sitter_jsonnet_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",
]
}
]
}
28 changes: 28 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"

using namespace v8;

extern "C" TSLanguage * tree_sitter_jsonnet();

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

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

NODE_MODULE(tree_sitter_jsonnet_binding, Init)

} // namespace
19 changes: 19 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
try {
module.exports = require("../../build/Release/tree_sitter_jsonnet_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_jsonnet_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}
19 changes: 19 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -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());
}
52 changes: 52 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! This crate provides jsonnet 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_jsonnet::language()).expect("Error loading jsonnet 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_jsonnet() -> 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_jsonnet() }
}

/// 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 jsonnet language");
}
}
61 changes: 61 additions & 0 deletions vendored_parsers/tree-sitter-jsonnet/examples/doc_1.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Define a local function.
// Default arguments are like Python:
local my_function(x, y=10) = x + y;

// Define a local multiline function.
local multiline_function(x) =
// One can nest locals.
local temp = x * 2;
// Every local ends with a semi-colon.
[temp, temp + 1];

local object = {
// A method
my_method(x): x * x,
};

{
// Functions are first class citizens.
call_inline_function:
(function(x) x * x)(5),

call_multiline_function: multiline_function(4),

// Using the variable fetches the function,
// the parens call the function.
call: my_function(true),

// Like python, parameters can be named at
// call time.
named_params: my_function(x=2),
// This allows changing their order
named_params2: my_function(y=3, x=2),

// object.my_method returns the function,
// which is then called like any other.
call_method1: object.my_method(3),

standard_lib:
std.join(' ', std.split('foo/bar', '/')),

len: [
std.length('hello'),
std.length([1, 2, 3]),
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
Loading
Loading