+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import { GenerateDtsOptions, Rule } from "@chevrotain/types";
|
||||
export declare function generateCstDts(productions: Record<string, Rule>, options?: GenerateDtsOptions): string;
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import { buildModel } from "./model.js";
|
||||
import { genDts } from "./generate.js";
|
||||
const defaultOptions = {
|
||||
includeVisitorInterface: true,
|
||||
visitorInterfaceName: "ICstNodeVisitor",
|
||||
};
|
||||
export function generateCstDts(productions, options) {
|
||||
const effectiveOptions = Object.assign(Object.assign({}, defaultOptions), options);
|
||||
const model = buildModel(productions);
|
||||
return genDts(model, effectiveOptions);
|
||||
}
|
||||
//# sourceMappingURL=api.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEvC,MAAM,cAAc,GAAiC;IACnD,uBAAuB,EAAE,IAAI;IAC7B,oBAAoB,EAAE,iBAAiB;CACxC,CAAC;AAEF,MAAM,UAAU,cAAc,CAC5B,WAAiC,EACjC,OAA4B;IAE5B,MAAM,gBAAgB,mCACjB,cAAc,GACd,OAAO,CACX,CAAC;IAEF,MAAM,KAAK,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAEtC,OAAO,MAAM,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;AACzC,CAAC"}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { GenerateDtsOptions } from "@chevrotain/types";
|
||||
import { CstNodeTypeDefinition } from "./model.js";
|
||||
export declare function genDts(model: CstNodeTypeDefinition[], options: Required<GenerateDtsOptions>): string;
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
export function genDts(model, options) {
|
||||
let contentParts = [];
|
||||
contentParts = contentParts.concat(`import type { CstNode, ICstVisitor, IToken } from "chevrotain";`);
|
||||
contentParts = contentParts.concat(model.flatMap((node) => genCstNodeTypes(node)));
|
||||
if (options.includeVisitorInterface) {
|
||||
contentParts = contentParts.concat(genVisitor(options.visitorInterfaceName, model));
|
||||
}
|
||||
return contentParts.join("\n\n") + "\n";
|
||||
}
|
||||
function genCstNodeTypes(node) {
|
||||
const nodeCstInterface = genNodeInterface(node);
|
||||
const nodeChildrenInterface = genNodeChildrenType(node);
|
||||
return [nodeCstInterface, nodeChildrenInterface];
|
||||
}
|
||||
function genNodeInterface(node) {
|
||||
const nodeInterfaceName = getNodeInterfaceName(node.name);
|
||||
const childrenTypeName = getNodeChildrenTypeName(node.name);
|
||||
return `export interface ${nodeInterfaceName} extends CstNode {
|
||||
name: "${node.name}";
|
||||
children: ${childrenTypeName};
|
||||
}`;
|
||||
}
|
||||
function genNodeChildrenType(node) {
|
||||
const typeName = getNodeChildrenTypeName(node.name);
|
||||
return `export type ${typeName} = {
|
||||
${node.properties.map((property) => genChildProperty(property)).join("\n ")}
|
||||
};`;
|
||||
}
|
||||
function genChildProperty(prop) {
|
||||
const typeName = buildTypeString(prop.type);
|
||||
return `${prop.name}${prop.optional ? "?" : ""}: ${typeName}[];`;
|
||||
}
|
||||
function genVisitor(name, nodes) {
|
||||
return `export interface ${name}<IN, OUT> extends ICstVisitor<IN, OUT> {
|
||||
${nodes.map((node) => genVisitorFunction(node)).join("\n ")}
|
||||
}`;
|
||||
}
|
||||
function genVisitorFunction(node) {
|
||||
const childrenTypeName = getNodeChildrenTypeName(node.name);
|
||||
return `${node.name}(children: ${childrenTypeName}, param?: IN): OUT;`;
|
||||
}
|
||||
function buildTypeString(type) {
|
||||
if (Array.isArray(type)) {
|
||||
const typeNames = [...new Set(type.map((t) => getTypeString(t)))];
|
||||
const typeString = typeNames.join(" | ");
|
||||
return "(" + typeString + ")";
|
||||
}
|
||||
else {
|
||||
return getTypeString(type);
|
||||
}
|
||||
}
|
||||
function getTypeString(type) {
|
||||
if (type.kind === "token") {
|
||||
return "IToken";
|
||||
}
|
||||
return getNodeInterfaceName(type.name);
|
||||
}
|
||||
function getNodeInterfaceName(ruleName) {
|
||||
return ruleName.charAt(0).toUpperCase() + ruleName.slice(1) + "CstNode";
|
||||
}
|
||||
function getNodeChildrenTypeName(ruleName) {
|
||||
return ruleName.charAt(0).toUpperCase() + ruleName.slice(1) + "CstChildren";
|
||||
}
|
||||
//# sourceMappingURL=generate.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../../src/generate.ts"],"names":[],"mappings":"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"}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import type { Rule } from "@chevrotain/types";
|
||||
export declare function buildModel(productions: Record<string, Rule>): CstNodeTypeDefinition[];
|
||||
export type CstNodeTypeDefinition = {
|
||||
name: string;
|
||||
properties: PropertyTypeDefinition[];
|
||||
};
|
||||
export type PropertyTypeDefinition = {
|
||||
name: string;
|
||||
type: PropertyArrayType;
|
||||
optional: boolean;
|
||||
};
|
||||
export type PropertyArrayType = TokenArrayType | RuleArrayType | (TokenArrayType | RuleArrayType)[];
|
||||
export type TokenArrayType = {
|
||||
kind: "token";
|
||||
};
|
||||
export type RuleArrayType = {
|
||||
kind: "rule";
|
||||
name: string;
|
||||
};
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import { GAstVisitor, NonTerminal } from "@chevrotain/gast";
|
||||
export function buildModel(productions) {
|
||||
const generator = new CstNodeDefinitionGenerator();
|
||||
const allRules = Object.values(productions);
|
||||
return allRules.map((rule) => generator.visitRule(rule));
|
||||
}
|
||||
class CstNodeDefinitionGenerator extends GAstVisitor {
|
||||
visitRule(node) {
|
||||
const rawElements = this.visitEach(node.definition);
|
||||
const grouped = Object.groupBy(rawElements, (el) => el.propertyName);
|
||||
const properties = Object.entries(grouped).map(([propertyName, group]) => {
|
||||
const allNullable = !group.some((el) => !el.canBeNull);
|
||||
// In an alternation with a label a property name can have
|
||||
// multiple types.
|
||||
let propertyType = group[0].type;
|
||||
if (group.length > 1) {
|
||||
propertyType = group.map((g) => g.type);
|
||||
}
|
||||
return {
|
||||
name: propertyName,
|
||||
type: propertyType,
|
||||
optional: allNullable,
|
||||
};
|
||||
});
|
||||
return {
|
||||
name: node.name,
|
||||
properties: properties,
|
||||
};
|
||||
}
|
||||
visitAlternative(node) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
visitOption(node) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
visitRepetition(node) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
visitRepetitionMandatory(node) {
|
||||
return this.visitEach(node.definition);
|
||||
}
|
||||
visitRepetitionMandatoryWithSeparator(node) {
|
||||
return this.visitEach(node.definition).concat({
|
||||
propertyName: node.separator.name,
|
||||
canBeNull: true,
|
||||
type: getType(node.separator),
|
||||
});
|
||||
}
|
||||
visitRepetitionWithSeparator(node) {
|
||||
return this.visitEachAndOverrideWith(node.definition, {
|
||||
canBeNull: true,
|
||||
}).concat({
|
||||
propertyName: node.separator.name,
|
||||
canBeNull: true,
|
||||
type: getType(node.separator),
|
||||
});
|
||||
}
|
||||
visitAlternation(node) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
visitTerminal(node) {
|
||||
return [
|
||||
{
|
||||
propertyName: node.label || node.terminalType.name,
|
||||
canBeNull: false,
|
||||
type: getType(node),
|
||||
},
|
||||
];
|
||||
}
|
||||
visitNonTerminal(node) {
|
||||
return [
|
||||
{
|
||||
propertyName: node.label || node.nonTerminalName,
|
||||
canBeNull: false,
|
||||
type: getType(node),
|
||||
},
|
||||
];
|
||||
}
|
||||
visitEachAndOverrideWith(definition, override) {
|
||||
return this.visitEach(definition).map((definition) => (Object.assign(Object.assign({}, definition), override)));
|
||||
}
|
||||
visitEach(definition) {
|
||||
return definition.flatMap((definition) => this.visit(definition));
|
||||
}
|
||||
}
|
||||
function getType(production) {
|
||||
if (production instanceof NonTerminal) {
|
||||
return {
|
||||
kind: "rule",
|
||||
name: production.referencedRule.name,
|
||||
};
|
||||
}
|
||||
return { kind: "token" };
|
||||
}
|
||||
//# sourceMappingURL=model.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/model.ts"],"names":[],"mappings":"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"}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "@chevrotain/cst-dts-gen",
|
||||
"version": "12.0.0",
|
||||
"description": "Generates type definitions for Chevrotain CST nodes",
|
||||
"keywords": [],
|
||||
"bugs": {
|
||||
"url": "https://github.com/Chevrotain/chevrotain/issues"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"types": "./lib/src/api.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./lib/src/api.js",
|
||||
"types": "./lib/src/api.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"lib/src/**/*.js",
|
||||
"lib/src/**/*.map",
|
||||
"lib/src/**/*.d.ts",
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/Chevrotain/chevrotain.git"
|
||||
},
|
||||
"scripts": {
|
||||
"---------- CI FLOWS --------": "",
|
||||
"ci": "pnpm run build test",
|
||||
"build": "npm-run-all clean compile",
|
||||
"test": "echo \"tests are in a different package\"",
|
||||
"---------- BUILD STEPS --------": "",
|
||||
"clean": "shx rm -rf lib",
|
||||
"compile:watch": "tsc -w",
|
||||
"compile": "tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"@chevrotain/gast": "12.0.0",
|
||||
"@chevrotain/types": "12.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "d3b4d05d5dd53d53d27175119bc7c9ed8056e3d5"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import { GenerateDtsOptions, Rule } from "@chevrotain/types";
|
||||
import { buildModel } from "./model.js";
|
||||
import { genDts } from "./generate.js";
|
||||
|
||||
const defaultOptions: Required<GenerateDtsOptions> = {
|
||||
includeVisitorInterface: true,
|
||||
visitorInterfaceName: "ICstNodeVisitor",
|
||||
};
|
||||
|
||||
export function generateCstDts(
|
||||
productions: Record<string, Rule>,
|
||||
options?: GenerateDtsOptions,
|
||||
): string {
|
||||
const effectiveOptions = {
|
||||
...defaultOptions,
|
||||
...options,
|
||||
};
|
||||
|
||||
const model = buildModel(productions);
|
||||
|
||||
return genDts(model, effectiveOptions);
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
import { GenerateDtsOptions } from "@chevrotain/types";
|
||||
import {
|
||||
CstNodeTypeDefinition,
|
||||
PropertyArrayType,
|
||||
PropertyTypeDefinition,
|
||||
RuleArrayType,
|
||||
TokenArrayType,
|
||||
} from "./model.js";
|
||||
|
||||
export function genDts(
|
||||
model: CstNodeTypeDefinition[],
|
||||
options: Required<GenerateDtsOptions>,
|
||||
): string {
|
||||
let contentParts: string[] = [];
|
||||
|
||||
contentParts = contentParts.concat(
|
||||
`import type { CstNode, ICstVisitor, IToken } from "chevrotain";`,
|
||||
);
|
||||
|
||||
contentParts = contentParts.concat(
|
||||
model.flatMap((node) => genCstNodeTypes(node)),
|
||||
);
|
||||
|
||||
if (options.includeVisitorInterface) {
|
||||
contentParts = contentParts.concat(
|
||||
genVisitor(options.visitorInterfaceName, model),
|
||||
);
|
||||
}
|
||||
|
||||
return contentParts.join("\n\n") + "\n";
|
||||
}
|
||||
|
||||
function genCstNodeTypes(node: CstNodeTypeDefinition) {
|
||||
const nodeCstInterface = genNodeInterface(node);
|
||||
const nodeChildrenInterface = genNodeChildrenType(node);
|
||||
|
||||
return [nodeCstInterface, nodeChildrenInterface];
|
||||
}
|
||||
|
||||
function genNodeInterface(node: CstNodeTypeDefinition) {
|
||||
const nodeInterfaceName = getNodeInterfaceName(node.name);
|
||||
const childrenTypeName = getNodeChildrenTypeName(node.name);
|
||||
|
||||
return `export interface ${nodeInterfaceName} extends CstNode {
|
||||
name: "${node.name}";
|
||||
children: ${childrenTypeName};
|
||||
}`;
|
||||
}
|
||||
|
||||
function genNodeChildrenType(node: CstNodeTypeDefinition) {
|
||||
const typeName = getNodeChildrenTypeName(node.name);
|
||||
|
||||
return `export type ${typeName} = {
|
||||
${node.properties.map((property) => genChildProperty(property)).join("\n ")}
|
||||
};`;
|
||||
}
|
||||
|
||||
function genChildProperty(prop: PropertyTypeDefinition) {
|
||||
const typeName = buildTypeString(prop.type);
|
||||
return `${prop.name}${prop.optional ? "?" : ""}: ${typeName}[];`;
|
||||
}
|
||||
|
||||
function genVisitor(name: string, nodes: CstNodeTypeDefinition[]) {
|
||||
return `export interface ${name}<IN, OUT> extends ICstVisitor<IN, OUT> {
|
||||
${nodes.map((node) => genVisitorFunction(node)).join("\n ")}
|
||||
}`;
|
||||
}
|
||||
|
||||
function genVisitorFunction(node: CstNodeTypeDefinition) {
|
||||
const childrenTypeName = getNodeChildrenTypeName(node.name);
|
||||
return `${node.name}(children: ${childrenTypeName}, param?: IN): OUT;`;
|
||||
}
|
||||
|
||||
function buildTypeString(type: PropertyArrayType) {
|
||||
if (Array.isArray(type)) {
|
||||
const typeNames = [...new Set(type.map((t) => getTypeString(t)))];
|
||||
const typeString = typeNames.join(" | ");
|
||||
return "(" + typeString + ")";
|
||||
} else {
|
||||
return getTypeString(type);
|
||||
}
|
||||
}
|
||||
|
||||
function getTypeString(type: TokenArrayType | RuleArrayType) {
|
||||
if (type.kind === "token") {
|
||||
return "IToken";
|
||||
}
|
||||
return getNodeInterfaceName(type.name);
|
||||
}
|
||||
|
||||
function getNodeInterfaceName(ruleName: string) {
|
||||
return ruleName.charAt(0).toUpperCase() + ruleName.slice(1) + "CstNode";
|
||||
}
|
||||
|
||||
function getNodeChildrenTypeName(ruleName: string) {
|
||||
return ruleName.charAt(0).toUpperCase() + ruleName.slice(1) + "CstChildren";
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
import type {
|
||||
Alternation,
|
||||
Alternative,
|
||||
IProduction,
|
||||
Option,
|
||||
Repetition,
|
||||
RepetitionMandatory,
|
||||
RepetitionMandatoryWithSeparator,
|
||||
RepetitionWithSeparator,
|
||||
Rule,
|
||||
Terminal,
|
||||
TokenType,
|
||||
} from "@chevrotain/types";
|
||||
import { GAstVisitor, NonTerminal } from "@chevrotain/gast";
|
||||
|
||||
export function buildModel(
|
||||
productions: Record<string, Rule>,
|
||||
): CstNodeTypeDefinition[] {
|
||||
const generator = new CstNodeDefinitionGenerator();
|
||||
const allRules = Object.values(productions);
|
||||
return allRules.map((rule) => generator.visitRule(rule));
|
||||
}
|
||||
|
||||
export type CstNodeTypeDefinition = {
|
||||
name: string;
|
||||
properties: PropertyTypeDefinition[];
|
||||
};
|
||||
|
||||
export type PropertyTypeDefinition = {
|
||||
name: string;
|
||||
type: PropertyArrayType;
|
||||
optional: boolean;
|
||||
};
|
||||
|
||||
export type PropertyArrayType =
|
||||
| TokenArrayType
|
||||
| RuleArrayType
|
||||
| (TokenArrayType | RuleArrayType)[];
|
||||
|
||||
export type TokenArrayType = { kind: "token" };
|
||||
export type RuleArrayType = {
|
||||
kind: "rule";
|
||||
name: string;
|
||||
};
|
||||
|
||||
class CstNodeDefinitionGenerator extends GAstVisitor {
|
||||
visitRule(node: Rule): CstNodeTypeDefinition {
|
||||
const rawElements = this.visitEach(node.definition);
|
||||
|
||||
const grouped = Object.groupBy(rawElements, (el) => el.propertyName);
|
||||
const properties = Object.entries(grouped).map(([propertyName, group]) => {
|
||||
const allNullable = !group!.some((el) => !el.canBeNull);
|
||||
|
||||
// In an alternation with a label a property name can have
|
||||
// multiple types.
|
||||
let propertyType: PropertyArrayType = group![0].type;
|
||||
if (group!.length > 1) {
|
||||
propertyType = group!.map((g) => g.type);
|
||||
}
|
||||
|
||||
return {
|
||||
name: propertyName,
|
||||
type: propertyType,
|
||||
optional: allNullable,
|
||||
} as PropertyTypeDefinition;
|
||||
});
|
||||
|
||||
return {
|
||||
name: node.name,
|
||||
properties: properties,
|
||||
};
|
||||
}
|
||||
|
||||
visitAlternative(node: Alternative) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
|
||||
visitOption(node: Option) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
|
||||
visitRepetition(node: Repetition) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
|
||||
visitRepetitionMandatory(node: RepetitionMandatory) {
|
||||
return this.visitEach(node.definition);
|
||||
}
|
||||
|
||||
visitRepetitionMandatoryWithSeparator(
|
||||
node: RepetitionMandatoryWithSeparator,
|
||||
) {
|
||||
return this.visitEach(node.definition).concat({
|
||||
propertyName: node.separator.name,
|
||||
canBeNull: true,
|
||||
type: getType(node.separator),
|
||||
});
|
||||
}
|
||||
|
||||
visitRepetitionWithSeparator(node: RepetitionWithSeparator) {
|
||||
return this.visitEachAndOverrideWith(node.definition, {
|
||||
canBeNull: true,
|
||||
}).concat({
|
||||
propertyName: node.separator.name,
|
||||
canBeNull: true,
|
||||
type: getType(node.separator),
|
||||
});
|
||||
}
|
||||
|
||||
visitAlternation(node: Alternation) {
|
||||
return this.visitEachAndOverrideWith(node.definition, { canBeNull: true });
|
||||
}
|
||||
|
||||
visitTerminal(node: Terminal): PropertyTupleElement[] {
|
||||
return [
|
||||
{
|
||||
propertyName: node.label || node.terminalType.name,
|
||||
canBeNull: false,
|
||||
type: getType(node),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
visitNonTerminal(node: NonTerminal): PropertyTupleElement[] {
|
||||
return [
|
||||
{
|
||||
propertyName: node.label || node.nonTerminalName,
|
||||
canBeNull: false,
|
||||
type: getType(node),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private visitEachAndOverrideWith(
|
||||
definition: IProduction[],
|
||||
override: Partial<PropertyTupleElement>,
|
||||
) {
|
||||
return this.visitEach(definition).map(
|
||||
(definition) => ({ ...definition, ...override }) as PropertyTupleElement,
|
||||
);
|
||||
}
|
||||
|
||||
private visitEach(definition: IProduction[]) {
|
||||
return definition.flatMap(
|
||||
(definition) => this.visit(definition) as PropertyTupleElement[],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
type PropertyTupleElement = {
|
||||
propertyName: string;
|
||||
canBeNull: boolean;
|
||||
type: TokenArrayType | RuleArrayType;
|
||||
};
|
||||
|
||||
function getType(
|
||||
production: Terminal | NonTerminal | TokenType,
|
||||
): TokenArrayType | RuleArrayType {
|
||||
if (production instanceof NonTerminal) {
|
||||
return {
|
||||
kind: "rule",
|
||||
name: production.referencedRule.name,
|
||||
};
|
||||
}
|
||||
|
||||
return { kind: "token" };
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { Rule, Terminal, NonTerminal, Option, Repetition, RepetitionMandatory, RepetitionMandatoryWithSeparator, RepetitionWithSeparator, Alternation, Alternative, serializeGrammar, serializeProduction, } from "./model.js";
|
||||
export { GAstVisitor } from "./visitor.js";
|
||||
export { getProductionDslName, isOptionalProd, isBranchingProd, isSequenceProd, } from "./helpers.js";
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export { Rule, Terminal, NonTerminal, Option, Repetition, RepetitionMandatory, RepetitionMandatoryWithSeparator, RepetitionWithSeparator, Alternation, Alternative, serializeGrammar, serializeProduction, } from "./model.js";
|
||||
export { GAstVisitor } from "./visitor.js";
|
||||
export { getProductionDslName, isOptionalProd, isBranchingProd, isSequenceProd, } from "./helpers.js";
|
||||
//# sourceMappingURL=api.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,QAAQ,EACR,WAAW,EACX,MAAM,EACN,UAAU,EACV,mBAAmB,EACnB,gCAAgC,EAChC,uBAAuB,EACvB,WAAW,EACX,WAAW,EACX,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,EACL,oBAAoB,EACpB,cAAc,EACd,eAAe,EACf,cAAc,GACf,MAAM,cAAc,CAAC"}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { NonTerminal } from "./model.js";
|
||||
import type { IProduction, IProductionWithOccurrence } from "@chevrotain/types";
|
||||
export declare function isSequenceProd(prod: IProduction): prod is {
|
||||
definition: IProduction[];
|
||||
} & IProduction;
|
||||
export declare function isOptionalProd(prod: IProduction, alreadyVisited?: NonTerminal[]): boolean;
|
||||
export declare function isBranchingProd(prod: IProduction): prod is {
|
||||
definition: IProduction[];
|
||||
} & IProduction;
|
||||
export declare function getProductionDslName(prod: IProductionWithOccurrence): string;
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
import { AbstractProduction, Alternation, Alternative, NonTerminal, Option, Repetition, RepetitionMandatory, RepetitionMandatoryWithSeparator, RepetitionWithSeparator, Rule, Terminal, } from "./model.js";
|
||||
export function isSequenceProd(prod) {
|
||||
return (prod instanceof Alternative ||
|
||||
prod instanceof Option ||
|
||||
prod instanceof Repetition ||
|
||||
prod instanceof RepetitionMandatory ||
|
||||
prod instanceof RepetitionMandatoryWithSeparator ||
|
||||
prod instanceof RepetitionWithSeparator ||
|
||||
prod instanceof Terminal ||
|
||||
prod instanceof Rule);
|
||||
}
|
||||
export function isOptionalProd(prod, alreadyVisited = []) {
|
||||
const isDirectlyOptional = prod instanceof Option ||
|
||||
prod instanceof Repetition ||
|
||||
prod instanceof RepetitionWithSeparator;
|
||||
if (isDirectlyOptional) {
|
||||
return true;
|
||||
}
|
||||
// note that this can cause infinite loop if one optional empty TOP production has a cyclic dependency with another
|
||||
// empty optional top rule
|
||||
// may be indirectly optional ((A?B?C?) | (D?E?F?))
|
||||
if (prod instanceof Alternation) {
|
||||
// for OR its enough for just one of the alternatives to be optional
|
||||
return prod.definition.some((subProd) => {
|
||||
return isOptionalProd(subProd, alreadyVisited);
|
||||
});
|
||||
}
|
||||
else if (prod instanceof NonTerminal && alreadyVisited.includes(prod)) {
|
||||
// avoiding stack overflow due to infinite recursion
|
||||
return false;
|
||||
}
|
||||
else if (prod instanceof AbstractProduction) {
|
||||
if (prod instanceof NonTerminal) {
|
||||
alreadyVisited.push(prod);
|
||||
}
|
||||
return prod.definition.every((subProd) => {
|
||||
return isOptionalProd(subProd, alreadyVisited);
|
||||
});
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
export function isBranchingProd(prod) {
|
||||
return prod instanceof Alternation;
|
||||
}
|
||||
export function getProductionDslName(prod) {
|
||||
/* istanbul ignore else */
|
||||
if (prod instanceof NonTerminal) {
|
||||
return "SUBRULE";
|
||||
}
|
||||
else if (prod instanceof Option) {
|
||||
return "OPTION";
|
||||
}
|
||||
else if (prod instanceof Alternation) {
|
||||
return "OR";
|
||||
}
|
||||
else if (prod instanceof RepetitionMandatory) {
|
||||
return "AT_LEAST_ONE";
|
||||
}
|
||||
else if (prod instanceof RepetitionMandatoryWithSeparator) {
|
||||
return "AT_LEAST_ONE_SEP";
|
||||
}
|
||||
else if (prod instanceof RepetitionWithSeparator) {
|
||||
return "MANY_SEP";
|
||||
}
|
||||
else if (prod instanceof Repetition) {
|
||||
return "MANY";
|
||||
}
|
||||
else if (prod instanceof Terminal) {
|
||||
return "CONSUME";
|
||||
/* c8 ignore next 3 */
|
||||
}
|
||||
else {
|
||||
throw Error("non exhaustive match");
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=helpers.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,WAAW,EACX,MAAM,EACN,UAAU,EACV,mBAAmB,EACnB,gCAAgC,EAChC,uBAAuB,EACvB,IAAI,EACJ,QAAQ,GACT,MAAM,YAAY,CAAC;AAGpB,MAAM,UAAU,cAAc,CAC5B,IAAiB;IAEjB,OAAO,CACL,IAAI,YAAY,WAAW;QAC3B,IAAI,YAAY,MAAM;QACtB,IAAI,YAAY,UAAU;QAC1B,IAAI,YAAY,mBAAmB;QACnC,IAAI,YAAY,gCAAgC;QAChD,IAAI,YAAY,uBAAuB;QACvC,IAAI,YAAY,QAAQ;QACxB,IAAI,YAAY,IAAI,CACrB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,cAAc,CAC5B,IAAiB,EACjB,iBAAgC,EAAE;IAElC,MAAM,kBAAkB,GACtB,IAAI,YAAY,MAAM;QACtB,IAAI,YAAY,UAAU;QAC1B,IAAI,YAAY,uBAAuB,CAAC;IAC1C,IAAI,kBAAkB,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,mHAAmH;IACnH,0BAA0B;IAC1B,mDAAmD;IACnD,IAAI,IAAI,YAAY,WAAW,EAAE,CAAC;QAChC,oEAAoE;QACpE,OAAqB,IAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,OAAoB,EAAE,EAAE;YAClE,OAAO,cAAc,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC;IACL,CAAC;SAAM,IAAI,IAAI,YAAY,WAAW,IAAI,cAAc,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACxE,oDAAoD;QACpD,OAAO,KAAK,CAAC;IACf,CAAC;SAAM,IAAI,IAAI,YAAY,kBAAkB,EAAE,CAAC;QAC9C,IAAI,IAAI,YAAY,WAAW,EAAE,CAAC;YAChC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;QACD,OAA4B,IAAK,CAAC,UAAU,CAAC,KAAK,CAChD,CAAC,OAAoB,EAAE,EAAE;YACvB,OAAO,cAAc,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;QACjD,CAAC,CACF,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,MAAM,UAAU,eAAe,CAC7B,IAAiB;IAEjB,OAAO,IAAI,YAAY,WAAW,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,IAA+B;IAClE,0BAA0B;IAC1B,IAAI,IAAI,YAAY,WAAW,EAAE,CAAC;QAChC,OAAO,SAAS,CAAC;IACnB,CAAC;SAAM,IAAI,IAAI,YAAY,MAAM,EAAE,CAAC;QAClC,OAAO,QAAQ,CAAC;IAClB,CAAC;SAAM,IAAI,IAAI,YAAY,WAAW,EAAE,CAAC;QACvC,OAAO,IAAI,CAAC;IACd,CAAC;SAAM,IAAI,IAAI,YAAY,mBAAmB,EAAE,CAAC;QAC/C,OAAO,cAAc,CAAC;IACxB,CAAC;SAAM,IAAI,IAAI,YAAY,gCAAgC,EAAE,CAAC;QAC5D,OAAO,kBAAkB,CAAC;IAC5B,CAAC;SAAM,IAAI,IAAI,YAAY,uBAAuB,EAAE,CAAC;QACnD,OAAO,UAAU,CAAC;IACpB,CAAC;SAAM,IAAI,IAAI,YAAY,UAAU,EAAE,CAAC;QACtC,OAAO,MAAM,CAAC;IAChB,CAAC;SAAM,IAAI,IAAI,YAAY,QAAQ,EAAE,CAAC;QACpC,OAAO,SAAS,CAAC;QACjB,sBAAsB;IACxB,CAAC;SAAM,CAAC;QACN,MAAM,KAAK,CAAC,sBAAsB,CAAC,CAAC;IACtC,CAAC;AACH,CAAC"}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
import type { IGASTVisitor, IProduction, IProductionWithOccurrence, ISerializedGast, TokenType } from "@chevrotain/types";
|
||||
export declare abstract class AbstractProduction<T extends IProduction = IProduction> implements IProduction {
|
||||
protected _definition: T[];
|
||||
get definition(): T[];
|
||||
set definition(value: T[]);
|
||||
constructor(_definition: T[]);
|
||||
accept(visitor: IGASTVisitor): void;
|
||||
}
|
||||
export declare class NonTerminal extends AbstractProduction implements IProductionWithOccurrence {
|
||||
nonTerminalName: string;
|
||||
label?: string;
|
||||
referencedRule: Rule;
|
||||
idx: number;
|
||||
constructor(options: {
|
||||
nonTerminalName: string;
|
||||
label?: string;
|
||||
referencedRule?: Rule;
|
||||
idx?: number;
|
||||
});
|
||||
set definition(definition: IProduction[]);
|
||||
get definition(): IProduction[];
|
||||
accept(visitor: IGASTVisitor): void;
|
||||
}
|
||||
export declare class Rule extends AbstractProduction {
|
||||
name: string;
|
||||
orgText: string;
|
||||
constructor(options: {
|
||||
name: string;
|
||||
definition: IProduction[];
|
||||
orgText?: string;
|
||||
});
|
||||
}
|
||||
export declare class Alternative extends AbstractProduction {
|
||||
ignoreAmbiguities: boolean;
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
ignoreAmbiguities?: boolean;
|
||||
});
|
||||
}
|
||||
export declare class Option extends AbstractProduction implements IProductionWithOccurrence {
|
||||
idx: number;
|
||||
maxLookahead?: number;
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
idx?: number;
|
||||
maxLookahead?: number;
|
||||
});
|
||||
}
|
||||
export declare class RepetitionMandatory extends AbstractProduction implements IProductionWithOccurrence {
|
||||
idx: number;
|
||||
maxLookahead?: number;
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
idx?: number;
|
||||
maxLookahead?: number;
|
||||
});
|
||||
}
|
||||
export declare class RepetitionMandatoryWithSeparator extends AbstractProduction implements IProductionWithOccurrence {
|
||||
separator: TokenType;
|
||||
idx: number;
|
||||
maxLookahead?: number;
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
separator: TokenType;
|
||||
idx?: number;
|
||||
});
|
||||
}
|
||||
export declare class Repetition extends AbstractProduction implements IProductionWithOccurrence {
|
||||
separator: TokenType;
|
||||
idx: number;
|
||||
maxLookahead?: number;
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
idx?: number;
|
||||
maxLookahead?: number;
|
||||
});
|
||||
}
|
||||
export declare class RepetitionWithSeparator extends AbstractProduction implements IProductionWithOccurrence {
|
||||
separator: TokenType;
|
||||
idx: number;
|
||||
maxLookahead?: number;
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
separator: TokenType;
|
||||
idx?: number;
|
||||
});
|
||||
}
|
||||
export declare class Alternation extends AbstractProduction<Alternative> implements IProductionWithOccurrence {
|
||||
idx: number;
|
||||
ignoreAmbiguities: boolean;
|
||||
hasPredicates: boolean;
|
||||
maxLookahead?: number;
|
||||
get definition(): Alternative[];
|
||||
set definition(value: Alternative[]);
|
||||
constructor(options: {
|
||||
definition: Alternative[];
|
||||
idx?: number;
|
||||
ignoreAmbiguities?: boolean;
|
||||
hasPredicates?: boolean;
|
||||
maxLookahead?: number;
|
||||
});
|
||||
}
|
||||
export declare class Terminal implements IProductionWithOccurrence {
|
||||
terminalType: TokenType;
|
||||
label?: string;
|
||||
idx: number;
|
||||
constructor(options: {
|
||||
terminalType: TokenType;
|
||||
label?: string;
|
||||
idx?: number;
|
||||
});
|
||||
accept(visitor: IGASTVisitor): void;
|
||||
}
|
||||
export interface ISerializedBasic extends ISerializedGast {
|
||||
type: "Alternative" | "Option" | "RepetitionMandatory" | "Repetition" | "Alternation";
|
||||
idx?: number;
|
||||
}
|
||||
export interface ISerializedGastRule extends ISerializedGast {
|
||||
type: "Rule";
|
||||
name: string;
|
||||
orgText: string;
|
||||
}
|
||||
export interface ISerializedNonTerminal extends ISerializedGast {
|
||||
type: "NonTerminal";
|
||||
name: string;
|
||||
label?: string;
|
||||
idx: number;
|
||||
}
|
||||
export interface ISerializedTerminal extends ISerializedGast {
|
||||
type: "Terminal";
|
||||
name: string;
|
||||
terminalLabel?: string;
|
||||
label?: string;
|
||||
pattern?: string;
|
||||
idx: number;
|
||||
}
|
||||
export interface ISerializedTerminalWithSeparator extends ISerializedGast {
|
||||
type: "RepetitionMandatoryWithSeparator" | "RepetitionWithSeparator";
|
||||
idx: number;
|
||||
separator: ISerializedTerminal;
|
||||
}
|
||||
export type ISerializedGastAny = ISerializedBasic | ISerializedGastRule | ISerializedNonTerminal | ISerializedTerminal | ISerializedTerminalWithSeparator;
|
||||
export declare function serializeGrammar(topRules: Rule[]): ISerializedGast[];
|
||||
export declare function serializeProduction(node: IProduction): ISerializedGast;
|
||||
+226
@@ -0,0 +1,226 @@
|
||||
// TODO: duplicated code to avoid extracting another sub-package -- how to avoid?
|
||||
function tokenLabel(tokType) {
|
||||
if (hasTokenLabel(tokType)) {
|
||||
return tokType.LABEL;
|
||||
}
|
||||
else {
|
||||
return tokType.name;
|
||||
}
|
||||
}
|
||||
// TODO: duplicated code to avoid extracting another sub-package -- how to avoid?
|
||||
function hasTokenLabel(obj) {
|
||||
return typeof obj.LABEL === "string" && obj.LABEL !== "";
|
||||
}
|
||||
export class AbstractProduction {
|
||||
get definition() {
|
||||
return this._definition;
|
||||
}
|
||||
set definition(value) {
|
||||
this._definition = value;
|
||||
}
|
||||
constructor(_definition) {
|
||||
this._definition = _definition;
|
||||
}
|
||||
accept(visitor) {
|
||||
visitor.visit(this);
|
||||
this.definition.forEach((prod) => {
|
||||
prod.accept(visitor);
|
||||
});
|
||||
}
|
||||
}
|
||||
export class NonTerminal extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super([]);
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
set definition(definition) {
|
||||
// immutable
|
||||
}
|
||||
get definition() {
|
||||
if (this.referencedRule !== undefined) {
|
||||
return this.referencedRule.definition;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
accept(visitor) {
|
||||
visitor.visit(this);
|
||||
// don't visit children of a reference, we will get cyclic infinite loops if we do so
|
||||
}
|
||||
}
|
||||
export class Rule extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.orgText = "";
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class Alternative extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.ignoreAmbiguities = false;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class Option extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class RepetitionMandatory extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class RepetitionMandatoryWithSeparator extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class Repetition extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class RepetitionWithSeparator extends AbstractProduction {
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class Alternation extends AbstractProduction {
|
||||
get definition() {
|
||||
return this._definition;
|
||||
}
|
||||
set definition(value) {
|
||||
this._definition = value;
|
||||
}
|
||||
constructor(options) {
|
||||
super(options.definition);
|
||||
this.idx = 1;
|
||||
this.ignoreAmbiguities = false;
|
||||
this.hasPredicates = false;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
export class Terminal {
|
||||
constructor(options) {
|
||||
this.idx = 1;
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
accept(visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
export function serializeGrammar(topRules) {
|
||||
return topRules.map(serializeProduction);
|
||||
}
|
||||
export function serializeProduction(node) {
|
||||
function convertDefinition(definition) {
|
||||
return definition.map(serializeProduction);
|
||||
}
|
||||
/* istanbul ignore else */
|
||||
if (node instanceof NonTerminal) {
|
||||
const serializedNonTerminal = {
|
||||
type: "NonTerminal",
|
||||
name: node.nonTerminalName,
|
||||
idx: node.idx,
|
||||
};
|
||||
if (typeof node.label === "string") {
|
||||
serializedNonTerminal.label = node.label;
|
||||
}
|
||||
return serializedNonTerminal;
|
||||
}
|
||||
else if (node instanceof Alternative) {
|
||||
return {
|
||||
type: "Alternative",
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof Option) {
|
||||
return {
|
||||
type: "Option",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof RepetitionMandatory) {
|
||||
return {
|
||||
type: "RepetitionMandatory",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof RepetitionMandatoryWithSeparator) {
|
||||
return {
|
||||
type: "RepetitionMandatoryWithSeparator",
|
||||
idx: node.idx,
|
||||
separator: (serializeProduction(new Terminal({ terminalType: node.separator }))),
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof RepetitionWithSeparator) {
|
||||
return {
|
||||
type: "RepetitionWithSeparator",
|
||||
idx: node.idx,
|
||||
separator: (serializeProduction(new Terminal({ terminalType: node.separator }))),
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof Repetition) {
|
||||
return {
|
||||
type: "Repetition",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof Alternation) {
|
||||
return {
|
||||
type: "Alternation",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
}
|
||||
else if (node instanceof Terminal) {
|
||||
const serializedTerminal = {
|
||||
type: "Terminal",
|
||||
name: node.terminalType.name,
|
||||
label: tokenLabel(node.terminalType),
|
||||
idx: node.idx,
|
||||
};
|
||||
if (typeof node.label === "string") {
|
||||
serializedTerminal.terminalLabel = node.label;
|
||||
}
|
||||
const pattern = node.terminalType.PATTERN;
|
||||
if (node.terminalType.PATTERN) {
|
||||
serializedTerminal.pattern =
|
||||
pattern instanceof RegExp ? pattern.source : pattern;
|
||||
}
|
||||
return serializedTerminal;
|
||||
}
|
||||
else if (node instanceof Rule) {
|
||||
return {
|
||||
type: "Rule",
|
||||
name: node.name,
|
||||
orgText: node.orgText,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
/* c8 ignore next 3 */
|
||||
}
|
||||
else {
|
||||
throw Error("non exhaustive match");
|
||||
}
|
||||
}
|
||||
function pickOnlyDefined(obj) {
|
||||
return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined));
|
||||
}
|
||||
//# sourceMappingURL=model.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+15
@@ -0,0 +1,15 @@
|
||||
import { Alternation, Alternative, NonTerminal, Option, Repetition, RepetitionMandatory, RepetitionMandatoryWithSeparator, RepetitionWithSeparator, Rule, Terminal } from "./model.js";
|
||||
import type { IProduction } from "@chevrotain/types";
|
||||
export declare abstract class GAstVisitor {
|
||||
visit(node: IProduction): any;
|
||||
visitNonTerminal(node: NonTerminal): any;
|
||||
visitAlternative(node: Alternative): any;
|
||||
visitOption(node: Option): any;
|
||||
visitRepetition(node: Repetition): any;
|
||||
visitRepetitionMandatory(node: RepetitionMandatory): any;
|
||||
visitRepetitionMandatoryWithSeparator(node: RepetitionMandatoryWithSeparator): any;
|
||||
visitRepetitionWithSeparator(node: RepetitionWithSeparator): any;
|
||||
visitAlternation(node: Alternation): any;
|
||||
visitTerminal(node: Terminal): any;
|
||||
visitRule(node: Rule): any;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import { Alternation, Alternative, NonTerminal, Option, Repetition, RepetitionMandatory, RepetitionMandatoryWithSeparator, RepetitionWithSeparator, Rule, Terminal, } from "./model.js";
|
||||
export class GAstVisitor {
|
||||
visit(node) {
|
||||
const nodeAny = node;
|
||||
switch (nodeAny.constructor) {
|
||||
case NonTerminal:
|
||||
return this.visitNonTerminal(nodeAny);
|
||||
case Alternative:
|
||||
return this.visitAlternative(nodeAny);
|
||||
case Option:
|
||||
return this.visitOption(nodeAny);
|
||||
case RepetitionMandatory:
|
||||
return this.visitRepetitionMandatory(nodeAny);
|
||||
case RepetitionMandatoryWithSeparator:
|
||||
return this.visitRepetitionMandatoryWithSeparator(nodeAny);
|
||||
case RepetitionWithSeparator:
|
||||
return this.visitRepetitionWithSeparator(nodeAny);
|
||||
case Repetition:
|
||||
return this.visitRepetition(nodeAny);
|
||||
case Alternation:
|
||||
return this.visitAlternation(nodeAny);
|
||||
case Terminal:
|
||||
return this.visitTerminal(nodeAny);
|
||||
case Rule:
|
||||
return this.visitRule(nodeAny);
|
||||
/* c8 ignore next 2 */
|
||||
default:
|
||||
throw Error("non exhaustive match");
|
||||
}
|
||||
}
|
||||
/* c8 ignore next */
|
||||
visitNonTerminal(node) { }
|
||||
/* c8 ignore next */
|
||||
visitAlternative(node) { }
|
||||
/* c8 ignore next */
|
||||
visitOption(node) { }
|
||||
/* c8 ignore next */
|
||||
visitRepetition(node) { }
|
||||
/* c8 ignore next */
|
||||
visitRepetitionMandatory(node) { }
|
||||
/* c8 ignore next 3 */
|
||||
visitRepetitionMandatoryWithSeparator(node) { }
|
||||
/* c8 ignore next */
|
||||
visitRepetitionWithSeparator(node) { }
|
||||
/* c8 ignore next */
|
||||
visitAlternation(node) { }
|
||||
/* c8 ignore next */
|
||||
visitTerminal(node) { }
|
||||
/* c8 ignore next */
|
||||
visitRule(node) { }
|
||||
}
|
||||
//# sourceMappingURL=visitor.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"visitor.js","sourceRoot":"","sources":["../../src/visitor.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,WAAW,EACX,WAAW,EACX,MAAM,EACN,UAAU,EACV,mBAAmB,EACnB,gCAAgC,EAChC,uBAAuB,EACvB,IAAI,EACJ,QAAQ,GACT,MAAM,YAAY,CAAC;AAGpB,MAAM,OAAgB,WAAW;IACxB,KAAK,CAAC,IAAiB;QAC5B,MAAM,OAAO,GAAQ,IAAI,CAAC;QAC1B,QAAQ,OAAO,CAAC,WAAW,EAAE,CAAC;YAC5B,KAAK,WAAW;gBACd,OAAO,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;YACxC,KAAK,WAAW;gBACd,OAAO,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;YACxC,KAAK,MAAM;gBACT,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;YACnC,KAAK,mBAAmB;gBACtB,OAAO,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC;YAChD,KAAK,gCAAgC;gBACnC,OAAO,IAAI,CAAC,qCAAqC,CAAC,OAAO,CAAC,CAAC;YAC7D,KAAK,uBAAuB;gBAC1B,OAAO,IAAI,CAAC,4BAA4B,CAAC,OAAO,CAAC,CAAC;YACpD,KAAK,UAAU;gBACb,OAAO,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;YACvC,KAAK,WAAW;gBACd,OAAO,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;YACxC,KAAK,QAAQ;gBACX,OAAO,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;YACrC,KAAK,IAAI;gBACP,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACjC,sBAAsB;YACtB;gBACE,MAAM,KAAK,CAAC,sBAAsB,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;IAED,oBAAoB;IACb,gBAAgB,CAAC,IAAiB,IAAQ,CAAC;IAElD,oBAAoB;IACb,gBAAgB,CAAC,IAAiB,IAAQ,CAAC;IAElD,oBAAoB;IACb,WAAW,CAAC,IAAY,IAAQ,CAAC;IAExC,oBAAoB;IACb,eAAe,CAAC,IAAgB,IAAQ,CAAC;IAEhD,oBAAoB;IACb,wBAAwB,CAAC,IAAyB,IAAQ,CAAC;IAElE,sBAAsB;IACf,qCAAqC,CAC1C,IAAsC,IAChC,CAAC;IAET,oBAAoB;IACb,4BAA4B,CAAC,IAA6B,IAAQ,CAAC;IAE1E,oBAAoB;IACb,gBAAgB,CAAC,IAAiB,IAAQ,CAAC;IAElD,oBAAoB;IACb,aAAa,CAAC,IAAc,IAAQ,CAAC;IAE5C,oBAAoB;IACb,SAAS,CAAC,IAAU,IAAQ,CAAC;CACrC"}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "@chevrotain/gast",
|
||||
"version": "12.0.0",
|
||||
"description": "Grammar AST structure for Chevrotain Parsers",
|
||||
"keywords": [],
|
||||
"bugs": {
|
||||
"url": "https://github.com/Chevrotain/chevrotain/issues"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"types": "./lib/src/api.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./lib/src/api.js",
|
||||
"types": "./lib/src/api.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"lib/src/**/*.js",
|
||||
"lib/src/**/*.map",
|
||||
"lib/src/**/*.d.ts",
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/Chevrotain/chevrotain.git"
|
||||
},
|
||||
"scripts": {
|
||||
"---------- CI FLOWS --------": "",
|
||||
"ci": "pnpm run build test",
|
||||
"build": "npm-run-all clean compile",
|
||||
"test": "npm-run-all coverage",
|
||||
"---------- DEV FLOWS --------": "",
|
||||
"update-snapshots": "node ./scripts/update-snapshots.js",
|
||||
"---------- BUILD STEPS --------": "",
|
||||
"clean": "shx rm -rf lib coverage",
|
||||
"compile:watch": "tsc -w",
|
||||
"compile": "tsc",
|
||||
"coverage": "c8 mocha --enable-source-maps"
|
||||
},
|
||||
"dependencies": {
|
||||
"@chevrotain/types": "12.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "d3b4d05d5dd53d53d27175119bc7c9ed8056e3d5"
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
export {
|
||||
Rule,
|
||||
Terminal,
|
||||
NonTerminal,
|
||||
Option,
|
||||
Repetition,
|
||||
RepetitionMandatory,
|
||||
RepetitionMandatoryWithSeparator,
|
||||
RepetitionWithSeparator,
|
||||
Alternation,
|
||||
Alternative,
|
||||
serializeGrammar,
|
||||
serializeProduction,
|
||||
} from "./model.js";
|
||||
|
||||
export { GAstVisitor } from "./visitor.js";
|
||||
|
||||
export {
|
||||
getProductionDslName,
|
||||
isOptionalProd,
|
||||
isBranchingProd,
|
||||
isSequenceProd,
|
||||
} from "./helpers.js";
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
import {
|
||||
AbstractProduction,
|
||||
Alternation,
|
||||
Alternative,
|
||||
NonTerminal,
|
||||
Option,
|
||||
Repetition,
|
||||
RepetitionMandatory,
|
||||
RepetitionMandatoryWithSeparator,
|
||||
RepetitionWithSeparator,
|
||||
Rule,
|
||||
Terminal,
|
||||
} from "./model.js";
|
||||
import type { IProduction, IProductionWithOccurrence } from "@chevrotain/types";
|
||||
|
||||
export function isSequenceProd(
|
||||
prod: IProduction,
|
||||
): prod is { definition: IProduction[] } & IProduction {
|
||||
return (
|
||||
prod instanceof Alternative ||
|
||||
prod instanceof Option ||
|
||||
prod instanceof Repetition ||
|
||||
prod instanceof RepetitionMandatory ||
|
||||
prod instanceof RepetitionMandatoryWithSeparator ||
|
||||
prod instanceof RepetitionWithSeparator ||
|
||||
prod instanceof Terminal ||
|
||||
prod instanceof Rule
|
||||
);
|
||||
}
|
||||
|
||||
export function isOptionalProd(
|
||||
prod: IProduction,
|
||||
alreadyVisited: NonTerminal[] = [],
|
||||
): boolean {
|
||||
const isDirectlyOptional =
|
||||
prod instanceof Option ||
|
||||
prod instanceof Repetition ||
|
||||
prod instanceof RepetitionWithSeparator;
|
||||
if (isDirectlyOptional) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// note that this can cause infinite loop if one optional empty TOP production has a cyclic dependency with another
|
||||
// empty optional top rule
|
||||
// may be indirectly optional ((A?B?C?) | (D?E?F?))
|
||||
if (prod instanceof Alternation) {
|
||||
// for OR its enough for just one of the alternatives to be optional
|
||||
return (<Alternation>prod).definition.some((subProd: IProduction) => {
|
||||
return isOptionalProd(subProd, alreadyVisited);
|
||||
});
|
||||
} else if (prod instanceof NonTerminal && alreadyVisited.includes(prod)) {
|
||||
// avoiding stack overflow due to infinite recursion
|
||||
return false;
|
||||
} else if (prod instanceof AbstractProduction) {
|
||||
if (prod instanceof NonTerminal) {
|
||||
alreadyVisited.push(prod);
|
||||
}
|
||||
return (<AbstractProduction>prod).definition.every(
|
||||
(subProd: IProduction) => {
|
||||
return isOptionalProd(subProd, alreadyVisited);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isBranchingProd(
|
||||
prod: IProduction,
|
||||
): prod is { definition: IProduction[] } & IProduction {
|
||||
return prod instanceof Alternation;
|
||||
}
|
||||
|
||||
export function getProductionDslName(prod: IProductionWithOccurrence): string {
|
||||
/* istanbul ignore else */
|
||||
if (prod instanceof NonTerminal) {
|
||||
return "SUBRULE";
|
||||
} else if (prod instanceof Option) {
|
||||
return "OPTION";
|
||||
} else if (prod instanceof Alternation) {
|
||||
return "OR";
|
||||
} else if (prod instanceof RepetitionMandatory) {
|
||||
return "AT_LEAST_ONE";
|
||||
} else if (prod instanceof RepetitionMandatoryWithSeparator) {
|
||||
return "AT_LEAST_ONE_SEP";
|
||||
} else if (prod instanceof RepetitionWithSeparator) {
|
||||
return "MANY_SEP";
|
||||
} else if (prod instanceof Repetition) {
|
||||
return "MANY";
|
||||
} else if (prod instanceof Terminal) {
|
||||
return "CONSUME";
|
||||
/* c8 ignore next 3 */
|
||||
} else {
|
||||
throw Error("non exhaustive match");
|
||||
}
|
||||
}
|
||||
+390
@@ -0,0 +1,390 @@
|
||||
import type {
|
||||
IGASTVisitor,
|
||||
IProduction,
|
||||
IProductionWithOccurrence,
|
||||
ISerializedGast,
|
||||
TokenType,
|
||||
} from "@chevrotain/types";
|
||||
|
||||
// TODO: duplicated code to avoid extracting another sub-package -- how to avoid?
|
||||
function tokenLabel(tokType: TokenType): string {
|
||||
if (hasTokenLabel(tokType)) {
|
||||
return tokType.LABEL;
|
||||
} else {
|
||||
return tokType.name;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: duplicated code to avoid extracting another sub-package -- how to avoid?
|
||||
function hasTokenLabel(
|
||||
obj: TokenType,
|
||||
): obj is TokenType & Pick<Required<TokenType>, "LABEL"> {
|
||||
return typeof obj.LABEL === "string" && obj.LABEL !== "";
|
||||
}
|
||||
|
||||
export abstract class AbstractProduction<
|
||||
T extends IProduction = IProduction,
|
||||
> implements IProduction {
|
||||
public get definition(): T[] {
|
||||
return this._definition;
|
||||
}
|
||||
public set definition(value: T[]) {
|
||||
this._definition = value;
|
||||
}
|
||||
|
||||
constructor(protected _definition: T[]) {}
|
||||
|
||||
accept(visitor: IGASTVisitor): void {
|
||||
visitor.visit(this);
|
||||
this.definition.forEach((prod) => {
|
||||
prod.accept(visitor);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class NonTerminal
|
||||
extends AbstractProduction
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public nonTerminalName!: string;
|
||||
public label?: string;
|
||||
public referencedRule!: Rule;
|
||||
public idx: number = 1;
|
||||
|
||||
constructor(options: {
|
||||
nonTerminalName: string;
|
||||
label?: string;
|
||||
referencedRule?: Rule;
|
||||
idx?: number;
|
||||
}) {
|
||||
super([]);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
|
||||
set definition(definition: IProduction[]) {
|
||||
// immutable
|
||||
}
|
||||
|
||||
get definition(): IProduction[] {
|
||||
if (this.referencedRule !== undefined) {
|
||||
return this.referencedRule.definition;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
accept(visitor: IGASTVisitor): void {
|
||||
visitor.visit(this);
|
||||
// don't visit children of a reference, we will get cyclic infinite loops if we do so
|
||||
}
|
||||
}
|
||||
|
||||
export class Rule extends AbstractProduction {
|
||||
public name!: string;
|
||||
public orgText: string = "";
|
||||
|
||||
constructor(options: {
|
||||
name: string;
|
||||
definition: IProduction[];
|
||||
orgText?: string;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class Alternative extends AbstractProduction {
|
||||
public ignoreAmbiguities: boolean = false;
|
||||
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
ignoreAmbiguities?: boolean;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class Option
|
||||
extends AbstractProduction
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public idx: number = 1;
|
||||
public maxLookahead?: number;
|
||||
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
idx?: number;
|
||||
maxLookahead?: number;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class RepetitionMandatory
|
||||
extends AbstractProduction
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public idx: number = 1;
|
||||
public maxLookahead?: number;
|
||||
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
idx?: number;
|
||||
maxLookahead?: number;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class RepetitionMandatoryWithSeparator
|
||||
extends AbstractProduction
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public separator!: TokenType;
|
||||
public idx: number = 1;
|
||||
public maxLookahead?: number;
|
||||
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
separator: TokenType;
|
||||
idx?: number;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class Repetition
|
||||
extends AbstractProduction
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public separator!: TokenType;
|
||||
public idx: number = 1;
|
||||
public maxLookahead?: number;
|
||||
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
idx?: number;
|
||||
maxLookahead?: number;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class RepetitionWithSeparator
|
||||
extends AbstractProduction
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public separator!: TokenType;
|
||||
public idx: number = 1;
|
||||
public maxLookahead?: number;
|
||||
|
||||
constructor(options: {
|
||||
definition: IProduction[];
|
||||
separator: TokenType;
|
||||
idx?: number;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class Alternation
|
||||
extends AbstractProduction<Alternative>
|
||||
implements IProductionWithOccurrence
|
||||
{
|
||||
public idx: number = 1;
|
||||
public ignoreAmbiguities: boolean = false;
|
||||
public hasPredicates: boolean = false;
|
||||
public maxLookahead?: number;
|
||||
|
||||
public get definition(): Alternative[] {
|
||||
return this._definition;
|
||||
}
|
||||
public set definition(value: Alternative[]) {
|
||||
this._definition = value;
|
||||
}
|
||||
|
||||
constructor(options: {
|
||||
definition: Alternative[];
|
||||
idx?: number;
|
||||
ignoreAmbiguities?: boolean;
|
||||
hasPredicates?: boolean;
|
||||
maxLookahead?: number;
|
||||
}) {
|
||||
super(options.definition);
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
}
|
||||
|
||||
export class Terminal implements IProductionWithOccurrence {
|
||||
public terminalType!: TokenType;
|
||||
public label?: string;
|
||||
public idx: number = 1;
|
||||
|
||||
constructor(options: {
|
||||
terminalType: TokenType;
|
||||
label?: string;
|
||||
idx?: number;
|
||||
}) {
|
||||
Object.assign(this, pickOnlyDefined(options));
|
||||
}
|
||||
|
||||
accept(visitor: IGASTVisitor): void {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ISerializedBasic extends ISerializedGast {
|
||||
type:
|
||||
| "Alternative"
|
||||
| "Option"
|
||||
| "RepetitionMandatory"
|
||||
| "Repetition"
|
||||
| "Alternation";
|
||||
idx?: number;
|
||||
}
|
||||
|
||||
export interface ISerializedGastRule extends ISerializedGast {
|
||||
type: "Rule";
|
||||
name: string;
|
||||
orgText: string;
|
||||
}
|
||||
|
||||
export interface ISerializedNonTerminal extends ISerializedGast {
|
||||
type: "NonTerminal";
|
||||
name: string;
|
||||
label?: string;
|
||||
idx: number;
|
||||
}
|
||||
|
||||
export interface ISerializedTerminal extends ISerializedGast {
|
||||
type: "Terminal";
|
||||
name: string;
|
||||
terminalLabel?: string;
|
||||
label?: string;
|
||||
pattern?: string;
|
||||
idx: number;
|
||||
}
|
||||
|
||||
export interface ISerializedTerminalWithSeparator extends ISerializedGast {
|
||||
type: "RepetitionMandatoryWithSeparator" | "RepetitionWithSeparator";
|
||||
idx: number;
|
||||
separator: ISerializedTerminal;
|
||||
}
|
||||
|
||||
export type ISerializedGastAny =
|
||||
| ISerializedBasic
|
||||
| ISerializedGastRule
|
||||
| ISerializedNonTerminal
|
||||
| ISerializedTerminal
|
||||
| ISerializedTerminalWithSeparator;
|
||||
|
||||
export function serializeGrammar(topRules: Rule[]): ISerializedGast[] {
|
||||
return topRules.map(serializeProduction);
|
||||
}
|
||||
|
||||
export function serializeProduction(node: IProduction): ISerializedGast {
|
||||
function convertDefinition(definition: IProduction[]): ISerializedGast[] {
|
||||
return definition.map(serializeProduction);
|
||||
}
|
||||
/* istanbul ignore else */
|
||||
if (node instanceof NonTerminal) {
|
||||
const serializedNonTerminal: ISerializedNonTerminal = {
|
||||
type: "NonTerminal",
|
||||
name: node.nonTerminalName,
|
||||
idx: node.idx,
|
||||
};
|
||||
|
||||
if (typeof node.label === "string") {
|
||||
serializedNonTerminal.label = node.label;
|
||||
}
|
||||
|
||||
return serializedNonTerminal;
|
||||
} else if (node instanceof Alternative) {
|
||||
return <ISerializedBasic>{
|
||||
type: "Alternative",
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof Option) {
|
||||
return <ISerializedBasic>{
|
||||
type: "Option",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof RepetitionMandatory) {
|
||||
return <ISerializedBasic>{
|
||||
type: "RepetitionMandatory",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof RepetitionMandatoryWithSeparator) {
|
||||
return <ISerializedTerminalWithSeparator>{
|
||||
type: "RepetitionMandatoryWithSeparator",
|
||||
idx: node.idx,
|
||||
separator: <ISerializedTerminal>(
|
||||
serializeProduction(new Terminal({ terminalType: node.separator }))
|
||||
),
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof RepetitionWithSeparator) {
|
||||
return <ISerializedTerminalWithSeparator>{
|
||||
type: "RepetitionWithSeparator",
|
||||
idx: node.idx,
|
||||
separator: <ISerializedTerminal>(
|
||||
serializeProduction(new Terminal({ terminalType: node.separator }))
|
||||
),
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof Repetition) {
|
||||
return <ISerializedBasic>{
|
||||
type: "Repetition",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof Alternation) {
|
||||
return <ISerializedBasic>{
|
||||
type: "Alternation",
|
||||
idx: node.idx,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
} else if (node instanceof Terminal) {
|
||||
const serializedTerminal = <ISerializedTerminal>{
|
||||
type: "Terminal",
|
||||
name: node.terminalType.name,
|
||||
label: tokenLabel(node.terminalType),
|
||||
idx: node.idx,
|
||||
};
|
||||
|
||||
if (typeof node.label === "string") {
|
||||
serializedTerminal.terminalLabel = node.label;
|
||||
}
|
||||
|
||||
const pattern = node.terminalType.PATTERN;
|
||||
if (node.terminalType.PATTERN) {
|
||||
serializedTerminal.pattern =
|
||||
pattern instanceof RegExp ? (<any>pattern).source : pattern;
|
||||
}
|
||||
|
||||
return serializedTerminal;
|
||||
} else if (node instanceof Rule) {
|
||||
return <ISerializedGastRule>{
|
||||
type: "Rule",
|
||||
name: node.name,
|
||||
orgText: node.orgText,
|
||||
definition: convertDefinition(node.definition),
|
||||
};
|
||||
/* c8 ignore next 3 */
|
||||
} else {
|
||||
throw Error("non exhaustive match");
|
||||
}
|
||||
}
|
||||
|
||||
function pickOnlyDefined(obj: Object): any {
|
||||
return Object.fromEntries(
|
||||
Object.entries(obj).filter(([, v]) => v !== undefined),
|
||||
);
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
Alternation,
|
||||
Alternative,
|
||||
NonTerminal,
|
||||
Option,
|
||||
Repetition,
|
||||
RepetitionMandatory,
|
||||
RepetitionMandatoryWithSeparator,
|
||||
RepetitionWithSeparator,
|
||||
Rule,
|
||||
Terminal,
|
||||
} from "./model.js";
|
||||
import type { IProduction } from "@chevrotain/types";
|
||||
|
||||
export abstract class GAstVisitor {
|
||||
public visit(node: IProduction): any {
|
||||
const nodeAny: any = node;
|
||||
switch (nodeAny.constructor) {
|
||||
case NonTerminal:
|
||||
return this.visitNonTerminal(nodeAny);
|
||||
case Alternative:
|
||||
return this.visitAlternative(nodeAny);
|
||||
case Option:
|
||||
return this.visitOption(nodeAny);
|
||||
case RepetitionMandatory:
|
||||
return this.visitRepetitionMandatory(nodeAny);
|
||||
case RepetitionMandatoryWithSeparator:
|
||||
return this.visitRepetitionMandatoryWithSeparator(nodeAny);
|
||||
case RepetitionWithSeparator:
|
||||
return this.visitRepetitionWithSeparator(nodeAny);
|
||||
case Repetition:
|
||||
return this.visitRepetition(nodeAny);
|
||||
case Alternation:
|
||||
return this.visitAlternation(nodeAny);
|
||||
case Terminal:
|
||||
return this.visitTerminal(nodeAny);
|
||||
case Rule:
|
||||
return this.visitRule(nodeAny);
|
||||
/* c8 ignore next 2 */
|
||||
default:
|
||||
throw Error("non exhaustive match");
|
||||
}
|
||||
}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitNonTerminal(node: NonTerminal): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitAlternative(node: Alternative): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitOption(node: Option): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitRepetition(node: Repetition): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitRepetitionMandatory(node: RepetitionMandatory): any {}
|
||||
|
||||
/* c8 ignore next 3 */
|
||||
public visitRepetitionMandatoryWithSeparator(
|
||||
node: RepetitionMandatoryWithSeparator,
|
||||
): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitRepetitionWithSeparator(node: RepetitionWithSeparator): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitAlternation(node: Alternation): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitTerminal(node: Terminal): any {}
|
||||
|
||||
/* c8 ignore next */
|
||||
public visitRule(node: Rule): any {}
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { RegExpParser } from "./regexp-parser.js";
|
||||
export { BaseRegExpVisitor } from "./base-regexp-visitor.js";
|
||||
//# sourceMappingURL=api.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
export class BaseRegExpVisitor {
|
||||
visitChildren(node) {
|
||||
for (const key in node) {
|
||||
const child = node[key];
|
||||
/* istanbul ignore else */
|
||||
if (node.hasOwnProperty(key)) {
|
||||
if (child.type !== undefined) {
|
||||
this.visit(child);
|
||||
}
|
||||
else if (Array.isArray(child)) {
|
||||
child.forEach((subChild) => {
|
||||
this.visit(subChild);
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
visit(node) {
|
||||
switch (node.type) {
|
||||
case "Pattern":
|
||||
this.visitPattern(node);
|
||||
break;
|
||||
case "Flags":
|
||||
this.visitFlags(node);
|
||||
break;
|
||||
case "Disjunction":
|
||||
this.visitDisjunction(node);
|
||||
break;
|
||||
case "Alternative":
|
||||
this.visitAlternative(node);
|
||||
break;
|
||||
case "StartAnchor":
|
||||
this.visitStartAnchor(node);
|
||||
break;
|
||||
case "EndAnchor":
|
||||
this.visitEndAnchor(node);
|
||||
break;
|
||||
case "WordBoundary":
|
||||
this.visitWordBoundary(node);
|
||||
break;
|
||||
case "NonWordBoundary":
|
||||
this.visitNonWordBoundary(node);
|
||||
break;
|
||||
case "Lookahead":
|
||||
this.visitLookahead(node);
|
||||
break;
|
||||
case "NegativeLookahead":
|
||||
this.visitNegativeLookahead(node);
|
||||
break;
|
||||
case "Lookbehind":
|
||||
this.visitLookbehind(node);
|
||||
break;
|
||||
case "NegativeLookbehind":
|
||||
this.visitNegativeLookbehind(node);
|
||||
break;
|
||||
case "Character":
|
||||
this.visitCharacter(node);
|
||||
break;
|
||||
case "Set":
|
||||
this.visitSet(node);
|
||||
break;
|
||||
case "Group":
|
||||
this.visitGroup(node);
|
||||
break;
|
||||
case "GroupBackReference":
|
||||
this.visitGroupBackReference(node);
|
||||
break;
|
||||
case "Quantifier":
|
||||
this.visitQuantifier(node);
|
||||
break;
|
||||
}
|
||||
this.visitChildren(node);
|
||||
}
|
||||
visitPattern(node) { }
|
||||
visitFlags(node) { }
|
||||
visitDisjunction(node) { }
|
||||
visitAlternative(node) { }
|
||||
// Assertion
|
||||
visitStartAnchor(node) { }
|
||||
visitEndAnchor(node) { }
|
||||
visitWordBoundary(node) { }
|
||||
visitNonWordBoundary(node) { }
|
||||
visitLookahead(node) { }
|
||||
visitNegativeLookahead(node) { }
|
||||
visitLookbehind(node) { }
|
||||
visitNegativeLookbehind(node) { }
|
||||
// atoms
|
||||
visitCharacter(node) { }
|
||||
visitSet(node) { }
|
||||
visitGroup(node) { }
|
||||
visitGroupBackReference(node) { }
|
||||
visitQuantifier(node) { }
|
||||
}
|
||||
//# sourceMappingURL=base-regexp-visitor.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"base-regexp-visitor.js","sourceRoot":"","sources":["../../src/base-regexp-visitor.ts"],"names":[],"mappings":"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"}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
import { cc } from "./utils.js";
|
||||
export const digitsCharCodes = [];
|
||||
for (let i = cc("0"); i <= cc("9"); i++) {
|
||||
digitsCharCodes.push(i);
|
||||
}
|
||||
export const wordCharCodes = [cc("_")].concat(digitsCharCodes);
|
||||
for (let i = cc("a"); i <= cc("z"); i++) {
|
||||
wordCharCodes.push(i);
|
||||
}
|
||||
for (let i = cc("A"); i <= cc("Z"); i++) {
|
||||
wordCharCodes.push(i);
|
||||
}
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#character-classes
|
||||
export const whitespaceCodes = [
|
||||
cc(" "),
|
||||
cc("\f"),
|
||||
cc("\n"),
|
||||
cc("\r"),
|
||||
cc("\t"),
|
||||
cc("\v"),
|
||||
cc("\t"),
|
||||
cc("\u00a0"),
|
||||
cc("\u1680"),
|
||||
cc("\u2000"),
|
||||
cc("\u2001"),
|
||||
cc("\u2002"),
|
||||
cc("\u2003"),
|
||||
cc("\u2004"),
|
||||
cc("\u2005"),
|
||||
cc("\u2006"),
|
||||
cc("\u2007"),
|
||||
cc("\u2008"),
|
||||
cc("\u2009"),
|
||||
cc("\u200a"),
|
||||
cc("\u2028"),
|
||||
cc("\u2029"),
|
||||
cc("\u202f"),
|
||||
cc("\u205f"),
|
||||
cc("\u3000"),
|
||||
cc("\ufeff"),
|
||||
];
|
||||
//# sourceMappingURL=character-classes.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"character-classes.js","sourceRoot":"","sources":["../../src/character-classes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAEhC,MAAM,CAAC,MAAM,eAAe,GAAa,EAAE,CAAC;AAC5C,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IACxC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC;AAED,MAAM,CAAC,MAAM,aAAa,GAAa,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;AACzE,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IACxC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC;AAED,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IACxC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC;AAED,4GAA4G;AAC5G,MAAM,CAAC,MAAM,eAAe,GAAa;IACvC,EAAE,CAAC,GAAG,CAAC;IACP,EAAE,CAAC,IAAI,CAAC;IACR,EAAE,CAAC,IAAI,CAAC;IACR,EAAE,CAAC,IAAI,CAAC;IACR,EAAE,CAAC,IAAI,CAAC;IACR,EAAE,CAAC,IAAI,CAAC;IACR,EAAE,CAAC,IAAI,CAAC;IACR,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;CACb,CAAC"}
|
||||
+744
@@ -0,0 +1,744 @@
|
||||
import { addFlag, ASSERT_EXISTS, ASSERT_NEVER_REACH_HERE, cc, insertToSet, isCharacter, } from "./utils.js";
|
||||
import { digitsCharCodes, whitespaceCodes, wordCharCodes, } from "./character-classes.js";
|
||||
// consts and utilities
|
||||
const hexDigitPattern = /[0-9a-fA-F]/;
|
||||
const decimalPattern = /[0-9]/;
|
||||
const decimalPatternNoZero = /[1-9]/;
|
||||
// https://hackernoon.com/the-madness-of-parsing-real-world-javascript-regexps-d9ee336df983
|
||||
// https://www.ecma-international.org/ecma-262/8.0/index.html#prod-Pattern
|
||||
export class RegExpParser {
|
||||
constructor() {
|
||||
this.idx = 0;
|
||||
this.input = "";
|
||||
this.groupIdx = 0;
|
||||
}
|
||||
saveState() {
|
||||
return {
|
||||
idx: this.idx,
|
||||
input: this.input,
|
||||
groupIdx: this.groupIdx,
|
||||
};
|
||||
}
|
||||
restoreState(newState) {
|
||||
this.idx = newState.idx;
|
||||
this.input = newState.input;
|
||||
this.groupIdx = newState.groupIdx;
|
||||
}
|
||||
pattern(input) {
|
||||
// parser state
|
||||
this.idx = 0;
|
||||
this.input = input;
|
||||
this.groupIdx = 0;
|
||||
this.consumeChar("/");
|
||||
const value = this.disjunction();
|
||||
this.consumeChar("/");
|
||||
const flags = {
|
||||
type: "Flags",
|
||||
loc: { begin: this.idx, end: input.length },
|
||||
global: false,
|
||||
ignoreCase: false,
|
||||
multiLine: false,
|
||||
unicode: false,
|
||||
sticky: false,
|
||||
};
|
||||
while (this.isRegExpFlag()) {
|
||||
switch (this.popChar()) {
|
||||
case "g":
|
||||
addFlag(flags, "global");
|
||||
break;
|
||||
case "i":
|
||||
addFlag(flags, "ignoreCase");
|
||||
break;
|
||||
case "m":
|
||||
addFlag(flags, "multiLine");
|
||||
break;
|
||||
case "u":
|
||||
addFlag(flags, "unicode");
|
||||
break;
|
||||
case "y":
|
||||
addFlag(flags, "sticky");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this.idx !== this.input.length) {
|
||||
throw Error("Redundant input: " + this.input.substring(this.idx));
|
||||
}
|
||||
return {
|
||||
type: "Pattern",
|
||||
flags: flags,
|
||||
value: value,
|
||||
loc: this.loc(0),
|
||||
};
|
||||
}
|
||||
disjunction() {
|
||||
const alts = [];
|
||||
const begin = this.idx;
|
||||
alts.push(this.alternative());
|
||||
while (this.peekChar() === "|") {
|
||||
this.consumeChar("|");
|
||||
alts.push(this.alternative());
|
||||
}
|
||||
return { type: "Disjunction", value: alts, loc: this.loc(begin) };
|
||||
}
|
||||
alternative() {
|
||||
const terms = [];
|
||||
const begin = this.idx;
|
||||
while (this.isTerm()) {
|
||||
terms.push(this.term());
|
||||
}
|
||||
return { type: "Alternative", value: terms, loc: this.loc(begin) };
|
||||
}
|
||||
term() {
|
||||
if (this.isAssertion()) {
|
||||
return this.assertion();
|
||||
}
|
||||
else {
|
||||
return this.atom();
|
||||
}
|
||||
}
|
||||
assertion() {
|
||||
const begin = this.idx;
|
||||
switch (this.popChar()) {
|
||||
case "^":
|
||||
return {
|
||||
type: "StartAnchor",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
case "$":
|
||||
return { type: "EndAnchor", loc: this.loc(begin) };
|
||||
// '\b' or '\B'
|
||||
case "\\":
|
||||
switch (this.popChar()) {
|
||||
case "b":
|
||||
return {
|
||||
type: "WordBoundary",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
case "B":
|
||||
return {
|
||||
type: "NonWordBoundary",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
}
|
||||
/* c8 ignore next */
|
||||
throw Error("Invalid Assertion Escape");
|
||||
// '(?=' or '(?!'
|
||||
case "(":
|
||||
this.consumeChar("?");
|
||||
let type;
|
||||
switch (this.popChar()) {
|
||||
case "=":
|
||||
type = "Lookahead";
|
||||
break;
|
||||
case "!":
|
||||
type = "NegativeLookahead";
|
||||
break;
|
||||
case "<": {
|
||||
switch (this.popChar()) {
|
||||
case "=":
|
||||
type = "Lookbehind";
|
||||
break;
|
||||
case "!":
|
||||
type = "NegativeLookbehind";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_EXISTS(type);
|
||||
const disjunction = this.disjunction();
|
||||
this.consumeChar(")");
|
||||
return {
|
||||
type: type,
|
||||
value: disjunction,
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
quantifier(isBacktracking = false) {
|
||||
let range = undefined;
|
||||
const begin = this.idx;
|
||||
switch (this.popChar()) {
|
||||
case "*":
|
||||
range = {
|
||||
atLeast: 0,
|
||||
atMost: Infinity,
|
||||
};
|
||||
break;
|
||||
case "+":
|
||||
range = {
|
||||
atLeast: 1,
|
||||
atMost: Infinity,
|
||||
};
|
||||
break;
|
||||
case "?":
|
||||
range = {
|
||||
atLeast: 0,
|
||||
atMost: 1,
|
||||
};
|
||||
break;
|
||||
case "{":
|
||||
const atLeast = this.integerIncludingZero();
|
||||
switch (this.popChar()) {
|
||||
case "}":
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: atLeast,
|
||||
};
|
||||
break;
|
||||
case ",":
|
||||
let atMost;
|
||||
if (this.isDigit()) {
|
||||
atMost = this.integerIncludingZero();
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: atMost,
|
||||
};
|
||||
}
|
||||
else {
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: Infinity,
|
||||
};
|
||||
}
|
||||
this.consumeChar("}");
|
||||
break;
|
||||
}
|
||||
// throwing exceptions from "ASSERT_EXISTS" during backtracking
|
||||
// causes severe performance degradations
|
||||
if (isBacktracking === true && range === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
ASSERT_EXISTS(range);
|
||||
break;
|
||||
}
|
||||
// throwing exceptions from "ASSERT_EXISTS" during backtracking
|
||||
// causes severe performance degradations
|
||||
if (isBacktracking === true && range === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(range)) {
|
||||
if (this.peekChar(0) === "?") {
|
||||
this.consumeChar("?");
|
||||
range.greedy = false;
|
||||
}
|
||||
else {
|
||||
range.greedy = true;
|
||||
}
|
||||
range.type = "Quantifier";
|
||||
range.loc = this.loc(begin);
|
||||
return range;
|
||||
}
|
||||
}
|
||||
atom() {
|
||||
let atom;
|
||||
const begin = this.idx;
|
||||
switch (this.peekChar()) {
|
||||
case ".":
|
||||
atom = this.dotAll();
|
||||
break;
|
||||
case "\\":
|
||||
atom = this.atomEscape();
|
||||
break;
|
||||
case "[":
|
||||
atom = this.characterClass();
|
||||
break;
|
||||
case "(":
|
||||
atom = this.group();
|
||||
break;
|
||||
}
|
||||
if (atom === undefined && this.isPatternCharacter()) {
|
||||
atom = this.patternCharacter();
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(atom)) {
|
||||
atom.loc = this.loc(begin);
|
||||
if (this.isQuantifier()) {
|
||||
atom.quantifier = this.quantifier();
|
||||
}
|
||||
return atom;
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
dotAll() {
|
||||
this.consumeChar(".");
|
||||
return {
|
||||
type: "Set",
|
||||
complement: true,
|
||||
value: [cc("\n"), cc("\r"), cc("\u2028"), cc("\u2029")],
|
||||
};
|
||||
}
|
||||
atomEscape() {
|
||||
this.consumeChar("\\");
|
||||
switch (this.peekChar()) {
|
||||
case "1":
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
case "5":
|
||||
case "6":
|
||||
case "7":
|
||||
case "8":
|
||||
case "9":
|
||||
return this.decimalEscapeAtom();
|
||||
case "d":
|
||||
case "D":
|
||||
case "s":
|
||||
case "S":
|
||||
case "w":
|
||||
case "W":
|
||||
return this.characterClassEscape();
|
||||
case "f":
|
||||
case "n":
|
||||
case "r":
|
||||
case "t":
|
||||
case "v":
|
||||
return this.controlEscapeAtom();
|
||||
case "c":
|
||||
return this.controlLetterEscapeAtom();
|
||||
case "0":
|
||||
return this.nulCharacterAtom();
|
||||
case "x":
|
||||
return this.hexEscapeSequenceAtom();
|
||||
case "u":
|
||||
return this.regExpUnicodeEscapeSequenceAtom();
|
||||
default:
|
||||
return this.identityEscapeAtom();
|
||||
}
|
||||
}
|
||||
decimalEscapeAtom() {
|
||||
const value = this.positiveInteger();
|
||||
return { type: "GroupBackReference", value: value };
|
||||
}
|
||||
characterClassEscape() {
|
||||
let set;
|
||||
let complement = false;
|
||||
switch (this.popChar()) {
|
||||
case "d":
|
||||
set = digitsCharCodes;
|
||||
break;
|
||||
case "D":
|
||||
set = digitsCharCodes;
|
||||
complement = true;
|
||||
break;
|
||||
case "s":
|
||||
set = whitespaceCodes;
|
||||
break;
|
||||
case "S":
|
||||
set = whitespaceCodes;
|
||||
complement = true;
|
||||
break;
|
||||
case "w":
|
||||
set = wordCharCodes;
|
||||
break;
|
||||
case "W":
|
||||
set = wordCharCodes;
|
||||
complement = true;
|
||||
break;
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(set)) {
|
||||
return { type: "Set", value: set, complement: complement };
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
controlEscapeAtom() {
|
||||
let escapeCode;
|
||||
switch (this.popChar()) {
|
||||
case "f":
|
||||
escapeCode = cc("\f");
|
||||
break;
|
||||
case "n":
|
||||
escapeCode = cc("\n");
|
||||
break;
|
||||
case "r":
|
||||
escapeCode = cc("\r");
|
||||
break;
|
||||
case "t":
|
||||
escapeCode = cc("\t");
|
||||
break;
|
||||
case "v":
|
||||
escapeCode = cc("\v");
|
||||
break;
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(escapeCode)) {
|
||||
return { type: "Character", value: escapeCode };
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
controlLetterEscapeAtom() {
|
||||
this.consumeChar("c");
|
||||
const letter = this.popChar();
|
||||
if (/[a-zA-Z]/.test(letter) === false) {
|
||||
throw Error("Invalid ");
|
||||
}
|
||||
const letterCode = letter.toUpperCase().charCodeAt(0) - 64;
|
||||
return { type: "Character", value: letterCode };
|
||||
}
|
||||
nulCharacterAtom() {
|
||||
// TODO implement '[lookahead ∉ DecimalDigit]'
|
||||
// TODO: for the deprecated octal escape sequence
|
||||
this.consumeChar("0");
|
||||
return { type: "Character", value: cc("\0") };
|
||||
}
|
||||
hexEscapeSequenceAtom() {
|
||||
this.consumeChar("x");
|
||||
return this.parseHexDigits(2);
|
||||
}
|
||||
regExpUnicodeEscapeSequenceAtom() {
|
||||
this.consumeChar("u");
|
||||
return this.parseHexDigits(4);
|
||||
}
|
||||
identityEscapeAtom() {
|
||||
// TODO: implement "SourceCharacter but not UnicodeIDContinue"
|
||||
// // http://unicode.org/reports/tr31/#Specific_Character_Adjustments
|
||||
const escapedChar = this.popChar();
|
||||
return { type: "Character", value: cc(escapedChar) };
|
||||
}
|
||||
classPatternCharacterAtom() {
|
||||
switch (this.peekChar()) {
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
// istanbul ignore next
|
||||
case "\\":
|
||||
// istanbul ignore next
|
||||
case "]":
|
||||
throw Error("TBD");
|
||||
default:
|
||||
const nextChar = this.popChar();
|
||||
return { type: "Character", value: cc(nextChar) };
|
||||
}
|
||||
}
|
||||
characterClass() {
|
||||
const set = [];
|
||||
let complement = false;
|
||||
this.consumeChar("[");
|
||||
if (this.peekChar(0) === "^") {
|
||||
this.consumeChar("^");
|
||||
complement = true;
|
||||
}
|
||||
while (this.isClassAtom()) {
|
||||
const from = this.classAtom();
|
||||
const isFromSingleChar = from.type === "Character";
|
||||
if (isCharacter(from) && this.isRangeDash()) {
|
||||
this.consumeChar("-");
|
||||
const to = this.classAtom();
|
||||
const isToSingleChar = to.type === "Character";
|
||||
// a range can only be used when both sides are single characters
|
||||
if (isCharacter(to)) {
|
||||
if (to.value < from.value) {
|
||||
throw Error("Range out of order in character class");
|
||||
}
|
||||
set.push({ from: from.value, to: to.value });
|
||||
}
|
||||
else {
|
||||
// literal dash
|
||||
insertToSet(from.value, set);
|
||||
set.push(cc("-"));
|
||||
insertToSet(to.value, set);
|
||||
}
|
||||
}
|
||||
else {
|
||||
insertToSet(from.value, set);
|
||||
}
|
||||
}
|
||||
this.consumeChar("]");
|
||||
return { type: "Set", complement: complement, value: set };
|
||||
}
|
||||
classAtom() {
|
||||
switch (this.peekChar()) {
|
||||
// istanbul ignore next
|
||||
case "]":
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
throw Error("TBD");
|
||||
case "\\":
|
||||
return this.classEscape();
|
||||
default:
|
||||
return this.classPatternCharacterAtom();
|
||||
}
|
||||
}
|
||||
classEscape() {
|
||||
this.consumeChar("\\");
|
||||
switch (this.peekChar()) {
|
||||
// Matches a backspace.
|
||||
// (Not to be confused with \b word boundary outside characterClass)
|
||||
case "b":
|
||||
this.consumeChar("b");
|
||||
return { type: "Character", value: cc("\u0008") };
|
||||
case "d":
|
||||
case "D":
|
||||
case "s":
|
||||
case "S":
|
||||
case "w":
|
||||
case "W":
|
||||
return this.characterClassEscape();
|
||||
case "f":
|
||||
case "n":
|
||||
case "r":
|
||||
case "t":
|
||||
case "v":
|
||||
return this.controlEscapeAtom();
|
||||
case "c":
|
||||
return this.controlLetterEscapeAtom();
|
||||
case "0":
|
||||
return this.nulCharacterAtom();
|
||||
case "x":
|
||||
return this.hexEscapeSequenceAtom();
|
||||
case "u":
|
||||
return this.regExpUnicodeEscapeSequenceAtom();
|
||||
default:
|
||||
return this.identityEscapeAtom();
|
||||
}
|
||||
}
|
||||
group() {
|
||||
let capturing = true;
|
||||
this.consumeChar("(");
|
||||
switch (this.peekChar(0)) {
|
||||
case "?":
|
||||
this.consumeChar("?");
|
||||
this.consumeChar(":");
|
||||
capturing = false;
|
||||
break;
|
||||
default:
|
||||
this.groupIdx++;
|
||||
break;
|
||||
}
|
||||
const value = this.disjunction();
|
||||
this.consumeChar(")");
|
||||
const groupAst = {
|
||||
type: "Group",
|
||||
capturing: capturing,
|
||||
value: value,
|
||||
};
|
||||
if (capturing) {
|
||||
groupAst["idx"] = this.groupIdx;
|
||||
}
|
||||
return groupAst;
|
||||
}
|
||||
positiveInteger() {
|
||||
let number = this.popChar();
|
||||
// istanbul ignore next - can't ever get here due to previous lookahead checks
|
||||
// still implementing this error checking in case this ever changes.
|
||||
if (decimalPatternNoZero.test(number) === false) {
|
||||
throw Error("Expecting a positive integer");
|
||||
}
|
||||
while (decimalPattern.test(this.peekChar(0))) {
|
||||
number += this.popChar();
|
||||
}
|
||||
return parseInt(number, 10);
|
||||
}
|
||||
integerIncludingZero() {
|
||||
let number = this.popChar();
|
||||
if (decimalPattern.test(number) === false) {
|
||||
throw Error("Expecting an integer");
|
||||
}
|
||||
while (decimalPattern.test(this.peekChar(0))) {
|
||||
number += this.popChar();
|
||||
}
|
||||
return parseInt(number, 10);
|
||||
}
|
||||
patternCharacter() {
|
||||
const nextChar = this.popChar();
|
||||
switch (nextChar) {
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
// istanbul ignore next
|
||||
case "^":
|
||||
// istanbul ignore next
|
||||
case "$":
|
||||
// istanbul ignore next
|
||||
case "\\":
|
||||
// istanbul ignore next
|
||||
case ".":
|
||||
// istanbul ignore next
|
||||
case "*":
|
||||
// istanbul ignore next
|
||||
case "+":
|
||||
// istanbul ignore next
|
||||
case "?":
|
||||
// istanbul ignore next
|
||||
case "(":
|
||||
// istanbul ignore next
|
||||
case ")":
|
||||
// istanbul ignore next
|
||||
case "[":
|
||||
// istanbul ignore next
|
||||
case "|":
|
||||
// istanbul ignore next
|
||||
throw Error("TBD");
|
||||
default:
|
||||
return { type: "Character", value: cc(nextChar) };
|
||||
}
|
||||
}
|
||||
isRegExpFlag() {
|
||||
switch (this.peekChar(0)) {
|
||||
case "g":
|
||||
case "i":
|
||||
case "m":
|
||||
case "u":
|
||||
case "y":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isRangeDash() {
|
||||
return this.peekChar() === "-" && this.isClassAtom(1);
|
||||
}
|
||||
isDigit() {
|
||||
return decimalPattern.test(this.peekChar(0));
|
||||
}
|
||||
isClassAtom(howMuch = 0) {
|
||||
switch (this.peekChar(howMuch)) {
|
||||
case "]":
|
||||
case "\n":
|
||||
case "\r":
|
||||
case "\u2028":
|
||||
case "\u2029":
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
isTerm() {
|
||||
return this.isAtom() || this.isAssertion();
|
||||
}
|
||||
isAtom() {
|
||||
if (this.isPatternCharacter()) {
|
||||
return true;
|
||||
}
|
||||
switch (this.peekChar(0)) {
|
||||
case ".":
|
||||
case "\\": // atomEscape
|
||||
case "[": // characterClass
|
||||
// TODO: isAtom must be called before isAssertion - disambiguate
|
||||
case "(": // group
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isAssertion() {
|
||||
switch (this.peekChar(0)) {
|
||||
case "^":
|
||||
case "$":
|
||||
return true;
|
||||
// '\b' or '\B'
|
||||
case "\\":
|
||||
switch (this.peekChar(1)) {
|
||||
case "b":
|
||||
case "B":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
// '(?=' or '(?!' or `(?<=` or `(?<!`
|
||||
case "(":
|
||||
return (this.peekChar(1) === "?" &&
|
||||
(this.peekChar(2) === "=" ||
|
||||
this.peekChar(2) === "!" ||
|
||||
(this.peekChar(2) === "<" &&
|
||||
(this.peekChar(3) === "=" || this.peekChar(3) === "!"))));
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isQuantifier() {
|
||||
const prevState = this.saveState();
|
||||
try {
|
||||
return this.quantifier(true) !== undefined;
|
||||
}
|
||||
catch (e) {
|
||||
return false;
|
||||
}
|
||||
finally {
|
||||
this.restoreState(prevState);
|
||||
}
|
||||
}
|
||||
isPatternCharacter() {
|
||||
switch (this.peekChar()) {
|
||||
case "^":
|
||||
case "$":
|
||||
case "\\":
|
||||
case ".":
|
||||
case "*":
|
||||
case "+":
|
||||
case "?":
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "|":
|
||||
case "/":
|
||||
case "\n":
|
||||
case "\r":
|
||||
case "\u2028":
|
||||
case "\u2029":
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
parseHexDigits(howMany) {
|
||||
let hexString = "";
|
||||
for (let i = 0; i < howMany; i++) {
|
||||
const hexChar = this.popChar();
|
||||
if (hexDigitPattern.test(hexChar) === false) {
|
||||
throw Error("Expecting a HexDecimal digits");
|
||||
}
|
||||
hexString += hexChar;
|
||||
}
|
||||
const charCode = parseInt(hexString, 16);
|
||||
return { type: "Character", value: charCode };
|
||||
}
|
||||
peekChar(howMuch = 0) {
|
||||
return this.input[this.idx + howMuch];
|
||||
}
|
||||
popChar() {
|
||||
const nextChar = this.peekChar(0);
|
||||
this.consumeChar(undefined);
|
||||
return nextChar;
|
||||
}
|
||||
consumeChar(char) {
|
||||
if (char !== undefined && this.input[this.idx] !== char) {
|
||||
throw Error("Expected: '" +
|
||||
char +
|
||||
"' but found: '" +
|
||||
this.input[this.idx] +
|
||||
"' at offset: " +
|
||||
this.idx);
|
||||
}
|
||||
if (this.idx >= this.input.length) {
|
||||
throw Error("Unexpected end of input");
|
||||
}
|
||||
this.idx++;
|
||||
}
|
||||
loc(begin) {
|
||||
return { begin: begin, end: this.idx };
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=regexp-parser.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+35
@@ -0,0 +1,35 @@
|
||||
export function cc(char) {
|
||||
return char.charCodeAt(0);
|
||||
}
|
||||
export function insertToSet(item, set) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function (subItem) {
|
||||
set.push(subItem);
|
||||
});
|
||||
}
|
||||
else {
|
||||
set.push(item);
|
||||
}
|
||||
}
|
||||
export function addFlag(flagObj, flagKey) {
|
||||
if (flagObj[flagKey] === true) {
|
||||
throw "duplicate flag " + flagKey;
|
||||
}
|
||||
const x = flagObj[flagKey];
|
||||
flagObj[flagKey] = true;
|
||||
}
|
||||
export function ASSERT_EXISTS(obj) {
|
||||
// istanbul ignore next
|
||||
if (obj === undefined) {
|
||||
throw Error("Internal Error - Should never get here!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// istanbul ignore next
|
||||
export function ASSERT_NEVER_REACH_HERE() {
|
||||
throw Error("Internal Error - Should never get here!");
|
||||
}
|
||||
export function isCharacter(obj) {
|
||||
return obj["type"] === "Character";
|
||||
}
|
||||
//# sourceMappingURL=utils.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,EAAE,CAAC,IAAY;IAC7B,OAAO,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC5B,CAAC;AAED,MAAM,UAAU,WAAW,CAAI,IAAa,EAAE,GAAQ;IACpD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,CAAC,UAAU,OAAO;YAC5B,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,OAAO,CACrB,OAAoB,EACpB,OAAkD;IAElD,IAAI,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;QAC9B,MAAM,iBAAiB,GAAG,OAAO,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,GAAY,OAAO,CAAC,OAAO,CAAC,CAAC;IACpC,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,aAAa,CAAa,GAAQ;IAChD,uBAAuB;IACvB,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,MAAM,KAAK,CAAC,yCAAyC,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,uBAAuB;AACvB,MAAM,UAAU,uBAAuB;IACrC,MAAM,KAAK,CAAC,yCAAyC,CAAC,CAAC;AACzD,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,GAAqB;IAC/C,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC;AACrC,CAAC"}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "@chevrotain/regexp-to-ast",
|
||||
"version": "12.0.0",
|
||||
"description": "Parses a Regular Expression and outputs an AST",
|
||||
"keywords": [
|
||||
"regExp",
|
||||
"parser",
|
||||
"regular expression"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://github.com/Chevrotain/chevrotain/issues"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"types": "./types.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./lib/src/api.js",
|
||||
"types": "./types.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"lib/src/**/*.js",
|
||||
"lib/src/**/*.map",
|
||||
"src/**/*.ts",
|
||||
"types.d.ts"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/Chevrotain/chevrotain.git"
|
||||
},
|
||||
"scripts": {
|
||||
"---------- CI FLOWS --------": "",
|
||||
"ci": "pnpm run build test",
|
||||
"build": "npm-run-all clean compile",
|
||||
"test": "npm-run-all coverage",
|
||||
"---------- BUILD STEPS --------": "",
|
||||
"clean": "shx rm -rf lib coverage",
|
||||
"compile:watch": "tsc -w",
|
||||
"compile": "tsc",
|
||||
"coverage": "c8 mocha"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "d3b4d05d5dd53d53d27175119bc7c9ed8056e3d5"
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { RegExpParser } from "./regexp-parser.js";
|
||||
export { BaseRegExpVisitor } from "./base-regexp-visitor.js";
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
import type {
|
||||
Alternative,
|
||||
Assertion,
|
||||
Character,
|
||||
Disjunction,
|
||||
Group,
|
||||
GroupBackReference,
|
||||
IRegExpAST,
|
||||
Quantifier,
|
||||
RegExpAstPart,
|
||||
RegExpFlags,
|
||||
RegExpPattern,
|
||||
Set,
|
||||
} from "../types";
|
||||
|
||||
export class BaseRegExpVisitor {
|
||||
public visitChildren(node: IRegExpAST) {
|
||||
for (const key in node) {
|
||||
const child = (node as any)[key];
|
||||
/* istanbul ignore else */
|
||||
if (node.hasOwnProperty(key)) {
|
||||
if (child.type !== undefined) {
|
||||
this.visit(child);
|
||||
} else if (Array.isArray(child)) {
|
||||
child.forEach((subChild) => {
|
||||
this.visit(subChild);
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public visit(node: RegExpAstPart): void {
|
||||
switch (node.type) {
|
||||
case "Pattern":
|
||||
this.visitPattern(node);
|
||||
break;
|
||||
case "Flags":
|
||||
this.visitFlags(node);
|
||||
break;
|
||||
case "Disjunction":
|
||||
this.visitDisjunction(node);
|
||||
break;
|
||||
case "Alternative":
|
||||
this.visitAlternative(node);
|
||||
break;
|
||||
case "StartAnchor":
|
||||
this.visitStartAnchor(node);
|
||||
break;
|
||||
case "EndAnchor":
|
||||
this.visitEndAnchor(node);
|
||||
break;
|
||||
case "WordBoundary":
|
||||
this.visitWordBoundary(node);
|
||||
break;
|
||||
case "NonWordBoundary":
|
||||
this.visitNonWordBoundary(node);
|
||||
break;
|
||||
case "Lookahead":
|
||||
this.visitLookahead(node);
|
||||
break;
|
||||
case "NegativeLookahead":
|
||||
this.visitNegativeLookahead(node);
|
||||
break;
|
||||
case "Lookbehind":
|
||||
this.visitLookbehind(node);
|
||||
break;
|
||||
case "NegativeLookbehind":
|
||||
this.visitNegativeLookbehind(node);
|
||||
break;
|
||||
case "Character":
|
||||
this.visitCharacter(node);
|
||||
break;
|
||||
case "Set":
|
||||
this.visitSet(node);
|
||||
break;
|
||||
case "Group":
|
||||
this.visitGroup(node);
|
||||
break;
|
||||
case "GroupBackReference":
|
||||
this.visitGroupBackReference(node);
|
||||
break;
|
||||
case "Quantifier":
|
||||
this.visitQuantifier(node);
|
||||
break;
|
||||
}
|
||||
|
||||
this.visitChildren(node);
|
||||
}
|
||||
|
||||
public visitPattern(node: RegExpPattern): void {}
|
||||
|
||||
public visitFlags(node: RegExpFlags): void {}
|
||||
|
||||
public visitDisjunction(node: Disjunction): void {}
|
||||
|
||||
public visitAlternative(node: Alternative): void {}
|
||||
|
||||
// Assertion
|
||||
public visitStartAnchor(node: Assertion): void {}
|
||||
|
||||
public visitEndAnchor(node: Assertion): void {}
|
||||
|
||||
public visitWordBoundary(node: Assertion): void {}
|
||||
|
||||
public visitNonWordBoundary(node: Assertion): void {}
|
||||
|
||||
public visitLookahead(node: Assertion): void {}
|
||||
|
||||
public visitNegativeLookahead(node: Assertion): void {}
|
||||
|
||||
public visitLookbehind(node: Assertion): void {}
|
||||
|
||||
public visitNegativeLookbehind(node: Assertion): void {}
|
||||
|
||||
// atoms
|
||||
public visitCharacter(node: Character): void {}
|
||||
|
||||
public visitSet(node: Set): void {}
|
||||
|
||||
public visitGroup(node: Group): void {}
|
||||
|
||||
public visitGroupBackReference(node: GroupBackReference): void {}
|
||||
|
||||
public visitQuantifier(node: Quantifier): void {}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
import { cc } from "./utils.js";
|
||||
|
||||
export const digitsCharCodes: number[] = [];
|
||||
for (let i = cc("0"); i <= cc("9"); i++) {
|
||||
digitsCharCodes.push(i);
|
||||
}
|
||||
|
||||
export const wordCharCodes: number[] = [cc("_")].concat(digitsCharCodes);
|
||||
for (let i = cc("a"); i <= cc("z"); i++) {
|
||||
wordCharCodes.push(i);
|
||||
}
|
||||
|
||||
for (let i = cc("A"); i <= cc("Z"); i++) {
|
||||
wordCharCodes.push(i);
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#character-classes
|
||||
export const whitespaceCodes: number[] = [
|
||||
cc(" "),
|
||||
cc("\f"),
|
||||
cc("\n"),
|
||||
cc("\r"),
|
||||
cc("\t"),
|
||||
cc("\v"),
|
||||
cc("\t"),
|
||||
cc("\u00a0"),
|
||||
cc("\u1680"),
|
||||
cc("\u2000"),
|
||||
cc("\u2001"),
|
||||
cc("\u2002"),
|
||||
cc("\u2003"),
|
||||
cc("\u2004"),
|
||||
cc("\u2005"),
|
||||
cc("\u2006"),
|
||||
cc("\u2007"),
|
||||
cc("\u2008"),
|
||||
cc("\u2009"),
|
||||
cc("\u200a"),
|
||||
cc("\u2028"),
|
||||
cc("\u2029"),
|
||||
cc("\u202f"),
|
||||
cc("\u205f"),
|
||||
cc("\u3000"),
|
||||
cc("\ufeff"),
|
||||
];
|
||||
+858
@@ -0,0 +1,858 @@
|
||||
import type {
|
||||
Alternative,
|
||||
Assertion,
|
||||
Atom,
|
||||
Character,
|
||||
Disjunction,
|
||||
Group,
|
||||
GroupBackReference,
|
||||
Location,
|
||||
Quantifier,
|
||||
Range,
|
||||
RegExpFlags,
|
||||
RegExpPattern,
|
||||
Set,
|
||||
Term,
|
||||
} from "../types";
|
||||
import {
|
||||
addFlag,
|
||||
ASSERT_EXISTS,
|
||||
ASSERT_NEVER_REACH_HERE,
|
||||
cc,
|
||||
insertToSet,
|
||||
isCharacter,
|
||||
} from "./utils.js";
|
||||
import {
|
||||
digitsCharCodes,
|
||||
whitespaceCodes,
|
||||
wordCharCodes,
|
||||
} from "./character-classes.js";
|
||||
|
||||
// consts and utilities
|
||||
const hexDigitPattern = /[0-9a-fA-F]/;
|
||||
const decimalPattern = /[0-9]/;
|
||||
const decimalPatternNoZero = /[1-9]/;
|
||||
|
||||
// https://hackernoon.com/the-madness-of-parsing-real-world-javascript-regexps-d9ee336df983
|
||||
// https://www.ecma-international.org/ecma-262/8.0/index.html#prod-Pattern
|
||||
export class RegExpParser {
|
||||
protected idx: number = 0;
|
||||
protected input: string = "";
|
||||
protected groupIdx: number = 0;
|
||||
|
||||
protected saveState() {
|
||||
return {
|
||||
idx: this.idx,
|
||||
input: this.input,
|
||||
groupIdx: this.groupIdx,
|
||||
};
|
||||
}
|
||||
|
||||
protected restoreState(newState: {
|
||||
idx: number;
|
||||
input: string;
|
||||
groupIdx: number;
|
||||
}) {
|
||||
this.idx = newState.idx;
|
||||
this.input = newState.input;
|
||||
this.groupIdx = newState.groupIdx;
|
||||
}
|
||||
|
||||
public pattern(input: string): RegExpPattern {
|
||||
// parser state
|
||||
this.idx = 0;
|
||||
this.input = input;
|
||||
this.groupIdx = 0;
|
||||
|
||||
this.consumeChar("/");
|
||||
const value = this.disjunction();
|
||||
this.consumeChar("/");
|
||||
|
||||
const flags: RegExpFlags = {
|
||||
type: "Flags",
|
||||
loc: { begin: this.idx, end: input.length },
|
||||
global: false,
|
||||
ignoreCase: false,
|
||||
multiLine: false,
|
||||
unicode: false,
|
||||
sticky: false,
|
||||
};
|
||||
|
||||
while (this.isRegExpFlag()) {
|
||||
switch (this.popChar()) {
|
||||
case "g":
|
||||
addFlag(flags, "global");
|
||||
break;
|
||||
case "i":
|
||||
addFlag(flags, "ignoreCase");
|
||||
break;
|
||||
case "m":
|
||||
addFlag(flags, "multiLine");
|
||||
break;
|
||||
case "u":
|
||||
addFlag(flags, "unicode");
|
||||
break;
|
||||
case "y":
|
||||
addFlag(flags, "sticky");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.idx !== this.input.length) {
|
||||
throw Error("Redundant input: " + this.input.substring(this.idx));
|
||||
}
|
||||
return {
|
||||
type: "Pattern",
|
||||
flags: flags,
|
||||
value: value,
|
||||
loc: this.loc(0),
|
||||
};
|
||||
}
|
||||
|
||||
protected disjunction(): Disjunction {
|
||||
const alts = [];
|
||||
const begin = this.idx;
|
||||
|
||||
alts.push(this.alternative());
|
||||
|
||||
while (this.peekChar() === "|") {
|
||||
this.consumeChar("|");
|
||||
alts.push(this.alternative());
|
||||
}
|
||||
|
||||
return { type: "Disjunction", value: alts, loc: this.loc(begin) };
|
||||
}
|
||||
|
||||
protected alternative(): Alternative {
|
||||
const terms = [];
|
||||
const begin = this.idx;
|
||||
|
||||
while (this.isTerm()) {
|
||||
terms.push(this.term());
|
||||
}
|
||||
|
||||
return { type: "Alternative", value: terms, loc: this.loc(begin) };
|
||||
}
|
||||
|
||||
protected term(): Term {
|
||||
if (this.isAssertion()) {
|
||||
return this.assertion();
|
||||
} else {
|
||||
return this.atom();
|
||||
}
|
||||
}
|
||||
|
||||
protected assertion(): Assertion {
|
||||
const begin = this.idx;
|
||||
switch (this.popChar()) {
|
||||
case "^":
|
||||
return {
|
||||
type: "StartAnchor",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
case "$":
|
||||
return { type: "EndAnchor", loc: this.loc(begin) };
|
||||
// '\b' or '\B'
|
||||
case "\\":
|
||||
switch (this.popChar()) {
|
||||
case "b":
|
||||
return {
|
||||
type: "WordBoundary",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
case "B":
|
||||
return {
|
||||
type: "NonWordBoundary",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
}
|
||||
/* c8 ignore next */
|
||||
throw Error("Invalid Assertion Escape");
|
||||
// '(?=' or '(?!'
|
||||
case "(":
|
||||
this.consumeChar("?");
|
||||
|
||||
let type:
|
||||
| "Lookahead"
|
||||
| "NegativeLookahead"
|
||||
| "Lookbehind"
|
||||
| "NegativeLookbehind"
|
||||
| undefined;
|
||||
switch (this.popChar()) {
|
||||
case "=":
|
||||
type = "Lookahead";
|
||||
break;
|
||||
case "!":
|
||||
type = "NegativeLookahead";
|
||||
break;
|
||||
case "<": {
|
||||
switch (this.popChar()) {
|
||||
case "=":
|
||||
type = "Lookbehind";
|
||||
break;
|
||||
case "!":
|
||||
type = "NegativeLookbehind";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_EXISTS(type);
|
||||
|
||||
const disjunction = this.disjunction();
|
||||
|
||||
this.consumeChar(")");
|
||||
|
||||
return {
|
||||
type: type!,
|
||||
value: disjunction,
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
|
||||
protected quantifier(
|
||||
isBacktracking: boolean = false,
|
||||
): Quantifier | undefined {
|
||||
let range: Partial<Quantifier> | undefined = undefined;
|
||||
const begin = this.idx;
|
||||
switch (this.popChar()) {
|
||||
case "*":
|
||||
range = {
|
||||
atLeast: 0,
|
||||
atMost: Infinity,
|
||||
};
|
||||
break;
|
||||
case "+":
|
||||
range = {
|
||||
atLeast: 1,
|
||||
atMost: Infinity,
|
||||
};
|
||||
break;
|
||||
case "?":
|
||||
range = {
|
||||
atLeast: 0,
|
||||
atMost: 1,
|
||||
};
|
||||
break;
|
||||
case "{":
|
||||
const atLeast = this.integerIncludingZero();
|
||||
switch (this.popChar()) {
|
||||
case "}":
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: atLeast,
|
||||
};
|
||||
break;
|
||||
case ",":
|
||||
let atMost;
|
||||
if (this.isDigit()) {
|
||||
atMost = this.integerIncludingZero();
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: atMost,
|
||||
};
|
||||
} else {
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: Infinity,
|
||||
};
|
||||
}
|
||||
this.consumeChar("}");
|
||||
break;
|
||||
}
|
||||
// throwing exceptions from "ASSERT_EXISTS" during backtracking
|
||||
// causes severe performance degradations
|
||||
if (isBacktracking === true && range === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
ASSERT_EXISTS(range);
|
||||
break;
|
||||
}
|
||||
|
||||
// throwing exceptions from "ASSERT_EXISTS" during backtracking
|
||||
// causes severe performance degradations
|
||||
if (isBacktracking === true && range === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(range)) {
|
||||
if (this.peekChar(0) === "?") {
|
||||
this.consumeChar("?");
|
||||
range.greedy = false;
|
||||
} else {
|
||||
range.greedy = true;
|
||||
}
|
||||
|
||||
range.type = "Quantifier";
|
||||
range.loc = this.loc(begin);
|
||||
return range as Quantifier;
|
||||
}
|
||||
}
|
||||
|
||||
protected atom(): Atom {
|
||||
let atom: Omit<Atom, "loc" | "type"> | undefined;
|
||||
const begin = this.idx;
|
||||
switch (this.peekChar()) {
|
||||
case ".":
|
||||
atom = this.dotAll();
|
||||
break;
|
||||
case "\\":
|
||||
atom = this.atomEscape();
|
||||
break;
|
||||
case "[":
|
||||
atom = this.characterClass();
|
||||
break;
|
||||
case "(":
|
||||
atom = this.group();
|
||||
break;
|
||||
}
|
||||
|
||||
if (atom === undefined && this.isPatternCharacter()) {
|
||||
atom = this.patternCharacter();
|
||||
}
|
||||
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS<Atom>(atom)) {
|
||||
atom.loc = this.loc(begin);
|
||||
|
||||
if (this.isQuantifier()) {
|
||||
atom.quantifier = this.quantifier();
|
||||
}
|
||||
|
||||
return atom;
|
||||
}
|
||||
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
|
||||
protected dotAll(): Omit<Set, "loc"> {
|
||||
this.consumeChar(".");
|
||||
return {
|
||||
type: "Set",
|
||||
complement: true,
|
||||
value: [cc("\n"), cc("\r"), cc("\u2028"), cc("\u2029")],
|
||||
};
|
||||
}
|
||||
|
||||
protected atomEscape(): Omit<GroupBackReference | Set | Character, "loc"> {
|
||||
this.consumeChar("\\");
|
||||
|
||||
switch (this.peekChar()) {
|
||||
case "1":
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
case "5":
|
||||
case "6":
|
||||
case "7":
|
||||
case "8":
|
||||
case "9":
|
||||
return this.decimalEscapeAtom();
|
||||
case "d":
|
||||
case "D":
|
||||
case "s":
|
||||
case "S":
|
||||
case "w":
|
||||
case "W":
|
||||
return this.characterClassEscape();
|
||||
case "f":
|
||||
case "n":
|
||||
case "r":
|
||||
case "t":
|
||||
case "v":
|
||||
return this.controlEscapeAtom();
|
||||
case "c":
|
||||
return this.controlLetterEscapeAtom();
|
||||
case "0":
|
||||
return this.nulCharacterAtom();
|
||||
case "x":
|
||||
return this.hexEscapeSequenceAtom();
|
||||
case "u":
|
||||
return this.regExpUnicodeEscapeSequenceAtom();
|
||||
default:
|
||||
return this.identityEscapeAtom();
|
||||
}
|
||||
}
|
||||
|
||||
protected decimalEscapeAtom(): Omit<GroupBackReference, "loc"> {
|
||||
const value = this.positiveInteger();
|
||||
|
||||
return { type: "GroupBackReference", value: value };
|
||||
}
|
||||
|
||||
protected characterClassEscape(): Omit<Set, "loc"> {
|
||||
let set: (number | Range)[] | undefined;
|
||||
let complement = false;
|
||||
switch (this.popChar()) {
|
||||
case "d":
|
||||
set = digitsCharCodes;
|
||||
break;
|
||||
case "D":
|
||||
set = digitsCharCodes;
|
||||
complement = true;
|
||||
break;
|
||||
case "s":
|
||||
set = whitespaceCodes;
|
||||
break;
|
||||
case "S":
|
||||
set = whitespaceCodes;
|
||||
complement = true;
|
||||
break;
|
||||
case "w":
|
||||
set = wordCharCodes;
|
||||
break;
|
||||
case "W":
|
||||
set = wordCharCodes;
|
||||
complement = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(set)) {
|
||||
return { type: "Set", value: set, complement: complement };
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
|
||||
protected controlEscapeAtom(): Omit<Character, "loc"> {
|
||||
let escapeCode;
|
||||
switch (this.popChar()) {
|
||||
case "f":
|
||||
escapeCode = cc("\f");
|
||||
break;
|
||||
case "n":
|
||||
escapeCode = cc("\n");
|
||||
break;
|
||||
case "r":
|
||||
escapeCode = cc("\r");
|
||||
break;
|
||||
case "t":
|
||||
escapeCode = cc("\t");
|
||||
break;
|
||||
case "v":
|
||||
escapeCode = cc("\v");
|
||||
break;
|
||||
}
|
||||
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(escapeCode)) {
|
||||
return { type: "Character", value: escapeCode };
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
|
||||
protected controlLetterEscapeAtom(): Omit<Character, "loc"> {
|
||||
this.consumeChar("c");
|
||||
const letter = this.popChar();
|
||||
if (/[a-zA-Z]/.test(letter) === false) {
|
||||
throw Error("Invalid ");
|
||||
}
|
||||
|
||||
const letterCode = letter.toUpperCase().charCodeAt(0) - 64;
|
||||
return { type: "Character", value: letterCode };
|
||||
}
|
||||
|
||||
protected nulCharacterAtom(): Omit<Character, "loc"> {
|
||||
// TODO implement '[lookahead ∉ DecimalDigit]'
|
||||
// TODO: for the deprecated octal escape sequence
|
||||
this.consumeChar("0");
|
||||
return { type: "Character", value: cc("\0") };
|
||||
}
|
||||
|
||||
protected hexEscapeSequenceAtom(): Omit<Character, "loc"> {
|
||||
this.consumeChar("x");
|
||||
return this.parseHexDigits(2);
|
||||
}
|
||||
|
||||
protected regExpUnicodeEscapeSequenceAtom(): Omit<Character, "loc"> {
|
||||
this.consumeChar("u");
|
||||
return this.parseHexDigits(4);
|
||||
}
|
||||
|
||||
protected identityEscapeAtom(): Omit<Character, "loc"> {
|
||||
// TODO: implement "SourceCharacter but not UnicodeIDContinue"
|
||||
// // http://unicode.org/reports/tr31/#Specific_Character_Adjustments
|
||||
const escapedChar = this.popChar();
|
||||
return { type: "Character", value: cc(escapedChar) };
|
||||
}
|
||||
|
||||
protected classPatternCharacterAtom(): Omit<Character, "loc"> {
|
||||
switch (this.peekChar()) {
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
// istanbul ignore next
|
||||
case "\\":
|
||||
// istanbul ignore next
|
||||
case "]":
|
||||
throw Error("TBD");
|
||||
default:
|
||||
const nextChar = this.popChar();
|
||||
return { type: "Character", value: cc(nextChar) };
|
||||
}
|
||||
}
|
||||
|
||||
protected characterClass(): Omit<Set, "loc"> {
|
||||
const set: (number | Range)[] = [];
|
||||
let complement = false;
|
||||
this.consumeChar("[");
|
||||
if (this.peekChar(0) === "^") {
|
||||
this.consumeChar("^");
|
||||
complement = true;
|
||||
}
|
||||
|
||||
while (this.isClassAtom()) {
|
||||
const from = this.classAtom();
|
||||
const isFromSingleChar = from.type === "Character";
|
||||
if (isCharacter(from) && this.isRangeDash()) {
|
||||
this.consumeChar("-");
|
||||
const to = this.classAtom();
|
||||
const isToSingleChar = to.type === "Character";
|
||||
|
||||
// a range can only be used when both sides are single characters
|
||||
if (isCharacter(to)) {
|
||||
if (to.value < from.value) {
|
||||
throw Error("Range out of order in character class");
|
||||
}
|
||||
set.push({ from: from.value, to: to.value });
|
||||
} else {
|
||||
// literal dash
|
||||
insertToSet(from.value, set);
|
||||
set.push(cc("-"));
|
||||
insertToSet(to.value, set);
|
||||
}
|
||||
} else {
|
||||
insertToSet(from.value, set);
|
||||
}
|
||||
}
|
||||
|
||||
this.consumeChar("]");
|
||||
|
||||
return { type: "Set", complement: complement, value: set };
|
||||
}
|
||||
|
||||
protected classAtom(): Omit<Character | Set, "loc"> {
|
||||
switch (this.peekChar()) {
|
||||
// istanbul ignore next
|
||||
case "]":
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
throw Error("TBD");
|
||||
case "\\":
|
||||
return this.classEscape();
|
||||
default:
|
||||
return this.classPatternCharacterAtom();
|
||||
}
|
||||
}
|
||||
|
||||
protected classEscape(): Omit<Character | Set, "loc"> {
|
||||
this.consumeChar("\\");
|
||||
switch (this.peekChar()) {
|
||||
// Matches a backspace.
|
||||
// (Not to be confused with \b word boundary outside characterClass)
|
||||
case "b":
|
||||
this.consumeChar("b");
|
||||
return { type: "Character", value: cc("\u0008") };
|
||||
case "d":
|
||||
case "D":
|
||||
case "s":
|
||||
case "S":
|
||||
case "w":
|
||||
case "W":
|
||||
return this.characterClassEscape();
|
||||
case "f":
|
||||
case "n":
|
||||
case "r":
|
||||
case "t":
|
||||
case "v":
|
||||
return this.controlEscapeAtom();
|
||||
case "c":
|
||||
return this.controlLetterEscapeAtom();
|
||||
case "0":
|
||||
return this.nulCharacterAtom();
|
||||
case "x":
|
||||
return this.hexEscapeSequenceAtom();
|
||||
case "u":
|
||||
return this.regExpUnicodeEscapeSequenceAtom();
|
||||
default:
|
||||
return this.identityEscapeAtom();
|
||||
}
|
||||
}
|
||||
|
||||
protected group(): Omit<Group, "loc"> {
|
||||
let capturing = true;
|
||||
this.consumeChar("(");
|
||||
switch (this.peekChar(0)) {
|
||||
case "?":
|
||||
this.consumeChar("?");
|
||||
this.consumeChar(":");
|
||||
capturing = false;
|
||||
break;
|
||||
default:
|
||||
this.groupIdx++;
|
||||
break;
|
||||
}
|
||||
const value = this.disjunction();
|
||||
this.consumeChar(")");
|
||||
|
||||
const groupAst: Omit<Group, "loc"> = {
|
||||
type: "Group",
|
||||
capturing: capturing,
|
||||
value: value,
|
||||
};
|
||||
|
||||
if (capturing) {
|
||||
groupAst["idx"] = this.groupIdx;
|
||||
}
|
||||
|
||||
return groupAst;
|
||||
}
|
||||
|
||||
protected positiveInteger(): number {
|
||||
let number = this.popChar();
|
||||
|
||||
// istanbul ignore next - can't ever get here due to previous lookahead checks
|
||||
// still implementing this error checking in case this ever changes.
|
||||
if (decimalPatternNoZero.test(number) === false) {
|
||||
throw Error("Expecting a positive integer");
|
||||
}
|
||||
|
||||
while (decimalPattern.test(this.peekChar(0))) {
|
||||
number += this.popChar();
|
||||
}
|
||||
|
||||
return parseInt(number, 10);
|
||||
}
|
||||
|
||||
protected integerIncludingZero(): number {
|
||||
let number = this.popChar();
|
||||
if (decimalPattern.test(number) === false) {
|
||||
throw Error("Expecting an integer");
|
||||
}
|
||||
|
||||
while (decimalPattern.test(this.peekChar(0))) {
|
||||
number += this.popChar();
|
||||
}
|
||||
|
||||
return parseInt(number, 10);
|
||||
}
|
||||
|
||||
protected patternCharacter(): Omit<Character, "loc"> {
|
||||
const nextChar = this.popChar();
|
||||
switch (nextChar) {
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
// istanbul ignore next
|
||||
case "^":
|
||||
// istanbul ignore next
|
||||
case "$":
|
||||
// istanbul ignore next
|
||||
case "\\":
|
||||
// istanbul ignore next
|
||||
case ".":
|
||||
// istanbul ignore next
|
||||
case "*":
|
||||
// istanbul ignore next
|
||||
case "+":
|
||||
// istanbul ignore next
|
||||
case "?":
|
||||
// istanbul ignore next
|
||||
case "(":
|
||||
// istanbul ignore next
|
||||
case ")":
|
||||
// istanbul ignore next
|
||||
case "[":
|
||||
// istanbul ignore next
|
||||
case "|":
|
||||
// istanbul ignore next
|
||||
throw Error("TBD");
|
||||
default:
|
||||
return { type: "Character", value: cc(nextChar) };
|
||||
}
|
||||
}
|
||||
protected isRegExpFlag(): boolean {
|
||||
switch (this.peekChar(0)) {
|
||||
case "g":
|
||||
case "i":
|
||||
case "m":
|
||||
case "u":
|
||||
case "y":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected isRangeDash(): boolean {
|
||||
return this.peekChar() === "-" && this.isClassAtom(1);
|
||||
}
|
||||
|
||||
protected isDigit(): boolean {
|
||||
return decimalPattern.test(this.peekChar(0));
|
||||
}
|
||||
|
||||
protected isClassAtom(howMuch = 0): boolean {
|
||||
switch (this.peekChar(howMuch)) {
|
||||
case "]":
|
||||
case "\n":
|
||||
case "\r":
|
||||
case "\u2028":
|
||||
case "\u2029":
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected isTerm() {
|
||||
return this.isAtom() || this.isAssertion();
|
||||
}
|
||||
|
||||
protected isAtom(): boolean {
|
||||
if (this.isPatternCharacter()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (this.peekChar(0)) {
|
||||
case ".":
|
||||
case "\\": // atomEscape
|
||||
case "[": // characterClass
|
||||
// TODO: isAtom must be called before isAssertion - disambiguate
|
||||
case "(": // group
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected isAssertion(): boolean {
|
||||
switch (this.peekChar(0)) {
|
||||
case "^":
|
||||
case "$":
|
||||
return true;
|
||||
// '\b' or '\B'
|
||||
case "\\":
|
||||
switch (this.peekChar(1)) {
|
||||
case "b":
|
||||
case "B":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
// '(?=' or '(?!' or `(?<=` or `(?<!`
|
||||
case "(":
|
||||
return (
|
||||
this.peekChar(1) === "?" &&
|
||||
(this.peekChar(2) === "=" ||
|
||||
this.peekChar(2) === "!" ||
|
||||
(this.peekChar(2) === "<" &&
|
||||
(this.peekChar(3) === "=" || this.peekChar(3) === "!")))
|
||||
);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected isQuantifier(): boolean {
|
||||
const prevState = this.saveState();
|
||||
try {
|
||||
return this.quantifier(true) !== undefined;
|
||||
} catch (e) {
|
||||
return false;
|
||||
} finally {
|
||||
this.restoreState(prevState);
|
||||
}
|
||||
}
|
||||
|
||||
protected isPatternCharacter(): boolean {
|
||||
switch (this.peekChar()) {
|
||||
case "^":
|
||||
case "$":
|
||||
case "\\":
|
||||
case ".":
|
||||
case "*":
|
||||
case "+":
|
||||
case "?":
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "|":
|
||||
case "/":
|
||||
case "\n":
|
||||
case "\r":
|
||||
case "\u2028":
|
||||
case "\u2029":
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected parseHexDigits(howMany: number): Omit<Character, "loc"> {
|
||||
let hexString = "";
|
||||
for (let i = 0; i < howMany; i++) {
|
||||
const hexChar = this.popChar();
|
||||
if (hexDigitPattern.test(hexChar) === false) {
|
||||
throw Error("Expecting a HexDecimal digits");
|
||||
}
|
||||
hexString += hexChar;
|
||||
}
|
||||
const charCode = parseInt(hexString, 16);
|
||||
return { type: "Character", value: charCode };
|
||||
}
|
||||
|
||||
protected peekChar(howMuch = 0): string {
|
||||
return this.input[this.idx + howMuch];
|
||||
}
|
||||
|
||||
protected popChar(): string {
|
||||
const nextChar = this.peekChar(0);
|
||||
this.consumeChar(undefined);
|
||||
return nextChar;
|
||||
}
|
||||
|
||||
protected consumeChar(char: string | undefined): void {
|
||||
if (char !== undefined && this.input[this.idx] !== char) {
|
||||
throw Error(
|
||||
"Expected: '" +
|
||||
char +
|
||||
"' but found: '" +
|
||||
this.input[this.idx] +
|
||||
"' at offset: " +
|
||||
this.idx,
|
||||
);
|
||||
}
|
||||
|
||||
if (this.idx >= this.input.length) {
|
||||
throw Error("Unexpected end of input");
|
||||
}
|
||||
this.idx++;
|
||||
}
|
||||
|
||||
protected loc(begin: number): Location {
|
||||
return { begin: begin, end: this.idx };
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import type { Character, IRegExpAST, RegExpFlags } from "../types";
|
||||
|
||||
export function cc(char: string): number {
|
||||
return char.charCodeAt(0);
|
||||
}
|
||||
|
||||
export function insertToSet<T>(item: T | T[], set: T[]) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function (subItem) {
|
||||
set.push(subItem);
|
||||
});
|
||||
} else {
|
||||
set.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
export function addFlag(
|
||||
flagObj: RegExpFlags,
|
||||
flagKey: keyof Omit<RegExpFlags, keyof IRegExpAST>,
|
||||
) {
|
||||
if (flagObj[flagKey] === true) {
|
||||
throw "duplicate flag " + flagKey;
|
||||
}
|
||||
|
||||
const x: boolean = flagObj[flagKey];
|
||||
flagObj[flagKey] = true;
|
||||
}
|
||||
|
||||
export function ASSERT_EXISTS<T = Object>(obj: any): obj is T {
|
||||
// istanbul ignore next
|
||||
if (obj === undefined) {
|
||||
throw Error("Internal Error - Should never get here!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// istanbul ignore next
|
||||
export function ASSERT_NEVER_REACH_HERE(): any {
|
||||
throw Error("Internal Error - Should never get here!");
|
||||
}
|
||||
|
||||
export function isCharacter(obj: { type: string }): obj is Character {
|
||||
return obj["type"] === "Character";
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
export class RegExpParser {
|
||||
pattern: (input: string) => RegExpPattern;
|
||||
}
|
||||
|
||||
export interface IRegExpAST {
|
||||
type: string;
|
||||
loc: Location;
|
||||
}
|
||||
|
||||
export interface Location {
|
||||
begin: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
export type RegExpAstPart =
|
||||
| RegExpPattern
|
||||
| RegExpFlags
|
||||
| Disjunction
|
||||
| Alternative
|
||||
| Atom
|
||||
| Assertion
|
||||
| Quantifier;
|
||||
|
||||
export interface RegExpPattern extends IRegExpAST {
|
||||
type: "Pattern";
|
||||
flags: RegExpFlags;
|
||||
value: Disjunction;
|
||||
}
|
||||
|
||||
export interface RegExpFlags extends IRegExpAST {
|
||||
type: "Flags";
|
||||
global: boolean;
|
||||
ignoreCase: boolean;
|
||||
multiLine: boolean;
|
||||
unicode: boolean;
|
||||
sticky: boolean;
|
||||
}
|
||||
|
||||
export interface Disjunction extends IRegExpAST {
|
||||
type: "Disjunction";
|
||||
value: Alternative[];
|
||||
}
|
||||
|
||||
export interface Alternative extends IRegExpAST {
|
||||
type: "Alternative";
|
||||
value: Term[];
|
||||
}
|
||||
|
||||
export type Term = Atom | Assertion;
|
||||
|
||||
export interface Assertion extends IRegExpAST {
|
||||
type:
|
||||
| "StartAnchor"
|
||||
| "EndAnchor"
|
||||
| "WordBoundary"
|
||||
| "NonWordBoundary"
|
||||
| "Lookahead"
|
||||
| "NegativeLookahead"
|
||||
| "Lookbehind"
|
||||
| "NegativeLookbehind";
|
||||
|
||||
value?: Disjunction;
|
||||
}
|
||||
|
||||
export type Atom = Character | Set | Group | GroupBackReference;
|
||||
|
||||
export interface Character extends IRegExpAST {
|
||||
type: "Character";
|
||||
value: number;
|
||||
quantifier?: Quantifier;
|
||||
}
|
||||
|
||||
export type Range = { from: number; to: number };
|
||||
|
||||
export interface Set extends IRegExpAST {
|
||||
type: "Set";
|
||||
complement: boolean;
|
||||
value: (number | Range)[];
|
||||
quantifier?: Quantifier;
|
||||
}
|
||||
|
||||
export interface Group extends IRegExpAST {
|
||||
type: "Group";
|
||||
value: Disjunction;
|
||||
capturing: boolean;
|
||||
idx?: number;
|
||||
quantifier?: Quantifier;
|
||||
}
|
||||
|
||||
export interface GroupBackReference extends IRegExpAST {
|
||||
type: "GroupBackReference";
|
||||
value: number;
|
||||
quantifier?: Quantifier;
|
||||
}
|
||||
|
||||
export interface Quantifier extends IRegExpAST {
|
||||
type: "Quantifier";
|
||||
atLeast: number;
|
||||
atMost: number;
|
||||
greedy: boolean;
|
||||
}
|
||||
|
||||
export class BaseRegExpVisitor {
|
||||
/**
|
||||
* The entry point visitor method.
|
||||
* This will dispatch to the specific visitor method.
|
||||
*/
|
||||
visit(node: IRegExpAST): void;
|
||||
|
||||
/**
|
||||
* The entry point for visiting the children of a node.
|
||||
* Override this to filter the types of children visited
|
||||
* or to support new types of children in extended ASTs.
|
||||
*/
|
||||
visitChildren(node: IRegExpAST): void;
|
||||
|
||||
/**
|
||||
* The specific visitor methods
|
||||
* Override some of these of create custom visitors.
|
||||
*/
|
||||
visitPattern(node: RegExpPattern): void;
|
||||
visitFlags(node: RegExpFlags): void;
|
||||
visitDisjunction(node: Disjunction): void;
|
||||
visitAlternative(node: Alternative): void;
|
||||
visitStartAnchor(node: Assertion): void;
|
||||
visitEndAnchor(node: Assertion): void;
|
||||
visitWordBoundary(node: Assertion): void;
|
||||
visitNonWordBoundary(node: Assertion): void;
|
||||
visitLookahead(node: Assertion): void;
|
||||
visitNegativeLookahead(node: Assertion): void;
|
||||
visitLookbehind(node: Assertion): void;
|
||||
visitNegativeLookbehind(node: Assertion): void;
|
||||
visitCharacter(node: Character): void;
|
||||
visitSet(node: Set): void;
|
||||
visitGroup(Node: Group): void;
|
||||
visitGroupBackReference(Node: GroupBackReference): void;
|
||||
visitQuantifier(Node: Quantifier): void;
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+2778
File diff suppressed because it is too large
Load Diff
+50
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"name": "@chevrotain/types",
|
||||
"version": "12.0.0",
|
||||
"description": "Type Signatures and API Website for Chevrotain",
|
||||
"keywords": [],
|
||||
"bugs": {
|
||||
"url": "https://github.com/Chevrotain/chevrotain/issues"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"author": {
|
||||
"name": "Shahar Soel"
|
||||
},
|
||||
"files": [
|
||||
"api.d.ts",
|
||||
"README.md",
|
||||
"LICENSE.TXT"
|
||||
],
|
||||
"type": "module",
|
||||
"types": "./api.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./api.d.ts"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/Chevrotain/chevrotain.git"
|
||||
},
|
||||
"homepage": "https://chevrotain.io/documentation/",
|
||||
"scripts": {
|
||||
"---------- CI FLOWS --------": "",
|
||||
"ci": "pnpm run build",
|
||||
"build": "npm-run-all clean compile api-site:build",
|
||||
"postversion": "npm-run-all api-site:build api-site:upload",
|
||||
"---------- BUILD STEPS --------": "",
|
||||
"clean": "shx rm -rf dev lib",
|
||||
"compile:watch": "tsc -w",
|
||||
"compile": "tsc",
|
||||
"api-site:build": "typedoc api.d.ts --out dev/docs --excludeExternals --excludePrivate",
|
||||
"api-site:upload": "./scripts/api-site-upload.sh"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typedoc": "0.28.17",
|
||||
"typescript": "5.9.3"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "d3b4d05d5dd53d53d27175119bc7c9ed8056e3d5"
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { PRINT_WARNING, PRINT_ERROR } from "./print.js";
|
||||
export { timer } from "./timer.js";
|
||||
export { toFastProperties } from "./to-fast-properties.js";
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export { PRINT_WARNING, PRINT_ERROR } from "./print.js";
|
||||
export { timer } from "./timer.js";
|
||||
export { toFastProperties } from "./to-fast-properties.js";
|
||||
//# sourceMappingURL=api.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare function PRINT_ERROR(msg: string): void;
|
||||
export declare function PRINT_WARNING(msg: string): void;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
export function PRINT_ERROR(msg) {
|
||||
/* istanbul ignore else - can't override global.console in node.js */
|
||||
if (console && console.error) {
|
||||
console.error(`Error: ${msg}`);
|
||||
}
|
||||
}
|
||||
export function PRINT_WARNING(msg) {
|
||||
/* istanbul ignore else - can't override global.console in node.js*/
|
||||
if (console && console.warn) {
|
||||
// TODO: modify docs accordingly
|
||||
console.warn(`Warning: ${msg}`);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=print.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"print.js","sourceRoot":"","sources":["../../src/print.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,qEAAqE;IACrE,IAAI,OAAO,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;QAC7B,OAAO,CAAC,KAAK,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC;IACjC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,oEAAoE;IACpE,IAAI,OAAO,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QAC5B,gCAAgC;QAChC,OAAO,CAAC,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC,CAAC;IAClC,CAAC;AACH,CAAC"}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export declare function timer<T>(func: () => T): {
|
||||
time: number;
|
||||
value: T;
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
export function timer(func) {
|
||||
const start = new Date().getTime();
|
||||
const val = func();
|
||||
const end = new Date().getTime();
|
||||
const total = end - start;
|
||||
return { time: total, value: val };
|
||||
}
|
||||
//# sourceMappingURL=timer.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"timer.js","sourceRoot":"","sources":["../../src/timer.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,KAAK,CAAI,IAAa;IACpC,MAAM,KAAK,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;IACnC,MAAM,GAAG,GAAG,IAAI,EAAE,CAAC;IACnB,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;IACjC,MAAM,KAAK,GAAG,GAAG,GAAG,KAAK,CAAC;IAC1B,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;AACrC,CAAC"}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function toFastProperties(toBecomeFast: any): any;
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// based on: https://github.com/petkaantonov/bluebird/blob/b97c0d2d487e8c5076e8bd897e0dcd4622d31846/src/util.js#L201-L216
|
||||
export function toFastProperties(toBecomeFast) {
|
||||
function FakeConstructor() { }
|
||||
// If our object is used as a constructor, it would receive
|
||||
FakeConstructor.prototype = toBecomeFast;
|
||||
const fakeInstance = new FakeConstructor();
|
||||
function fakeAccess() {
|
||||
return typeof fakeInstance.bar;
|
||||
}
|
||||
// help V8 understand this is a "real" prototype by actually using
|
||||
// the fake instance.
|
||||
fakeAccess();
|
||||
fakeAccess();
|
||||
// Always true condition to suppress the Firefox warning of unreachable
|
||||
// code after a return statement.
|
||||
if (1)
|
||||
return toBecomeFast;
|
||||
// Eval prevents optimization of this method (even though this is dead code)
|
||||
// - https://esbuild.github.io/content-types/#direct-eval
|
||||
/* istanbul ignore next */
|
||||
// tslint:disable-next-line
|
||||
(0, eval)(toBecomeFast);
|
||||
}
|
||||
//# sourceMappingURL=to-fast-properties.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"to-fast-properties.js","sourceRoot":"","sources":["../../src/to-fast-properties.ts"],"names":[],"mappings":"AAAA,yHAAyH;AACzH,MAAM,UAAU,gBAAgB,CAAC,YAAiB;IAChD,SAAS,eAAe,KAAI,CAAC;IAE7B,2DAA2D;IAC3D,eAAe,CAAC,SAAS,GAAG,YAAY,CAAC;IACzC,MAAM,YAAY,GAAG,IAAK,eAAuB,EAAE,CAAC;IAEpD,SAAS,UAAU;QACjB,OAAO,OAAO,YAAY,CAAC,GAAG,CAAC;IACjC,CAAC;IAED,kEAAkE;IAClE,qBAAqB;IACrB,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IAEb,uEAAuE;IACvE,iCAAiC;IACjC,IAAI,CAAC;QAAE,OAAO,YAAY,CAAC;IAE3B,4EAA4E;IAC5E,yDAAyD;IACzD,0BAA0B;IAC1B,2BAA2B;IAC3B,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,YAAY,CAAC,CAAC;AAC1B,CAAC"}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "@chevrotain/utils",
|
||||
"version": "12.0.0",
|
||||
"description": "common utilities",
|
||||
"keywords": [],
|
||||
"bugs": {
|
||||
"url": "https://github.com/Chevrotain/chevrotain/issues"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"author": {
|
||||
"name": "Shahar Soel"
|
||||
},
|
||||
"type": "module",
|
||||
"types": "./lib/src/api.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./lib/src/api.js",
|
||||
"types": "./lib/src/api.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"lib/src/**/*.js",
|
||||
"lib/src/**/*.map",
|
||||
"lib/src/**/*.d.ts",
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/Chevrotain/chevrotain.git"
|
||||
},
|
||||
"scripts": {
|
||||
"---------- CI FLOWS --------": "",
|
||||
"ci": "npm-run-all build test",
|
||||
"build": "npm-run-all clean compile",
|
||||
"test": "npm-run-all coverage",
|
||||
"---------- BUILD STEPS --------": "",
|
||||
"clean": "shx rm -rf lib coverage",
|
||||
"compile:watch": "tsc -w",
|
||||
"compile": "tsc",
|
||||
"coverage": "c8 mocha --enable-source-maps"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "d3b4d05d5dd53d53d27175119bc7c9ed8056e3d5"
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { PRINT_WARNING, PRINT_ERROR } from "./print.js";
|
||||
export { timer } from "./timer.js";
|
||||
export { toFastProperties } from "./to-fast-properties.js";
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
export function PRINT_ERROR(msg: string) {
|
||||
/* istanbul ignore else - can't override global.console in node.js */
|
||||
if (console && console.error) {
|
||||
console.error(`Error: ${msg}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function PRINT_WARNING(msg: string) {
|
||||
/* istanbul ignore else - can't override global.console in node.js*/
|
||||
if (console && console.warn) {
|
||||
// TODO: modify docs accordingly
|
||||
console.warn(`Warning: ${msg}`);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
export function timer<T>(func: () => T): { time: number; value: T } {
|
||||
const start = new Date().getTime();
|
||||
const val = func();
|
||||
const end = new Date().getTime();
|
||||
const total = end - start;
|
||||
return { time: total, value: val };
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
// based on: https://github.com/petkaantonov/bluebird/blob/b97c0d2d487e8c5076e8bd897e0dcd4622d31846/src/util.js#L201-L216
|
||||
export function toFastProperties(toBecomeFast: any) {
|
||||
function FakeConstructor() {}
|
||||
|
||||
// If our object is used as a constructor, it would receive
|
||||
FakeConstructor.prototype = toBecomeFast;
|
||||
const fakeInstance = new (FakeConstructor as any)();
|
||||
|
||||
function fakeAccess() {
|
||||
return typeof fakeInstance.bar;
|
||||
}
|
||||
|
||||
// help V8 understand this is a "real" prototype by actually using
|
||||
// the fake instance.
|
||||
fakeAccess();
|
||||
fakeAccess();
|
||||
|
||||
// Always true condition to suppress the Firefox warning of unreachable
|
||||
// code after a return statement.
|
||||
if (1) return toBecomeFast;
|
||||
|
||||
// Eval prevents optimization of this method (even though this is dead code)
|
||||
// - https://esbuild.github.io/content-types/#direct-eval
|
||||
/* istanbul ignore next */
|
||||
// tslint:disable-next-line
|
||||
(0, eval)(toBecomeFast);
|
||||
}
|
||||
Reference in New Issue
Block a user