@noldova/context.parsers 0.0.0-stage → 0.0.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.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +94 -2
  3. package/api/index.d.ts +1378 -0
  4. package/api/index.js +30 -0
  5. package/api/index.js.map +1 -0
  6. package/exceptions/invalid-offset.exception.js +31 -0
  7. package/exceptions/invalid-offset.exception.js.map +1 -0
  8. package/exceptions/invalid-span.exception.js +33 -0
  9. package/exceptions/invalid-span.exception.js.map +1 -0
  10. package/exceptions/line-out-of-bounds.exception.js +23 -0
  11. package/exceptions/line-out-of-bounds.exception.js.map +1 -0
  12. package/exceptions/parser.exception.js +14 -0
  13. package/exceptions/parser.exception.js.map +1 -0
  14. package/models/diagnostic-collector.js +31 -0
  15. package/models/diagnostic-collector.js.map +1 -0
  16. package/models/nodes/comment.node.js +14 -0
  17. package/models/nodes/comment.node.js.map +1 -0
  18. package/models/nodes/composite.node.js +16 -0
  19. package/models/nodes/composite.node.js.map +1 -0
  20. package/models/nodes/end-of-file.node.js +19 -0
  21. package/models/nodes/end-of-file.node.js.map +1 -0
  22. package/models/nodes/literal.node.js +14 -0
  23. package/models/nodes/literal.node.js.map +1 -0
  24. package/models/nodes/node.js +18 -0
  25. package/models/nodes/node.js.map +1 -0
  26. package/models/nodes/value.node.js +16 -0
  27. package/models/nodes/value.node.js.map +1 -0
  28. package/models/nodes/whitespace.node.js +14 -0
  29. package/models/nodes/whitespace.node.js.map +1 -0
  30. package/models/parser-context.js +17 -0
  31. package/models/parser-context.js.map +1 -0
  32. package/models/parser-result.js +16 -0
  33. package/models/parser-result.js.map +1 -0
  34. package/models/sources/source-line-map.js +67 -0
  35. package/models/sources/source-line-map.js.map +1 -0
  36. package/models/sources/source-line.js +31 -0
  37. package/models/sources/source-line.js.map +1 -0
  38. package/models/sources/source-position.js +23 -0
  39. package/models/sources/source-position.js.map +1 -0
  40. package/models/sources/source-span.js +20 -0
  41. package/models/sources/source-span.js.map +1 -0
  42. package/models/sources/source.js +36 -0
  43. package/models/sources/source.js.map +1 -0
  44. package/package.json +26 -4
  45. package/resources.js +38 -0
  46. package/resources.js.map +1 -0
  47. package/services/lexer.js +38 -0
  48. package/services/lexer.js.map +1 -0
  49. package/services/parser.js +96 -0
  50. package/services/parser.js.map +1 -0
  51. package/services/source.reader.js +93 -0
  52. package/services/source.reader.js.map +1 -0
  53. package/types/node-type.js +9 -0
  54. package/types/node-type.js.map +1 -0
  55. package/types/value-node-type.js +9 -0
  56. package/types/value-node-type.js.map +1 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Noldova
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,95 @@
1
- # Temporary Holding Version
1
+ # noldova.context.parsers
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The shared foundation for CONTEXT language parsers: an immutable source with
4
+ spans and line positions, a reader over it, and the lexer and parser base
5
+ classes every language parser builds on. It contains no grammar; TypeScript,
6
+ Regex, and future languages are separate packages.
7
+
8
+ ## Model
9
+
10
+ - `Source` holds the text once. `SourceSpan` is a half-open UTF-16 range in it;
11
+ `SourceLineMap` maps offsets to lines and back.
12
+ - `SourceReader` is a cursor over a source or a bounded region of it. At the end
13
+ it returns the null character and code point 0, so a lexer compares characters
14
+ instead of checking for the end; where a null character can be legitimate
15
+ input, `isAtEnd` decides.
16
+ - `Node` is the base of every syntax node: a span and optional trivia. A
17
+ language adds behavior to nodes through declaration merging in its own
18
+ package; grammar code recognizes node types with `instanceof`.
19
+ - `Lexer` owns its reader, reports into the parser context's diagnostic
20
+ collector, and produces one node per call. It checks two invariants for
21
+ every language: a lex either advances or produces the `EndOfFileNode`
22
+ exactly at the end, and re-lexing obeys the same rules.
23
+ - `Parser` holds one current node. It advances, consumes by node type, looks
24
+ ahead and tries interpretations with exact rewind, and re-lexes the current
25
+ node when the grammar position changes what a token means, such as `>>`
26
+ inside type arguments or `/` beginning a regular expression.
27
+ - `ParserContext` carries grammar state and owns the published
28
+ `DiagnosticCollector`. A language derives its own context for its flags and
29
+ caches.
30
+
31
+ ## Diagnostics
32
+
33
+ One sequence, owned by the parser context, receives every diagnostic in the
34
+ order it is reported: the lexer's as each node is lexed, the grammar's as it
35
+ parses. That is the sequence grammar decisions may consult. A rewind restores
36
+ the current node, the reader offset, and the sequence length, and nothing is
37
+ ever removed from the middle. A re-lex removes nothing either; the replacement
38
+ scan reports after what is already there, and a language whose scans can
39
+ repeat a diagnostic at the same position applies its own suppression rule, as
40
+ TypeScript does with tsc's same-offset rule.
41
+
42
+ ## Trust boundary
43
+
44
+ Positions handed back to the reader, through `seek`, `advance`, a relative peek,
45
+ or span creation, come from the reader itself and are not validated again;
46
+ this keeps the per-token path free of checks. Everything that accepts an offset
47
+ or span from elsewhere validates it: `Source`, the `SourceReader` constructor,
48
+ `SourceLineMap`, `SourceLine`, and `SourcePosition`.
49
+
50
+ ## Writing a language parser
51
+
52
+ ```ts
53
+ import { DiagnosticCollector, EndOfFileNode, Lexer, LiteralNode, Node, Parser, ParserContext, Source, SourceSpan } from "@noldova/context.parsers";
54
+
55
+ class TextLiteralNode extends LiteralNode {
56
+ public constructor(value: string, span: SourceSpan) {
57
+ super(value, span);
58
+ }
59
+ }
60
+
61
+ class TextLexer extends Lexer {
62
+ public constructor(source: Source, diagnosticCollector: DiagnosticCollector) {
63
+ super(source, diagnosticCollector);
64
+ }
65
+
66
+ protected override lexNode(): Node {
67
+ const startOffset = this.reader.currentOffset;
68
+ if (this.reader.isAtEnd)
69
+ return new EndOfFileNode(new SourceSpan(startOffset, 0));
70
+
71
+ const text = this.reader.readWhile(() => true).toString();
72
+ return new TextLiteralNode(text, new SourceSpan(startOffset, text.length));
73
+ }
74
+ }
75
+
76
+ class TextParser extends Parser<LiteralNode> {
77
+ public constructor(context: ParserContext, lexer: TextLexer) {
78
+ super(context, lexer);
79
+ }
80
+
81
+ protected override parseRootNode(): LiteralNode {
82
+ const root = this.requireCurrentNode(LiteralNode);
83
+ this.advance();
84
+ return root;
85
+ }
86
+ }
87
+
88
+ const source = new Source("text");
89
+ const context = new ParserContext(source);
90
+ const result = new TextParser(context, new TextLexer(source, context.diagnosticCollector)).parse();
91
+ console.assert(result.rootNode.value === "text" && result.diagnostics.length === 0);
92
+ ```
93
+
94
+ The declaration file `src/api/index.d.ts` documents every member with its
95
+ contract and examples.