added java actions to code generation and hoisting

This commit is contained in:
overflowerror 2021-11-19 15:05:36 +01:00
parent f2e581d026
commit b8f405811a
5 changed files with 36 additions and 19 deletions

View file

@ -39,6 +39,7 @@ 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
import org.eclipse.xtext.JavaAction
abstract class AbstractAntlrGrammarGenerator {
@ -377,7 +378,11 @@ abstract class AbstractAntlrGrammarGenerator {
}
protected dispatch def String ebnf2(AbstractSemanticPredicate it, AntlrOptions options, boolean supportActions) '''
{«JavaCodeUtils.getSource(it.code)»}?=>
{«JavaCodeUtils.getSource(code)»}?=>
'''
protected dispatch def String ebnf2(JavaAction it, AntlrOptions options, boolean supportActions) '''
{«JavaCodeUtils.getSource(code)»}
'''
protected def String ebnf(Keyword it) {

View file

@ -67,4 +67,9 @@ public interface HoistingGuard extends Guard {
}
};
}
static HoistingGuard action() {
// technically not a terminal, but it behaves the same
return terminal();
}
}

View file

@ -9,7 +9,6 @@
package org.eclipse.xtext.xtext.generator.parser.antlr.hoisting;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.List;
@ -21,17 +20,14 @@ import java.util.stream.IntStream;
import org.apache.log4j.Logger;
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;
import org.eclipse.xtext.EnumRule;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.JavaAction;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.UnorderedGroup;
import org.eclipse.xtext.util.Tuples;
import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.utils.StreamUtils;
@ -204,7 +200,8 @@ public class HoistingProcessor {
} else if (path instanceof Group) {
return getTokenForIndexesGroup((Group) path, prefix, needsLength);
} else if (path instanceof Action ||
path instanceof AbstractSemanticPredicate
path instanceof AbstractSemanticPredicate ||
path instanceof JavaAction
) {
// TODO: make sure empty token analysis paths don't cause problems down the line
return TokenAnalysisPaths.empty(prefix);
@ -444,22 +441,16 @@ public class HoistingProcessor {
return groupCache.computeIfAbsent((Group) element, this::findGuardForGroup);
} else if (element instanceof AbstractSemanticPredicate) {
return new PredicateGuard((AbstractSemanticPredicate) element);
} else if (element instanceof Keyword) {
} else if (Token.isToken(element)) {
return HoistingGuard.terminal();
} else if (element instanceof RuleCall) {
} else if (isParserRule(element)) {
RuleCall call = (RuleCall) element;
AbstractRule rule = call.getRule();
if (rule instanceof TerminalRule || rule instanceof EnumRule) {
return HoistingGuard.terminal();
} else {
// rule is parser rule
// TODO: check for enum rules
// TODO: findGuardForElement can't deal with cardinalities
return findGuardForRule((ParserRule) rule);
}
// TODO: findGuardForElement can't deal with cardinalities
return findGuardForRule((ParserRule) call.getRule());
} else if (element instanceof Action) {
// TODO: Maybe find better indicator for "we don't care about this element"
return HoistingGuard.unguarded();
} else if (element instanceof JavaAction) {
return HoistingGuard.action();
} else if (element instanceof UnorderedGroup) {
// TODO: No support for Unordered Groups yet.
throw new UnsupportedOperationException("unordered groups are not yet supported");

View file

@ -28,6 +28,7 @@ import org.eclipse.xtext.EnumRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.JavaAction;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
@ -954,6 +955,16 @@ public abstract class AbstractAntlrGrammarGenerator {
return _builder.toString();
}
protected String _ebnf2(final JavaAction it, final AntlrOptions options, final boolean supportActions) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
String _source = JavaCodeUtils.getSource(it.getCode());
_builder.append(_source);
_builder.append("}");
_builder.newLineIfNotEmpty();
return _builder.toString();
}
protected String ebnf(final Keyword it) {
String _xifexpression = null;
boolean _isCombinedGrammar = this.isCombinedGrammar();
@ -1188,6 +1199,8 @@ public abstract class AbstractAntlrGrammarGenerator {
return _ebnf2((Assignment)it, options, supportActions);
} else if (it instanceof EnumLiteralDeclaration) {
return _ebnf2((EnumLiteralDeclaration)it, options, supportActions);
} else if (it instanceof JavaAction) {
return _ebnf2((JavaAction)it, options, supportActions);
} else if (it instanceof Keyword) {
return _ebnf2((Keyword)it, options, supportActions);
} else if (it instanceof RuleCall) {

View file

@ -24,6 +24,7 @@ import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.EnumLiteralDeclaration;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.JavaAction;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
@ -678,6 +679,8 @@ public abstract class AbstractAntlrGrammarWithActionsGenerator extends AbstractA
return _ebnf2((Assignment)it, options, supportActions);
} else if (it instanceof EnumLiteralDeclaration) {
return _ebnf2((EnumLiteralDeclaration)it, options, supportActions);
} else if (it instanceof JavaAction) {
return _ebnf2((JavaAction)it, options, supportActions);
} else if (it instanceof Keyword) {
return _ebnf2((Keyword)it, options, supportActions);
} else if (it instanceof RuleCall) {