public void testMetamodel() throws Exception { AbstractRule rule = GrammarUtil.findRuleForName(getGrammarAccess().getGrammar(), "OverridableParserRule2"); assertNotNull("rule", rule); TypeRef ref = rule.getType(); assertNotNull("ref", ref); final EClass clazz = (EClass) ref.getClassifier(); assertNotNull("class", clazz); assertEquals("AType2", clazz.getName()); assertEquals(2, clazz.getESuperTypes().size()); Set<String> expectedNames = new HashSet<String>(Arrays.asList(new String[] {"AType", "RootRule"})); Iterator<String> iter = Iterables.transform( clazz.getESuperTypes(), new Function<EClass, String>() { public String apply(EClass param) { return param.getName(); } }) .iterator(); while (iter.hasNext()) { String name = iter.next(); assertTrue("name = '" + name + "'", expectedNames.remove(name)); } assertTrue(expectedNames.toString(), expectedNames.isEmpty()); }
public String defaultValue(final AbstractElement ele, final Set<AbstractElement> visited) { String _switchResult = null; boolean _matched = false; if (!_matched) { boolean _add = visited.add(ele); boolean _not = (!_add); if (_not) { _matched = true; _switchResult = ""; } } if (!_matched) { boolean _isOptionalCardinality = GrammarUtil.isOptionalCardinality(ele); if (_isOptionalCardinality) { _matched = true; _switchResult = ""; } } if (!_matched) { if (ele instanceof Alternatives) { _matched = true; EList<AbstractElement> _elements = ((Alternatives) ele).getElements(); AbstractElement _head = IterableExtensions.<AbstractElement>head(_elements); _switchResult = this.defaultValue(_head, visited); } } if (!_matched) { if (ele instanceof Group) { _matched = true; EList<AbstractElement> _elements = ((Group) ele).getElements(); final Function1<AbstractElement, String> _function = new Function1<AbstractElement, String>() { @Override public String apply(final AbstractElement e) { return AbstractSyntacticSequencer.this.defaultValue(e, visited); } }; List<String> _map = ListExtensions.<AbstractElement, String>map(_elements, _function); _switchResult = IterableExtensions.join(_map); } } if (!_matched) { if (ele instanceof Keyword) { _matched = true; _switchResult = ((Keyword) ele).getValue(); } } if (!_matched) { if (ele instanceof RuleCall) { _matched = true; AbstractRule _rule = ((RuleCall) ele).getRule(); AbstractElement _alternatives = _rule.getAlternatives(); _switchResult = this.defaultValue(_alternatives, visited); } } if (!_matched) { _switchResult = ""; } return _switchResult; }
protected boolean hasIdRule(final CrossReference crossRef) { AbstractElement _terminal = crossRef.getTerminal(); if ((_terminal instanceof RuleCall)) { AbstractElement _terminal_1 = crossRef.getTerminal(); AbstractRule _rule = ((RuleCall) _terminal_1).getRule(); final String ruleName = _rule.getName(); boolean _or = false; boolean _or_1 = false; boolean _equals = Objects.equal(ruleName, "IdOrSuper"); if (_equals) { _or_1 = true; } else { boolean _equals_1 = Objects.equal(ruleName, "ValidID"); _or_1 = _equals_1; } if (_or_1) { _or = true; } else { boolean _equals_2 = Objects.equal(ruleName, "FeatureCallID"); _or = _equals_2; } return _or; } return false; }
@Inject public void computeDirectiveRules() { List<AbstractRule> allRules = GrammarUtil.allRules(jstGrammarAccess.getGrammar()); for (AbstractRule rule : allRules) { if (rule instanceof TerminalRule && rule.getName().startsWith("DIRECTIVE")) { directiveRules.add((TerminalRule) rule); } } }
private String getDefaultAntlrRuleName(AbstractRule rule) { if (rule instanceof ParserRule || rule instanceof EnumRule) { return "rule" + rule.getName(); } if (rule instanceof TerminalRule) { return "RULE_" + rule.getName().toUpperCase(); } throw new IllegalArgumentException(rule.eClass().getName()); }
protected String _localVar(final RuleCall it) { AbstractRule _rule = it.getRule(); String _name = _rule.getName(); String _plus = ("this_" + _name); String _plus_1 = (_plus + "_"); ParserRule _containingParserRule = GrammarUtil.containingParserRule(it); List<AbstractElement> _contentsAsList = this.contentsAsList(_containingParserRule); int _indexOf = _contentsAsList.indexOf(it); return (_plus_1 + Integer.valueOf(_indexOf)); }
private String getInheritedUniqueName(AbstractRule rule, Set<String> usedNames) { String grammarName = GrammarUtil.getSimpleName(GrammarUtil.getGrammar(rule)); String candidate = grammarName + rule.getName(); int i = 1; while (usedNames.contains(candidate)) { candidate = grammarName + i + rule.getName(); i++; } return candidate; }
@Override protected Collection<FollowElement> getFollowElements( AbstractInternalContentAssistParser parser) { if (rule == null || rule.eIsProxy()) return Collections.emptyList(); String methodName = "entryRule" + rule.getName(); PolymorphicDispatcher<Collection<FollowElement>> dispatcher = new PolymorphicDispatcher<Collection<FollowElement>>( methodName, 0, 0, Collections.singletonList(parser)); dispatcher.invoke(); return parser.getFollowElements(); }
protected String _toXtext(final RuleCall rc) { AbstractRule _rule = rc.getRule(); String _name = _rule.getName(); String _elvis = null; String _cardinality = rc.getCardinality(); if (_cardinality != null) { _elvis = _cardinality; } else { _elvis = ""; } return (_name + _elvis); }
public void assertPredicate(final String expectation, final String grammar) { try { EObject _model = this.getModel(grammar); final Grammar parsed = ((Grammar) _model); EList<AbstractRule> _rules = parsed.getRules(); AbstractRule _head = IterableExtensions.<AbstractRule>head(_rules); final AbstractElement body = _head.getAlternatives(); final AbstractElement predicate = AntlrGrammarGenUtil.getPredicatedElement(body); String _xtext = this.toXtext(predicate); Assert.assertEquals(expectation, _xtext); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected AbstractElement _predicatedElement(final RuleCall it) { AbstractElement _xifexpression = null; boolean _isPredicated = it.isPredicated(); if (_isPredicated) { _xifexpression = it; } else { AbstractRule _rule = it.getRule(); AbstractElement _alternatives = _rule.getAlternatives(); EList<AbstractElement> _elements = ((Group) _alternatives).getElements(); AbstractElement _head = IterableExtensions.<AbstractElement>head(_elements); _xifexpression = this.predicatedElement(_head); } return _xifexpression; }
public RuleNames(Grammar grammar, boolean installAdapter) { this.contextGrammar = grammar; Adapter adapter = new Adapter(this); if (installAdapter) { installAdapterIfMissing(adapter, grammar); } List<AbstractRule> allRules = GrammarUtil.allRules(grammar); ImmutableListMultimap.Builder<String, AbstractRule> simpleNameToRulesBuilder = ImmutableListMultimap.builder(); ImmutableMap.Builder<String, AbstractRule> qualifiedNameToRuleBuilder = ImmutableMap.builder(); ImmutableBiMap.Builder<String, AbstractRule> uniqueNameToRuleBuilder = ImmutableBiMap.builder(); ImmutableBiMap.Builder<String, AbstractRule> antlrNameToRuleBuilder = ImmutableBiMap.builder(); Map<String, AbstractRule> names = Maps.newHashMap(); Set<String> usedAntlrNames = Sets.newHashSet(); Set<String> usedUniqueNames = Sets.newHashSet(); for (AbstractRule rule : allRules) { String name = rule.getName(); simpleNameToRulesBuilder.put(name, rule); String qualifiedName = getQualifiedName(rule); qualifiedNameToRuleBuilder.put(qualifiedName, rule); String uniqueName = name; String antlrRuleName; if (names.containsKey(name)) { name = qualifiedName; uniqueName = getInheritedUniqueName(rule, usedUniqueNames); antlrRuleName = getInheritedAntlrRuleName(rule, usedAntlrNames); } else { antlrRuleName = getDefaultAntlrRuleName(rule); } names.put(name, rule); if (!usedUniqueNames.add(uniqueName)) { throw new IllegalStateException(uniqueName); } uniqueNameToRuleBuilder.put(uniqueName, rule); if (!usedAntlrNames.add(antlrRuleName)) { throw new IllegalStateException(antlrRuleName); } antlrNameToRuleBuilder.put(antlrRuleName, rule); if (installAdapter) { installAdapterIfMissing(adapter, rule); } } simpleNameToRules = simpleNameToRulesBuilder.build(); qualifiedNameToRule = qualifiedNameToRuleBuilder.build(); nameToRule = ImmutableBiMap.copyOf(names); uniqueNameToRule = uniqueNameToRuleBuilder.build(); antlrNameToRule = antlrNameToRuleBuilder.build(); this.allRules = ImmutableList.copyOf(allRules); }
public static RuleNames tryGetRuleNames(AbstractRule rule) { Adapter adapter = (Adapter) EcoreUtil.getAdapter(rule.eAdapters(), RuleNames.class); if (adapter == null) { return null; } return adapter.getRuleNames(); }
public static RuleNames getRuleNames(AbstractRule rule) { Adapter adapter = (Adapter) EcoreUtil.getAdapter(rule.eAdapters(), RuleNames.class); if (adapter == null) { throw new IllegalStateException("Cannot find adapter"); } return adapter.getRuleNames(); }
public CharSequence unassignedCalledTokenRuleName(final AbstractRule rule) { StringConcatenation _builder = new StringConcatenation(); _builder.append("get"); String _name = rule.getName(); _builder.append(_name, ""); _builder.append("Token"); return _builder; }
public static AbstractElement getPredicatedElement(AbstractElement element) { if (element.isPredicated()) { return element; } if (element instanceof Assignment) { return getPredicatedElement(((Assignment) element).getTerminal()); } if (element instanceof RuleCall) { final RuleCall ruleCall = (RuleCall) element; final AbstractRule calledRule = ruleCall.getRule(); Group group = (Group) calledRule.getAlternatives(); AbstractElement first = group.getElements().get(0); AbstractElement result = getPredicatedElement(first); if (ruleCall.getArguments().isEmpty()) { return result; } EcoreUtil.Copier copier = new EcoreUtil.Copier() { private static final long serialVersionUID = 1L; @Override public EObject copy(EObject eObject) { if (eObject instanceof ParameterReference) { ParameterReference paramRef = (ParameterReference) eObject; Parameter parameter = paramRef.getParameter(); if (calledRule == GrammarUtil.containingRule(parameter)) { for (NamedArgument givenArgument : ruleCall.getArguments()) { if (givenArgument.getParameter() == parameter) { EObject result = copy(givenArgument.getValue()); return result; } } throw new IllegalStateException(); } } return super.copy(eObject); } }; AbstractElement clone = (AbstractElement) copier.copy(result); copier.copyReferences(); return clone; } return element; }
private static void getAllKeywords(Set<String> kws, AbstractRule parserRule) { for (TreeIterator<EObject> tit = parserRule.eAllContents(); tit.hasNext(); ) { Object ele = tit.next(); if (ele instanceof Keyword) { kws.add(((Keyword) ele).getValue()); } else if (ele instanceof RuleCall) { getAllKeywords(kws, ((RuleCall) ele).getRule()); } } }
private String getInheritedAntlrRuleName(AbstractRule rule, Set<String> usedNames) { if (rule instanceof ParserRule || rule instanceof EnumRule) { String candidate = "super" + rule.getName(); int i = 1; while (usedNames.contains(candidate)) { candidate = "super" + i + rule.getName(); i++; } return candidate; } if (rule instanceof TerminalRule) { String candidate = "SUPER_" + rule.getName(); int i = 1; while (usedNames.contains(candidate)) { candidate = "SUPER_" + i + "_" + rule.getName(); i++; } return candidate; } throw new IllegalArgumentException(rule.eClass().getName()); }
protected boolean _predicated(final RuleCall it) { boolean _or = false; boolean _isPredicated = it.isPredicated(); if (_isPredicated) { _or = true; } else { boolean _xblockexpression = false; { AbstractRule _rule = it.getRule(); final AbstractElement group = _rule.getAlternatives(); boolean _xifexpression = false; if ((group instanceof Group)) { EList<AbstractElement> _elements = ((Group) group).getElements(); AbstractElement _head = IterableExtensions.<AbstractElement>head(_elements); _xifexpression = this.predicated(_head); } else { _xifexpression = false; } _xblockexpression = _xifexpression; } _or = _xblockexpression; } return _or; }
public CharSequence toStringLiteral(final AbstractElement it) { CharSequence _switchResult = null; boolean _matched = false; if (!_matched) { if (it instanceof RuleCall) { AbstractRule _rule = ((RuleCall) it).getRule(); boolean _notEquals = (!Objects.equal(_rule, null)); if (_notEquals) { _matched = true; StringConcatenation _builder = new StringConcatenation(); _builder.append("\""); AbstractRule _rule_1 = ((RuleCall) it).getRule(); String _name = _rule_1.getName(); _builder.append(_name, ""); _builder.append("\""); _switchResult = _builder; } } } if (!_matched) { if (it instanceof Keyword) { _matched = true; StringConcatenation _builder = new StringConcatenation(); _builder.append("\""); String _value = ((Keyword) it).getValue(); String _stringInAntlrAction = AntlrGrammarGenUtil.toStringInAntlrAction(_value); _builder.append(_stringInAntlrAction, ""); _builder.append("\""); _switchResult = _builder; } } if (!_matched) { _switchResult = "null"; } return _switchResult; }
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) { EList<NamedArgument> _arguments = origRuleCall.getArguments(); boolean _isEmpty = _arguments.isEmpty(); if (_isEmpty) { return Collections.<Parameter>emptySet(); } EList<NamedArgument> _arguments_1 = origRuleCall.getArguments(); final Function1<NamedArgument, Boolean> _function = (NamedArgument it) -> { Condition _value = it.getValue(); return Boolean.valueOf(this.evaluate(_value)); }; Iterable<NamedArgument> _filter = IterableExtensions.<NamedArgument>filter(_arguments_1, _function); final Function1<NamedArgument, Parameter> _function_1 = (NamedArgument it) -> { return it.getParameter(); }; Iterable<Parameter> _map = IterableExtensions.<NamedArgument, Parameter>map(_filter, _function_1); Set<Parameter> result = IterableExtensions.<Parameter>toSet(_map); 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) { Condition _guardCondition_1 = group.getGuardCondition(); boolean _evaluate = this.evaluate(_guardCondition_1); 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) { EClass _eClass_2 = result.eClass(); String _name = _eClass_2.getName(); String _plus = ("copy is: \'" + _name); String _plus_1 = (_plus + "\' but original was: \'"); EClass _eClass_3 = ((AbstractElement) eObject).eClass(); String _name_1 = _eClass_3.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) { ConditionEvaluator _conditionEvaluator = new ConditionEvaluator(paramValues); boolean result = _conditionEvaluator.evaluate(condition); return result; } }; AbstractElement _alternatives = orig.getAlternatives(); EObject _copy = copier.copy(_alternatives); 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; }
public String getQualifiedName(AbstractRule rule) { return GrammarUtil.getGrammar(rule).getName() + "." + rule.getName(); }
protected String toString(AbstractRule rule) { return rule == null ? "null" : rule.getName(); }
public String genGetUnassignedRuleCallToken( final JavaFile file, final AbstractRule rule, final boolean isAbstract) { if ((rule instanceof TerminalRule)) { boolean _and = false; if (!this.detectSyntheticTerminals) { _and = false; } else { boolean _isSyntheticTerminalRule = this.syntheticTerminalDetector.isSyntheticTerminalRule(((TerminalRule) rule)); _and = _isSyntheticTerminalRule; } if (_and) { StringConcatenation _builder = new StringConcatenation(); _builder.append("/**"); _builder.newLine(); _builder.append(" "); _builder.append( "* Synthetic terminal rule. The concrete syntax is to be specified by clients."); _builder.newLine(); { if ((!isAbstract)) { _builder.append(" * Defaults to the empty string."); } } _builder.newLineIfNotEmpty(); _builder.append(" "); _builder.append("*/"); _builder.newLine(); _builder.append("protected "); { if (isAbstract) { _builder.append("abstract "); } } _builder.append("String "); CharSequence _unassignedCalledTokenRuleName = this.unassignedCalledTokenRuleName(rule); _builder.append(_unassignedCalledTokenRuleName, ""); _builder.append("(EObject semanticObject, RuleCall ruleCall, INode node)"); { if (isAbstract) { _builder.append(";"); } else { _builder.append(" { return \"\"; }"); } } _builder.newLineIfNotEmpty(); return _builder.toString(); } } StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("/**"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("* "); ICompositeNode _node = NodeModelUtils.getNode(rule); String _textWithoutComments = this.textWithoutComments(_node); String _trim = _textWithoutComments.trim(); String _replace = _trim.replace("\n", "\n* "); _builder_1.append(_replace, " "); _builder_1.newLineIfNotEmpty(); _builder_1.append(" "); _builder_1.append("*/"); _builder_1.newLine(); _builder_1.append("protected String "); CharSequence _unassignedCalledTokenRuleName_1 = this.unassignedCalledTokenRuleName(rule); _builder_1.append(_unassignedCalledTokenRuleName_1, ""); _builder_1.append("(EObject semanticObject, RuleCall ruleCall, INode node) {"); _builder_1.newLineIfNotEmpty(); _builder_1.append("\t"); _builder_1.append("if (node != null)"); _builder_1.newLine(); _builder_1.append("\t\t"); _builder_1.append("return getTokenText(node);"); _builder_1.newLine(); _builder_1.append("\t"); _builder_1.append("return \""); AbstractElement _alternatives = rule.getAlternatives(); HashSet<AbstractElement> _newHashSet = CollectionLiterals.<AbstractElement>newHashSet(); String _defaultValue = this.defaultValue(_alternatives, _newHashSet); String _convertToJavaString = Strings.convertToJavaString(_defaultValue); _builder_1.append(_convertToJavaString, "\t"); _builder_1.append("\";"); _builder_1.newLineIfNotEmpty(); _builder_1.append("}"); _builder_1.newLine(); return _builder_1.toString(); }