cspell-grammar 0.1.5 → 6.1.3

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.
Files changed (135) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +96 -11
  3. package/bin.js +6 -0
  4. package/dist/app.d.ts +7 -0
  5. package/dist/app.js +67 -0
  6. package/dist/grammars/index.d.ts +3 -0
  7. package/dist/grammars/index.js +29 -0
  8. package/dist/grammars/markdown.d.ts +3 -0
  9. package/dist/grammars/markdown.js +36 -0
  10. package/dist/grammars/simple.d.ts +3 -0
  11. package/dist/grammars/simple.js +112 -0
  12. package/dist/grammars/typescript.d.ts +3 -0
  13. package/dist/grammars/typescript.js +165 -0
  14. package/dist/index.d.ts +3 -2
  15. package/dist/index.js +5 -5
  16. package/dist/mappers/appendMappedText.d.ts +3 -0
  17. package/dist/mappers/appendMappedText.js +45 -0
  18. package/dist/mappers/types.d.ts +17 -0
  19. package/dist/{visualize → mappers}/types.js +0 -0
  20. package/dist/mappers/typescript.d.ts +6 -0
  21. package/dist/mappers/typescript.js +147 -0
  22. package/dist/parser/grammar.d.ts +5 -0
  23. package/dist/parser/grammar.js +9 -0
  24. package/dist/parser/grammarDefinition.d.ts +113 -0
  25. package/dist/parser/grammarDefinition.js +3 -0
  26. package/dist/parser/grammarNormalized.d.ts +114 -0
  27. package/dist/parser/grammarNormalized.js +3 -0
  28. package/dist/parser/grammarNormalizer.d.ts +13 -0
  29. package/dist/parser/grammarNormalizer.js +267 -0
  30. package/dist/parser/grammarTypesHelpers.d.ts +7 -0
  31. package/dist/parser/grammarTypesHelpers.js +32 -0
  32. package/dist/parser/index.d.ts +5 -0
  33. package/dist/parser/index.js +9 -0
  34. package/dist/parser/matchResult.d.ts +13 -0
  35. package/dist/parser/matchResult.js +56 -0
  36. package/dist/parser/parser.d.ts +12 -0
  37. package/dist/parser/parser.js +32 -0
  38. package/dist/parser/processors/procMatchingRule.d.ts +21 -0
  39. package/dist/parser/processors/procMatchingRule.js +155 -0
  40. package/dist/parser/scope.d.ts +41 -0
  41. package/dist/parser/scope.js +78 -0
  42. package/dist/parser/tokenizeLine.d.ts +6 -0
  43. package/dist/parser/tokenizeLine.js +131 -0
  44. package/dist/parser/types.d.ts +95 -0
  45. package/dist/parser/types.js +3 -0
  46. package/dist/parser/util.d.ts +2 -0
  47. package/dist/parser/util.js +8 -0
  48. package/dist/parser/validateGrammar.d.ts +12 -0
  49. package/dist/parser/validateGrammar.js +64 -0
  50. package/dist/parsers/typescript/TypeScriptParser.d.ts +2 -0
  51. package/dist/parsers/typescript/TypeScriptParser.js +93 -0
  52. package/dist/parsers/typescript/index.d.ts +2 -0
  53. package/dist/parsers/typescript/index.js +6 -0
  54. package/dist/viewer/escapeMarkdown.d.ts +3 -0
  55. package/dist/viewer/escapeMarkdown.js +45 -0
  56. package/dist/viewer/markdownHelper.d.ts +2 -0
  57. package/dist/viewer/markdownHelper.js +9 -0
  58. package/dist/viewer/visualizeAsMD.d.ts +5 -0
  59. package/dist/viewer/visualizeAsMD.js +38 -0
  60. package/package.json +58 -77
  61. package/CHANGELOG.md +0 -4
  62. package/dist/fixtures/fixtures.d.ts +0 -16
  63. package/dist/fixtures/fixtures.js +0 -41
  64. package/dist/fixtures/fixtures.js.map +0 -1
  65. package/dist/fixtures/index.d.ts +0 -1
  66. package/dist/fixtures/index.js +0 -7
  67. package/dist/fixtures/index.js.map +0 -1
  68. package/dist/grammar/grammar.d.ts +0 -31
  69. package/dist/grammar/grammar.js +0 -95
  70. package/dist/grammar/grammar.js.map +0 -1
  71. package/dist/grammar/grammarDefinition.d.ts +0 -66
  72. package/dist/grammar/grammarDefinition.js +0 -6
  73. package/dist/grammar/grammarDefinition.js.map +0 -1
  74. package/dist/grammar/grammarFiles.d.ts +0 -2
  75. package/dist/grammar/grammarFiles.js +0 -30
  76. package/dist/grammar/grammarFiles.js.map +0 -1
  77. package/dist/grammar/index.d.ts +0 -4
  78. package/dist/grammar/index.js +0 -9
  79. package/dist/grammar/index.js.map +0 -1
  80. package/dist/grammar/pattern.d.ts +0 -10
  81. package/dist/grammar/pattern.js +0 -76
  82. package/dist/grammar/pattern.js.map +0 -1
  83. package/dist/grammar/regexpUtil.d.ts +0 -15
  84. package/dist/grammar/regexpUtil.js +0 -101
  85. package/dist/grammar/regexpUtil.js.map +0 -1
  86. package/dist/grammar/registry.d.ts +0 -11
  87. package/dist/grammar/registry.js +0 -39
  88. package/dist/grammar/registry.js.map +0 -1
  89. package/dist/grammar/tokenize.d.ts +0 -41
  90. package/dist/grammar/tokenize.js +0 -374
  91. package/dist/grammar/tokenize.js.map +0 -1
  92. package/dist/grammar/tokenizeFile.d.ts +0 -5
  93. package/dist/grammar/tokenizeFile.js +0 -14
  94. package/dist/grammar/tokenizeFile.js.map +0 -1
  95. package/dist/index.js.map +0 -1
  96. package/dist/util/cacheMap.d.ts +0 -14
  97. package/dist/util/cacheMap.js +0 -28
  98. package/dist/util/cacheMap.js.map +0 -1
  99. package/dist/util/display.d.ts +0 -3
  100. package/dist/util/display.js +0 -37
  101. package/dist/util/display.js.map +0 -1
  102. package/dist/visualize/index.d.ts +0 -5
  103. package/dist/visualize/index.js +0 -10
  104. package/dist/visualize/index.js.map +0 -1
  105. package/dist/visualize/tokenColorizer.d.ts +0 -23
  106. package/dist/visualize/tokenColorizer.js +0 -76
  107. package/dist/visualize/tokenColorizer.js.map +0 -1
  108. package/dist/visualize/tokenizeToAnsi.d.ts +0 -5
  109. package/dist/visualize/tokenizeToAnsi.js +0 -79
  110. package/dist/visualize/tokenizeToAnsi.js.map +0 -1
  111. package/dist/visualize/tokenizeToMd.d.ts +0 -4
  112. package/dist/visualize/tokenizeToMd.js +0 -41
  113. package/dist/visualize/tokenizeToMd.js.map +0 -1
  114. package/dist/visualize/types.d.ts +0 -3
  115. package/dist/visualize/types.js.map +0 -1
  116. package/src/.DS_Store +0 -0
  117. package/src/fixtures/fixtures.ts +0 -65
  118. package/src/fixtures/index.ts +0 -1
  119. package/src/grammar/grammar.ts +0 -121
  120. package/src/grammar/grammarDefinition.ts +0 -82
  121. package/src/grammar/grammarFiles.ts +0 -30
  122. package/src/grammar/index.ts +0 -4
  123. package/src/grammar/pattern.ts +0 -82
  124. package/src/grammar/regexpUtil.ts +0 -128
  125. package/src/grammar/registry.ts +0 -40
  126. package/src/grammar/tokenize.ts +0 -453
  127. package/src/grammar/tokenizeFile.ts +0 -19
  128. package/src/index.ts +0 -2
  129. package/src/util/cacheMap.ts +0 -35
  130. package/src/util/display.ts +0 -38
  131. package/src/visualize/index.ts +0 -5
  132. package/src/visualize/tokenColorizer.ts +0 -105
  133. package/src/visualize/tokenizeToAnsi.ts +0 -92
  134. package/src/visualize/tokenizeToMd.ts +0 -43
  135. package/src/visualize/types.ts +0 -5
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 Jason Dent
3
+ Copyright (c) 2017 Jason Dent
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,15 +1,100 @@
1
- # Grammar Parser
2
- tmLanguage Grammar parser for cSpell
1
+ # `cspell-grammar`
3
2
 
4
- **WIP** -- This is a *Work In Progress*
3
+ CSpell Grammar is used to generate a parser. The Parser is used to add context / scope to parts of a document, making it easier to define the parts to spell spell checked.
5
4
 
6
- This language parser support tmLanguage style grammars.
5
+ This is to address the issues and limitations related to `ignoreRegExpList` and `includeRegExpList`.
7
6
 
8
- ## Notes
7
+ The parser is use to add `scope` to sections of a document. The `scope` can then be used to apply spell checking rules.
9
8
 
10
- - Works ONLY with Node 8 and above or Javascript engines that support RegExp look back.
11
- - Most tmLanguage grammars use RegExp based upon Oniguruma engine. This grammar engine attempts to
12
- simulate the features of Oniguruma, but it may fail. At the grammar engine's core, XRegExp is used.
13
- So any limitations in XRegExp are also limitations in the grammar.
14
- - `\A`, `\G`, `\Z`, `\z` flags are simulated in a best attempt, but it is not possible to get them
15
- exactly right.
9
+ Example: Only check comments and strings
10
+
11
+ ```yaml
12
+ rules:
13
+ '*': false
14
+ comment: true
15
+ string: true
16
+ ```
17
+
18
+ It can be even more powerful like controlling the language settings based upon scope.
19
+
20
+ ```yaml
21
+ rules:
22
+ comment:
23
+ language: en
24
+ string:
25
+ language: en,fr
26
+ dictionaries: ['marketing-terms'],
27
+ caseSensitive: true
28
+ string.javascript:
29
+ caseSensitive: false
30
+ ```
31
+
32
+ Rules are applied in the order they match the scope of the text.
33
+
34
+ When checking JavaScript files with the above example rules:
35
+
36
+ - strings will:
37
+ - use the locale `en,fr`
38
+ - the `marketing-terms` dictionary will be enabled
39
+ - `caseSensitive` will be `true`
40
+ - everything else:
41
+ - locale: `en`
42
+ - `caseSensitive` will be `false`
43
+
44
+ At its core, `cspell-grammar` uses a simplified form of the TextMate grammar.
45
+
46
+ ## Reasoning
47
+
48
+ Why use a grammar parser? Couldn't a colorizer / highlighter or a language AST be used?
49
+ At one level, needs of the spell checker are simpler and different from colorizers or language AST parsers.
50
+ The goal of a spell checker is to spell check **_relevant_** text. The spell check does not need to care about
51
+ the syntactical correctness of a document or presentation.
52
+
53
+ The goal of a grammar parser for the spell checker is to allow the user to decide:
54
+
55
+ 1. What text should be checked.
56
+ 1. Which dictionaries (or languages) should be used.
57
+ 1. Are accents and case important
58
+
59
+ Note: CSpell is a pure JavaScript application, so including the Oniguruma is not an option.
60
+
61
+ ### Considerations
62
+
63
+ - Parsing a document should be fast - meaning the grammar should be as simple as possible to meet
64
+ the needs of the spell checker and not focus on scope detail. This is where a colorizer grammar is
65
+ not a good fit to be used.
66
+ - AST's are a bit of an overkill for a spell checker. They provide too much detail while not bringing much benefit
67
+ from the detail.
68
+
69
+ ## Transformation
70
+
71
+ Consider the following bit of LaTeX:
72
+
73
+ ```latex
74
+ k\"{o}nnen
75
+ können
76
+ ```
77
+
78
+ <!--- cspell:ignore können nnen -->
79
+
80
+ For the spell checker to work correctly, the `\"{o}` should be transformed into `ö` before it is checked against the German dictionary.
81
+
82
+ This creates a few challenges.
83
+
84
+ Possible options:
85
+
86
+ 1. Simple whole document substitution
87
+ - Challenges
88
+ - It is not context aware and might replace the wrong text.
89
+ - It changes the location of the words and messes up issue reporting (some sort of Map would be needed to get the correct line / character offset).
90
+
91
+ - Advantages
92
+ - Easy to implement except for the context and mapping.
93
+
94
+ 1. Scope level substitution
95
+ Transformations occur at the scope level.
96
+ - Challenges
97
+ - offset mapping is still and issue (maybe)
98
+ - need a way to merge text with adjacent scopes after transformation
99
+ - Advantages
100
+ - it is context aware
package/bin.js ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const app = require('./dist/app.js');
5
+
6
+ app.run(process.argv);
package/dist/app.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Run simple parser
3
+ * @param args -- command line arguments
4
+ * @returns Promise
5
+ */
6
+ export declare function run(args: string[]): Promise<void>;
7
+ //# sourceMappingURL=app.d.ts.map
package/dist/app.js ADDED
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.run = void 0;
27
+ const fs_1 = require("fs");
28
+ const path = __importStar(require("path"));
29
+ const typescript_1 = require("./parsers/typescript");
30
+ const parsers = {
31
+ '.ts': typescript_1.parser,
32
+ };
33
+ /**
34
+ * Run simple parser
35
+ * @param args -- command line arguments
36
+ * @returns Promise
37
+ */
38
+ async function run(args) {
39
+ // early out if there are not enough arguments
40
+ if (args.length < 3) {
41
+ console.log('usage...');
42
+ return;
43
+ }
44
+ const filename = args.slice(2).filter((p) => !p.startsWith('-'))[0];
45
+ if (!filename) {
46
+ console.log('filename missing');
47
+ return;
48
+ }
49
+ const ext = path.extname(filename);
50
+ const parser = parsers[ext];
51
+ if (!parser) {
52
+ console.log(`No parser for ${path.basename(filename)}`);
53
+ return;
54
+ }
55
+ console.log(`File: ${path.basename(filename)} Parser: ${parser.name}`);
56
+ const content = await fs_1.promises.readFile(filename, 'utf-8');
57
+ const result = parser.parse(content, filename);
58
+ for (const pt of result.parsedTexts) {
59
+ emit(pt);
60
+ }
61
+ }
62
+ exports.run = run;
63
+ function emit(pt) {
64
+ const t = pt.text.replace(/\t/g, '↦').replace(/\r?\n/g, '↩︎').replace(/\r/g, '⇠');
65
+ console.log(`${pt.range[0]}-${pt.range[1]}\t${t}\t${pt.scope?.toString() || ''}`);
66
+ }
67
+ //# sourceMappingURL=app.js.map
@@ -0,0 +1,3 @@
1
+ export * as TypeScript from './typescript';
2
+ export * as Markdown from './markdown';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Markdown = exports.TypeScript = void 0;
27
+ exports.TypeScript = __importStar(require("./typescript"));
28
+ exports.Markdown = __importStar(require("./markdown"));
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { GrammarDef } from '..';
2
+ export declare const grammar: GrammarDef;
3
+ //# sourceMappingURL=markdown.d.ts.map
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grammar = void 0;
4
+ const repository = {
5
+ frontMatter: { patterns: [] },
6
+ block: { patterns: ['#raw'] },
7
+ fenced_code_block: {
8
+ patterns: ['#fenced_code_block_unknown'],
9
+ },
10
+ fenced_code_block_unknown: {
11
+ name: 'markup.fenced_code.block.markdown',
12
+ begin: /(^)(\s*)(`{3,}|~{3,})\s*(?=([^`~]*)?$)/,
13
+ beginCaptures: {
14
+ '3': 'punctuation.definition.markdown',
15
+ '4': 'fenced_code.block.language',
16
+ },
17
+ // eslint-disable-next-line no-useless-backreference
18
+ end: /(^|\G)(\2|\s{0,3})(\3)\s*$/,
19
+ endCaptures: {
20
+ '3': 'punctuation.definition.markdown',
21
+ },
22
+ },
23
+ raw: {
24
+ name: 'markup.inline.raw.string.markdown',
25
+ captures: 'punctuation.definition.raw.markdown',
26
+ begin: /`+/,
27
+ end: '\\1',
28
+ },
29
+ };
30
+ exports.grammar = {
31
+ name: 'Markdown',
32
+ scopeName: 'text.html.markdown',
33
+ patterns: ['#frontMatter', '#block'],
34
+ repository,
35
+ };
36
+ //# sourceMappingURL=markdown.js.map
@@ -0,0 +1,3 @@
1
+ import { GrammarDef } from '..';
2
+ export declare const grammar: GrammarDef;
3
+ //# sourceMappingURL=simple.d.ts.map
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grammar = void 0;
4
+ const repository = {
5
+ statements: {
6
+ patterns: ['#braces', '#comment', '#string'],
7
+ },
8
+ braces: {
9
+ name: 'braces.s',
10
+ patterns: ['#braces_paren', '#braces_curly'],
11
+ },
12
+ braces_paren: {
13
+ captures: 'paren.braces.s',
14
+ contentName: 'content.braces.s',
15
+ begin: '(',
16
+ end: ')',
17
+ patterns: ['$self'],
18
+ },
19
+ braces_curly: {
20
+ captures: 'curly.braces.s',
21
+ contentName: 'content.braces.s',
22
+ begin: '{',
23
+ end: '}',
24
+ patterns: ['$self'],
25
+ },
26
+ comment: {
27
+ patterns: ['#comment_line', '#comment_doc_block', '#comment_block'],
28
+ },
29
+ comment_line: {
30
+ patterns: ['#comment_line_double_slash', '#comment_line_hash'],
31
+ },
32
+ comment_line_hash: {
33
+ name: 'comment.line.s',
34
+ match: /(#).*\s?/,
35
+ captures: {
36
+ '1': 'punctuation.s',
37
+ },
38
+ },
39
+ comment_line_double_slash: {
40
+ name: 'comment.line.s',
41
+ match: /(\/\/).*\s?/,
42
+ captures: {
43
+ '1': 'punctuation.s',
44
+ },
45
+ },
46
+ comment_doc_block: {
47
+ name: 'comment.block.s',
48
+ begin: '/**',
49
+ end: '*/',
50
+ captures: {
51
+ '0': 'punctuation.s',
52
+ },
53
+ },
54
+ comment_block: {
55
+ name: 'comment.block.s',
56
+ begin: '/*',
57
+ end: '*/',
58
+ captures: {
59
+ '0': 'punctuation.s',
60
+ },
61
+ },
62
+ string: {
63
+ patterns: [{ include: '#string_q_single' }, { include: '#string_q_double' }, { include: '#string_template' }],
64
+ },
65
+ string_q_single: {
66
+ name: 'string.quoted.single.s',
67
+ begin: "'",
68
+ end: /'|((?:[^\\\n])$)/,
69
+ patterns: [{ include: '#string_character_escape' }],
70
+ captures: {
71
+ '0': 'punctuation.s',
72
+ },
73
+ },
74
+ string_q_double: {
75
+ name: 'string.quoted.double.s',
76
+ begin: '"',
77
+ end: /"|((?:[^\\\n])$)/,
78
+ patterns: [{ include: '#string_character_escape' }],
79
+ captures: {
80
+ '0': 'punctuation.s',
81
+ },
82
+ },
83
+ string_template: {
84
+ name: 'string.template.s',
85
+ begin: '`',
86
+ end: '`',
87
+ patterns: [{ include: '#string_character_escape' }, '#template_embedded'],
88
+ captures: {
89
+ '0': 'punctuation.s',
90
+ },
91
+ },
92
+ string_character_escape: {
93
+ name: 'constant.character.escape.s',
94
+ match: /\\(x[0-9A-Fa-f]{2}|[0-3][0-7]{0,2}|[4-7][0-7]?|.|$)/,
95
+ },
96
+ template_embedded: {
97
+ name: 'embedded.template.s',
98
+ begin: '${',
99
+ end: '}',
100
+ patterns: ['#statements'],
101
+ captures: {
102
+ '0': 'punctuation.s',
103
+ },
104
+ },
105
+ };
106
+ exports.grammar = {
107
+ name: 'Simple',
108
+ scopeName: 'source.s',
109
+ patterns: ['#statements'],
110
+ repository,
111
+ };
112
+ //# sourceMappingURL=simple.js.map
@@ -0,0 +1,3 @@
1
+ import { GrammarDef } from '..';
2
+ export declare const grammar: GrammarDef;
3
+ //# sourceMappingURL=typescript.d.ts.map
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grammar = void 0;
4
+ const repository = {
5
+ statements: {
6
+ name: 'code.ts',
7
+ patterns: [
8
+ '#keyword',
9
+ '#regexp',
10
+ '#string',
11
+ '#comment',
12
+ '#braces',
13
+ '#punctuation',
14
+ '#space',
15
+ { name: 'identifier', match: /[^\s;,!|&:^%{}[\]()*/+=<>]+/ },
16
+ ],
17
+ },
18
+ keyword: {
19
+ patterns: ['#keywordBase', '#standardTypes', '#standardLib'],
20
+ },
21
+ keywordBase: {
22
+ name: 'keyword.typescript.ts',
23
+ match: /\b(?:any|as|async|await|bigint|boolean|break|case|catch|const|continue|do|else|enum|export|extends|false|finally|for|from|function|get|if|implements|in|instanceof|interface|import|let|map|module|new|new|null|number|of|package|private|public|require|return|set|static|string|super|switch|this|throw|true|try|type|typeof|unknown|undefined|var|void|while|yield)\b/,
24
+ },
25
+ standardTypes: {
26
+ name: 'keyword.type.ts',
27
+ match: /\b(?:Promise|Record|Omit|Extract|Exclude|BigInt|Array)\b/,
28
+ },
29
+ standardLib: {
30
+ name: 'keyword.lib.ts',
31
+ match: /\b(?:console|process|window)\b/,
32
+ },
33
+ string: {
34
+ patterns: ['#string_q_single', '#string_q_double', '#string_template'],
35
+ },
36
+ string_q_single: {
37
+ name: 'string.quoted.single.ts',
38
+ begin: "'",
39
+ end: /'|((?:[^\\\n])$)/,
40
+ captures: 'punctuation.string.ts',
41
+ patterns: [{ include: '#string_character_escape' }],
42
+ },
43
+ string_q_double: {
44
+ name: 'string.quoted.double.ts',
45
+ begin: '"',
46
+ end: /"|((?:[^\\\n])$)/,
47
+ captures: 'punctuation.string.ts',
48
+ patterns: [{ include: '#string_character_escape' }],
49
+ },
50
+ string_template: {
51
+ name: 'string.template.ts',
52
+ begin: '`',
53
+ end: '`',
54
+ captures: 'punctuation.string.ts',
55
+ patterns: [
56
+ {
57
+ name: 'meta.template.expression.ts',
58
+ contentName: 'meta.embedded.line.ts',
59
+ begin: '${',
60
+ end: '}',
61
+ patterns: ['#statements'],
62
+ captures: 'punctuation.definition.template.expression.ts',
63
+ },
64
+ { include: '#string_character_escape' },
65
+ ],
66
+ },
67
+ string_character_escape: {
68
+ name: 'constant.character.escape.ts',
69
+ match: /\\(x[0-9A-Fa-f]{2}|[0-3][0-7]{0,2}|[4-7][0-7]?|u[0-9A-Fa-f]{4}|.|\r?\n?$)/,
70
+ },
71
+ braces: {
72
+ patterns: [
73
+ {
74
+ begin: '(',
75
+ end: ')',
76
+ captures: 'punctuation.meta.brace.ts',
77
+ patterns: ['#statements'],
78
+ name: 'meta.brace.ts',
79
+ contentName: 'code.ts',
80
+ },
81
+ {
82
+ begin: '{',
83
+ end: '}',
84
+ captures: 'punctuation.meta.brace.ts',
85
+ patterns: ['#statements'],
86
+ name: 'meta.brace.ts',
87
+ contentName: 'code.ts',
88
+ },
89
+ {
90
+ begin: '[',
91
+ end: ']',
92
+ captures: 'punctuation.meta.brace.ts',
93
+ patterns: ['#statements'],
94
+ name: 'meta.brace.ts',
95
+ contentName: 'code.ts',
96
+ },
97
+ ],
98
+ },
99
+ punctuation: {
100
+ name: 'punctuation.ts',
101
+ match: /[-;:,!|&^%*/+=<>\n\r]/,
102
+ },
103
+ space: {
104
+ name: 'punctuation.space.ts',
105
+ match: /\s+/,
106
+ },
107
+ comment: {
108
+ patterns: [
109
+ {
110
+ name: 'comment.line.ts',
111
+ comment: 'line comment',
112
+ begin: '//',
113
+ end: /(?=$)/,
114
+ captures: 'punctuation.definition.comment.ts',
115
+ },
116
+ {
117
+ name: 'comment.block.documentation.ts',
118
+ comment: 'DocBlock',
119
+ begin: /\/\*\*(?!\/)/,
120
+ captures: 'punctuation.definition.comment.ts',
121
+ end: '*/',
122
+ },
123
+ {
124
+ name: 'comment.block.ts',
125
+ begin: '/*',
126
+ end: '*/',
127
+ captures: 'punctuation.definition.comment.ts',
128
+ },
129
+ ],
130
+ },
131
+ regexp: {
132
+ name: 'regexp.ts',
133
+ begin: /\/(?![/*])/,
134
+ end: /\/([a-z]*)/i,
135
+ beginCaptures: 'punctuation.begin.regexp.ts',
136
+ endCaptures: 'punctuation.end.regexp.ts',
137
+ patterns: ['#regexp_escape', '#regexp_brace'],
138
+ },
139
+ regexp_escape: {
140
+ name: 'escape.regexp.ts',
141
+ match: /\\./,
142
+ },
143
+ regexp_brace: {
144
+ name: 'brace.regexp.ts',
145
+ begin: '[',
146
+ end: ']',
147
+ contentName: 'character-class.regexp.ts',
148
+ patterns: ['#regexp_escape'],
149
+ },
150
+ };
151
+ exports.grammar = {
152
+ name: 'TypeScript',
153
+ scopeName: 'source.ts',
154
+ patterns: [
155
+ {
156
+ name: 'comment.line.shebang.ts',
157
+ match: /^#!.*(?=$)/,
158
+ },
159
+ {
160
+ include: '#statements',
161
+ },
162
+ ],
163
+ repository,
164
+ };
165
+ //# sourceMappingURL=typescript.js.map
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export * from './grammar';
2
- export * from './visualize';
1
+ export { compileGrammar, tokenizeLine, tokenizeText } from './parser';
2
+ export type { Grammar, GrammarDef, Pattern, Repository } from './parser';
3
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./grammar"));
7
- __export(require("./visualize"));
3
+ exports.tokenizeText = exports.tokenizeLine = exports.compileGrammar = void 0;
4
+ var parser_1 = require("./parser");
5
+ Object.defineProperty(exports, "compileGrammar", { enumerable: true, get: function () { return parser_1.compileGrammar; } });
6
+ Object.defineProperty(exports, "tokenizeLine", { enumerable: true, get: function () { return parser_1.tokenizeLine; } });
7
+ Object.defineProperty(exports, "tokenizeText", { enumerable: true, get: function () { return parser_1.tokenizeText; } });
8
8
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { MappedText } from './types';
2
+ export declare function appendMappedText(a: MappedText, b: MappedText): MappedText;
3
+ //# sourceMappingURL=appendMappedText.d.ts.map
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.appendMappedText = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ function appendMappedText(a, b) {
9
+ if (!a.map && !b.map) {
10
+ return { text: a.text + b.text };
11
+ }
12
+ const aLen = a.text.length;
13
+ const bLen = b.text.length;
14
+ const aMap = [0, 0].concat(a.map || [0, 0, aLen, aLen]);
15
+ const bMap = [0, 0].concat(b.map || [0, 0, bLen, bLen]);
16
+ (0, assert_1.default)(aMap[aMap.length - 1] === aLen);
17
+ (0, assert_1.default)(bMap[bMap.length - 1] === bLen);
18
+ (0, assert_1.default)((aMap.length & 1) === 0);
19
+ (0, assert_1.default)((bMap.length & 1) === 0);
20
+ return {
21
+ text: a.text + b.text,
22
+ map: joinMaps(aMap, bMap),
23
+ };
24
+ }
25
+ exports.appendMappedText = appendMappedText;
26
+ function joinMaps(aMap, bMap) {
27
+ const n = aMap.length - 1;
28
+ const offsets = [aMap[n - 1], aMap[n]];
29
+ const ab = aMap.concat(bMap.map((v, i) => v + offsets[i & 1]));
30
+ // Normalize the map by removing duplicate entries
31
+ const r = [0, 0];
32
+ let last0 = 0, last1 = 0;
33
+ for (let i = 0; i < ab.length; i += 2) {
34
+ const v0 = ab[i];
35
+ const v1 = ab[i + 1];
36
+ if (v0 === last0 && v1 === last1) {
37
+ continue;
38
+ }
39
+ r.push(v0, v1);
40
+ last0 = v0;
41
+ last1 = v1;
42
+ }
43
+ return r;
44
+ }
45
+ //# sourceMappingURL=appendMappedText.js.map
@@ -0,0 +1,17 @@
1
+ export interface MappedText {
2
+ text: string;
3
+ /**
4
+ * `(i, j)` number pairs where
5
+ * - `i` is the offset in the source
6
+ * - `j` is the offset in the destination
7
+ *
8
+ * Example:
9
+ * - source text = `"caf\xe9"`
10
+ * - mapped text = `"café"`
11
+ * - map = `[3, 3, 7, 4]`, which is equivalent to `[0, 0, 3, 3, 7, 4]`
12
+ * where the `[0, 0]` is unnecessary.
13
+ *
14
+ */
15
+ map?: number[] | undefined;
16
+ }
17
+ //# sourceMappingURL=types.d.ts.map
File without changes
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Mappers for TypeScript and JavaScript
3
+ */
4
+ import { MappedText } from './types';
5
+ export declare function mapRawString(text: string): MappedText;
6
+ //# sourceMappingURL=typescript.d.ts.map