Skip to content

Commit

Permalink
WIP on DSL method idx as a parameter\n part of #1004
Browse files Browse the repository at this point in the history
  • Loading branch information
bd82 committed Sep 13, 2019
1 parent 1dc658e commit 727311a
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 27 deletions.
151 changes: 125 additions & 26 deletions packages/chevrotain/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,69 @@ declare abstract class BaseParser {
*/
/* protected */ ACTION<T>(impl: () => T): T

/**
* Like `CONSUME` with the numerical suffix as a parameter, e.g:
* consume(0, X) === CONSUME(X)
* consume(1, X) === CONSUME1(X)
* consume(2, X) === CONSUME2(X)
* ...
* @see CONSUME
*/
/* protected */ consume(
idx: number,
tokType: TokenType,
options?: ConsumeMethodOpts
): IToken

/**
* Like `OPTION` with the numerical suffix as a parameter, e.g:
* option(0, X) === OPTION(X)
* option(1, X) === OPTION1(X)
* option(2, X) === OPTION2(X)
* ...
* @see SUBRULE
*/
/* protected */ option<OUT>(
idx: number,
actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>
): OUT

/**
* Like `OR` with the numerical suffix as a parameter, e.g:
* or(0, X) === OR(X)
* or(1, X) === OR1(X)
* or(2, X) === OR2(X)
* ...
* @see OR
*/
/* protected */ or(idx: number, altsOrOpts: IOrAlt[] | OrMethodOpts): any

/**
* Like `MANY` with the numerical suffix as a parameter, e.g:
* many(0, X) === MANY(X)
* many(1, X) === MANY1(X)
* many(2, X) === MANY2(X)
* ...
* @see MANY
*/
/* protected */ many(
idx: number,
actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>
): void

/**
* Like `AT_LEAST_ONE` with the numerical suffix as a parameter, e.g:
* atLeastOne(0, X) === AT_LEAST_ONE(X)
* atLeastOne(1, X) === AT_LEAST_ONE1(X)
* atLeastOne(2, X) === AT_LEAST_ONE2(X)
* ...
* @see AT_LEAST_ONE
*/
/* protected */ atLeastOne(
idx: number,
actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>
): void

/**
*
* A Parsing DSL method use to consume a single Token.
Expand Down Expand Up @@ -879,26 +942,6 @@ export declare class Parser extends BaseParser {
config?: IRuleConfig<T>
): (idxInCallingRule?: number, ...args: any[]) => T | any

/**
* The Parsing DSL Method is used by one rule to call another.
* It is equivalent to a non-Terminal in EBNF notation.
*
* This may seem redundant as it does not actually do much.
* However using it is **mandatory** for all sub rule invocations.
*
* Calling another rule without wrapping in SUBRULE(...)
* will cause errors/mistakes in the Parser's self analysis phase,
* which will lead to errors in error recovery/automatic lookahead calculation
* and any other functionality relying on the Parser's self analysis
* output.
*
* As in CONSUME the index in the method name indicates the occurrence
* of the sub rule invocation in its rule.
*
* @param ruleToCall - The rule to invoke.
* @param options - optional properties to modify the behavior of SUBRULE.
* @returns The result of invoking ruleToCall.
*/
/* protected */ SUBRULE<T>(
ruleToCall: (idx: number) => T,
options?: SubruleMethodOpts
Expand Down Expand Up @@ -1001,7 +1044,7 @@ export declare class CstParser extends BaseParser {
/* protected */ static performSelfAnalysis(parserInstance: Parser): void

/**
* @see Parser.RULE
* Creates a Grammar Rule
*/
/* protected */ RULE(
name: string,
Expand All @@ -1010,7 +1053,8 @@ export declare class CstParser extends BaseParser {
): (idxInCallingRule?: number, ...args: any[]) => CstNode

/**
* @see Parser.RULE
* Overrides a Grammar Rule
* See usage example in: https://github.com/SAP/chevrotain/blob/master/examples/parser/versioning/versioning.js
*/
/* protected */ OVERRIDE_RULE<T>(
name: string,
Expand All @@ -1019,7 +1063,34 @@ export declare class CstParser extends BaseParser {
): (idxInCallingRule?: number, ...args: any[]) => CstNode

/**
* @see Parser.SUBRULE
* Like `SUBRULE` with the numerical suffix as a parameter, e.g:
* subrule(0, X) === SUBRULE(X)
* subrule(1, X) === SUBRULE1(X)
* subrule(2, X) === SUBRULE2(X)
* ...
* @see SUBRULE
*/
/* protected */ subrule(
ruleToCall: (idx: number) => CstNode,
options?: SubruleMethodOpts
): CstNode

/**
* The Parsing DSL Method is used by one rule to call another.
* It is equivalent to a non-Terminal in EBNF notation.
*
* This may seem redundant as it does not actually do much.
* However using it is **mandatory** for all sub rule invocations.
*
* Calling another rule without wrapping in SUBRULE(...)
* will cause errors/mistakes in the Parser's self analysis phase,
* which will lead to errors in error recovery/automatic lookahead calculation
* and any other functionality relying on the Parser's self analysis
* output.
*
* As in CONSUME the index in the method name indicates the occurrence
* of the sub rule invocation in its rule.
*
*/
/* protected */ SUBRULE(
ruleToCall: (idx: number) => CstNode,
Expand Down Expand Up @@ -1123,7 +1194,7 @@ export declare class EmbeddedActionsParser extends BaseParser {
// TODO: remove `outputCST` from the config options in the constructor

/**
* @see Parser.RULE
* Creates a Grammar Rule
*/
/* protected */ RULE<T>(
name: string,
Expand All @@ -1132,7 +1203,8 @@ export declare class EmbeddedActionsParser extends BaseParser {
): (idxInCallingRule?: number, ...args: any[]) => T

/**
* @see Parser.OVERRIDE_RULE
* Overrides a Grammar Rule
* See usage example in: https://github.com/SAP/chevrotain/blob/master/examples/parser/versioning/versioning.js
*/
/* protected */ OVERRIDE_RULE<T>(
name: string,
Expand All @@ -1141,7 +1213,34 @@ export declare class EmbeddedActionsParser extends BaseParser {
): (idxInCallingRule?: number, ...args: any[]) => T

/**
* @see BaseParser.SUBRULE
* Like `SUBRULE` with the numerical suffix as a parameter, e.g:
* subrule(0, X) === SUBRULE(X)
* subrule(1, X) === SUBRULE1(X)
* subrule(2, X) === SUBRULE2(X)
* ...
* @see SUBRULE
*/
/* protected */ subrule<T>(
ruleToCall: (idx: number) => T,
options?: SubruleMethodOpts
): T

/**
* The Parsing DSL Method is used by one rule to call another.
* It is equivalent to a non-Terminal in EBNF notation.
*
* This may seem redundant as it does not actually do much.
* However using it is **mandatory** for all sub rule invocations.
*
* Calling another rule without wrapping in SUBRULE(...)
* will cause errors/mistakes in the Parser's self analysis phase,
* which will lead to errors in error recovery/automatic lookahead calculation
* and any other functionality relying on the Parser's self analysis
* output.
*
* As in CONSUME the index in the method name indicates the occurrence
* of the sub rule invocation in its rule.
*
*/
/* protected */ SUBRULE<T>(
ruleToCall: (idx: number) => T,
Expand Down
53 changes: 52 additions & 1 deletion packages/chevrotain/src/parse/parser/traits/recognizer_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DSLMethodOptsWithErr,
GrammarAction,
IAnyOrAlt,
IOrAlt,
IRuleConfig,
ISerializedGast,
IToken,
Expand All @@ -22,7 +23,7 @@ import { MixedInParser } from "./parser_traits"
import { Rule, serializeGrammar } from "../../grammar/gast/gast_public"

/**
* This trait is responsible for implementing the offical API
* This trait is responsible for implementing the public API
* for defining Chevrotain parsers, i.e:
* - CONSUME
* - RULE
Expand All @@ -34,6 +35,56 @@ export class RecognizerApi {
return impl.call(this)
}

consume(
this: MixedInParser,
idx: number,
tokType: TokenType,
options?: ConsumeMethodOpts
): IToken {
return this.consumeInternal(tokType, idx, options)
}

subrule<T>(
this: MixedInParser,
idx: number,
ruleToCall: (idx: number) => T,
options?: SubruleMethodOpts
): T {
return this.subruleInternal(ruleToCall, idx, options)
}

option<OUT>(
this: MixedInParser,
idx: number,
actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>
): OUT {
return this.optionInternal(actionORMethodDef, idx)
}

or(
this: MixedInParser,
idx: number,
altsOrOpts: IOrAlt[] | OrMethodOpts
): any {
return this.orInternal(altsOrOpts, idx)
}

many(
this: MixedInParser,
idx: number,
actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>
): void {
return this.manyInternal(idx, actionORMethodDef)
}

atLeastOne(
this: MixedInParser,
idx: number,
actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>
): void {
return this.atLeastOneInternal(idx, actionORMethodDef)
}

CONSUME(
this: MixedInParser,
tokType: TokenType,
Expand Down

0 comments on commit 727311a

Please sign in to comment.