1- import { Parser , isRecognitionException } from "chevrotain" ;
1+ import { CstParser , isRecognitionException } from "chevrotain" ;
22import { allTokens , tokens as t } from "./tokens.js" ;
33import * as lexicalStructure from "./productions/lexical-structure.js" ;
44import * as typesValuesVariables from "./productions/types-values-and-variables.js" ;
@@ -35,7 +35,7 @@ import { shouldNotFormat } from "./comments.js";
3535 * TODO: document guide lines for using back tracking
3636 *
3737 */
38- export default class JavaParser extends Parser {
38+ export default class JavaParser extends CstParser {
3939 constructor ( ) {
4040 super ( allTokens , {
4141 maxLookahead : 1 ,
@@ -76,16 +76,16 @@ export default class JavaParser extends Parser {
7676 }
7777
7878 cstPostNonTerminal ( ruleCstResult , ruleName ) {
79+ if ( this . isBackTracking ( ) ) {
80+ return ;
81+ }
7982 super . cstPostNonTerminal ( ruleCstResult , ruleName ) ;
80- if ( this . isBackTracking ( ) === false ) {
81- this . mostEnclosiveCstNodeByStartOffset [
82- ruleCstResult . location . startOffset
83- ] = ruleCstResult ;
84- this . mostEnclosiveCstNodeByEndOffset [ ruleCstResult . location . endOffset ] =
85- ruleCstResult ;
83+ this . mostEnclosiveCstNodeByStartOffset [ ruleCstResult . location . startOffset ] =
84+ ruleCstResult ;
85+ this . mostEnclosiveCstNodeByEndOffset [ ruleCstResult . location . endOffset ] =
86+ ruleCstResult ;
8687
87- shouldNotFormat ( ruleCstResult , this . onOffCommentPairs ) ;
88- }
88+ shouldNotFormat ( ruleCstResult , this . onOffCommentPairs ) ;
8989 }
9090
9191 BACKTRACK_LOOKAHEAD ( production , errValue = false ) {
@@ -94,16 +94,26 @@ export default class JavaParser extends Parser {
9494 // TODO: "saveRecogState" does not handle the occurrence stack
9595 const orgState = this . saveRecogState ( ) ;
9696 try {
97- // hack to enable outputting none CST values from grammar rules.
98- this . outputCst = false ;
99- return production . call ( this ) ;
97+ // hack to enable outputting non-CST values from grammar rules.
98+ const { ruleName, originalGrammarAction } = production ;
99+ try {
100+ this . ruleInvocationStateUpdate (
101+ this . fullRuleNameToShort [ ruleName ] ,
102+ ruleName ,
103+ this . subruleIdx
104+ ) ;
105+ return originalGrammarAction . call ( this ) ;
106+ } catch ( e ) {
107+ return this . invokeRuleCatch ( e , true , ( ) => undefined ) ;
108+ } finally {
109+ this . ruleFinallyStateUpdate ( ) ;
110+ }
100111 } catch ( e ) {
101112 if ( isRecognitionException ( e ) ) {
102113 return errValue ;
103114 }
104115 throw e ;
105116 } finally {
106- this . outputCst = true ;
107117 this . reloadRecogState ( orgState ) ;
108118 this . isBackTrackingStack . pop ( ) ;
109119 }
0 commit comments