Search in sources :

Example 66 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class XtextLinkerTest method testQualifiedRuleCall_02.

@Test
public void testQualifiedRuleCall_02() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate test \'http://test\'");
    _builder.newLine();
    _builder.append("Rule: name=ID;");
    _builder.newLine();
    _builder.append("terminal STRING: super;");
    _builder.newLine();
    final String grammarAsString = _builder.toString();
    final XtextResource resource = this.getResourceFromString(grammarAsString);
    EObject _get = resource.getContents().get(0);
    Grammar grammar = ((Grammar) _get);
    AbstractRule _get_1 = grammar.getRules().get(1);
    final TerminalRule string = ((TerminalRule) _get_1);
    AbstractElement _alternatives = string.getAlternatives();
    final RuleCall callToSuper = ((RuleCall) _alternatives);
    Assert.assertEquals(GrammarUtil.findRuleForName(IterableExtensions.<Grammar>head(grammar.getUsedGrammars()), "STRING"), callToSuper.getRule());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Example 67 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class ContextPDAProviderTest method getParserRule.

protected String getParserRule(String body) throws Exception {
    Grammar grammar = (Grammar) getModel(HEADER + body);
    List<String> result = Lists.newArrayList();
    PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
    formatter.setStateFormatter(new ToStr());
    formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
    formatter.sortFollowers();
    IContextPDAProvider pdaProvider = get(IContextPDAProvider.class);
    SerializationContextMap<Pda<ISerState, RuleCall>> pdas = pdaProvider.getContextPDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> ctx : pdas.sortedCopy().values()) {
        result.add(Joiner.on(", ").join(ctx.getContexts()) + ":");
        Pda<ISerState, RuleCall> pda = ctx.getValue();
        result.add("  " + formatter.format(pda).replace("\n", "\n  "));
    // StackTraceElement ele = Thread.currentThread().getStackTrace()[2];
    // String name = getClass().getSimpleName() + "_" + ele.getMethodName() + "_" + ctx.getSecond() + ".pdf";
    // new PdaToDot<ISerState, RuleCall>().draw(pda, "dot/" + name, "-T pdf");
    }
    return Joiner.on("\n").join(result);
}
Also used : Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) ISerState(org.eclipse.xtext.serializer.analysis.ISerState) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) PdaListFormatter(org.eclipse.xtext.util.formallang.PdaListFormatter) IContextPDAProvider(org.eclipse.xtext.serializer.analysis.IContextPDAProvider)

Example 68 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class ContextTypePDAProviderTest method getParserRule.

protected String getParserRule(String body) throws Exception {
    Grammar grammar = (Grammar) getModel(HEADER + body);
    // drawGrammar("pdf/" + getName(), grammar);
    List<String> result = Lists.newArrayList();
    PdaListFormatter<ISerState, RuleCall> formatter = new PdaListFormatter<ISerState, RuleCall>();
    formatter.setStateFormatter(new ToStr());
    formatter.setStackitemFormatter(new GrammarElementTitleSwitch().showAssignments().hideCardinality());
    formatter.sortFollowers();
    IContextTypePDAProvider typePDAProvider = get(IContextTypePDAProvider.class);
    SerializationContextMap<Pda<ISerState, RuleCall>> pdas = typePDAProvider.getContextTypePDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> ctx : pdas.sortedCopy().values()) {
        result.add(Joiner.on(", ").join(ctx.getContexts()) + ":");
        result.add("  " + formatter.format(ctx.getValue()).replace("\n", "\n  "));
    }
    return Joiner.on("\n").join(result);
}
Also used : Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) IContextTypePDAProvider(org.eclipse.xtext.serializer.analysis.IContextTypePDAProvider) ISerState(org.eclipse.xtext.serializer.analysis.ISerState) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) PdaListFormatter(org.eclipse.xtext.util.formallang.PdaListFormatter)

Example 69 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class SyntacticSequencerPDAProviderNavigatorTest method getParserRule.

protected ISynAbsorberState getParserRule(String body, String name, String typeName) throws Exception {
    Grammar grammar = (Grammar) getModel(HEADER + body);
    // SyntacticSequencerPDA2SimpleDot.drawGrammar("pdf/" + getName(),
    // grammar);
    // SyntacticSequencerPDA2ExtendedDot.drawGrammar(createSequenceParserPDAProvider(),
    // "pdf/" + getName(), grammar);
    ISyntacticSequencerPDAProvider pdaProvider = get(ISyntacticSequencerPDAProvider.class);
    SerializationContextMap<ISynAbsorberState> pdas = pdaProvider.getSyntacticSequencerPDAs(grammar);
    for (SerializationContextMap.Entry<ISynAbsorberState> e : pdas.sortedCopy().values()) {
        for (ISerializationContext context : e.getContexts()) {
            if (context.getAssignedAction() != null)
                continue;
            ISynAbsorberState pda = e.getValue();
            ParserRule rule = context.getParserRule();
            EClass type = context.getType();
            boolean nameMatches = rule == null || name == null || rule.getName().equals(name);
            boolean typeMatches = type == null || typeName == null || typeName.equals(type.getName());
            if (nameMatches && typeMatches)
                return pda;
        }
    }
    throw new IllegalStateException();
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) ISyntacticSequencerPDAProvider(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider) SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) Grammar(org.eclipse.xtext.Grammar)

Example 70 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class NodeModelUtilsTest method testFindActualSemanticObjectFor_06.

@Test
public void testFindActualSemanticObjectFor_06() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model: name=ID;";
    Grammar grammar = (Grammar) getModel(grammarString);
    ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.lastIndexOf(" "));
    EObject object = NodeModelUtils.findActualSemanticObjectFor(leafNode);
    assertTrue(object instanceof Assignment);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Aggregations

Grammar (org.eclipse.xtext.Grammar)356 Test (org.junit.Test)273 ParserRule (org.eclipse.xtext.ParserRule)151 AbstractRule (org.eclipse.xtext.AbstractRule)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)101 EObject (org.eclipse.emf.ecore.EObject)70 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 XtextResource (org.eclipse.xtext.resource.XtextResource)33 RuleCall (org.eclipse.xtext.RuleCall)28 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)23 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)23 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)21 Assignment (org.eclipse.xtext.Assignment)21 EPackage (org.eclipse.emf.ecore.EPackage)20 AbstractElement (org.eclipse.xtext.AbstractElement)17 TerminalRule (org.eclipse.xtext.TerminalRule)17 Resource (org.eclipse.emf.ecore.resource.Resource)16 EClassifier (org.eclipse.emf.ecore.EClassifier)13 InternalEObject (org.eclipse.emf.ecore.InternalEObject)13 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)12