use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class AbstractInternalContentAssistParser method createLookAheadTerminal.
@Override
public LookAheadTerminal createLookAheadTerminal(Token token) {
Grammar grammar = getGrammar();
String tokenName = getTokenNames()[token.getType()];
tokenName = getValueForTokenName(tokenName);
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 = TokenTool.getLexerRuleName(tokenName);
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 KeywordInspector method inspectKeywordHidesTerminalRule.
public void inspectKeywordHidesTerminalRule(Keyword keyword) {
AbstractRule container = GrammarUtil.containingRule(keyword);
if (container instanceof TerminalRule)
return;
Grammar grammar = GrammarUtil.getGrammar(container);
List<TerminalRule> rules = GrammarUtil.allTerminalRules(grammar);
for (TerminalRule rule : rules) {
if (!rule.isFragment()) {
AbstractElement element = rule.getAlternatives();
if (element instanceof Keyword && Strings.isEmpty(element.getCardinality())) {
String value = ((Keyword) element).getValue();
if (value.equals(keyword.getValue()))
acceptor.acceptError("The keyword '" + value + "' hides the terminal rule " + rule.getName() + ".", keyword, XtextPackage.Literals.KEYWORD__VALUE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
}
}
}
}
use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class OverriddenValueInspector method caseRuleCall.
@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;
}
use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class XtextTransientValueService2 method isValueTransient.
@Override
public ValueTransient isValueTransient(EObject owner, EStructuralFeature feature) {
if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
final TypeRef typeRef = (TypeRef) owner;
final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
if (m == null || Strings.isEmpty(m.getAlias()))
return YES;
} else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
final AbstractRule rule = (AbstractRule) owner;
if (!GrammarUtil.isDatatypeRule(rule) && (rule instanceof ParserRule || rule instanceof EnumRule)) {
final TypeRef returnType = rule.getType();
if (returnType == null || returnType.getClassifier() == null)
return YES;
else if (rule.getName().equals(returnType.getClassifier().getName())) {
if (isValueTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel()) == YES)
return PREFERABLY;
else
return NO;
} else if (GrammarUtil.isDatatypeRule(rule)) {
return NodeModelUtils.getNode(returnType) == null ? YES : NO;
}
} else if (rule instanceof TerminalRule || GrammarUtil.isDatatypeRule(rule)) {
final TypeRef returnType = rule.getType();
if (returnType == null)
return YES;
if (rule instanceof TerminalRule) {
if (((TerminalRule) rule).isFragment()) {
return YES;
}
}
if (GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier()))
return PREFERABLY;
return NO;
}
} else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
final CrossReference ref = (CrossReference) owner;
if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null) {
if ("ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
return PREFERABLY;
else
return NO;
}
return YES;
} else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
if (decl.getEnumLiteral() != null && decl.getLiteral() != null) {
if (Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName()))
return PREFERABLY;
return NO;
}
return YES;
} else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
return YES;
} else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
if (((NamedArgument) owner).isCalledByName()) {
return NO;
}
return YES;
}
return super.isValueTransient(owner, feature);
}
use of org.eclipse.xtext.TerminalRule in project xtext-core by eclipse.
the class ToEcoreTrafoTest method testAbstractLanguageToMetamodel.
@Test
public void testAbstractLanguageToMetamodel() throws Exception {
XtextResource r = getResource("classpath:/" + AbstractTestLanguage.class.getName().replace('.', '/') + ".xtext");
Grammar element = (Grammar) r.getParseResult().getRootASTElement();
if (!r.getErrors().isEmpty()) {
EList<Diagnostic> errors = r.getErrors();
for (Diagnostic syntaxError : errors) {
logger.debug(syntaxError.getMessage() + " - " + syntaxError.getLine());
}
fail(errors.toString());
}
List<TerminalRule> lexerRules = GrammarUtil.allTerminalRules(element);
assertEquals(8, lexerRules.size());
List<EPackage> list = Xtext2EcoreTransformer.doGetGeneratedPackages(element);
assertNotNull(list);
assertEquals(0, list.size());
}
Aggregations