use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class XtextAntlrGeneratorFragment2 method generateProductionTokenSource.
public JavaFileAccess generateProductionTokenSource() {
JavaFileAccess _xblockexpression = null;
{
@Extension final GrammarNaming naming = this.productionNaming;
final JavaFileAccess file = this.fileFactory.createJavaFile(naming.getTokenSourceClass(this.getGrammar()));
final Function1<TerminalRule, Boolean> _function = (TerminalRule it) -> {
return Boolean.valueOf(Collections.<String>unmodifiableSet(CollectionLiterals.<String>newHashSet("BEGIN", "INDENT", "OPEN")).contains(it.getName().toUpperCase()));
};
final Iterable<TerminalRule> open = IterableExtensions.<TerminalRule>filter(GrammarUtil.allTerminalRules(this.getGrammar()), _function);
final Function1<TerminalRule, Boolean> _function_1 = (TerminalRule it) -> {
return Boolean.valueOf(Collections.<String>unmodifiableSet(CollectionLiterals.<String>newHashSet("END", "DEDENT", "CLOSE")).contains(it.getName().toUpperCase()));
};
final Iterable<TerminalRule> close = IterableExtensions.<TerminalRule>filter(GrammarUtil.allTerminalRules(this.getGrammar()), _function_1);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("public class ");
String _simpleName = naming.getTokenSourceClass(XtextAntlrGeneratorFragment2.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
_builder.append(AbstractIndentationTokenSource.class);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("public ");
String _simpleName_1 = naming.getTokenSourceClass(XtextAntlrGeneratorFragment2.this.getGrammar()).getSimpleName();
_builder.append(_simpleName_1, "\t");
_builder.append("(");
_builder.append(TokenSource.class, "\t");
_builder.append(" delegate) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("super(delegate);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("@Override");
_builder.newLine();
_builder.append("\t");
_builder.append("protected boolean shouldSplitTokenImpl(");
_builder.append(Token.class, "\t");
_builder.append(" token) {");
_builder.newLineIfNotEmpty();
{
final Function1<TerminalRule, TerminalRule> _function = (TerminalRule it) -> {
return AntlrGrammarGenUtil.<TerminalRule>getOriginalElement(it);
};
final Function1<TerminalRule, Boolean> _function_1 = (TerminalRule it) -> {
String _upperCase = it.getName().toUpperCase();
return Boolean.valueOf(Objects.equal(_upperCase, "WS"));
};
boolean _exists = IterableExtensions.<TerminalRule>exists(ListExtensions.<TerminalRule, TerminalRule>map(GrammarUtil.allTerminalRules(XtextAntlrGeneratorFragment2.this.getGrammar()), _function), _function_1);
if (_exists) {
_builder.append("\t\t");
_builder.append("// TODO Review assumption");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return token.getType() == ");
TypeReference _internalParserClass = naming.getInternalParserClass(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_internalParserClass, "\t\t");
_builder.append(".RULE_WS;");
_builder.newLineIfNotEmpty();
} else {
_builder.append("\t\t");
_builder.append("// TODO Implement me");
_builder.newLine();
_builder.append("\t\t");
_builder.append("throw new UnsupportedOperationException(\"Implement me\");");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("@Override");
_builder.newLine();
_builder.append("\t");
_builder.append("protected int getBeginTokenType() {");
_builder.newLine();
{
int _size = IterableExtensions.size(open);
boolean _equals = (_size == 1);
if (_equals) {
_builder.append("\t\t");
_builder.append("// TODO Review assumption");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return ");
TypeReference _internalParserClass_1 = naming.getInternalParserClass(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_internalParserClass_1, "\t\t");
_builder.append(".");
String _ruleName = XtextAntlrGeneratorFragment2.this.grammarUtil.ruleName(IterableExtensions.<TerminalRule>head(open));
_builder.append(_ruleName, "\t\t");
_builder.append(";");
_builder.newLineIfNotEmpty();
} else {
_builder.append("\t\t");
_builder.append("// TODO Implement me");
_builder.newLine();
_builder.append("\t\t");
_builder.append("throw new UnsupportedOperationException(\"Implement me\");");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("@Override");
_builder.newLine();
_builder.append("\t");
_builder.append("protected int getEndTokenType() {");
_builder.newLine();
{
int _size_1 = IterableExtensions.size(close);
boolean _equals_1 = (_size_1 == 1);
if (_equals_1) {
_builder.append("\t\t");
_builder.append("// TODO Review assumption");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return ");
TypeReference _internalParserClass_2 = naming.getInternalParserClass(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_internalParserClass_2, "\t\t");
_builder.append(".");
String _ruleName_1 = XtextAntlrGeneratorFragment2.this.grammarUtil.ruleName(IterableExtensions.<TerminalRule>head(close));
_builder.append(_ruleName_1, "\t\t");
_builder.append(";");
_builder.newLineIfNotEmpty();
} else {
_builder.append("\t\t");
_builder.append("// TODO Implement me");
_builder.newLine();
_builder.append("\t\t");
_builder.append("throw new UnsupportedOperationException(\"Implement me\");");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
file.setContent(_client);
_xblockexpression = file;
}
return _xblockexpression;
}
use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class SerializerFragment2 method generateSyntacticSequencer.
protected void generateSyntacticSequencer() {
boolean _isGenerateXtendStub = this.isGenerateXtendStub();
if (_isGenerateXtendStub) {
TypeReference _syntacticSequencerClass = this.getSyntacticSequencerClass(this.getGrammar());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("class ");
String _simpleName = SerializerFragment2.this.getSyntacticSequencerClass(SerializerFragment2.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
TypeReference _abstractSyntacticSequencerClass = SerializerFragment2.this.getAbstractSyntacticSequencerClass(SerializerFragment2.this.getGrammar());
_builder.append(_abstractSyntacticSequencerClass);
_builder.append(" {");
_builder.newLineIfNotEmpty();
{
if (SerializerFragment2.this.detectSyntheticTerminals) {
{
final Function1<TerminalRule, Boolean> _function = (TerminalRule it) -> {
return Boolean.valueOf(SerializerFragment2.this.syntheticTerminalDetector.isSyntheticTerminalRule(it));
};
Iterable<TerminalRule> _filter = IterableExtensions.<TerminalRule>filter(GrammarUtil.allTerminalRules(SerializerFragment2.this.getGrammar()), _function);
for (final TerminalRule rule : _filter) {
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("/**");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("* Stub implementation for a synthetic terminal rule. Defaults to the empty string.");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("\t");
_builder.append("// TODO review the concrete syntax for the terminal");
_builder.newLine();
_builder.append("\t");
_builder.append("override ");
CharSequence _unassignedCalledTokenRuleName = SerializerFragment2.this.unassignedCalledTokenRuleName(rule);
_builder.append(_unassignedCalledTokenRuleName, "\t");
_builder.append("(");
_builder.append(EObject.class, "\t");
_builder.append(" semanticObject, ");
_builder.append(RuleCall.class, "\t");
_builder.append(" ruleCall, ");
_builder.append(INode.class, "\t");
_builder.append(" node) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("return \'\'");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
}
}
}
}
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createXtendFile(_syntacticSequencerClass, _client).writeTo(this.getProjectConfig().getRuntime().getSrc());
} else {
TypeReference _syntacticSequencerClass_1 = this.getSyntacticSequencerClass(this.getGrammar());
StringConcatenationClient _client_1 = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("public class ");
String _simpleName = SerializerFragment2.this.getSyntacticSequencerClass(SerializerFragment2.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
TypeReference _abstractSyntacticSequencerClass = SerializerFragment2.this.getAbstractSyntacticSequencerClass(SerializerFragment2.this.getGrammar());
_builder.append(_abstractSyntacticSequencerClass);
_builder.append(" {");
_builder.newLineIfNotEmpty();
{
if (SerializerFragment2.this.detectSyntheticTerminals) {
{
final Function1<TerminalRule, Boolean> _function = (TerminalRule it) -> {
return Boolean.valueOf(SerializerFragment2.this.syntheticTerminalDetector.isSyntheticTerminalRule(it));
};
Iterable<TerminalRule> _filter = IterableExtensions.<TerminalRule>filter(GrammarUtil.allTerminalRules(SerializerFragment2.this.getGrammar()), _function);
for (final TerminalRule rule : _filter) {
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("/**");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("* Stub implementation for a synthetic terminal rule. Defaults to the empty string.");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("\t");
_builder.append("// TODO review the concrete syntax for the terminal");
_builder.newLine();
_builder.append("\t");
_builder.append("@Override");
_builder.newLine();
_builder.append("\t");
_builder.append("public String ");
CharSequence _unassignedCalledTokenRuleName = SerializerFragment2.this.unassignedCalledTokenRuleName(rule);
_builder.append(_unassignedCalledTokenRuleName, "\t");
_builder.append("(");
_builder.append(EObject.class, "\t");
_builder.append(" semanticObject, ");
_builder.append(RuleCall.class, "\t");
_builder.append(" ruleCall, ");
_builder.append(INode.class, "\t");
_builder.append(" node) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("return \"\";");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
}
}
}
}
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createJavaFile(_syntacticSequencerClass_1, _client_1).writeTo(this.getProjectConfig().getRuntime().getSrc());
}
}
use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class FlattenedGrammarAccess method copyRuleBodies.
private Multimap<TerminalRule, AbstractRule> copyRuleBodies(final List<AbstractRule> copies, final Map<RuleWithParameterValues, AbstractRule> origToCopy) {
abstract class __FlattenedGrammarAccess_1 extends EcoreUtil.Copier {
final __FlattenedGrammarAccess_1 _this__FlattenedGrammarAccess_1 = this;
abstract Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall);
abstract void mergePredicates(final AbstractElement into, final AbstractElement from);
abstract void mergeCardinalities(final AbstractElement into, final AbstractElement from);
abstract boolean evaluate(final Condition condition);
}
final HashMultimap<TerminalRule, AbstractRule> calledFrom = HashMultimap.<TerminalRule, AbstractRule>create();
for (final AbstractRule copy : copies) {
{
AbstractRule orig = RuleWithParameterValues.getOriginalRule(copy);
final Set<Parameter> paramValues = RuleWithParameterValues.getParamValues(copy);
EcoreUtil.Copier copier = new __FlattenedGrammarAccess_1() {
@Override
protected void copyReference(final EReference eReference, final EObject eObject, final EObject copyEObject) {
if ((eReference == XtextPackage.Literals.RULE_CALL__RULE)) {
RuleCall origRuleCall = ((RuleCall) eObject);
RuleCall copyRuleCall = ((RuleCall) copyEObject);
AbstractRule _rule = origRuleCall.getRule();
Set<Parameter> _parameterConfig = this.getParameterConfig(origRuleCall, copyRuleCall);
RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(_rule, _parameterConfig);
AbstractRule calledCopy = origToCopy.get(_ruleWithParameterValues);
copyRuleCall.setRule(calledCopy);
if ((calledCopy instanceof TerminalRule)) {
calledFrom.put(((TerminalRule) calledCopy), copy);
}
} else {
super.copyReference(eReference, eObject, copyEObject);
}
}
Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall) {
boolean _isEmpty = origRuleCall.getArguments().isEmpty();
if (_isEmpty) {
return Collections.<Parameter>emptySet();
}
final Function1<NamedArgument, Boolean> _function = (NamedArgument it) -> {
return Boolean.valueOf(this.evaluate(it.getValue()));
};
final Function1<NamedArgument, Parameter> _function_1 = (NamedArgument it) -> {
return it.getParameter();
};
Set<Parameter> result = IterableExtensions.<Parameter>toSet(IterableExtensions.<NamedArgument, Parameter>map(IterableExtensions.<NamedArgument>filter(origRuleCall.getArguments(), _function), _function_1));
return result;
}
@Override
protected void copyContainment(final EReference eReference, final EObject eObject, final EObject copyEObject) {
boolean _matched = false;
if (Objects.equal(eReference, XtextPackage.Literals.RULE_CALL__ARGUMENTS)) {
_matched = true;
}
if (!_matched) {
if (Objects.equal(eReference, XtextPackage.Literals.GROUP__GUARD_CONDITION)) {
_matched = true;
}
}
if (_matched) {
return;
}
super.copyContainment(eReference, eObject, copyEObject);
}
@Override
public EObject copy(final EObject eObject) {
if ((eObject instanceof Group)) {
Group group = ((Group) eObject);
Condition _guardCondition = group.getGuardCondition();
boolean _tripleNotEquals = (_guardCondition != null);
if (_tripleNotEquals) {
boolean _evaluate = this.evaluate(group.getGuardCondition());
boolean _not = (!_evaluate);
if (_not) {
return null;
}
}
}
EObject result = super.copy(eObject);
if ((result instanceof CompoundElement)) {
List<AbstractElement> elements = ((CompoundElement) result).getElements();
int _size = elements.size();
boolean _tripleEquals = (_size == 1);
if (_tripleEquals) {
if (((!((CompoundElement) result).isFirstSetPredicated()) && (!((CompoundElement) result).isPredicated()))) {
AbstractElement element = elements.get(0);
this.mergeCardinalities(element, ((AbstractElement) result));
this.mergePredicates(element, ((AbstractElement) result));
return element;
} else {
AbstractElement element_1 = elements.get(0);
this.mergePredicates(((AbstractElement) result), element_1);
element_1.setFirstSetPredicated(false);
element_1.setPredicated(false);
}
}
}
if ((eObject instanceof AbstractElement)) {
OriginalElement original = new OriginalElement(((AbstractElement) eObject));
EClass _eClass = ((AbstractElement) eObject).eClass();
EClass _eClass_1 = result.eClass();
boolean _notEquals = (!Objects.equal(_eClass, _eClass_1));
if (_notEquals) {
String _name = result.eClass().getName();
String _plus = ("copy is: \'" + _name);
String _plus_1 = (_plus + "\' but original was: \'");
String _name_1 = ((AbstractElement) eObject).eClass().getName();
String _plus_2 = (_plus_1 + _name_1);
String _plus_3 = (_plus_2 + "\'");
throw new IllegalStateException(_plus_3);
}
original.attachToEmfObject(result);
}
return result;
}
void mergePredicates(final AbstractElement into, final AbstractElement from) {
boolean _isPredicated = from.isPredicated();
if (_isPredicated) {
into.setPredicated(true);
into.setFirstSetPredicated(false);
} else {
if (((!into.isPredicated()) && from.isFirstSetPredicated())) {
into.setFirstSetPredicated(true);
}
}
}
void mergeCardinalities(final AbstractElement into, final AbstractElement from) {
String c1 = into.getCardinality();
String c2 = from.getCardinality();
String _switchResult = null;
boolean _matched = false;
if ((Objects.equal(c1, "*") || Objects.equal(c2, "*"))) {
_matched = true;
}
if (!_matched) {
if ((Objects.equal(c1, "+") && Objects.equal(c2, "?"))) {
_matched = true;
}
}
if (!_matched) {
if ((Objects.equal(c1, "?") && Objects.equal(c2, "+"))) {
_matched = true;
}
}
if (_matched) {
_switchResult = "*";
}
if (!_matched) {
if (Objects.equal(c1, null)) {
_matched = true;
_switchResult = c2;
}
}
if (!_matched) {
_switchResult = c1;
}
into.setCardinality(_switchResult);
}
boolean evaluate(final Condition condition) {
boolean result = new ConditionEvaluator(paramValues).evaluate(condition);
return result;
}
};
EObject _copy = copier.copy(orig.getAlternatives());
AbstractElement copiedBody = ((AbstractElement) _copy);
copier.copyReferences();
copy.setAlternatives(copiedBody);
if ((orig instanceof ParserRule)) {
ParserRule castedCopy = ((ParserRule) copy);
boolean _isDefinesHiddenTokens = ((ParserRule) orig).isDefinesHiddenTokens();
if (_isDefinesHiddenTokens) {
castedCopy.setDefinesHiddenTokens(true);
EList<AbstractRule> _hiddenTokens = ((ParserRule) orig).getHiddenTokens();
for (final AbstractRule rule : _hiddenTokens) {
{
RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(rule);
final AbstractRule copiedTerminalRule = origToCopy.get(_ruleWithParameterValues);
EList<AbstractRule> _hiddenTokens_1 = castedCopy.getHiddenTokens();
_hiddenTokens_1.add(copiedTerminalRule);
calledFrom.put(((TerminalRule) copiedTerminalRule), castedCopy);
}
}
}
}
}
}
return calledFrom;
}
use of org.eclipse.xtext.TerminalRule in project xtext-xtend by eclipse.
the class FlexerBasedInternalContentAssistParser method createLookAheadTerminal.
@Override
public LookAheadTerminal createLookAheadTerminal(Token token) {
Grammar grammar = getGrammar();
String tokenName = getTokenDefMap().get(token.getType());
if (tokenName.charAt(0) == '\'') {
LookaheadKeyword result = new LookaheadKeyword();
result.setKeyword(tokenName.substring(1, tokenName.length() - 1));
result.setToken(token);
return result;
}
LookAheadTerminalRuleCall result = new LookAheadTerminalRuleCall();
result.setToken(token);
String ruleName = tokenName.substring(5);
if (terminalRules == null)
terminalRules = GrammarUtil.allTerminalRules(grammar);
for (TerminalRule rule : terminalRules) {
if (rule.getName().equalsIgnoreCase(ruleName)) {
result.setRule(rule);
return result;
}
}
throw new IllegalArgumentException("tokenType " + token.getType() + " seems to be invalid.");
}
use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class IdeContentProposalProvider method _createProposals.
protected void _createProposals(final Assignment assignment, final ContentAssistContext context, final IIdeContentProposalAcceptor acceptor) {
final AbstractElement terminal = assignment.getTerminal();
if ((terminal instanceof CrossReference)) {
this.createProposals(terminal, context, acceptor);
} else {
if ((terminal instanceof RuleCall)) {
final AbstractRule rule = ((RuleCall) terminal).getRule();
if (((rule instanceof TerminalRule) && context.getPrefix().isEmpty())) {
String _xifexpression = null;
String _name = rule.getName();
boolean _equals = Objects.equal(_name, "STRING");
if (_equals) {
String _feature = assignment.getFeature();
String _plus = ("\"" + _feature);
_xifexpression = (_plus + "\"");
} else {
_xifexpression = assignment.getFeature();
}
final String proposal = _xifexpression;
final Procedure1<ContentAssistEntry> _function = (ContentAssistEntry it) -> {
String _name_1 = rule.getName();
boolean _equals_1 = Objects.equal(_name_1, "STRING");
if (_equals_1) {
ArrayList<TextRegion> _editPositions = it.getEditPositions();
int _offset = context.getOffset();
int _plus_1 = (_offset + 1);
int _length = proposal.length();
int _minus = (_length - 2);
TextRegion _textRegion = new TextRegion(_plus_1, _minus);
_editPositions.add(_textRegion);
it.setKind(ContentAssistEntry.KIND_TEXT);
} else {
ArrayList<TextRegion> _editPositions_1 = it.getEditPositions();
int _offset_1 = context.getOffset();
int _length_1 = proposal.length();
TextRegion _textRegion_1 = new TextRegion(_offset_1, _length_1);
_editPositions_1.add(_textRegion_1);
it.setKind(ContentAssistEntry.KIND_VALUE);
}
it.setDescription(rule.getName());
};
final ContentAssistEntry entry = this.proposalCreator.createProposal(proposal, context, _function);
acceptor.accept(entry, this.proposalPriorities.getDefaultPriority(entry));
}
}
}
}
Aggregations