@plurnk/plurnk-grammar 0.1.1 → 0.2.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.
@@ -4,8 +4,21 @@ import { AbstractParseTreeVisitor } from "antlr4ng";
4
4
 
5
5
  import { DocumentContext } from "./plurnkParser.ts";
6
6
  import { StatementContext } from "./plurnkParser.ts";
7
- import { OpenTagContext } from "./plurnkParser.ts";
8
- import { SignalContext } from "./plurnkParser.ts";
7
+ import { FindStatementContext } from "./plurnkParser.ts";
8
+ import { ReadStatementContext } from "./plurnkParser.ts";
9
+ import { EditStatementContext } from "./plurnkParser.ts";
10
+ import { CopyStatementContext } from "./plurnkParser.ts";
11
+ import { MoveStatementContext } from "./plurnkParser.ts";
12
+ import { ShowStatementContext } from "./plurnkParser.ts";
13
+ import { HideStatementContext } from "./plurnkParser.ts";
14
+ import { SendStatementContext } from "./plurnkParser.ts";
15
+ import { ExecStatementContext } from "./plurnkParser.ts";
16
+ import { TagOpModifiersContext } from "./plurnkParser.ts";
17
+ import { SendModifiersContext } from "./plurnkParser.ts";
18
+ import { ExecModifiersContext } from "./plurnkParser.ts";
19
+ import { TagSignalContext } from "./plurnkParser.ts";
20
+ import { IntSignalContext } from "./plurnkParser.ts";
21
+ import { IdentSignalContext } from "./plurnkParser.ts";
9
22
  import { PathContext } from "./plurnkParser.ts";
10
23
  import { LineMarkerContext } from "./plurnkParser.ts";
11
24
  import { BodyContext } from "./plurnkParser.ts";
@@ -32,17 +45,95 @@ export class plurnkParserVisitor<Result> extends AbstractParseTreeVisitor<Result
32
45
  */
33
46
  visitStatement?: (ctx: StatementContext) => Result;
34
47
  /**
35
- * Visit a parse tree produced by `plurnkParser.openTag`.
48
+ * Visit a parse tree produced by `plurnkParser.findStatement`.
36
49
  * @param ctx the parse tree
37
50
  * @return the visitor result
38
51
  */
39
- visitOpenTag?: (ctx: OpenTagContext) => Result;
52
+ visitFindStatement?: (ctx: FindStatementContext) => Result;
40
53
  /**
41
- * Visit a parse tree produced by `plurnkParser.signal`.
54
+ * Visit a parse tree produced by `plurnkParser.readStatement`.
42
55
  * @param ctx the parse tree
43
56
  * @return the visitor result
44
57
  */
45
- visitSignal?: (ctx: SignalContext) => Result;
58
+ visitReadStatement?: (ctx: ReadStatementContext) => Result;
59
+ /**
60
+ * Visit a parse tree produced by `plurnkParser.editStatement`.
61
+ * @param ctx the parse tree
62
+ * @return the visitor result
63
+ */
64
+ visitEditStatement?: (ctx: EditStatementContext) => Result;
65
+ /**
66
+ * Visit a parse tree produced by `plurnkParser.copyStatement`.
67
+ * @param ctx the parse tree
68
+ * @return the visitor result
69
+ */
70
+ visitCopyStatement?: (ctx: CopyStatementContext) => Result;
71
+ /**
72
+ * Visit a parse tree produced by `plurnkParser.moveStatement`.
73
+ * @param ctx the parse tree
74
+ * @return the visitor result
75
+ */
76
+ visitMoveStatement?: (ctx: MoveStatementContext) => Result;
77
+ /**
78
+ * Visit a parse tree produced by `plurnkParser.showStatement`.
79
+ * @param ctx the parse tree
80
+ * @return the visitor result
81
+ */
82
+ visitShowStatement?: (ctx: ShowStatementContext) => Result;
83
+ /**
84
+ * Visit a parse tree produced by `plurnkParser.hideStatement`.
85
+ * @param ctx the parse tree
86
+ * @return the visitor result
87
+ */
88
+ visitHideStatement?: (ctx: HideStatementContext) => Result;
89
+ /**
90
+ * Visit a parse tree produced by `plurnkParser.sendStatement`.
91
+ * @param ctx the parse tree
92
+ * @return the visitor result
93
+ */
94
+ visitSendStatement?: (ctx: SendStatementContext) => Result;
95
+ /**
96
+ * Visit a parse tree produced by `plurnkParser.execStatement`.
97
+ * @param ctx the parse tree
98
+ * @return the visitor result
99
+ */
100
+ visitExecStatement?: (ctx: ExecStatementContext) => Result;
101
+ /**
102
+ * Visit a parse tree produced by `plurnkParser.tagOpModifiers`.
103
+ * @param ctx the parse tree
104
+ * @return the visitor result
105
+ */
106
+ visitTagOpModifiers?: (ctx: TagOpModifiersContext) => Result;
107
+ /**
108
+ * Visit a parse tree produced by `plurnkParser.sendModifiers`.
109
+ * @param ctx the parse tree
110
+ * @return the visitor result
111
+ */
112
+ visitSendModifiers?: (ctx: SendModifiersContext) => Result;
113
+ /**
114
+ * Visit a parse tree produced by `plurnkParser.execModifiers`.
115
+ * @param ctx the parse tree
116
+ * @return the visitor result
117
+ */
118
+ visitExecModifiers?: (ctx: ExecModifiersContext) => Result;
119
+ /**
120
+ * Visit a parse tree produced by `plurnkParser.tagSignal`.
121
+ * @param ctx the parse tree
122
+ * @return the visitor result
123
+ */
124
+ visitTagSignal?: (ctx: TagSignalContext) => Result;
125
+ /**
126
+ * Visit a parse tree produced by `plurnkParser.intSignal`.
127
+ * @param ctx the parse tree
128
+ * @return the visitor result
129
+ */
130
+ visitIntSignal?: (ctx: IntSignalContext) => Result;
131
+ /**
132
+ * Visit a parse tree produced by `plurnkParser.identSignal`.
133
+ * @param ctx the parse tree
134
+ * @return the visitor result
135
+ */
136
+ visitIdentSignal?: (ctx: IdentSignalContext) => Result;
46
137
  /**
47
138
  * Visit a parse tree produced by `plurnkParser.path`.
48
139
  * @param ctx the parse tree
package/src/index.ts CHANGED
@@ -1,142 +1,29 @@
1
- import {
2
- BaseErrorListener,
3
- CharStream,
4
- CommonTokenStream,
5
- type RecognitionException,
6
- type Recognizer,
7
- type Token,
8
- } from "antlr4ng";
9
- import { plurnkLexer } from "./generated/plurnkLexer.ts";
10
- import { plurnkParser } from "./generated/plurnkParser.ts";
11
- import { buildStatement, type PlurnkStatement, type Position } from "./ast.ts";
12
- import { PlurnkParseError } from "./errors.ts";
13
- import { PlurnkErrorStrategy, translateLexerMessage } from "./error-strategy.ts";
14
-
15
- export { PlurnkParseError } from "./errors.ts";
16
- export type { ErrorSource } from "./errors.ts";
17
- export {
18
- type PlurnkStatement,
19
- type PlurnkOp,
20
- type Position,
21
- type LineMarker,
22
- type FindStatement,
23
- type ReadStatement,
24
- type EditStatement,
25
- type CopyStatement,
26
- type MoveStatement,
27
- type ShowStatement,
28
- type HideStatement,
29
- type SendStatement,
30
- type ExecStatement,
31
- } from "./ast.ts";
32
-
33
- export type ParseItem =
34
- | { kind: "statement"; statement: PlurnkStatement }
35
- | { kind: "error"; error: PlurnkParseError }
36
- | { kind: "text"; text: string; position: Position };
37
-
38
- export type ParseResult = {
39
- items: ParseItem[];
40
- unparsedTail?: { from: Position; reason: string };
41
- };
42
-
43
- class RecordingListener extends BaseErrorListener {
44
- readonly errors: PlurnkParseError[];
45
- readonly source: "lexer" | "parser";
46
-
47
- constructor(source: "lexer" | "parser", errors: PlurnkParseError[]) {
48
- super();
49
- this.source = source;
50
- this.errors = errors;
51
- }
52
-
53
- override syntaxError(
54
- recognizer: Recognizer<any>,
55
- _offendingSymbol: Token | null,
56
- line: number,
57
- column: number,
58
- msg: string,
59
- _e: RecognitionException | null,
60
- ): void {
61
- const translated = this.source === "lexer"
62
- ? translateLexerMessage(recognizer as plurnkLexer, msg)
63
- : msg;
64
- this.errors.push(new PlurnkParseError(line, column, this.source, translated));
65
- }
66
- }
67
-
68
- const errorInRange = (
69
- err: PlurnkParseError,
70
- start: { line: number; column: number },
71
- stop: { line: number; column: number },
72
- ): boolean => {
73
- if (err.line < start.line || err.line > stop.line) return false;
74
- if (err.line === start.line && err.column < start.column) return false;
75
- if (err.line === stop.line && err.column > stop.column) return false;
76
- return true;
77
- };
78
-
79
- export const parse = (input: string): ParseResult => {
80
- const lexer = new plurnkLexer(CharStream.fromString(input));
81
- const errors: PlurnkParseError[] = [];
82
- lexer.removeErrorListeners();
83
- lexer.addErrorListener(new RecordingListener("lexer", errors));
84
-
85
- const tokenStream = new CommonTokenStream(lexer);
86
- const parser = new plurnkParser(tokenStream);
87
- parser.removeErrorListeners();
88
- parser.addErrorListener(new RecordingListener("parser", errors));
89
- parser.errorHandler = new PlurnkErrorStrategy();
90
-
91
- const tree = parser.document();
92
-
93
- const items: ParseItem[] = [];
94
- const consumedErrors = new Set<PlurnkParseError>();
95
-
96
- for (const child of tree.children ?? []) {
97
- const ctx = child as any;
98
- const start = ctx.start ?? ctx.symbol;
99
- const stop = ctx.stop ?? ctx.symbol;
100
- if (!start) continue;
101
-
102
- if (ctx.ruleIndex === plurnkParser.RULE_statement) {
103
- const errForStatement = errors.find(
104
- (e) => !consumedErrors.has(e) && errorInRange(e, start, stop ?? start),
105
- );
106
- if (errForStatement) {
107
- consumedErrors.add(errForStatement);
108
- items.push({ kind: "error", error: errForStatement });
109
- } else {
110
- try {
111
- items.push({ kind: "statement", statement: buildStatement(ctx) });
112
- } catch (e) {
113
- if (e instanceof PlurnkParseError) {
114
- items.push({ kind: "error", error: e });
115
- } else {
116
- throw e;
117
- }
118
- }
119
- }
120
- } else if (ctx.symbol?.type === plurnkLexer.TEXT) {
121
- const position: Position = { line: start.line, column: start.column };
122
- items.push({ kind: "text", text: ctx.symbol.text ?? "", position });
123
- }
124
- }
125
-
126
- for (const err of errors) {
127
- if (!consumedErrors.has(err)) {
128
- items.push({ kind: "error", error: err });
129
- }
130
- }
131
-
132
- let unparsedTail: ParseResult["unparsedTail"];
133
- if (lexer.mode !== 0) {
134
- const lastToken = tokenStream.get(tokenStream.size - 1);
135
- unparsedTail = {
136
- from: { line: lastToken?.line ?? 0, column: lastToken?.column ?? 0 },
137
- reason: `lexer ended in non-default mode (${lexer.mode}); a statement was likely never closed`,
138
- };
139
- }
140
-
141
- return { items, unparsedTail };
142
- };
1
+ export { default as PlurnkParser } from "./PlurnkParser.ts";
2
+ export { default as PlurnkParseError } from "./PlurnkParseError.ts";
3
+ export { default as PlurnkErrorStrategy } from "./PlurnkErrorStrategy.ts";
4
+ export { default as AstBuilder } from "./AstBuilder.ts";
5
+ export { default as RecordingListener } from "./RecordingListener.ts";
6
+
7
+ export type { ErrorSource } from "./PlurnkParseError.ts";
8
+ export type {
9
+ CopyStatement,
10
+ EditStatement,
11
+ ExecStatement,
12
+ FindStatement,
13
+ HideStatement,
14
+ LineMarker,
15
+ LocalPath,
16
+ MatcherBody,
17
+ MoveStatement,
18
+ ParseItem,
19
+ ParseResult,
20
+ ParsedPath,
21
+ PlurnkOp,
22
+ PlurnkStatement,
23
+ Position,
24
+ ReadStatement,
25
+ SendBody,
26
+ SendStatement,
27
+ ShowStatement,
28
+ UrlPath,
29
+ } from "./types.ts";