added hoisting to generator

This commit is contained in:
overflowerror 2021-11-18 17:18:23 +01:00
parent 1cf3028bfa
commit aae59c9805
5 changed files with 67 additions and 3 deletions

View file

@ -37,6 +37,8 @@ import static extension org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGram
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
import java.lang.reflect.Parameter
import org.eclipse.xtext.AbstractSemanticPredicate
abstract class AbstractAntlrGrammarGenerator {
@ -289,6 +291,9 @@ abstract class AbstractAntlrGrammarGenerator {
protected def String compileEBNF(AbstractRule it, AntlrOptions options) '''
// Rule «originalElement.name»
«IF it instanceof ParserRule»
// Guard: «findGuardForRule.renderDescription»
«ENDIF»
«ruleName»«compileInit(options)»:
«IF it instanceof ParserRule && originalElement.datatypeRule»
«dataTypeEbnf(alternatives, true)»
@ -330,7 +335,10 @@ 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 |'»«findGuardForElement.renderPredicate»«e.dataTypeEbnf(supportActions)»«ENDFOR»
// «elements.size»
«FOR e:elements SEPARATOR '\n |'»
// «e.toString»
«e.findGuardForElement.renderPredicate»«e.dataTypeEbnf(supportActions)»«ENDFOR»
'''
protected dispatch def String dataTypeEbnf2(Group it, boolean supportActions) '''
@ -352,7 +360,10 @@ abstract class AbstractAntlrGrammarGenerator {
protected dispatch def String ebnf2(AbstractElement it, AntlrOptions options, boolean supportActions) '''ERROR «eClass.name» not matched'''
protected dispatch def String ebnf2(Alternatives it, AntlrOptions options, boolean supportActions) '''
«FOR element:elements SEPARATOR '\n |'»«element.ebnf(options, supportActions)»«ENDFOR»
// «elements.size»
«FOR element:elements SEPARATOR '\n |'»
// «element.toString»
«element.findGuardForElement.renderPredicate»«element.ebnf(options, supportActions)»«ENDFOR»
'''
protected dispatch def String ebnf2(Group it, AntlrOptions options, boolean supportActions) '''
@ -371,6 +382,10 @@ abstract class AbstractAntlrGrammarGenerator {
''
}
protected dispatch def String ebnf2(AbstractSemanticPredicate it, AntlrOptions options, boolean supportActions) '''
{«it.code.source»}?=>
'''
protected def String ebnf(Keyword it) {
if (combinedGrammar) "'" + value.toAntlrString + "'" else keywordHelper.getRuleName(value)
}

View file

@ -18,6 +18,7 @@ import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.AbstractSemanticPredicate;
import org.eclipse.xtext.Action;
import org.eclipse.xtext.Alternatives;
import org.eclipse.xtext.Assignment;
@ -635,6 +636,14 @@ public abstract class AbstractAntlrGrammarGenerator {
String _name = AntlrGrammarGenUtil.<AbstractRule>getOriginalElement(it).getName();
_builder.append(_name);
_builder.newLineIfNotEmpty();
{
if ((it instanceof ParserRule)) {
_builder.append("// Guard: ");
String _renderDescription = this._hoistingProcessor.findGuardForRule(((ParserRule)it)).renderDescription();
_builder.append(_renderDescription);
_builder.newLineIfNotEmpty();
}
}
String _ruleName = this._grammarAccessExtensions.ruleName(it);
_builder.append(_ruleName);
String _compileInit = this.compileInit(it, options);
@ -797,6 +806,10 @@ public abstract class AbstractAntlrGrammarGenerator {
protected String _dataTypeEbnf2(final Alternatives it, final boolean supportActions) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("// ");
int _size = it.getElements().size();
_builder.append(_size);
_builder.newLineIfNotEmpty();
{
EList<AbstractElement> _elements = it.getElements();
boolean _hasElements = false;
@ -806,7 +819,11 @@ public abstract class AbstractAntlrGrammarGenerator {
} else {
_builder.appendImmediate("\n |", "");
}
String _renderPredicate = this._hoistingProcessor.findGuardForElement(it).renderPredicate();
_builder.append("// ");
String _string = e.toString();
_builder.append(_string);
_builder.newLineIfNotEmpty();
String _renderPredicate = this._hoistingProcessor.findGuardForElement(e).renderPredicate();
_builder.append(_renderPredicate);
String _dataTypeEbnf = this.dataTypeEbnf(e, supportActions);
_builder.append(_dataTypeEbnf);
@ -869,6 +886,10 @@ public abstract class AbstractAntlrGrammarGenerator {
protected String _ebnf2(final Alternatives it, final AntlrOptions options, final boolean supportActions) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("// ");
int _size = it.getElements().size();
_builder.append(_size);
_builder.newLineIfNotEmpty();
{
EList<AbstractElement> _elements = it.getElements();
boolean _hasElements = false;
@ -878,6 +899,12 @@ public abstract class AbstractAntlrGrammarGenerator {
} else {
_builder.appendImmediate("\n |", "");
}
_builder.append("// ");
String _string = element.toString();
_builder.append(_string);
_builder.newLineIfNotEmpty();
String _renderPredicate = this._hoistingProcessor.findGuardForElement(element).renderPredicate();
_builder.append(_renderPredicate);
String _ebnf = this.ebnf(element, options, supportActions);
_builder.append(_ebnf);
}
@ -932,6 +959,16 @@ public abstract class AbstractAntlrGrammarGenerator {
return "";
}
protected String _ebnf2(final AbstractSemanticPredicate it, final AntlrOptions options, final boolean supportActions) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
String _source = it.getCode().getSource();
_builder.append(_source);
_builder.append("}?=>");
_builder.newLineIfNotEmpty();
return _builder.toString();
}
protected String ebnf(final Keyword it) {
String _xifexpression = null;
boolean _isCombinedGrammar = this.isCombinedGrammar();
@ -1158,6 +1195,8 @@ public abstract class AbstractAntlrGrammarGenerator {
return _ebnf2((Group)it, options, supportActions);
} else if (it instanceof UnorderedGroup) {
return _ebnf2((UnorderedGroup)it, options, supportActions);
} else if (it instanceof AbstractSemanticPredicate) {
return _ebnf2((AbstractSemanticPredicate)it, options, supportActions);
} else if (it instanceof Action) {
return _ebnf2((Action)it, options, supportActions);
} else if (it instanceof Assignment) {

View file

@ -14,6 +14,7 @@ import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.AbstractSemanticPredicate;
import org.eclipse.xtext.Action;
import org.eclipse.xtext.Alternatives;
import org.eclipse.xtext.Assignment;
@ -669,6 +670,8 @@ public abstract class AbstractAntlrGrammarWithActionsGenerator extends AbstractA
return _ebnf2((Group)it, options, supportActions);
} else if (it instanceof UnorderedGroup) {
return _ebnf2((UnorderedGroup)it, options, supportActions);
} else if (it instanceof AbstractSemanticPredicate) {
return _ebnf2((AbstractSemanticPredicate)it, options, supportActions);
} else if (it instanceof Action) {
return _ebnf2((Action)it, options, supportActions);
} else if (it instanceof Assignment) {

View file

@ -18,6 +18,7 @@ import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.AbstractSemanticPredicate;
import org.eclipse.xtext.Action;
import org.eclipse.xtext.Alternatives;
import org.eclipse.xtext.Assignment;
@ -1390,6 +1391,8 @@ public class AntlrContentAssistGrammarGenerator extends AbstractAntlrGrammarWith
return _ebnf2((Group)it, options, supportActions);
} else if (it instanceof UnorderedGroup) {
return _ebnf2((UnorderedGroup)it, options, supportActions);
} else if (it instanceof AbstractSemanticPredicate) {
return _ebnf2((AbstractSemanticPredicate)it, options, supportActions);
} else if (it instanceof Action) {
return _ebnf2((Action)it, options, supportActions);
} else if (it instanceof Assignment) {

View file

@ -18,6 +18,7 @@ import java.util.Set;
import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.AbstractSemanticPredicate;
import org.eclipse.xtext.Action;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.EnumRule;
@ -648,6 +649,9 @@ public class SyntacticSequencerPDAProvider implements ISyntacticSequencerPDAProv
return SynStateType.UNASSIGEND_ACTION_CALL;
else
return SynStateType.ASSIGNED_ACTION_CALL;
} else if (ele instanceof AbstractSemanticPredicate) {
// TODO sem-predicates: Do something useful here.
return SynStateType.ASSIGNED_ACTION_CALL;
} else if (GrammarUtil.containingCrossReference(ele) != null) {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;