added code generation for predicate hoisting

This commit is contained in:
overflowerror 2021-11-17 20:00:28 +01:00
parent add044ef5e
commit 984d27b57c
4 changed files with 18 additions and 3 deletions

View file

@ -36,6 +36,7 @@ import static extension org.eclipse.xtext.GrammarUtil.*
import static extension org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil.*
import static extension org.eclipse.xtext.xtext.generator.parser.antlr.TerminalRuleToLexerBody.*
import org.eclipse.xtext.xtext.generator.util.SyntheticTerminalDetector
import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.HoistingProcessor
abstract class AbstractAntlrGrammarGenerator {
@ -48,6 +49,9 @@ abstract class AbstractAntlrGrammarGenerator {
@Inject
protected extension SyntheticTerminalDetector
@Inject
protected extension HoistingProcessor
@Inject CodeConfig codeConfig
protected KeywordHelper keywordHelper
@ -326,7 +330,7 @@ abstract class AbstractAntlrGrammarGenerator {
protected dispatch def String dataTypeEbnf2(AbstractElement it, boolean supportActions) '''ERROR «eClass.name» not matched'''
protected dispatch def String dataTypeEbnf2(Alternatives it, boolean supportActions) '''
«FOR e:elements SEPARATOR '\n |'»«e.dataTypeEbnf(supportActions)»«ENDFOR»
«FOR e:elements SEPARATOR '\n |'»«findGuardForElement.renderPredicate»«e.dataTypeEbnf(supportActions)»«ENDFOR»
'''
protected dispatch def String dataTypeEbnf2(Group it, boolean supportActions) '''

View file

@ -12,6 +12,10 @@ package org.eclipse.xtext.xtext.generator.parser.antlr.hoisting;
* @author overflow - Initial contribution and API
*/
public interface HoistingGuard extends Guard {
default String renderPredicate() {
return "{" + render() + "}?=>";
}
boolean hasTerminal();
static public HoistingGuard unguarded() {

View file

@ -37,7 +37,7 @@ import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.utils.StreamUtils
/**
* @author overflow - Initial contribution and API
*/
public class Hoisting {
public class HoistingProcessor {
private Map<String, HoistingGuard> ruleCache = new HashMap<>();
private Map<Group, HoistingGuard> groupCache = new HashMap<>();
@ -376,7 +376,7 @@ public class Hoisting {
return groupGuard;
}
private HoistingGuard findGuardForElement(AbstractElement element) {
public HoistingGuard findGuardForElement(AbstractElement element) {
if (element instanceof Alternatives) {
return findGuardForAlternatives((Alternatives) element);
} else if (element instanceof Group) {

View file

@ -51,6 +51,7 @@ import org.eclipse.xtext.xtext.generator.parser.antlr.CombinedGrammarMarker;
import org.eclipse.xtext.xtext.generator.parser.antlr.GrammarNaming;
import org.eclipse.xtext.xtext.generator.parser.antlr.KeywordHelper;
import org.eclipse.xtext.xtext.generator.parser.antlr.TerminalRuleToLexerBody;
import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.HoistingProcessor;
import org.eclipse.xtext.xtext.generator.util.SyntheticTerminalDetector;
@SuppressWarnings("all")
@ -67,6 +68,10 @@ public abstract class AbstractAntlrGrammarGenerator {
@Extension
protected SyntheticTerminalDetector _syntheticTerminalDetector;
@Inject
@Extension
protected HoistingProcessor _hoistingProcessor;
@Inject
private CodeConfig codeConfig;
@ -801,6 +806,8 @@ public abstract class AbstractAntlrGrammarGenerator {
} else {
_builder.appendImmediate("\n |", "");
}
String _renderPredicate = this._hoistingProcessor.findGuardForElement(it).renderPredicate();
_builder.append(_renderPredicate);
String _dataTypeEbnf = this.dataTypeEbnf(e, supportActions);
_builder.append(_dataTypeEbnf);
}