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

feat: add the highlight querys and Rust bindings #18

Open
wants to merge 8 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/build
/node_modules

/bindings/rust
/Cargo.toml
/target
Cargo.lock
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "tree-sitter-vue"
description = "vue grammar for the tree-sitter parsing library"
version = "0.0.3"
keywords = ["incremental", "parsing", "vue", "highlight"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/xiaoxin-sky/tree-sitter-vue"
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.3"

[build-dependencies]
cc = "1.0"
26 changes: 26 additions & 0 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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);

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

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());
}
52 changes: 52 additions & 0 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! This crate provides YOUR_LANGUAGE_NAME 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_vue::language()).expect("Error loading YOUR_LANGUAGE_NAME 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_vue() -> 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_vue() }
}

/// 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 vue grammar");
}
}
24 changes: 24 additions & 0 deletions corpus/script.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
================================================================================
Script - script ts
================================================================================

<script lang="ts">
const ax:number = 1;
console.log(ax);
function x(axd:string){
console.log(ax);
}
</script>

-------------------------------------------------------------------------------

(component
(script_element
(start_tag
(tag_name)
(script_lang
(ts_lang)))
(raw_text)
(end_tag
(tag_name))
))
85 changes: 85 additions & 0 deletions corpus/spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,88 @@ Shorthands - v-on Shorthand (https://vuejs.org/v2/guide/syntax.html#v-on-Shortha
(text)
(end_tag
(tag_name))))


================================================================================
BuildIn-component - suspense (https://vuejs.org/v2/guide/syntax.html#v-on-Shorthand)
================================================================================

<template>
<Suspense data-id="1">asd</Suspense>
</template>

--------------------------------------------------------------------------------

(component
(template_element
(start_tag
(tag_name))
(text)
(suspense
(attribute
(attribute_name)
(quoted_attribute_value
(attribute_value)))
(text)
)
(text)
(end_tag
(tag_name))
)
)

================================================================================
BuildIn-component - suspense props (https://vuejs.org/v2/guide/syntax.html#v-on-Shorthand)
================================================================================

<template>
<Suspense timeout="1">asd</Suspense>
</template>

--------------------------------------------------------------------------------

(component
(template_element
(start_tag
(tag_name))
(text)
(suspense
(props
(quoted_attribute_value
(attribute_value)))
(text)
)
(text)
(end_tag
(tag_name))
)
)


================================================================================
BuildIn-component - component (https://vuejs.org/v2/guide/syntax.html#v-on-Shorthand)
================================================================================

<template>
<component is="dog">sss</component>
</template>

--------------------------------------------------------------------------------

(component
(template_element
(start_tag
(tag_name))
(text)
(vue_component
(quoted_attribute_value
(attribute_value))
(text)
)
(text)
(end_tag
(tag_name))
)
)


46 changes: 46 additions & 0 deletions corpus/style.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
================================================================================
Style - style scss (https://cn.vuejs.org/api/sfc-spec.html#pre-processors)
================================================================================

<style lang="scss">
.id{
background:red;
}
</style>

-------------------------------------------------------------------------------

(component
(style_element
(start_tag
(tag_name)
(style_lang
(scss_val)))
(raw_text)
(end_tag
(tag_name))
))

================================================================================
Style - style css
================================================================================

<style lang="css">
.id{
background:red;
@include ads();
}
</style>

-------------------------------------------------------------------------------

(component
(style_element
(start_tag
(tag_name)
(style_lang
(css_val)))
(raw_text)
(end_tag
(tag_name))
))
Loading