@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.
- package/README.md +3 -3
- package/bin/plurnk.js +2 -2
- package/package.json +9 -4
- package/plurnk.md +4 -4
- package/schema/Agent.json +18 -0
- package/schema/ChannelContent.json +14 -0
- package/schema/Entry.json +51 -0
- package/schema/LineMarker.json +13 -0
- package/schema/LogEntry.json +100 -0
- package/schema/Loop.json +20 -0
- package/schema/MatcherBody.json +60 -0
- package/schema/Packet.json +64 -0
- package/schema/Params.json +13 -0
- package/schema/ParsedPath.json +51 -0
- package/schema/PlurnkStatement.json +183 -0
- package/schema/Position.json +13 -0
- package/schema/ProviderDeclaration.json +16 -0
- package/schema/Run.json +21 -0
- package/schema/SchemeRegistration.json +37 -0
- package/schema/SendBody.json +13 -0
- package/schema/Session.json +20 -0
- package/schema/Turn.json +30 -0
- package/schema/Visibility.json +17 -0
- package/src/AstBuilder.ts +372 -0
- package/src/PlurnkErrorStrategy.ts +139 -0
- package/src/{errors.ts → PlurnkParseError.ts} +1 -2
- package/src/PlurnkParser.ts +92 -0
- package/src/RecordingListener.ts +34 -0
- package/src/Validator.ts +94 -0
- package/src/generated/plurnkLexer.ts +224 -176
- package/src/generated/plurnkParser.ts +1461 -195
- package/src/generated/plurnkParserVisitor.ts +97 -6
- package/src/index.ts +29 -142
- package/src/types.generated.ts +497 -0
- package/src/types.ts +30 -0
- package/SPEC.md +0 -625
- package/src/ast.ts +0 -348
- package/src/error-strategy.ts +0 -140
package/src/Validator.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Validator as CfValidator, type OutputUnit, type Schema } from "@cfworker/json-schema";
|
|
2
|
+
import positionSchema from "../schema/Position.json" with { type: "json" };
|
|
3
|
+
import lineMarkerSchema from "../schema/LineMarker.json" with { type: "json" };
|
|
4
|
+
import paramsSchema from "../schema/Params.json" with { type: "json" };
|
|
5
|
+
import channelContentSchema from "../schema/ChannelContent.json" with { type: "json" };
|
|
6
|
+
import parsedPathSchema from "../schema/ParsedPath.json" with { type: "json" };
|
|
7
|
+
import matcherBodySchema from "../schema/MatcherBody.json" with { type: "json" };
|
|
8
|
+
import sendBodySchema from "../schema/SendBody.json" with { type: "json" };
|
|
9
|
+
import entrySchema from "../schema/Entry.json" with { type: "json" };
|
|
10
|
+
import schemeRegistrationSchema from "../schema/SchemeRegistration.json" with { type: "json" };
|
|
11
|
+
import providerDeclarationSchema from "../schema/ProviderDeclaration.json" with { type: "json" };
|
|
12
|
+
import visibilitySchema from "../schema/Visibility.json" with { type: "json" };
|
|
13
|
+
import logEntrySchema from "../schema/LogEntry.json" with { type: "json" };
|
|
14
|
+
import plurnkStatementSchema from "../schema/PlurnkStatement.json" with { type: "json" };
|
|
15
|
+
import turnSchema from "../schema/Turn.json" with { type: "json" };
|
|
16
|
+
import loopSchema from "../schema/Loop.json" with { type: "json" };
|
|
17
|
+
import runSchema from "../schema/Run.json" with { type: "json" };
|
|
18
|
+
import sessionSchema from "../schema/Session.json" with { type: "json" };
|
|
19
|
+
import agentSchema from "../schema/Agent.json" with { type: "json" };
|
|
20
|
+
import packetSchema from "../schema/Packet.json" with { type: "json" };
|
|
21
|
+
|
|
22
|
+
export type ValidationResult = { valid: boolean; errors: OutputUnit[] };
|
|
23
|
+
|
|
24
|
+
// All foundational schemas — handy when a top-level shape transitively references many others.
|
|
25
|
+
const FOUNDATIONAL = [
|
|
26
|
+
positionSchema, lineMarkerSchema, paramsSchema, channelContentSchema,
|
|
27
|
+
parsedPathSchema, matcherBodySchema, sendBodySchema,
|
|
28
|
+
plurnkStatementSchema,
|
|
29
|
+
entrySchema, logEntrySchema,
|
|
30
|
+
schemeRegistrationSchema, providerDeclarationSchema,
|
|
31
|
+
visibilitySchema,
|
|
32
|
+
packetSchema,
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
export default class Validator {
|
|
36
|
+
static #position = new CfValidator(positionSchema as Schema, "2020-12");
|
|
37
|
+
static #lineMarker = new CfValidator(lineMarkerSchema as Schema, "2020-12");
|
|
38
|
+
static #params = new CfValidator(paramsSchema as Schema, "2020-12");
|
|
39
|
+
static #channelContent = new CfValidator(channelContentSchema as Schema, "2020-12");
|
|
40
|
+
static #parsedPath = Validator.#buildWithRefs(parsedPathSchema, [paramsSchema]);
|
|
41
|
+
static #matcherBody = new CfValidator(matcherBodySchema as Schema, "2020-12");
|
|
42
|
+
static #sendBody = new CfValidator(sendBodySchema as Schema, "2020-12");
|
|
43
|
+
static #entry = Validator.#buildWithRefs(entrySchema, [paramsSchema, channelContentSchema]);
|
|
44
|
+
static #schemeRegistration = new CfValidator(schemeRegistrationSchema as Schema, "2020-12");
|
|
45
|
+
static #providerDeclaration = new CfValidator(providerDeclarationSchema as Schema, "2020-12");
|
|
46
|
+
static #visibility = new CfValidator(visibilitySchema as Schema, "2020-12");
|
|
47
|
+
static #logEntry = Validator.#buildWithRefs(logEntrySchema, [paramsSchema, lineMarkerSchema]);
|
|
48
|
+
static #plurnkStatement = Validator.#buildWithRefs(
|
|
49
|
+
plurnkStatementSchema,
|
|
50
|
+
[positionSchema, lineMarkerSchema, paramsSchema, parsedPathSchema, matcherBodySchema, sendBodySchema],
|
|
51
|
+
);
|
|
52
|
+
static #turn = Validator.#buildWithRefs(turnSchema, FOUNDATIONAL);
|
|
53
|
+
static #loop = new CfValidator(loopSchema as Schema, "2020-12");
|
|
54
|
+
static #run = new CfValidator(runSchema as Schema, "2020-12");
|
|
55
|
+
static #session = Validator.#buildWithRefs(sessionSchema, [schemeRegistrationSchema]);
|
|
56
|
+
static #agent = Validator.#buildWithRefs(agentSchema, [providerDeclarationSchema, schemeRegistrationSchema]);
|
|
57
|
+
static #packet = Validator.#buildWithRefs(packetSchema, FOUNDATIONAL);
|
|
58
|
+
|
|
59
|
+
static #buildWithRefs(mainSchema: unknown, refSchemas: unknown[]): CfValidator {
|
|
60
|
+
const validator = new CfValidator(mainSchema as Schema, "2020-12");
|
|
61
|
+
const mainId = (mainSchema as { $id?: string }).$id;
|
|
62
|
+
for (const ref of refSchemas) {
|
|
63
|
+
const refId = (ref as { $id?: string }).$id;
|
|
64
|
+
if (refId && refId === mainId) continue;
|
|
65
|
+
validator.addSchema(ref as Schema);
|
|
66
|
+
}
|
|
67
|
+
return validator;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static validatePosition(obj: unknown): ValidationResult { return Validator.#run_(Validator.#position, obj); }
|
|
71
|
+
static validateLineMarker(obj: unknown): ValidationResult { return Validator.#run_(Validator.#lineMarker, obj); }
|
|
72
|
+
static validateParams(obj: unknown): ValidationResult { return Validator.#run_(Validator.#params, obj); }
|
|
73
|
+
static validateChannelContent(obj: unknown): ValidationResult { return Validator.#run_(Validator.#channelContent, obj); }
|
|
74
|
+
static validateParsedPath(obj: unknown): ValidationResult { return Validator.#run_(Validator.#parsedPath, obj); }
|
|
75
|
+
static validateMatcherBody(obj: unknown): ValidationResult { return Validator.#run_(Validator.#matcherBody, obj); }
|
|
76
|
+
static validateSendBody(obj: unknown): ValidationResult { return Validator.#run_(Validator.#sendBody, obj); }
|
|
77
|
+
static validateEntry(obj: unknown): ValidationResult { return Validator.#run_(Validator.#entry, obj); }
|
|
78
|
+
static validateSchemeRegistration(obj: unknown): ValidationResult { return Validator.#run_(Validator.#schemeRegistration, obj); }
|
|
79
|
+
static validateProviderDeclaration(obj: unknown): ValidationResult { return Validator.#run_(Validator.#providerDeclaration, obj); }
|
|
80
|
+
static validateVisibility(obj: unknown): ValidationResult { return Validator.#run_(Validator.#visibility, obj); }
|
|
81
|
+
static validateLogEntry(obj: unknown): ValidationResult { return Validator.#run_(Validator.#logEntry, obj); }
|
|
82
|
+
static validatePlurnkStatement(obj: unknown): ValidationResult { return Validator.#run_(Validator.#plurnkStatement, obj); }
|
|
83
|
+
static validateTurn(obj: unknown): ValidationResult { return Validator.#run_(Validator.#turn, obj); }
|
|
84
|
+
static validateLoop(obj: unknown): ValidationResult { return Validator.#run_(Validator.#loop, obj); }
|
|
85
|
+
static validateRun(obj: unknown): ValidationResult { return Validator.#run_(Validator.#run, obj); }
|
|
86
|
+
static validateSession(obj: unknown): ValidationResult { return Validator.#run_(Validator.#session, obj); }
|
|
87
|
+
static validateAgent(obj: unknown): ValidationResult { return Validator.#run_(Validator.#agent, obj); }
|
|
88
|
+
static validatePacket(obj: unknown): ValidationResult { return Validator.#run_(Validator.#packet, obj); }
|
|
89
|
+
|
|
90
|
+
static #run_(validator: CfValidator, obj: unknown): ValidationResult {
|
|
91
|
+
const result = validator.validate(obj);
|
|
92
|
+
return { valid: result.valid, errors: result.errors };
|
|
93
|
+
}
|
|
94
|
+
}
|