@konomanoasa/tree-sitter-diagnostic-sh 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "konomanoasa-tree-sitter-diagnostic-sh"
3
- version = "0.8.0"
3
+ version = "0.9.1"
4
4
  description = "Tree-sitter grammar for diagnostics of POSIX sh."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -31,7 +31,7 @@ path = "test/bindings.test.rs"
31
31
  tree-sitter-language = "=0.1.9"
32
32
 
33
33
  [dev-dependencies]
34
- tree-sitter = "=0.27.1"
34
+ tree-sitter = "=0.27.2"
35
35
 
36
36
  [build-dependencies]
37
- cc = "=1.10.0"
37
+ cc = "=1.11.3"
package/PARSING_POLICY.md CHANGED
@@ -1,13 +1,11 @@
1
1
  # Parsing Policy
2
2
 
3
- The parser supports the mandatory syntax of [POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/V3_chap02.html). Optional syntax,
4
- optional features, and implementation-specific extensions are not supported.
5
- Where POSIX does not uniquely determine the syntax interpretation, the parser
6
- follows [dash 0.5.13.5](https://manpages.debian.org/experimental/dash/dash.1.en.html). Explicit POSIX requirements take precedence.
3
+ The parser follows [POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/V3_chap02.html)
4
+ mandatory syntax, excluding optional features and extensions. Syntax not
5
+ determined by POSIX follows [dash 0.5.13.5](https://manpages.debian.org/experimental/dash/dash.1.en.html).
7
6
 
8
- The following commands can affect how subsequent source is parsed. The parser
9
- recognizes these commands but does not apply their effects.
7
+ These commands are parsed without changing how subsequent source is parsed.
10
8
 
11
- | Command | Effects not applied |
12
- | ------------------ | --------------------------------------------------- |
13
- | `alias`, `unalias` | Alias substitution or changes to alias definitions. |
9
+ | Command | Effects not applied |
10
+ | ------------------ | ----------------------------------- |
11
+ | `alias`, `unalias` | Alias substitution and definitions. |
package/README.md CHANGED
@@ -22,8 +22,7 @@ npm install @konomanoasa/tree-sitter-diagnostic-sh
22
22
  ## Development
23
23
 
24
24
  ```sh
25
- npm install
26
- npm run build
25
+ npm ci
27
26
  npm test
28
27
  ```
29
28
 
package/binding.gyp ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_diagnostic_sh_binding",
5
+ "sources": [
6
+ "bindings/node/binding.cc",
7
+ "src/parser.c",
8
+ "src/scanner.c"
9
+ ],
10
+ "include_dirs": ["src"],
11
+ "dependencies": [
12
+ "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except"
13
+ ],
14
+ "cflags_c": ["-std=c17"],
15
+ "cflags_cc": ["-std=c++17"],
16
+ "msvs_settings": {
17
+ "VCCLCompilerTool": {
18
+ "AdditionalOptions": ["/utf-8"],
19
+ "AdditionalOptions!": ["-std:c++20"],
20
+ "LanguageStandard": "stdcpp17",
21
+ "LanguageStandard_C": "stdc17"
22
+ }
23
+ },
24
+ "target_conditions": [
25
+ [
26
+ "OS==\"mac\"",
27
+ {
28
+ "xcode_settings": {
29
+ "CLANG_CXX_LANGUAGE_STANDARD": "c++17",
30
+ "GCC_C_LANGUAGE_STANDARD": "c17",
31
+ "MACOSX_DEPLOYMENT_TARGET": "14.0"
32
+ }
33
+ }
34
+ ]
35
+ ]
36
+ }
37
+ ]
38
+ }
@@ -0,0 +1,25 @@
1
+ #include <napi.h>
2
+
3
+ struct TSLanguage;
4
+ extern "C" const TSLanguage *tree_sitter_diagnostic_sh();
5
+
6
+ namespace {
7
+
8
+ constexpr napi_type_tag language_type_tag = {
9
+ 0x8AF2E5212AD58ABF,
10
+ 0xD5006CAD83ABBA16
11
+ };
12
+
13
+ Napi::Object Init(Napi::Env env, Napi::Object exports) {
14
+ auto language = Napi::External<TSLanguage>::New(
15
+ env,
16
+ const_cast<TSLanguage *>(tree_sitter_diagnostic_sh())
17
+ );
18
+ language.TypeTag(&language_type_tag);
19
+ exports["language"] = language;
20
+ return exports;
21
+ }
22
+
23
+ }
24
+
25
+ NODE_API_MODULE(tree_sitter_diagnostic_sh_binding, Init)
@@ -0,0 +1,5 @@
1
+ import type { Language } from "tree-sitter";
2
+
3
+ declare const binding: Language;
4
+
5
+ export default binding;
@@ -0,0 +1,10 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { fileURLToPath } from "node:url";
3
+ import load from "node-gyp-build";
4
+
5
+ const binding = load(fileURLToPath(new URL("../..", import.meta.url)));
6
+ binding.nodeTypeInfo = JSON.parse(
7
+ readFileSync(new URL("../../src/node-types.json", import.meta.url), "utf8"),
8
+ );
9
+
10
+ export default binding;
@@ -1,6 +1,6 @@
1
1
  #include <Python.h>
2
2
 
3
- #include "tree_sitter/parser.h"
3
+ #include "../../src/tree_sitter/parser.h"
4
4
 
5
5
  const TSLanguage *tree_sitter_diagnostic_sh(void);
6
6
 
@@ -6,5 +6,4 @@ unsafe extern "C" {
6
6
 
7
7
  pub const LANGUAGE: LanguageFn =
8
8
  unsafe { LanguageFn::from_raw(tree_sitter_diagnostic_sh) };
9
-
10
9
  pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
package/grammar.js CHANGED
@@ -50,7 +50,6 @@ function issue($) {
50
50
  export default grammar({
51
51
  name: "diagnostic_sh",
52
52
  externals: ($) => tokens.map(({ name }) => $[name]),
53
- // Prevents lexer fallback from accepting EOF mid-input.
54
53
  extras: ($) => [$._unmatchable],
55
54
  rules: {
56
55
  program: ($) =>
package/package.json CHANGED
@@ -1,13 +1,23 @@
1
1
  {
2
2
  "name": "@konomanoasa/tree-sitter-diagnostic-sh",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "description": "Tree-sitter grammar for diagnostics of POSIX sh.",
5
5
  "license": "MIT",
6
6
  "repository": "git+https://github.com/konomanoasa/tree-sitter-diagnostic-sh.git",
7
7
  "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./bindings/node/index.d.ts",
11
+ "default": "./bindings/node/index.js"
12
+ }
13
+ },
8
14
  "files": [
9
15
  "Cargo.toml",
10
16
  "PARSING_POLICY.md",
17
+ "binding.gyp",
18
+ "bindings/node/*.cc",
19
+ "bindings/node/*.js",
20
+ "bindings/node/*.ts",
11
21
  "bindings/python/*.c",
12
22
  "bindings/python/*.py",
13
23
  "bindings/python/*.pyi",
@@ -27,22 +37,33 @@
27
37
  "access": "public"
28
38
  },
29
39
  "scripts": {
30
- "build": "npm run tree-sitter -- build-all",
40
+ "build": "node-gyp rebuild && node-gyp configure -- -f compile_commands_json",
41
+ "build:parser": "npm run tree-sitter -- build-all",
31
42
  "check": "biome check . && tsc",
32
43
  "format": "biome format --write .",
33
44
  "generate": "npm run tree-sitter -- generate-all",
34
- "test": "node --test --test-concurrency=1 test/build.test.js test/cst.test.js test/incremental.test.js test/parser.test.js && node scripts/check-c.js --test",
45
+ "install": "node-gyp-build",
46
+ "test": "node --test --test-concurrency=1 test/bindings.test.js test/build.test.js test/cst.test.js test/incremental.test.js test/parser.test.js && node scripts/check-c.js --test",
35
47
  "test:performance": "node --test test/performance.test.js",
36
48
  "test:sanitize": "node scripts/check-c.js --sanitize",
37
49
  "tree-sitter": "node scripts/tree-sitter.js"
38
50
  },
51
+ "dependencies": {
52
+ "node-addon-api": "8.9.2",
53
+ "node-gyp-build": "4.8.4"
54
+ },
55
+ "peerDependencies": {
56
+ "tree-sitter": "0.25.1"
57
+ },
39
58
  "devDependencies": {
40
- "@biomejs/biome": "2.6.2",
41
- "@types/node": "26.6.5",
42
- "tree-sitter-cli": "0.27.1",
59
+ "@biomejs/biome": "2.6.9",
60
+ "@types/node": "26.6.11",
61
+ "node-gyp": "13.1.0",
62
+ "tree-sitter-cli": "0.27.2",
43
63
  "typescript": "7.0.2"
44
64
  },
45
65
  "allowScripts": {
66
+ "tree-sitter": true,
46
67
  "tree-sitter-cli": true
47
68
  }
48
69
  }
package/pyproject.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "konomanoasa-tree-sitter-diagnostic-sh"
3
- version = "0.8.0"
3
+ version = "0.9.1"
4
4
  description = "Tree-sitter grammar for diagnostics of POSIX sh."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -12,11 +12,11 @@ Repository = "https://github.com/konomanoasa/tree-sitter-diagnostic-sh"
12
12
  [dependency-groups]
13
13
  dev = [
14
14
  "poethepoet==0.49.0",
15
- "ruff==0.17.3",
15
+ "ruff==0.17.6",
16
16
  "setuptools==84.2.1",
17
- "tree-sitter==0.26.0",
17
+ "tree-sitter==0.27.0",
18
18
  "twine==7.0.0",
19
- "ty==0.0.95",
19
+ "ty==0.0.112",
20
20
  ]
21
21
 
22
22
  [build-system]
package/src/parser.c CHANGED
@@ -15225,8 +15225,8 @@ TS_PUBLIC const TSLanguage *tree_sitter_diagnostic_sh(void) {
15225
15225
  .max_reserved_word_set_size = 0,
15226
15226
  .metadata = {
15227
15227
  .major_version = 0,
15228
- .minor_version = 8,
15229
- .patch_version = 0,
15228
+ .minor_version = 9,
15229
+ .patch_version = 1,
15230
15230
  },
15231
15231
  };
15232
15232
  return &language;
package/src/scanner.c CHANGED
@@ -104,7 +104,6 @@ struct Parser {
104
104
  uint32_t stops;
105
105
  bool resumed;
106
106
  size_t backquote_end;
107
- int32_t closer;
108
107
  };
109
108
  struct Scanner {
110
109
  struct Event *events;
@@ -1427,8 +1426,7 @@ static size_t consumed_offset(const struct Input *input) {
1427
1426
  static struct Unit read_raw(struct Input *input) {
1428
1427
  if (input->position >= input->count)
1429
1428
  return (struct Unit){-1, input->end, input->end};
1430
- struct Unit unit = input->units[input->position++];
1431
- return unit;
1429
+ return input->units[input->position++];
1432
1430
  }
1433
1431
 
1434
1432
  static struct Unit pop(struct Input *input) {
@@ -1752,8 +1750,6 @@ static void quoted(struct Parser *parser, struct Text *text, int32_t quote) {
1752
1750
  literal ? BEGIN_SINGLE_QUOTED : BEGIN_DOUBLE_QUOTED,
1753
1751
  offset(input)
1754
1752
  );
1755
- int32_t parent_closer = parser->closer;
1756
- parser->closer = quote;
1757
1753
  pop(input);
1758
1754
  while (!parser->plan->failed) {
1759
1755
  int32_t c = peek_quoted(input, literal);
@@ -1778,7 +1774,6 @@ static void quoted(struct Parser *parser, struct Text *text, int32_t quote) {
1778
1774
  if (literal || c != '\\' || !double_quote_escape(parser, text, unit))
1779
1775
  text_push(parser, text, unit);
1780
1776
  }
1781
- parser->closer = parent_closer;
1782
1777
  owner_end(parser, owner, consumed_offset(input));
1783
1778
  unnest(parser);
1784
1779
  }
@@ -1864,9 +1859,8 @@ static void parameter_operator(
1864
1859
 
1865
1860
  static struct ParameterHead parameter_head(struct Parser *parser) {
1866
1861
  struct Input *input = &parser->input;
1867
- struct ParameterHead head =
1868
- {{TOKEN_TYPE_COUNT, offset(input), 0, 0}, false, false};
1869
- head.fault.end = head.fault.at;
1862
+ size_t at = offset(input);
1863
+ struct ParameterHead head = {{TOKEN_TYPE_COUNT, at, at, 0}, false, false};
1870
1864
  bool hash = peek(input) == '#';
1871
1865
  bool length = hash && length_operator(input);
1872
1866
  if (length) pop(input);
@@ -1926,8 +1920,6 @@ parameter_word(struct Parser *parser, bool double_quoted, struct Fault *fault) {
1926
1920
  static void parameter(struct Parser *parser, bool double_quoted) {
1927
1921
  struct Input *input = &parser->input;
1928
1922
  pop(input);
1929
- int32_t parent_closer = parser->closer;
1930
- parser->closer = '}';
1931
1923
  struct ParameterHead head = parameter_head(parser);
1932
1924
  struct Fault fault = head.fault;
1933
1925
  if (head.operand && head.pattern) double_quoted = false;
@@ -1952,7 +1944,6 @@ static void parameter(struct Parser *parser, bool double_quoted) {
1952
1944
  if (peek(input) == '}') pop(input);
1953
1945
  else if (fault.kind == TOKEN_TYPE_COUNT)
1954
1946
  input_end_gap(parser, PARAMETER_ENDING_GAP);
1955
- parser->closer = parent_closer;
1956
1947
  }
1957
1948
 
1958
1949
  static int hex_value(int32_t c) {
@@ -2043,7 +2034,8 @@ read_dollar_escape(struct Input *input, struct Unit slash) {
2043
2034
  read_raw(input);
2044
2035
  if (c == '\\') read_raw(input);
2045
2036
  escape.count = encode_value(c, escape.bytes);
2046
- escape.bytes[0] = c == '?' ? 127 : escape.bytes[0] & 31;
2037
+ escape.bytes[0] = escape.bytes[0] < 0x40 ? escape.bytes[0] | 0x40
2038
+ : escape.bytes[0] & 0x1f;
2047
2039
  }
2048
2040
  } else {
2049
2041
  const char *keys = "abefnrtv\"'\\";
@@ -2101,14 +2093,14 @@ static void dollar_quote(struct Parser *parser, struct Text *text) {
2101
2093
 
2102
2094
  struct BackquoteInput {
2103
2095
  struct Input *input;
2104
- bool quoted, literal_next;
2096
+ bool literal_next;
2105
2097
  };
2106
2098
 
2107
2099
  static struct Unit backquote_next(struct BackquoteInput *reader) {
2108
2100
  struct Input *input = reader->input;
2109
2101
  while (input->position < input->count) {
2110
2102
  struct Unit unit = input->units[input->position++];
2111
- if (!reader->quoted || reader->literal_next) {
2103
+ if (reader->literal_next) {
2112
2104
  reader->literal_next = false;
2113
2105
  return unit;
2114
2106
  }
@@ -2131,7 +2123,7 @@ static size_t backquote_body(
2131
2123
  size_t *end
2132
2124
  ) {
2133
2125
  struct Input *input = &parser->input;
2134
- struct BackquoteInput reader = {.input = input, .quoted = double_quoted};
2126
+ struct BackquoteInput reader = {.input = input};
2135
2127
  while (!parser->plan->failed && input->position < input->count) {
2136
2128
  struct Unit unit = backquote_next(&reader);
2137
2129
  if (unit.value < 0) break;
@@ -2347,16 +2339,16 @@ static void expansion(
2347
2339
  static void escaped_character(struct Parser *parser, struct Token *token) {
2348
2340
  struct Input *input = &parser->input;
2349
2341
  struct Unit slash = pop(input);
2342
+ bool complete = raw(input, input->position) >= 0;
2350
2343
  if (
2351
- raw(input, input->position) < 0 &&
2344
+ !complete &&
2352
2345
  (parser->backquote_end || input->end == parser->plan->source_end)
2353
2346
  ) {
2354
2347
  text_push(parser, &token->text, slash);
2355
2348
  return;
2356
2349
  }
2357
2350
  size_t owner = owner_begin(parser, BEGIN_ESCAPED_CHARACTER, slash.start);
2358
- if (raw(input, input->position) >= 0)
2359
- text_push(parser, &token->text, escaped_unit(input, slash));
2351
+ if (complete) text_push(parser, &token->text, escaped_unit(input, slash));
2360
2352
  else
2361
2353
  issue(
2362
2354
  parser,
@@ -2497,11 +2489,22 @@ static struct Fault token_fault(
2497
2489
  return fault;
2498
2490
  }
2499
2491
 
2492
+ static void text_bytes(
2493
+ struct Parser *parser,
2494
+ struct Text *text,
2495
+ const uint8_t *bytes,
2496
+ size_t count,
2497
+ size_t start,
2498
+ size_t end
2499
+ ) {
2500
+ for (size_t i = 0; i < count; i++)
2501
+ text_push(parser, text, (struct Unit){bytes[i], start, end});
2502
+ }
2503
+
2500
2504
  static void encode(struct Parser *parser, struct Text *text, struct Unit unit) {
2501
2505
  uint8_t bytes[4];
2502
2506
  size_t count = encode_value(unit.value, bytes);
2503
- for (size_t i = 0; i < count; i++)
2504
- text_push(parser, text, (struct Unit){bytes[i], unit.start, unit.end});
2507
+ text_bytes(parser, text, bytes, count, unit.start, unit.end);
2505
2508
  }
2506
2509
 
2507
2510
  enum DelimiterQuote {
@@ -2547,16 +2550,19 @@ static struct DelimiterGroup *delimiter_top(struct DelimiterReader *reader) {
2547
2550
  return reader->count ? &reader->groups[reader->count - 1] : NULL;
2548
2551
  }
2549
2552
 
2553
+ static bool delimiter_nest(const struct DelimiterReader *reader) {
2554
+ struct Plan *plan = reader->parser->plan;
2555
+ if (plan->depth + reader->count + reader->quotes < NESTING_LIMIT) return true;
2556
+ plan->failed = true;
2557
+ return false;
2558
+ }
2559
+
2550
2560
  static bool delimiter_push(
2551
2561
  struct DelimiterReader *reader,
2552
2562
  enum DelimiterGroupKind kind,
2553
2563
  int32_t closing
2554
2564
  ) {
2555
- struct Plan *plan = reader->parser->plan;
2556
- if (plan->depth + reader->count + reader->quotes >= NESTING_LIMIT) {
2557
- plan->failed = true;
2558
- return false;
2559
- }
2565
+ if (!delimiter_nest(reader)) return false;
2560
2566
  struct DelimiterGroup *grown = diagnostic_grow(
2561
2567
  reader->groups,
2562
2568
  &reader->capacity,
@@ -2564,7 +2570,7 @@ static bool delimiter_push(
2564
2570
  sizeof(*grown)
2565
2571
  );
2566
2572
  if (!grown) {
2567
- plan->failed = true;
2573
+ reader->parser->plan->failed = true;
2568
2574
  return false;
2569
2575
  }
2570
2576
  reader->groups = grown;
@@ -2590,11 +2596,18 @@ static void delimiter_pop(struct DelimiterReader *reader) {
2590
2596
  reader->quotes = group->quotes;
2591
2597
  }
2592
2598
 
2599
+ static size_t delimiter_outer(
2600
+ const struct DelimiterReader *reader,
2601
+ enum DelimiterGroupKind skipped
2602
+ ) {
2603
+ size_t depth = reader->count;
2604
+ while (depth && reader->groups[depth - 1].kind == skipped) depth--;
2605
+ return depth;
2606
+ }
2607
+
2593
2608
  static bool
2594
2609
  delimiter_close(struct DelimiterReader *reader, enum DelimiterGroupKind kind) {
2595
- size_t depth = reader->count;
2596
- while (depth && reader->groups[depth - 1].kind == DELIMITER_PARAMETER)
2597
- depth--;
2610
+ size_t depth = delimiter_outer(reader, DELIMITER_PARAMETER);
2598
2611
  if (!depth || reader->groups[depth - 1].kind != kind) return false;
2599
2612
  delimiter_copy(reader);
2600
2613
  while (reader->count >= depth) delimiter_pop(reader);
@@ -2605,10 +2618,7 @@ static void
2605
2618
  delimiter_single_quote(struct DelimiterReader *reader, bool dollar) {
2606
2619
  struct Parser *parser = reader->parser;
2607
2620
  struct Input *input = &parser->input;
2608
- if (parser->plan->depth + reader->count + reader->quotes >= NESTING_LIMIT) {
2609
- parser->plan->failed = true;
2610
- return;
2611
- }
2621
+ if (!delimiter_nest(reader)) return;
2612
2622
  reader->document.quoted = true;
2613
2623
  pop(input);
2614
2624
  bool terminated = false;
@@ -2627,15 +2637,18 @@ delimiter_single_quote(struct DelimiterReader *reader, bool dollar) {
2627
2637
  escape.source.end,
2628
2638
  parser->owner
2629
2639
  };
2630
- for (size_t i = 0; i < escape.count && !terminated; i++) {
2631
- if (escape.bytes[i] == 0) terminated = true;
2632
- else
2633
- text_push(
2634
- parser,
2635
- &reader->document.delimiter,
2636
- (struct Unit){escape.bytes[i], unit.start, escape.source.end}
2637
- );
2638
- }
2640
+ size_t count = 0;
2641
+ while (count < escape.count && escape.bytes[count]) count++;
2642
+ if (!terminated)
2643
+ text_bytes(
2644
+ parser,
2645
+ &reader->document.delimiter,
2646
+ escape.bytes,
2647
+ count,
2648
+ unit.start,
2649
+ escape.source.end
2650
+ );
2651
+ if (count < escape.count) terminated = true;
2639
2652
  } else if (!terminated) delimiter_encode(reader, unit);
2640
2653
  }
2641
2654
  reader->incomplete = true;
@@ -2691,12 +2704,14 @@ static void delimiter_parameter_prefix(struct DelimiterReader *reader) {
2691
2704
  struct Unit unit = pop(input);
2692
2705
  uint8_t bytes[4];
2693
2706
  size_t length = encode_value(c, bytes);
2694
- for (size_t i = 1; i < length; i++)
2695
- text_push(
2696
- reader->parser,
2697
- &reader->document.delimiter,
2698
- (struct Unit){bytes[i], unit.start, unit.end}
2699
- );
2707
+ text_bytes(
2708
+ reader->parser,
2709
+ &reader->document.delimiter,
2710
+ bytes + 1,
2711
+ length - 1,
2712
+ unit.start,
2713
+ unit.end
2714
+ );
2700
2715
  return;
2701
2716
  }
2702
2717
  bool name = is_name_start_character(c), positional = is_decimal_digit(c);
@@ -2740,10 +2755,33 @@ static void delimiter_dollar(struct DelimiterReader *reader) {
2740
2755
  }
2741
2756
  }
2742
2757
 
2743
- static bool delimiter_in_parameter(struct DelimiterReader *reader) {
2744
- size_t depth = reader->count;
2745
- while (depth && reader->groups[depth - 1].kind == DELIMITER_DOUBLE_QUOTE)
2746
- depth--;
2758
+ static void delimiter_double_quote(struct DelimiterReader *reader) {
2759
+ pop(&reader->parser->input);
2760
+ switch (reader->quote) {
2761
+ case DELIMITER_PARAMETER_QUOTED:
2762
+ if (delimiter_push(reader, DELIMITER_DOUBLE_QUOTE, '"'))
2763
+ reader->quote = DELIMITER_DOUBLE_QUOTED;
2764
+ break;
2765
+ case DELIMITER_DOUBLE_QUOTED:
2766
+ reader->document.quoted = true;
2767
+ if (
2768
+ reader->count && delimiter_top(reader)->kind == DELIMITER_DOUBLE_QUOTE
2769
+ )
2770
+ delimiter_pop(reader);
2771
+ else {
2772
+ reader->quote = DELIMITER_UNQUOTED;
2773
+ reader->quotes--;
2774
+ }
2775
+ break;
2776
+ default:
2777
+ if (!delimiter_nest(reader)) return;
2778
+ reader->quote = DELIMITER_DOUBLE_QUOTED;
2779
+ reader->quotes++;
2780
+ }
2781
+ }
2782
+
2783
+ static bool delimiter_in_parameter(const struct DelimiterReader *reader) {
2784
+ size_t depth = delimiter_outer(reader, DELIMITER_DOUBLE_QUOTE);
2747
2785
  return depth && reader->groups[depth - 1].kind == DELIMITER_PARAMETER;
2748
2786
  }
2749
2787
 
@@ -2793,40 +2831,14 @@ static bool delimiter_step(struct DelimiterReader *reader) {
2793
2831
  reader->quote == DELIMITER_DOUBLE_QUOTED ||
2794
2832
  reader->quote == DELIMITER_PARAMETER_QUOTED
2795
2833
  ) {
2796
- if (c != '"') delimiter_copy(reader);
2797
- else {
2798
- pop(input);
2799
- if (reader->quote == DELIMITER_PARAMETER_QUOTED) {
2800
- if (delimiter_push(reader, DELIMITER_DOUBLE_QUOTE, '"'))
2801
- reader->quote = DELIMITER_DOUBLE_QUOTED;
2802
- } else {
2803
- reader->document.quoted = true;
2804
- if (
2805
- reader->count && delimiter_top(reader)->kind == DELIMITER_DOUBLE_QUOTE
2806
- )
2807
- delimiter_pop(reader);
2808
- else {
2809
- reader->quote = DELIMITER_UNQUOTED;
2810
- reader->quotes--;
2811
- }
2812
- }
2813
- }
2834
+ if (c == '"') delimiter_double_quote(reader);
2835
+ else delimiter_copy(reader);
2814
2836
  } else if (!reader->count && separator(c)) return false;
2815
2837
  else if (reader->quote != DELIMITER_ARITHMETIC_QUOTED && c == '\'')
2816
2838
  delimiter_single_quote(reader, false);
2817
- else if (reader->quote != DELIMITER_ARITHMETIC_QUOTED && c == '"') {
2818
- if (
2819
- reader->parser->plan->depth + reader->count + reader->quotes >=
2820
- NESTING_LIMIT
2821
- ) {
2822
- reader->parser->plan->failed = true;
2823
- return false;
2824
- }
2825
- pop(input);
2826
- reader->quote = DELIMITER_DOUBLE_QUOTED;
2827
- reader->quotes++;
2828
- } else if (c == ')' && delimiter_close(reader, DELIMITER_COMMAND))
2829
- return true;
2839
+ else if (reader->quote != DELIMITER_ARITHMETIC_QUOTED && c == '"')
2840
+ delimiter_double_quote(reader);
2841
+ else if (c == ')' && delimiter_close(reader, DELIMITER_COMMAND)) return true;
2830
2842
  else if (reader->count && c == delimiter_top(reader)->closing) {
2831
2843
  if (delimiter_top(reader)->kind == DELIMITER_ARITHMETIC) {
2832
2844
  delimiter_copy(reader);
@@ -2925,6 +2937,10 @@ static void here_document_line(
2925
2937
  }
2926
2938
  }
2927
2939
 
2940
+ static bool line_complete(const struct Text *line) {
2941
+ return line->count && line->units[line->count - 1].value == '\n';
2942
+ }
2943
+
2928
2944
  static bool here_document_end_line(
2929
2945
  struct Parser *parser,
2930
2946
  const struct Document *here,
@@ -2939,7 +2955,7 @@ static bool here_document_end_line(
2939
2955
  bytes->count = 0;
2940
2956
  for (size_t i = *first; i < line->count && line->units[i].value != '\n'; i++)
2941
2957
  encode(parser, bytes, line->units[i]);
2942
- bool newline = line->count && line->units[line->count - 1].value == '\n';
2958
+ bool newline = line_complete(line);
2943
2959
  bool source_eof = parser->input.position == parser->input.count &&
2944
2960
  parser->input.end == parser->plan->source_end;
2945
2961
  bool found = bytes->count == here->delimiter.count &&
@@ -2965,7 +2981,7 @@ static bool here_document_lines(
2965
2981
  here_document_line(parser, here, &line);
2966
2982
  size_t first;
2967
2983
  found = here_document_end_line(parser, here, &line, &first, &bytes);
2968
- *at_line_start = line.count && line.units[line.count - 1].value == '\n';
2984
+ *at_line_start = line_complete(&line);
2969
2985
  if (found) *boundary = line_start;
2970
2986
  else
2971
2987
  for (size_t i = first; i < line.count; i++)
@@ -2994,7 +3010,6 @@ static bool here_document(
2994
3010
  ts_free(body.units);
2995
3011
  bool accepted_eof =
2996
3012
  input->end == parser->plan->source_end || parser->backquote_end != 0;
2997
- // Keep unstarted-body gaps out of outer bodies.
2998
3013
  if (!found && !accepted_eof)
2999
3014
  report(
3000
3015
  parser,
@@ -3006,6 +3021,12 @@ static bool here_document(
3006
3021
  return at_line_start;
3007
3022
  }
3008
3023
 
3024
+ static void clear_documents(struct Parser *parser) {
3025
+ for (size_t i = 0; i < parser->document_count; i++)
3026
+ ts_free(parser->documents[i].delimiter.units);
3027
+ parser->document_count = 0;
3028
+ }
3029
+
3009
3030
  static void documents(struct Parser *parser, bool at_line_start) {
3010
3031
  struct Input *input = &parser->input;
3011
3032
  size_t body_start = at_line_start ? consumed_offset(input) : input->end;
@@ -3019,9 +3040,7 @@ static void documents(struct Parser *parser, bool at_line_start) {
3019
3040
  at_line_start = here_document(parser, here, at_line_start, body_start);
3020
3041
  body_start = consumed_offset(input);
3021
3042
  }
3022
- for (size_t document = 0; document < parser->document_count; document++)
3023
- ts_free(parser->documents[document].delimiter.units);
3024
- parser->document_count = 0;
3043
+ clear_documents(parser);
3025
3044
  }
3026
3045
 
3027
3046
  static void take(struct Parser *parser) {
@@ -3250,7 +3269,7 @@ enum RecoveryBoundary {
3250
3269
  };
3251
3270
 
3252
3271
  static bool redirect_operand(struct Parser *parser, enum Lexeme kind) {
3253
- if ((kind == HERE || kind == HERE_STRIP) && !parser->ready)
3272
+ if (kind == HERE || kind == HERE_STRIP)
3254
3273
  declare_here_document(parser, kind == HERE_STRIP);
3255
3274
  bool word = look(parser)->kind == WORD;
3256
3275
  if (word) take(parser);
@@ -3843,8 +3862,7 @@ static void parse(struct Parser *parser) {
3843
3862
 
3844
3863
  static void clear_parser(struct Parser *parser) {
3845
3864
  ts_free(parser->token.text.units);
3846
- for (size_t i = 0; i < parser->document_count; i++)
3847
- ts_free(parser->documents[i].delimiter.units);
3865
+ clear_documents(parser);
3848
3866
  ts_free(parser->documents);
3849
3867
  }
3850
3868
 
@@ -1,20 +1,18 @@
1
+ use konomanoasa_tree_sitter_diagnostic_sh as grammar;
1
2
  use tree_sitter::Parser;
2
3
 
3
4
  #[test]
4
5
  fn parses_source_and_exposes_faults() {
5
- let language = konomanoasa_tree_sitter_diagnostic_sh::LANGUAGE.into();
6
+ let source = "cat >\n";
7
+ let language = grammar::LANGUAGE.into();
6
8
  let mut parser = Parser::new();
7
9
  parser.set_language(&language).unwrap();
8
10
  assert_eq!(language.name(), Some("diagnostic_sh"));
9
- let source = "cat >\n";
10
11
  let tree = parser.parse(source, None).unwrap();
11
12
  let root = tree.root_node();
12
13
  assert_eq!(root.kind(), "program");
13
- assert!(!root.has_error());
14
14
  assert_eq!(root.byte_range(), 0..source.len());
15
+ assert!(!root.has_error());
15
16
  assert!(root.to_sexp().contains("fault: (redirection_target_gap)"));
16
- assert!(
17
- konomanoasa_tree_sitter_diagnostic_sh::NODE_TYPES
18
- .contains("\"redirection_target_gap\"")
19
- );
17
+ assert!(grammar::NODE_TYPES.contains("\"redirection_target_gap\""));
20
18
  }
package/tree-sitter.json CHANGED
@@ -6,14 +6,11 @@
6
6
  "title": "Diagnostic POSIX sh",
7
7
  "scope": "source.diagnostic-sh",
8
8
  "path": ".",
9
- "external-files": [
10
- "src/tokens.h",
11
- "src/tokens.json"
12
- ]
9
+ "external-files": ["src/tokens.h", "src/tokens.json"]
13
10
  }
14
11
  ],
15
12
  "metadata": {
16
- "version": "0.8.0",
13
+ "version": "0.9.1",
17
14
  "license": "MIT",
18
15
  "description": "Tree-sitter grammar for diagnostics of POSIX sh.",
19
16
  "links": {
@@ -23,7 +20,7 @@
23
20
  "bindings": {
24
21
  "c": false,
25
22
  "go": false,
26
- "node": false,
23
+ "node": true,
27
24
  "python": true,
28
25
  "rust": true,
29
26
  "swift": false