Skip to content

Commit ffcc184

Browse files
committed
store
1 parent 67a145e commit ffcc184

24 files changed

+516
-210
lines changed

ts/packages/actionGrammar/src/grammarCompiler.ts

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { Rule, RuleDefinition, ValueNode } from "./grammarParser.js";
5-
6-
type StringPart = {
7-
type: "string";
8-
9-
value: string[];
10-
11-
/* TODO: cache the regexp?
12-
regexp?: RegExp;
13-
regexpWithPendingWildcards?: RegExp;
14-
*/
15-
};
16-
17-
type VarStringPart = {
18-
type: "wildcard";
19-
variable: string;
20-
21-
typeName: string; // Do we need this?
22-
};
23-
24-
type VarNumberPart = {
25-
type: "number";
26-
variable: string;
27-
};
28-
29-
type RulesPart = {
30-
type: "rules";
31-
32-
rules: GrammarRule[];
33-
name?: string; // Do we need this?
34-
35-
variable?: string;
36-
optional?: boolean | undefined;
37-
};
38-
39-
type GrammarPart = StringPart | VarStringPart | VarNumberPart | RulesPart;
40-
export type GrammarRule = {
41-
parts: GrammarPart[];
42-
value?: ValueNode | undefined;
43-
};
44-
45-
export type Grammar = {
46-
rules: GrammarRule[];
47-
};
4+
import {
5+
Grammar,
6+
GrammarPart,
7+
GrammarRule,
8+
StringPart,
9+
} from "./grammarTypes.js";
10+
import { Rule, RuleDefinition } from "./grammarRuleParser.js";
4811

4912
type DefinitionMap = Map<
5013
string,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import {
5+
Grammar,
6+
GrammarJson,
7+
GrammarPart,
8+
GrammarPartJson,
9+
GrammarRule,
10+
GrammarRuleJson,
11+
} from "./grammarTypes.js";
12+
13+
export function grammarFromJson(json: GrammarJson): Grammar {
14+
const start = json[0];
15+
const indexToRules: Map<number, GrammarRule[]> = new Map();
16+
function grammarRuleFromJson(r: GrammarRuleJson, json: GrammarJson) {
17+
return {
18+
parts: r.parts.map((p) => grammarPartFromJson(p, json)),
19+
value: r.value,
20+
};
21+
}
22+
function grammarPartFromJson(
23+
p: GrammarPartJson,
24+
json: GrammarJson,
25+
): GrammarPart {
26+
switch (p.type) {
27+
case "string":
28+
case "wildcard":
29+
case "number":
30+
return p;
31+
case "rules":
32+
let rules = indexToRules.get(p.index);
33+
if (rules === undefined) {
34+
rules = [];
35+
indexToRules.set(p.index, rules);
36+
for (const r of json[p.index]) {
37+
rules.push(grammarRuleFromJson(r, json));
38+
}
39+
}
40+
return {
41+
type: "rules",
42+
name: p.name,
43+
rules,
44+
variable: p.variable,
45+
optional: p.optional,
46+
};
47+
}
48+
}
49+
50+
return {
51+
rules: start.map((r) => grammarRuleFromJson(r, json)),
52+
};
53+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { compileGrammar, Grammar } from "./grammarCompiler.js";
5-
import { parseGrammar } from "./grammarParser.js";
4+
import { compileGrammar } from "./grammarCompiler.js";
5+
import { parseGrammarRules } from "./grammarRuleParser.js";
6+
import { Grammar } from "./grammarTypes.js";
67

7-
export function loadGrammar(fileName: string, content: string): Grammar {
8-
const definitions = parseGrammar(fileName, content);
8+
export function loadGrammarRules(fileName: string, content: string): Grammar {
9+
const definitions = parseGrammarRules(fileName, content);
910
const grammar = compileGrammar(definitions);
1011
return grammar;
1112
}

ts/packages/actionGrammar/src/grammarMatcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { ValueNode } from "./grammarParser.js";
4+
import { ValueNode } from "./grammarRuleParser.js";
55
import registerDebug from "debug";
66
// REVIEW: switch to RegExp.escape() when it becomes available.
77
import escapeMatch from "regexp.escape";
8-
import { Grammar, GrammarRule } from "./grammarCompiler.js";
8+
import { Grammar, GrammarRule } from "./grammarTypes.js";
99

1010
const debugMatch = registerDebug("typeagent:grammar:match");
1111

ts/packages/actionGrammar/src/grammarParser.ts renamed to ts/packages/actionGrammar/src/grammarRuleParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const debugParse = registerDebug("typeagent:grammar:parse");
4747
* <SingleLineComment> ::= "//" [^\n]* "\n"
4848
* <MultiLineComment> ::= "/*" .* "*\/"
4949
*/
50-
export function parseGrammar(
50+
export function parseGrammarRules(
5151
fileName: string,
5252
content: string,
5353
): RuleDefinition[] {
54-
const parser = new CacheGrammarParser(fileName, content);
54+
const parser = new GrammarRuleParser(fileName, content);
5555
const definitions = parser.parse();
5656
debugParse(JSON.stringify(definitions, undefined, 2));
5757
return definitions;
@@ -146,7 +146,7 @@ export function isExpressionSpecialChar(char: string) {
146146
return expressionsSpecialChar.includes(char);
147147
}
148148

149-
class CacheGrammarParser {
149+
class GrammarRuleParser {
150150
private curr: number = 0;
151151
constructor(
152152
private readonly fileName: string,

ts/packages/actionGrammar/src/grammarWriter.ts renamed to ts/packages/actionGrammar/src/grammarRuleWriter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
Rule,
99
RuleDefinition,
1010
ValueNode,
11-
} from "./grammarParser.js";
11+
} from "./grammarRuleParser.js";
1212

13-
export function writeGrammar(grammar: RuleDefinition[]): string {
13+
export function writeGrammarRules(grammar: RuleDefinition[]): string {
1414
const result: string[] = [];
1515
for (const def of grammar) {
1616
writeRuleDefinition(result, def);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import {
5+
Grammar,
6+
GrammarJson,
7+
GrammarPart,
8+
GrammarPartJson,
9+
GrammarRule,
10+
GrammarRuleJson,
11+
GrammarRules,
12+
} from "./grammarTypes.js";
13+
14+
export function grammarToJson(grammar: Grammar): GrammarJson {
15+
const json: GrammarRules[] = [];
16+
const rulesToIndex: Map<GrammarRule[], number> = new Map();
17+
let nextIndex = 1;
18+
19+
function grammarPartToJson(p: GrammarPart): GrammarPartJson {
20+
switch (p.type) {
21+
case "string":
22+
case "wildcard":
23+
case "number":
24+
return p;
25+
case "rules": {
26+
let index = rulesToIndex.get(p.rules);
27+
if (index === undefined) {
28+
index = nextIndex++;
29+
rulesToIndex.set(p.rules, index);
30+
json[index] = p.rules.map(grammarRuleToJson);
31+
}
32+
33+
return {
34+
name: p.name,
35+
type: "rules",
36+
index,
37+
variable: p.variable,
38+
optional: p.optional,
39+
};
40+
}
41+
}
42+
}
43+
44+
function grammarRuleToJson(r: GrammarRule): GrammarRuleJson {
45+
return {
46+
parts: r.parts.map(grammarPartToJson),
47+
value: r.value,
48+
};
49+
}
50+
51+
rulesToIndex.set(grammar.rules, 0);
52+
json[0] = grammar.rules.map(grammarRuleToJson);
53+
return json;
54+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { ValueNode } from "./grammarRuleParser.js";
5+
6+
/**
7+
* In memory types
8+
*/
9+
export type StringPart = {
10+
type: "string";
11+
12+
value: string[];
13+
14+
/* TODO: cache the regexp?
15+
regexp?: RegExp;
16+
regexpWithPendingWildcards?: RegExp;
17+
*/
18+
};
19+
20+
export type VarStringPart = {
21+
type: "wildcard";
22+
variable: string;
23+
typeName: string; // Not needed at runtime?
24+
};
25+
26+
export type VarNumberPart = {
27+
type: "number";
28+
variable: string;
29+
};
30+
31+
export type RulesPart = {
32+
type: "rules";
33+
34+
rules: GrammarRule[];
35+
name?: string | undefined; // For debugging
36+
37+
variable?: string | undefined;
38+
optional?: boolean | undefined;
39+
};
40+
41+
export type GrammarPart =
42+
| StringPart
43+
| VarStringPart
44+
| VarNumberPart
45+
| RulesPart;
46+
export type GrammarRule = {
47+
parts: GrammarPart[];
48+
value?: ValueNode | undefined;
49+
};
50+
51+
export type Grammar = {
52+
rules: GrammarRule[];
53+
};
54+
55+
/**
56+
* Serialized types
57+
*/
58+
export type StringPartJson = {
59+
type: "string";
60+
value: string[];
61+
};
62+
63+
export type VarStringPartJson = {
64+
type: "wildcard";
65+
variable: string;
66+
typeName: string;
67+
};
68+
69+
export type VarNumberPartJson = {
70+
type: "number";
71+
variable: string;
72+
};
73+
74+
export type RulePartJson = {
75+
type: "rules";
76+
name?: string | undefined;
77+
index: number;
78+
variable?: string | undefined;
79+
optional?: boolean | undefined;
80+
};
81+
82+
export type GrammarPartJson =
83+
| StringPartJson
84+
| VarStringPartJson
85+
| VarNumberPartJson
86+
| RulePartJson;
87+
88+
export type GrammarRuleJson = {
89+
parts: GrammarPartJson[];
90+
value?: ValueNode | undefined;
91+
};
92+
export type GrammarRules = GrammarRuleJson[];
93+
export type GrammarJson = GrammarRules[];
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
export { loadGrammar } from "./grammarLoader.js";
4+
export type { GrammarJson, Grammar } from "./grammarTypes.js";
5+
export { grammarFromJson } from "./grammarDeserializer.js";
6+
export { grammarToJson } from "./grammarSerializer.js";
7+
export { loadGrammarRules } from "./grammarLoader.js";
58
export { matchGrammar } from "./grammarMatcher.js";
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
export { ValueNode, Expr, Rule, RuleDefinition } from "./grammarParser.js";
5-
export { writeGrammar } from "./grammarWriter.js";
4+
export { ValueNode, Expr, Rule, RuleDefinition } from "./grammarRuleParser.js";
5+
export { writeGrammarRules } from "./grammarRuleWriter.js";

0 commit comments

Comments
 (0)