Skip to content

Commit

Permalink
edit grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
ilevyor committed Mar 13, 2024
1 parent 410b2ee commit 1ecb2b4
Show file tree
Hide file tree
Showing 46 changed files with 143,795 additions and 80,164 deletions.
19 changes: 19 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,25 @@ fn quote_snippet_rewrite() {
.unwrap();
}

#[test]
fn simple_toml() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language toml
|`[$foo]` where {
| $foo => `bar`
|}
|"#
.trim_margin()
.unwrap(),
source: r#"[workspace]"#.to_owned(),
expected: r#"[bar]"#.to_owned(),
}
})
.unwrap();
}

#[test]
fn multi_args_snippet() {
run_test_match({
Expand Down
4 changes: 2 additions & 2 deletions crates/language/src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ mod tests {
use super::*;

#[test]
fn pair_snippet() {
let snippet = "fmt.Println($foo)";
fn table_snippet() {
let snippet = "[workspace]";
let lang = Toml::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
Expand Down
Binary file modified crates/wasm-bindings/wasm_parsers/tree-sitter-toml.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions resources/edit_grammars.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ async function buildLanguage(language) {
`${METAVARIABLE_GRAMMARS}/c_build.rs`,
`${tsLangDir}/bindings/rust/build.rs`,
);
} else if (language === 'toml') {
await buildSimpleLanguage(log, language);
await fs.copyFile(
`${METAVARIABLE_GRAMMARS}/c_build.rs`,
`${tsLangDir}/bindings/rust/build.rs`,
);
} else {
await buildSimpleLanguage(log, language);
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ fn main() {
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);

// If your language uses an external scanner written in C,
// then include this block of code:

/*
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());
Expand Down
29 changes: 16 additions & 13 deletions resources/language-metavariables/tree-sitter-toml/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module.exports = grammar({
rules: {
document: $ =>
seq(
repeat(choice($.pair, newline)),
repeat(choice($.table, $.table_array_element)),
field('pair', repeat(choice($._pair, newline))),
field('table', repeat(choice($.table, $.table_array_element))),
),

comment: $ =>
Expand All @@ -48,25 +48,25 @@ module.exports = grammar({
table: $ =>
seq(
"[",
choice($.dotted_key, $._key),
field('key', choice($.dotted_key, $._key)),
"]",
$._line_ending_or_eof,
repeat(choice($.pair, newline)),
field('pair', repeat(choice($._pair, newline))),
),

table_array_element: $ =>
seq(
"[[",
choice($.dotted_key, $._key),
field('key', choice($.dotted_key, $._key)),
"]]",
$._line_ending_or_eof,
repeat(choice($.pair, newline)),
field('pair', repeat(choice($._pair, newline))),
),

pair: $ => seq($._inline_pair, $._line_ending_or_eof),
_inline_pair: $ => seq(choice($.dotted_key, $._key), "=", $._inline_value),
_pair: $ => seq(choice($.grit_metavariable, $.inline_pair), $._line_ending_or_eof),
inline_pair: $ => seq(field('key', choice($.dotted_key, $._key)), "=", field('value', $._inline_value)),

_key: $ => choice($.bare_key, $.quoted_key),
_key: $ => choice($.bare_key, $.quoted_key, $.grit_metavariable),
dotted_key: $ => seq(choice($.dotted_key, $._key), ".", $._key),
bare_key: $ => /[A-Za-z0-9_-]+/,
quoted_key: $ => choice($._basic_string, $._literal_string),
Expand All @@ -83,6 +83,7 @@ module.exports = grammar({
$.local_time,
$.array,
$.inline_table,
$.grit_metavariable
),

string: $ =>
Expand Down Expand Up @@ -190,9 +191,9 @@ module.exports = grammar({
repeat(newline),
optional(
seq(
$._inline_value,
field('value', $._inline_value),
repeat(newline),
repeat(seq(",", repeat(newline), $._inline_value, repeat(newline))),
repeat(seq(",", repeat(newline), field('value', $._inline_value), repeat(newline))),
optional(seq(",", repeat(newline))),
),
),
Expand All @@ -204,11 +205,13 @@ module.exports = grammar({
"{",
optional(
seq(
alias($._inline_pair, $.pair),
repeat(seq(",", alias($._inline_pair, $.pair))),
field('pair', alias($.inline_pair, $._pair)),
repeat(seq(",", field('pair', alias($.inline_pair, $._pair)))),
),
),
"}",
),

grit_metavariable: ($) => token(prec(100, choice("µ...", /µ[a-zA-Z_][a-zA-Z0-9_]*/))),
},
});
Loading

0 comments on commit 1ecb2b4

Please sign in to comment.