mirror of
https://github.com/pdemian/human2regex.git
synced 2025-05-16 12:30:09 -07:00
It's parsing!
This commit is contained in:
parent
bacb104d89
commit
bddc5d4f3b
@ -14,8 +14,10 @@ export class Human2RegexParser extends CstParser {
|
|||||||
|
|
||||||
public nodes: { [key: string]: (idxInCallingRule?: number, ...args: unknown[]) => CstNode } = {};
|
public nodes: { [key: string]: (idxInCallingRule?: number, ...args: unknown[]) => CstNode } = {};
|
||||||
|
|
||||||
|
public parse : (idxInCallingRule?: number, ...args: unknown[]) => CstNode;
|
||||||
|
|
||||||
constructor(private options: Human2RegexParserOptions = new Human2RegexParserOptions()) {
|
constructor(private options: Human2RegexParserOptions = new Human2RegexParserOptions()) {
|
||||||
super(T.AllTokens, { recoveryEnabled: true, maxLookahead: 2});
|
super(T.AllTokens, { recoveryEnabled: false, maxLookahead: 2});
|
||||||
|
|
||||||
if (Human2RegexParser.already_init) {
|
if (Human2RegexParser.already_init) {
|
||||||
throw new Error("Only 1 instance of Human2RegexParser allowed");
|
throw new Error("Only 1 instance of Human2RegexParser allowed");
|
||||||
@ -89,6 +91,7 @@ export class Human2RegexParser extends CstParser {
|
|||||||
$.AT_LEAST_ONE_SEP({
|
$.AT_LEAST_ONE_SEP({
|
||||||
SEP: T.Or,
|
SEP: T.Or,
|
||||||
DEF: () => {
|
DEF: () => {
|
||||||
|
$.OPTION3(() => $.CONSUME(T.A));
|
||||||
$.OR([
|
$.OR([
|
||||||
{ ALT: () => $.CONSUME(T.Anything) },
|
{ ALT: () => $.CONSUME(T.Anything) },
|
||||||
{ ALT: () => $.CONSUME(T.StringLiteral) },
|
{ ALT: () => $.CONSUME(T.StringLiteral) },
|
||||||
@ -123,6 +126,7 @@ export class Human2RegexParser extends CstParser {
|
|||||||
$.OPTION3(() => $.CONSUME2(T.Optional));
|
$.OPTION3(() => $.CONSUME2(T.Optional));
|
||||||
$.SUBRULE2(this.nodes.MatchSubStatement);
|
$.SUBRULE2(this.nodes.MatchSubStatement);
|
||||||
});
|
});
|
||||||
|
$.CONSUME(T.EndOfLine);
|
||||||
});
|
});
|
||||||
|
|
||||||
// using global matching
|
// using global matching
|
||||||
@ -141,6 +145,7 @@ export class Human2RegexParser extends CstParser {
|
|||||||
$.OPTION(() => $.CONSUME(T.Matching));
|
$.OPTION(() => $.CONSUME(T.Matching));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
$.CONSUME(T.EndOfLine);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.nodes.GroupStatement = $.RULE("GroupStatement", () => {
|
this.nodes.GroupStatement = $.RULE("GroupStatement", () => {
|
||||||
@ -153,6 +158,7 @@ export class Human2RegexParser extends CstParser {
|
|||||||
$.CONSUME(T.Called);
|
$.CONSUME(T.Called);
|
||||||
$.CONSUME(T.StringLiteral);
|
$.CONSUME(T.StringLiteral);
|
||||||
});
|
});
|
||||||
|
$.CONSUME2(T.EndOfLine);
|
||||||
$.CONSUME(T.Indent);
|
$.CONSUME(T.Indent);
|
||||||
$.AT_LEAST_ONE(this.nodes.Statement);
|
$.AT_LEAST_ONE(this.nodes.Statement);
|
||||||
$.CONSUME(T.Outdent);
|
$.CONSUME(T.Outdent);
|
||||||
@ -162,6 +168,7 @@ export class Human2RegexParser extends CstParser {
|
|||||||
$.OPTION3(() => $.CONSUME(T.Optional));
|
$.OPTION3(() => $.CONSUME(T.Optional));
|
||||||
$.CONSUME(T.Repeat);
|
$.CONSUME(T.Repeat);
|
||||||
$.OPTION(() => $.SUBRULE(this.nodes.CountSubStatement));
|
$.OPTION(() => $.SUBRULE(this.nodes.CountSubStatement));
|
||||||
|
$.CONSUME3(T.EndOfLine);
|
||||||
$.CONSUME(T.Indent);
|
$.CONSUME(T.Indent);
|
||||||
$.AT_LEAST_ONE(this.nodes.Statement);
|
$.AT_LEAST_ONE(this.nodes.Statement);
|
||||||
$.CONSUME(T.Outdent);
|
$.CONSUME(T.Outdent);
|
||||||
@ -173,15 +180,16 @@ export class Human2RegexParser extends CstParser {
|
|||||||
{ ALT: () => $.SUBRULE(this.nodes.GroupStatement) },
|
{ ALT: () => $.SUBRULE(this.nodes.GroupStatement) },
|
||||||
{ ALT: () => $.SUBRULE(this.nodes.RepeatStatement) }
|
{ ALT: () => $.SUBRULE(this.nodes.RepeatStatement) }
|
||||||
]);
|
]);
|
||||||
$.CONSUME(T.EndOfLine);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.nodes.Regex = $.RULE("Regex", () => {
|
this.nodes.Regex = $.RULE("Regex", () => {
|
||||||
$.OPTION(() => $.SUBRULE(this.nodes.UsingStatement));
|
$.MANY(() => $.SUBRULE(this.nodes.UsingStatement));
|
||||||
$.MANY(() => $.SUBRULE(this.nodes.Statement) );
|
$.MANY2(() => $.SUBRULE(this.nodes.Statement) );
|
||||||
});
|
});
|
||||||
|
|
||||||
this.performSelfAnalysis();
|
this.performSelfAnalysis();
|
||||||
|
|
||||||
|
this.parse = this.nodes.Regex;
|
||||||
}
|
}
|
||||||
|
|
||||||
//public set_options(options: Human2RegexParserOptions) : void {
|
//public set_options(options: Human2RegexParserOptions) : void {
|
||||||
|
@ -56,22 +56,13 @@ create an optional group
|
|||||||
match 0+ any thing
|
match 0+ any thing
|
||||||
`);
|
`);
|
||||||
|
|
||||||
for(const r of result.tokens) {
|
|
||||||
console.log(`[${r.tokenType.name}]: ${r.image}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(result.errors);
|
console.log(result.errors);
|
||||||
|
|
||||||
parser.input = result.tokens;
|
parser.input = result.tokens;
|
||||||
const regex = parser.nodes.regex;
|
const regex = parser.parse();
|
||||||
|
|
||||||
console.log(regex);
|
console.log(regex);
|
||||||
console.log(parser.errors);
|
console.log(parser.errors);
|
||||||
|
|
||||||
|
|
||||||
//interpreter.visit(regex);
|
//interpreter.visit(regex);
|
||||||
|
|
||||||
//parser.getBaseCstVisitorConstructor();
|
//parser.getBaseCstVisitorConstructor();
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user