grammar-well 1.1.1 → 1.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 (64) hide show
  1. package/build/compiler/compiler.d.ts +49 -48
  2. package/build/compiler/compiler.js +227 -226
  3. package/build/compiler/compiler.js.map +1 -1
  4. package/build/compiler/generator.d.ts +23 -23
  5. package/build/compiler/generator.js +213 -212
  6. package/build/compiler/generator.js.map +1 -1
  7. package/build/compiler/import-resolver.d.ts +15 -15
  8. package/build/compiler/import-resolver.js +36 -36
  9. package/build/compiler/outputs/javascript.d.ts +3 -3
  10. package/build/compiler/outputs/javascript.js +14 -14
  11. package/build/compiler/outputs/json.d.ts +2 -2
  12. package/build/compiler/outputs/json.js +7 -7
  13. package/build/compiler/outputs/typescript.d.ts +2 -2
  14. package/build/compiler/outputs/typescript.js +9 -8
  15. package/build/compiler/outputs/typescript.js.map +1 -1
  16. package/build/grammars/gwell.d.ts +1023 -997
  17. package/build/grammars/gwell.js +540 -536
  18. package/build/grammars/gwell.js.map +1 -1
  19. package/build/grammars/json.d.ts +151 -151
  20. package/build/grammars/json.js +111 -111
  21. package/build/grammars/number.d.ts +239 -239
  22. package/build/grammars/number.js +114 -114
  23. package/build/grammars/number.json +1 -1
  24. package/build/grammars/string.d.ts +116 -116
  25. package/build/grammars/string.js +49 -49
  26. package/build/grammars/string.json +1 -1
  27. package/build/grammars/whitespace.d.ts +51 -51
  28. package/build/grammars/whitespace.js +29 -29
  29. package/build/grammars/whitespace.json +1 -1
  30. package/build/index.d.ts +4 -4
  31. package/build/index.js +20 -20
  32. package/build/lexers/character-lexer.d.ts +27 -27
  33. package/build/lexers/character-lexer.js +70 -70
  34. package/build/lexers/stateful-lexer.d.ts +48 -48
  35. package/build/lexers/stateful-lexer.js +308 -308
  36. package/build/lexers/token-buffer.d.ts +32 -32
  37. package/build/lexers/token-buffer.js +91 -91
  38. package/build/parser/algorithms/cyk.d.ts +16 -16
  39. package/build/parser/algorithms/cyk.js +57 -57
  40. package/build/parser/algorithms/earley.d.ts +48 -48
  41. package/build/parser/algorithms/earley.js +157 -157
  42. package/build/parser/algorithms/lr.d.ts +10 -10
  43. package/build/parser/algorithms/lr.js +33 -33
  44. package/build/parser/parser.d.ts +26 -26
  45. package/build/parser/parser.js +73 -73
  46. package/build/typings.d.ts +199 -198
  47. package/build/typings.js +2 -2
  48. package/build/utility/general.d.ts +55 -46
  49. package/build/utility/general.js +165 -111
  50. package/build/utility/general.js.map +1 -1
  51. package/build/utility/lint.d.ts +2 -2
  52. package/build/utility/lint.js +27 -27
  53. package/build/utility/lr.d.ts +52 -56
  54. package/build/utility/lr.js +129 -130
  55. package/build/utility/lr.js.map +1 -1
  56. package/build/utility/text-format.d.ts +11 -11
  57. package/build/utility/text-format.js +83 -83
  58. package/package.json +1 -1
  59. package/src/compiler/generator.ts +1 -0
  60. package/src/compiler/outputs/typescript.ts +2 -1
  61. package/src/grammars/gwell.gwell +15 -13
  62. package/src/grammars/gwell.js +17 -13
  63. package/src/grammars/gwell.json +1 -1
  64. package/src/typings.ts +1 -0
@@ -1,92 +1,92 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TokenBuffer = void 0;
4
- class TokenBuffer {
5
- lexer;
6
- history = [];
7
- queued = '';
8
- $historyIndex = -1;
9
- get offset() { return this.active?.offset || 0; }
10
- get line() { return this.active?.line || 0; }
11
- get column() { return this.active?.column || 0; }
12
- get active() { return this.history[this.$historyIndex]; }
13
- get state() {
14
- return { historyIndex: this.$historyIndex, offset: this.offset };
15
- }
16
- constructor(lexer) {
17
- this.lexer = lexer;
18
- }
19
- reset(buffer) {
20
- this.lexer.feed(buffer);
21
- this.history = [];
22
- this.$historyIndex = -1;
23
- }
24
- restore(state) {
25
- if (this.history[state.historyIndex].offset != state.offset) {
26
- return;
27
- }
28
- this.$historyIndex = state.historyIndex;
29
- }
30
- feed(buffer, flush) {
31
- this.queued += buffer;
32
- if (flush) {
33
- this.flush();
34
- }
35
- }
36
- flush() {
37
- this.history = this.history.slice(this.$historyIndex);
38
- this.$historyIndex = 0;
39
- if (this.lexer.flush) {
40
- this.lexer.flush();
41
- }
42
- }
43
- previous() {
44
- if (this.$historyIndex > 0) {
45
- return this.history[--this.$historyIndex];
46
- }
47
- }
48
- next() {
49
- if (this.$historyIndex + 1 >= this.history.length) {
50
- this.lexerNext();
51
- }
52
- if (this.$historyIndex + 1 < this.history.length) {
53
- return this.history[++this.$historyIndex];
54
- }
55
- }
56
- peek(offset) {
57
- offset += this.$historyIndex;
58
- while ((offset >= this.history.length) && this.lexerNext()) {
59
- }
60
- if (offset >= 0 && offset < this.history.length)
61
- return this.history[offset];
62
- }
63
- lexerNext() {
64
- let token = this.lexer.next();
65
- if (typeof token === 'undefined' && this.queued) {
66
- this.lexer.feed(this.queued, this.$historyIndex >= 0 ? this.lexer.state() : undefined);
67
- this.queued = '';
68
- token = this.lexer.next();
69
- }
70
- if (token)
71
- this.history.push(token);
72
- return token;
73
- }
74
- [Symbol.iterator]() {
75
- return new TokenIterator(this);
76
- }
77
- }
78
- exports.TokenBuffer = TokenBuffer;
79
- class TokenIterator {
80
- buffer;
81
- constructor(buffer) {
82
- this.buffer = buffer;
83
- }
84
- next() {
85
- const token = this.buffer.next();
86
- return { value: token, done: !token };
87
- }
88
- [Symbol.iterator]() {
89
- return this;
90
- }
91
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenBuffer = void 0;
4
+ class TokenBuffer {
5
+ lexer;
6
+ history = [];
7
+ queued = '';
8
+ $historyIndex = -1;
9
+ get offset() { return this.active?.offset || 0; }
10
+ get line() { return this.active?.line || 0; }
11
+ get column() { return this.active?.column || 0; }
12
+ get active() { return this.history[this.$historyIndex]; }
13
+ get state() {
14
+ return { historyIndex: this.$historyIndex, offset: this.offset };
15
+ }
16
+ constructor(lexer) {
17
+ this.lexer = lexer;
18
+ }
19
+ reset(buffer) {
20
+ this.lexer.feed(buffer);
21
+ this.history = [];
22
+ this.$historyIndex = -1;
23
+ }
24
+ restore(state) {
25
+ if (this.history[state.historyIndex].offset != state.offset) {
26
+ return;
27
+ }
28
+ this.$historyIndex = state.historyIndex;
29
+ }
30
+ feed(buffer, flush) {
31
+ this.queued += buffer;
32
+ if (flush) {
33
+ this.flush();
34
+ }
35
+ }
36
+ flush() {
37
+ this.history = this.history.slice(this.$historyIndex);
38
+ this.$historyIndex = 0;
39
+ if (this.lexer.flush) {
40
+ this.lexer.flush();
41
+ }
42
+ }
43
+ previous() {
44
+ if (this.$historyIndex > 0) {
45
+ return this.history[--this.$historyIndex];
46
+ }
47
+ }
48
+ next() {
49
+ if (this.$historyIndex + 1 >= this.history.length) {
50
+ this.lexerNext();
51
+ }
52
+ if (this.$historyIndex + 1 < this.history.length) {
53
+ return this.history[++this.$historyIndex];
54
+ }
55
+ }
56
+ peek(offset) {
57
+ offset += this.$historyIndex;
58
+ while ((offset >= this.history.length) && this.lexerNext()) {
59
+ }
60
+ if (offset >= 0 && offset < this.history.length)
61
+ return this.history[offset];
62
+ }
63
+ lexerNext() {
64
+ let token = this.lexer.next();
65
+ if (typeof token === 'undefined' && this.queued) {
66
+ this.lexer.feed(this.queued, this.$historyIndex >= 0 ? this.lexer.state() : undefined);
67
+ this.queued = '';
68
+ token = this.lexer.next();
69
+ }
70
+ if (token)
71
+ this.history.push(token);
72
+ return token;
73
+ }
74
+ [Symbol.iterator]() {
75
+ return new TokenIterator(this);
76
+ }
77
+ }
78
+ exports.TokenBuffer = TokenBuffer;
79
+ class TokenIterator {
80
+ buffer;
81
+ constructor(buffer) {
82
+ this.buffer = buffer;
83
+ }
84
+ next() {
85
+ const token = this.buffer.next();
86
+ return { value: token, done: !token };
87
+ }
88
+ [Symbol.iterator]() {
89
+ return this;
90
+ }
91
+ }
92
92
  //# sourceMappingURL=token-buffer.js.map
@@ -1,16 +1,16 @@
1
- import { TokenBuffer } from "../../lexers/token-buffer";
2
- import { GrammarRule, LanguageDefinition, LexerToken } from "../../typings";
3
- export declare function CYK(language: LanguageDefinition & {
4
- tokens: TokenBuffer;
5
- }, _options?: {}): {
6
- results: any[];
7
- };
8
- export interface NonTerminal {
9
- rule: GrammarRule;
10
- left: NonTerminal | Terminal;
11
- right: NonTerminal | Terminal;
12
- }
13
- export interface Terminal {
14
- rule: GrammarRule;
15
- token: LexerToken;
16
- }
1
+ import { TokenBuffer } from "../../lexers/token-buffer";
2
+ import { GrammarRule, LanguageDefinition, LexerToken } from "../../typings";
3
+ export declare function CYK(language: LanguageDefinition & {
4
+ tokens: TokenBuffer;
5
+ }, _options?: {}): {
6
+ results: any[];
7
+ };
8
+ export interface NonTerminal {
9
+ rule: GrammarRule;
10
+ left: NonTerminal | Terminal;
11
+ right: NonTerminal | Terminal;
12
+ }
13
+ export interface Terminal {
14
+ rule: GrammarRule;
15
+ token: LexerToken;
16
+ }
@@ -1,58 +1,58 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CYK = void 0;
4
- const general_1 = require("../../utility/general");
5
- const parser_1 = require("../parser");
6
- function CYK(language, _options = {}) {
7
- const { grammar, tokens } = language;
8
- const terminals = [];
9
- const nonTerminals = [];
10
- for (const name in grammar.rules) {
11
- for (const rule of grammar.rules[name]) {
12
- const { symbols } = rule;
13
- if (parser_1.ParserUtility.SymbolIsTerminal(symbols[0])) {
14
- terminals.push(rule);
15
- }
16
- else {
17
- nonTerminals.push(rule);
18
- }
19
- }
20
- }
21
- let currentTokenIndex = -1;
22
- const chart = new general_1.Matrix(0, 0, () => new Map());
23
- for (const token of tokens) {
24
- currentTokenIndex++;
25
- chart.resize(currentTokenIndex + 2, currentTokenIndex + 2);
26
- for (const rule of terminals) {
27
- if (parser_1.ParserUtility.TokenMatchesSymbol(token, rule.symbols[0])) {
28
- chart.get(currentTokenIndex, currentTokenIndex).set(rule.name, { rule, token });
29
- }
30
- }
31
- for (let floor = currentTokenIndex; floor >= 0; floor--) {
32
- for (let inner = floor; inner <= currentTokenIndex; inner++) {
33
- const leftCell = chart.get(floor, inner);
34
- const rightCell = chart.get(inner + 1, currentTokenIndex);
35
- for (const rule of nonTerminals) {
36
- const { symbols: [leftSymbol, rightSymbol] } = rule;
37
- const left = leftCell.get(leftSymbol);
38
- const right = rightCell.get(rightSymbol);
39
- if (left && right) {
40
- chart.get(floor, currentTokenIndex).set(rule.name, { rule, left, right });
41
- }
42
- }
43
- }
44
- }
45
- }
46
- const results = Array.from(chart.get(0, currentTokenIndex).values()).map(v => GetValue(v));
47
- return { results };
48
- }
49
- exports.CYK = CYK;
50
- function GetValue(ref) {
51
- if (!ref)
52
- return;
53
- if ('token' in ref) {
54
- return parser_1.ParserUtility.PostProcess(ref.rule, [ref.token]);
55
- }
56
- return parser_1.ParserUtility.PostProcess(ref.rule, [GetValue(ref.left), GetValue(ref.right)]);
57
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CYK = void 0;
4
+ const general_1 = require("../../utility/general");
5
+ const parser_1 = require("../parser");
6
+ function CYK(language, _options = {}) {
7
+ const { grammar, tokens } = language;
8
+ const terminals = [];
9
+ const nonTerminals = [];
10
+ for (const name in grammar.rules) {
11
+ for (const rule of grammar.rules[name]) {
12
+ const { symbols } = rule;
13
+ if (parser_1.ParserUtility.SymbolIsTerminal(symbols[0])) {
14
+ terminals.push(rule);
15
+ }
16
+ else {
17
+ nonTerminals.push(rule);
18
+ }
19
+ }
20
+ }
21
+ let currentTokenIndex = -1;
22
+ const chart = new general_1.Matrix(0, 0, () => new Map());
23
+ for (const token of tokens) {
24
+ currentTokenIndex++;
25
+ chart.resize(currentTokenIndex + 2, currentTokenIndex + 2);
26
+ for (const rule of terminals) {
27
+ if (parser_1.ParserUtility.TokenMatchesSymbol(token, rule.symbols[0])) {
28
+ chart.get(currentTokenIndex, currentTokenIndex).set(rule.name, { rule, token });
29
+ }
30
+ }
31
+ for (let floor = currentTokenIndex; floor >= 0; floor--) {
32
+ for (let inner = floor; inner <= currentTokenIndex; inner++) {
33
+ const leftCell = chart.get(floor, inner);
34
+ const rightCell = chart.get(inner + 1, currentTokenIndex);
35
+ for (const rule of nonTerminals) {
36
+ const { symbols: [leftSymbol, rightSymbol] } = rule;
37
+ const left = leftCell.get(leftSymbol);
38
+ const right = rightCell.get(rightSymbol);
39
+ if (left && right) {
40
+ chart.get(floor, currentTokenIndex).set(rule.name, { rule, left, right });
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ const results = Array.from(chart.get(0, currentTokenIndex).values()).map(v => GetValue(v));
47
+ return { results };
48
+ }
49
+ exports.CYK = CYK;
50
+ function GetValue(ref) {
51
+ if (!ref)
52
+ return;
53
+ if ('token' in ref) {
54
+ return parser_1.ParserUtility.PostProcess(ref.rule, [ref.token]);
55
+ }
56
+ return parser_1.ParserUtility.PostProcess(ref.rule, [GetValue(ref.left), GetValue(ref.right)]);
57
+ }
58
58
  //# sourceMappingURL=cyk.js.map
@@ -1,48 +1,48 @@
1
- import { Dictionary, GrammarRule, LanguageDefinition } from "../../typings";
2
- import { TokenBuffer } from "../../lexers/token-buffer";
3
- export interface EarleyParserOptions {
4
- keepHistory?: boolean;
5
- }
6
- export declare function Earley(language: LanguageDefinition & {
7
- tokens: TokenBuffer;
8
- }, options?: EarleyParserOptions): {
9
- results: any[];
10
- info: {
11
- table: Column[];
12
- };
13
- };
14
- declare class Column {
15
- private rules;
16
- index: number;
17
- data: any;
18
- states: State[];
19
- wants: Dictionary<State[]>;
20
- scannable: State[];
21
- completed: Dictionary<State[]>;
22
- constructor(rules: Dictionary<GrammarRule[]>, index: number);
23
- process(): void;
24
- predict(exp: string): void;
25
- expects(): GrammarRule[];
26
- private complete;
27
- }
28
- declare class State {
29
- rule: GrammarRule;
30
- dot: number;
31
- reference: number;
32
- wantedBy: State[];
33
- isComplete: boolean;
34
- data: any;
35
- left: State;
36
- right: State | StateToken;
37
- constructor(rule: GrammarRule, dot: number, reference: number, wantedBy: State[]);
38
- nextState(child: State | StateToken): State;
39
- finish(): void;
40
- protected build(): any[];
41
- }
42
- interface StateToken {
43
- data: any;
44
- token: any;
45
- isToken: boolean;
46
- reference: number;
47
- }
48
- export {};
1
+ import { Dictionary, GrammarRule, LanguageDefinition } from "../../typings";
2
+ import { TokenBuffer } from "../../lexers/token-buffer";
3
+ export interface EarleyParserOptions {
4
+ keepHistory?: boolean;
5
+ }
6
+ export declare function Earley(language: LanguageDefinition & {
7
+ tokens: TokenBuffer;
8
+ }, options?: EarleyParserOptions): {
9
+ results: any[];
10
+ info: {
11
+ table: Column[];
12
+ };
13
+ };
14
+ declare class Column {
15
+ private rules;
16
+ index: number;
17
+ data: any;
18
+ states: State[];
19
+ wants: Dictionary<State[]>;
20
+ scannable: State[];
21
+ completed: Dictionary<State[]>;
22
+ constructor(rules: Dictionary<GrammarRule[]>, index: number);
23
+ process(): void;
24
+ predict(exp: string): void;
25
+ expects(): GrammarRule[];
26
+ private complete;
27
+ }
28
+ declare class State {
29
+ rule: GrammarRule;
30
+ dot: number;
31
+ reference: number;
32
+ wantedBy: State[];
33
+ isComplete: boolean;
34
+ data: any;
35
+ left: State;
36
+ right: State | StateToken;
37
+ constructor(rule: GrammarRule, dot: number, reference: number, wantedBy: State[]);
38
+ nextState(child: State | StateToken): State;
39
+ finish(): void;
40
+ protected build(): any[];
41
+ }
42
+ interface StateToken {
43
+ data: any;
44
+ token: any;
45
+ isToken: boolean;
46
+ reference: number;
47
+ }
48
+ export {};