Skip to content

Commit

Permalink
Fix syntax highlights of dashes in flags (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdgo and mrdgo authored Sep 17, 2024
1 parent dae9847 commit 91ada3c
Show file tree
Hide file tree
Showing 15 changed files with 284,029 additions and 255,790 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ path = "bindings/rust/lib.rs"
# path = "src/main.rs"

[dev-dependencies]
tree-sitter = "0.20.3"
tree-sitter-language = "0.1.0"
cc = "1.0"

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions bindings/go/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tree_sitter_nu_test
import (
"testing"

tree_sitter "github.com/smacker/go-tree-sitter"
"github.com/tree-sitter/tree-sitter-nu"
tree_sitter "github.com/tree-sitter/go-tree-sitter"
tree_sitter_nu "github.com/tree-sitter/tree-sitter-nu/bindings/go"
)

func TestCanLoadGrammar(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions bindings/go/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/tree-sitter/tree-sitter-nu

go 1.22
go 1.23

require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8
require github.com/tree-sitter/go-tree-sitter v0.23
4 changes: 2 additions & 2 deletions bindings/python/tree_sitter_nu/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ typedef struct TSLanguage TSLanguage;

TSLanguage *tree_sitter_nu(void);

static PyObject* _binding_language(PyObject *self, PyObject *args) {
return PyLong_FromVoidPtr(tree_sitter_nu());
static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
return PyCapsule_New(tree_sitter_nu(), "tree_sitter.Language", NULL);
}

static PyMethodDef methods[] = {
Expand Down
39 changes: 20 additions & 19 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
//! This crate provides nu language support for the [tree-sitter][] parsing library.
//! This crate provides Nu 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 code = r#"
//! "#;
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_nu::language()).expect("Error loading nu grammar");
//! let language = tree_sitter_nu::LANGUAGE;
//! parser
//! .set_language(&language.into())
//! .expect("Error loading Nu parser");
//! let tree = parser.parse(code, None).unwrap();
//! assert!(!tree.root_node().has_error());
//! ```
//!
//! [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;
use tree_sitter_language::LanguageFn;

extern "C" {
fn tree_sitter_nu() -> Language;
fn tree_sitter_nu() -> *const ();
}

/// 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_nu() }
}
/// The tree-sitter [`LanguageFn`] for this grammar.
pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_nu) };

/// 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: &str = include_str!("../../src/node-types.json");

// Uncomment these to include any queries that this grammar contains
// NOTE: 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");
// pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
// pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
// pub const TAGS_QUERY: &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 nu language");
.set_language(&super::LANGUAGE.into())
.expect("Error loading Nu parser");
}
}
27 changes: 13 additions & 14 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = grammar({

identifier: (_$) => token(/[_\p{XID_Start}][_\p{XID_Continue}]*/),

_long_flag_identifier: (_$) =>
long_flag_identifier: (_$) =>
token.immediate(/[\p{XID_Start}_][\p{XID_Continue}_-]*/),

_command_name: ($) =>
Expand Down Expand Up @@ -249,14 +249,15 @@ module.exports = grammar({
param_opt: ($) =>
seq(field("name", $.identifier), token.immediate(PUNC().question)),

param_long_flag: ($) =>
seq("--", alias($._long_flag_identifier, $.identifier)),
param_long_flag: ($) => seq(OPR().long_flag, $.long_flag_identifier),

flag_capsule: ($) =>
seq(BRACK().open_paren, $.param_short_flag, BRACK().close_paren),

param_short_flag: (_$) =>
seq("-", field("name", token.immediate(/[a-zA-Z0-9]/))),
param_short_flag: ($) =>
seq(OPR().minus, field("name", $.param_short_flag_identifier)),

param_short_flag_identifier: (_$) => token.immediate(/[a-zA-Z0-9]/),

/// Controls

Expand Down Expand Up @@ -1280,19 +1281,14 @@ module.exports = grammar({

_flag: ($) => prec.right(5, choice($.short_flag, $.long_flag)),

short_flag: (_$) =>
seq(token(OPR().minus), token.immediate(/[_\p{XID_Continue}]+/)),
short_flag: ($) => seq(OPR().minus, $.short_flag_identifier),

short_flag_identifier: (_$) => token.immediate(/[_\p{XID_Continue}]+/),

long_flag: ($) =>
prec.right(
10,
choice(
alias(seq(token(OPR().minus), token.immediate(OPR().minus)), "--"),
seq(
alias(seq(token(OPR().minus), token.immediate(OPR().minus)), "--"),
$._long_flag_identifier,
),
),
choice(OPR().long_flag, seq(OPR().long_flag, $.long_flag_identifier)),
),

unquoted: _unquoted_rule(false),
Expand Down Expand Up @@ -1750,6 +1746,9 @@ function OPR() {
power: "**",
append: "++",

// special
long_flag: "--",

// comparison
equal: "==",
not_equal: "!=",
Expand Down
11 changes: 6 additions & 5 deletions queries/nu/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ file_path: (val_string) @variable.parameter
";"
] @punctuation.special

(param_short_flag "-" @punctuation.delimiter)
(param_long_flag ["--"] @punctuation.delimiter)
(long_flag ["--"] @punctuation.delimiter)
(short_flag ["-"] @punctuation.delimiter)
(param_short_flag ["-"] @punctuation.delimiter)
(param_rest "..." @punctuation.delimiter)
(param_type [":"] @punctuation.special)
(param_value ["="] @punctuation.special)
Expand Down Expand Up @@ -217,11 +218,11 @@ key: (identifier) @property
param_name: (_) @variable.parameter)
(param_cmd
(cmd_identifier) @string)
(param_long_flag) @variable.parameter
(param_short_flag) @variable.parameter
(param_long_flag (long_flag_identifier) @variable.parameter)
(param_short_flag (param_short_flag_identifier) @variable.parameter)

(short_flag) @variable.parameter
(long_flag) @variable.parameter
(short_flag (short_flag_identifier) @variable.parameter)
(long_flag (long_flag_identifier) @variable.parameter)

(scope_pattern [(wild_card) @function])

Expand Down
92 changes: 26 additions & 66 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@
"value": "[_\\p{XID_Start}][_\\p{XID_Continue}]*"
}
},
"_long_flag_identifier": {
"long_flag_identifier": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
Expand Down Expand Up @@ -2379,13 +2379,8 @@
"value": "--"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_long_flag_identifier"
},
"named": true,
"value": "identifier"
"type": "SYMBOL",
"name": "long_flag_identifier"
}
]
},
Expand Down Expand Up @@ -2417,15 +2412,19 @@
"type": "FIELD",
"name": "name",
"content": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[a-zA-Z0-9]"
}
"type": "SYMBOL",
"name": "param_short_flag_identifier"
}
}
]
},
"param_short_flag_identifier": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[a-zA-Z0-9]"
}
},
"_control": {
"type": "PREC",
"value": 1,
Expand Down Expand Up @@ -10690,81 +10689,42 @@
"type": "SEQ",
"members": [
{
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
"type": "STRING",
"value": "-"
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[_\\p{XID_Continue}]+"
}
"type": "SYMBOL",
"name": "short_flag_identifier"
}
]
},
"short_flag_identifier": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[_\\p{XID_Continue}]+"
}
},
"long_flag": {
"type": "PREC_RIGHT",
"value": 10,
"content": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SEQ",
"members": [
{
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
}
]
},
"named": false,
"type": "STRING",
"value": "--"
},
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SEQ",
"members": [
{
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "-"
}
}
]
},
"named": false,
"type": "STRING",
"value": "--"
},
{
"type": "SYMBOL",
"name": "_long_flag_identifier"
"name": "long_flag_identifier"
}
]
}
Expand Down
Loading

0 comments on commit 91ada3c

Please sign in to comment.