org.eclipse.xtext.EnumRule Java Examples
The following examples show how to use
org.eclipse.xtext.EnumRule.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SequenceFeeder.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected void acceptRuleCall(RuleCall rc, Object value, String token, int index, INode node) { CrossReference crossRef = GrammarUtil.containingCrossReference(rc); if (crossRef != null) { if (rc.getRule() instanceof ParserRule) sequenceAcceptor.acceptAssignedCrossRefDatatype(rc, token, (EObject) value, index, getCompositeNode(node)); else if (rc.getRule() instanceof TerminalRule) sequenceAcceptor.acceptAssignedCrossRefTerminal(rc, token, (EObject) value, index, getLeafNode(node)); else if (rc.getRule() instanceof EnumRule) sequenceAcceptor.acceptAssignedCrossRefEnum(rc, token, (EObject) value, index, getCompositeNode(node)); } else { if (rc.getRule() instanceof ParserRule) { AbstractRule rule = rc.getRule(); if (rule.getType() != null && rule.getType().getClassifier() instanceof EClass) acceptEObjectRuleCall(rc, (EObject) value, getCompositeNode(node)); else sequenceAcceptor.acceptAssignedDatatype(rc, token, value, index, getCompositeNode(node)); } else if (rc.getRule() instanceof TerminalRule) sequenceAcceptor.acceptAssignedTerminal(rc, token, value, index, getLeafNode(node)); else if (rc.getRule() instanceof EnumRule) sequenceAcceptor.acceptAssignedEnum(rc, token, value, index, getCompositeNode(node)); } }
Example #2
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
private boolean deriveFeatures() { boolean result = true; for (AbstractRule rule : grammar.getRules()) { try { if (rule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) rule) && !isWildcardFragment(rule)) { deriveFeatures((ParserRule) rule); } else if (rule instanceof EnumRule) { deriveEnums((EnumRule) rule); } } catch (TransformationException e) { result = false; reportError(e); } } return result; }
Example #3
Source File: AbstractAntlrGrammarGenerator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected String _assignmentEbnf(final RuleCall it, final Assignment assignment, final AntlrOptions options, final boolean supportActions) { String _switchResult = null; AbstractRule _rule = it.getRule(); final AbstractRule rule = _rule; boolean _matched = false; if (rule instanceof EnumRule) { _matched=true; } if (!_matched) { if (rule instanceof ParserRule) { _matched=true; } } if (!_matched) { if (rule instanceof TerminalRule) { _matched=true; } } if (_matched) { _switchResult = this._grammarAccessExtensions.ruleName(rule); } if (!_matched) { throw new IllegalStateException(("assignmentEbnf is not supported for " + rule)); } return _switchResult; }
Example #4
Source File: OverriddenValueInspector.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override public Boolean caseRuleCall(RuleCall object) { AbstractRule calledRule = object.getRule(); if (calledRule == null || calledRule.eIsProxy() || calledRule instanceof TerminalRule || calledRule instanceof EnumRule) return Boolean.FALSE; ParserRule parserRule = (ParserRule) calledRule; if (GrammarUtil.isDatatypeRule(parserRule)) return Boolean.FALSE; if (parserRule.isFragment()) { visitFragment(object); if (GrammarUtil.isMultipleCardinality(object)) visitFragment(object); } if (!addVisited(parserRule)) return Boolean.FALSE; Multimap<String, AbstractElement> prevAssignedFeatures = assignedFeatures; assignedFeatures = newMultimap(); doSwitch(parserRule.getAlternatives()); for (String feature : assignedFeatures.keySet()) prevAssignedFeatures.put(feature, object); assignedFeatures = prevAssignedFeatures; removeVisited(parserRule); return Boolean.FALSE; }
Example #5
Source File: AbstractAntlrGrammarGenerator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected String crossrefEbnf(final AbstractRule it, final RuleCall call, final CrossReference ref, final boolean supportActions) { String _switchResult = null; boolean _matched = false; if (it instanceof EnumRule) { _matched=true; } if (!_matched) { if (it instanceof ParserRule) { _matched=true; } } if (!_matched) { if (it instanceof TerminalRule) { _matched=true; } } if (_matched) { _switchResult = this._grammarAccessExtensions.ruleName(it); } if (!_matched) { throw new IllegalStateException(("crossrefEbnf is not supported for " + it)); } return _switchResult; }
Example #6
Source File: FormatJavaValidator.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Verify that only rule self directives are used for terminal, enum and data type rules. * * @param model * the GrammarRule */ @Check public void checkDataTypeOrEnumRule(final GrammarRule model) { if (model.getTargetRule() instanceof TerminalRule || model.getTargetRule() instanceof EnumRule || (model.getTargetRule() instanceof ParserRule && GrammarUtil.isDatatypeRule((ParserRule) model.getTargetRule()))) { Iterator<EObject> grammarElementAccessors = collectGrammarElementAccessors(model); boolean selfAccessOnly = Iterators.all(grammarElementAccessors, new Predicate<EObject>() { @Override public boolean apply(final EObject input) { return input instanceof GrammarElementReference && ((GrammarElementReference) input).getSelf() != null; } }); if (!selfAccessOnly) { error(NLS.bind("For data type, enum or terminal rule {0} only ''rule'' directive may be used", model.getTargetRule().getName()), FormatPackage.Literals.GRAMMAR_RULE__DIRECTIVES, ILLEGAL_DIRECTIVE_CODE); } } }
Example #7
Source File: JFlexGeneratorFragmentTemplate.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
public String tokens(final Grammar it) { ArrayList<String> allKeywords = CollectionLiterals.<String>newArrayList(); List<ParserRule> _allParserRules = GrammarUtil.allParserRules(it); for (final ParserRule r : _allParserRules) { this.collectTokens(r, allKeywords); } List<EnumRule> _allEnumRules = GrammarUtil.allEnumRules(it); for (final EnumRule r_1 : _allEnumRules) { this.collectTokens(r_1, allKeywords); } StringConcatenation _builder = new StringConcatenation(); { List<String> _sort = IterableExtensions.<String>sort(allKeywords); for(final String k : _sort) { CharSequence _state = this.state(k); _builder.append(_state); _builder.newLineIfNotEmpty(); } } return _builder.toString(); }
Example #8
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkUnassignedRuleCallAllowed(final RuleCall call) { if (call.getRule() != null && !call.getRule().eIsProxy() && GrammarUtil.containingAssignment(call) == null) { AbstractRule container = GrammarUtil.containingRule(call); if (call.getRule() instanceof ParserRule) { if (container instanceof TerminalRule) { getMessageAcceptor().acceptError( "Cannot call parser rule from terminal rule.", call, XtextPackage.Literals.RULE_CALL__RULE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null); } else { ParserRule parserRule = (ParserRule) call.getRule(); if (!GrammarUtil.isDatatypeRule(parserRule) && !parserRule.isFragment()) { checkCurrentMustBeUnassigned(call); } } } if (call.getRule() instanceof EnumRule) { if (container instanceof TerminalRule) { getMessageAcceptor().acceptError( "Cannot call enum rule from terminal rule.", call, XtextPackage.Literals.RULE_CALL__RULE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null); } } } }
Example #9
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected String getRuleNameForErrorMessage(AbstractRule rule) { String ruleName = "datatype "; if (rule instanceof TerminalRule) ruleName = "terminal "; else if (rule instanceof EnumRule) ruleName = "enum "; return ruleName; }
Example #10
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkTerminalRuleAnnotations(AbstractRule rule){ if(rule instanceof TerminalRule || rule instanceof EnumRule) { if(hasAnnotation(rule, AnnotationNames.EXPORTED)) { error("Rule cannot be exported!",rule, XtextPackage.eINSTANCE.getAbstractRule_Name(), INVALID_ANNOTAION); } if(hasAnnotation(rule, AnnotationNames.DEPRECATED)) { error("Rule cannot be deprecated!",rule, XtextPackage.eINSTANCE.getAbstractRule_Name(), INVALID_ANNOTAION); } } }
Example #11
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testCheckRuleCallInUnorderedGroup_03() throws Exception { XtextValidator validator = get(XtextValidator.class); UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup(); RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall(); EnumRule enumRule = XtextFactory.eINSTANCE.createEnumRule(); ruleCall.setRule(enumRule); unorderedGroup.getElements().add(ruleCall); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false); validator.setMessageAcceptor(messageAcceptor); validator.checkRuleCallInUnorderedGroup(ruleCall); messageAcceptor.validate(); }
Example #12
Source File: GrammarParserTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testEnum_04() throws Exception { String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/4'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName;"; Grammar grammar = (Grammar) getModel(modelAsString); assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty()); checkEnums(grammar); EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage(); assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI()); EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum"); assertNotNull(eEnum); assertEquals(3, eEnum.getELiterals().size()); EEnumLiteral value = eEnum.getELiterals().get(0); assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName()); assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue()); assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral()); EnumRule rule = (EnumRule) grammar.getRules().get(1); assertEquals(eEnum, rule.getType().getClassifier()); EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives(); assertEquals(value, decl.getEnumLiteral()); assertNotNull(decl.getLiteral()); assertEquals(value.getLiteral(), decl.getLiteral().getValue()); }
Example #13
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkEnumLiteralIsUnique(EnumLiteralDeclaration decl) { EnumRule rule = GrammarUtil.containingEnumRule(decl); List<EnumLiteralDeclaration> declarations = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class); String literal = decl.getLiteral().getValue(); if (literal != null) { for (EnumLiteralDeclaration otherDecl : declarations) { if (otherDecl != decl && literal.equals(otherDecl.getLiteral().getValue())) { error("Enum literal '" + literal + "' is used multiple times in enum rule '" + rule.getName() + "'.", XtextPackage.Literals.ENUM_LITERAL_DECLARATION__LITERAL); } } } }
Example #14
Source File: RuleNames.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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()); }
Example #15
Source File: TreeConstructionReportImpl.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected String getDiagnosticMessage(AssignmentToken token) { Assignment ass = (Assignment) token.getGrammarElement(); Object value = token.getEObjectConsumer().getConsumable(ass.getFeature(), false); if (value == null) { EStructuralFeature f = token.getEObjectConsumer().getEObject().eClass() .getEStructuralFeature(ass.getFeature()); if (f == null) return "The current object of type '" + token.getEObjectConsumer().getEObject().eClass().getName() + "' does not have a feature named '" + ass.getFeature() + "'"; String cls = f.getEContainingClass() == token.getEObjectConsumer().getEObject().eClass() ? f .getEContainingClass().getName() : f.getEContainingClass().getName() + "(" + token.getEObjectConsumer().getEObject().eClass().getName() + ")"; String feat = cls + "." + f.getName(); if (f.isMany()) { int size = ((List<?>) token.getEObjectConsumer().getEObject().eGet(f)).size(); return "All " + size + " values of " + feat + " have been consumed. " + "More are needed to continue here."; } else return feat + " is not set."; } else { ErrorAcceptor err = new ErrorAcceptor(); for (RuleCall ruleCall : GrammarUtil.containedRuleCalls(token.getGrammarElement())) { if (ruleCall.getRule() instanceof EnumRule) { if (enumSerializer.isValid(token.getEObject(), ruleCall, value, err)) return null; } else if (ruleCall.getRule().getType().getClassifier() instanceof EDataType) { if (valueSerializer.isValid(token.getEObject(), ruleCall, value, err)) return null; } } return err.getMessage(); } }
Example #16
Source File: KeywordHelper.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private BiMap<CharSequence, String> createKeywordMap(Grammar grammar) { List<ParserRule> parserRules = GrammarUtil.allParserRules(grammar); List<EnumRule> enumRules = GrammarUtil.allEnumRules(grammar); Iterator<EObject> iter = Iterators.concat( EcoreUtil.<EObject>getAllContents(parserRules), EcoreUtil.<EObject>getAllContents(enumRules)); Iterator<Keyword> filtered = Iterators.filter(iter, Keyword.class); Iterator<String> transformed = Iterators.transform(filtered, new Function<Keyword, String>() { @Override public String apply(Keyword from) { return from.getValue(); } }); TreeSet<String> treeSet = Sets.newTreeSet(new Comparator<String>() { @Override public int compare(String o1, String o2) { if (o1.length() == o2.length()) return o1.compareTo(o2); return Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length())); } }); Iterators.addAll(treeSet, transformed); BiMap<CharSequence, String> result = HashBiMap.create(); for(String s: treeSet) { CharSequence key = createKey(s); String readableName = toAntlrTokenIdentifier(s); if (result.containsValue(readableName)) { int i = 1; String next = readableName + "_" + i; while(result.containsValue(next)) { i++; next = readableName + "_" + i; } readableName = next; } result.put(key, readableName); } return result; }
Example #17
Source File: GrammarParserTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testEnum_05() throws Exception { String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/5'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName = 'value';"; Grammar grammar = (Grammar) getModel(modelAsString); assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty()); checkEnums(grammar); EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage(); assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI()); EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum"); assertNotNull(eEnum); assertEquals(3, eEnum.getELiterals().size()); EEnumLiteral value = eEnum.getELiterals().get(0); assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName()); assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue()); assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral()); EnumRule rule = (EnumRule) grammar.getRules().get(1); assertEquals(eEnum, rule.getType().getClassifier()); EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives(); assertEquals(value, decl.getEnumLiteral()); assertNotNull(decl.getLiteral()); assertEquals("value", decl.getLiteral().getValue()); }
Example #18
Source File: GrammarAccessFragment2.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected StringConcatenationClient initializer(final AbstractRule it) { if (it instanceof EnumRule) { return _initializer((EnumRule)it); } else if (it instanceof ParserRule) { return _initializer((ParserRule)it); } else if (it instanceof TerminalRule) { return _initializer((TerminalRule)it); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(it).toString()); } }
Example #19
Source File: AbstractAntlrGrammarGenerator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected CharSequence compileRule(final Object it, final Grammar grammar, final AntlrOptions options) { if (it instanceof EnumRule) { return _compileRule((EnumRule)it, grammar, options); } else if (it instanceof ParserRule) { return _compileRule((ParserRule)it, grammar, options); } else if (it instanceof TerminalRule) { return _compileRule((TerminalRule)it, grammar, options); } else if (it instanceof String) { return _compileRule((String)it, grammar, options); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(it, grammar, options).toString()); } }
Example #20
Source File: Bug307519TestLanguageGrammarAccess.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
public EnumRule getEnumTRule() { return getEnumTAccess().getRule(); }
Example #21
Source File: MultiRuleEnumTestLanguageGrammarAccess.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public EnumRule getEnumRuleARule() { return getEnumRuleAAccess().getRule(); }
Example #22
Source File: MultiRuleEnumTestLanguageGrammarAccess.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public EnumRule getEnumRuleBRule() { return getEnumRuleBAccess().getRule(); }
Example #23
Source File: SequencerTestLanguageGrammarAccess.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public EnumRule getDefEnum3Rule() { return getDefEnum3Access().getRule(); }
Example #24
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
private void deriveEnums(EnumRule rule) { EEnum returnType = (EEnum) rule.getType().getClassifier(); if (returnType != null) { List<EnumLiteralDeclaration> decls = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class); for(EnumLiteralDeclaration decl : decls) { if (decl.getEnumLiteral() == null) { List<INode> nodes = NodeModelUtils.findNodesForFeature(decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL); if (!nodes.isEmpty()) { if (nodes.size() > 1) throw new IllegalStateException("Unexpected nodes found: " + nodes); INode node = nodes.get(0); String text = node.getText(); EEnumLiteral literal = null; if (rule.getType().getMetamodel() instanceof ReferencedMetamodel) { literal = returnType.getEEnumLiteral(text); } else { EEnumLiteral existing = returnType.getEEnumLiteral(text); if (existing == null) { literal = EcoreFactory.eINSTANCE.createEEnumLiteral(); int index = returnType.getELiterals().size(); returnType.getELiterals().add(literal); literal.setName(text); literal.setValue(index); if (decl.getLiteral() != null) { literal.setLiteral(decl.getLiteral().getValue()); } else { literal.setLiteral(text); } } else { literal = existing; } SourceAdapter.adapt(literal, decl); } if (literal == null) { reportError(new TransformationException(TransformationErrorCode.InvalidFeature, "Enum literal '" + text + "' does not exist.", decl)); } else { decl.setEnumLiteral(literal); } } } if (decl.getLiteral() == null && decl.getEnumLiteral() != null) { Keyword kw = XtextFactory.eINSTANCE.createKeyword(); kw.setValue(decl.getEnumLiteral().getLiteral()); decl.setLiteral(kw); } } } }
Example #25
Source File: Bug360834TestLanguageGrammarAccess.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
public EnumRule getVisibilityRule() { return getVisibilityAccess().getRule(); }
Example #26
Source File: AssignmentFinderTestLanguageGrammarAccess.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public EnumRule getEnum2Rule() { return getEnum2Access().getRule(); }
Example #27
Source File: Bug287941TestLanguageGrammarAccess.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
public EnumRule getBooleanOperatorRule() { return getBooleanOperatorAccess().getRule(); }
Example #28
Source File: Bug287941TestLanguageGrammarAccess.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
public EnumRule getStringOperatorRule() { return getStringOperatorAccess().getRule(); }
Example #29
Source File: ParserBasedContentAssistContextFactory.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
@Override public Boolean caseEnumRule(EnumRule object) { return doSwitch(object.getAlternatives()); }
Example #30
Source File: EnumRulesTestLanguageGrammarAccess.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public EnumRule getExistingEnumRule() { return getExistingEnumAccess().getRule(); }