use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class ContextTypePDAProvider method getContextTypePDAs.
@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextTypePDAs(Grammar grammar) {
SerializationContextMap<Pda<ISerState, RuleCall>> cached = cache.get(grammar);
if (cached != null)
return cached;
SerializationContextMap.Builder<Pda<ISerState, RuleCall>> builder = SerializationContextMap.builder();
SerializationContextMap<Pda<ISerState, RuleCall>> contextPDAs = pdaProvider.getContextPDAs(grammar);
for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : contextPDAs.values()) {
List<ISerializationContext> parents = e.getContexts();
Pda<ISerState, RuleCall> contextPDA = e.getValue();
try {
Map<ISerState, Integer> distances = nfaUtil.distanceToFinalStateMap(contextPDA);
Set<EClass> types = collectTypes(contextPDA, distances);
if (types.size() == 1) {
for (ISerializationContext parent : parents) {
TypeContext ctx = new TypeContext(parent, types.iterator().next());
builder.put(ctx, contextPDA);
}
} else {
for (EClass type : types) {
Pda<ISerState, RuleCall> filtered = filterByType(contextPDA, type, distances);
for (ISerializationContext parent : parents) {
TypeContext typeContext = new TypeContext(parent, type);
builder.put(typeContext, filtered);
}
}
}
} catch (Exception x) {
LOG.error("Error extracting PDAs for types for context '" + parents + "': " + x.getMessage(), x);
}
}
SerializationContextMap<Pda<ISerState, RuleCall>> result = builder.create();
cache.put(grammar, result);
return result;
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class GrammarPDAProvider method getGrammarPDAs.
@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
RuleNames names = RuleNames.getRuleNames(grammar, true);
RuleFilter filter = new RuleFilter();
filter.setDiscardTerminalRules(true);
filter.setDiscardUnreachableRules(false);
filter.setDiscardRuleTypeRef(false);
Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
AbstractRule original = withParams.getOriginal();
if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
try {
Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
result.put(context, pda);
} catch (Exception e) {
LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
}
}
}
return result.create();
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class SyntacticSequencerPDAProvider method getType.
protected SynStateType getType(ISerState state) {
switch(state.getType()) {
case ELEMENT:
AbstractElement ele = state.getGrammarElement();
Assignment ass;
if (ele instanceof Action) {
if (((Action) ele).getFeature() == null)
return SynStateType.UNASSIGEND_ACTION_CALL;
else
return SynStateType.ASSIGNED_ACTION_CALL;
} else if (GrammarUtil.containingCrossReference(ele) != null) {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;
if (rc.getRule() instanceof ParserRule)
return SynStateType.ASSIGNED_CROSSREF_DATATYPE_RULE_CALL;
if (rc.getRule() instanceof TerminalRule)
return SynStateType.ASSIGNED_CROSSREF_TERMINAL_RULE_CALL;
if (rc.getRule() instanceof EnumRule)
return SynStateType.ASSIGNED_CROSSREF_ENUM_RULE_CALL;
} else if (ele instanceof Keyword)
return SynStateType.ASSIGNED_CROSSREF_KEYWORD;
} else if ((ass = GrammarUtil.containingAssignment(ele)) != null) {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;
if (rc.getRule() instanceof ParserRule) {
if (rc.getRule().getType().getClassifier() instanceof EClass)
return SynStateType.ASSIGNED_PARSER_RULE_CALL;
return SynStateType.ASSIGNED_DATATYPE_RULE_CALL;
}
if (rc.getRule() instanceof TerminalRule)
return SynStateType.ASSIGNED_TERMINAL_RULE_CALL;
if (rc.getRule() instanceof EnumRule)
return SynStateType.ASSIGNED_ENUM_RULE_CALL;
} else if (ele instanceof Keyword) {
if (GrammarUtil.isBooleanAssignment(ass))
return SynStateType.ASSIGNED_BOOLEAN_KEYWORD;
else
return SynStateType.ASSIGNED_KEYWORD;
}
} else {
if (ele instanceof RuleCall) {
RuleCall rc = (RuleCall) ele;
if (rc.getRule() instanceof ParserRule)
return SynStateType.UNASSIGNED_DATATYPE_RULE_CALL;
if (rc.getRule() instanceof TerminalRule)
return SynStateType.UNASSIGNED_TERMINAL_RULE_CALL;
} else if (ele instanceof Keyword)
return SynStateType.UNASSIGEND_KEYWORD;
}
break;
case PUSH:
return SynStateType.UNASSIGNED_PARSER_RULE_ENTER;
case POP:
return SynStateType.UNASSIGNED_PARSER_RULE_EXIT;
case START:
return SynStateType.START;
case STOP:
return SynStateType.STOP;
}
throw new RuntimeException("no type found for " + state);
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class SyntacticSequencerPDAProvider method getSyntacticSequencerPDAs.
@Override
public SerializationContextMap<ISynAbsorberState> getSyntacticSequencerPDAs(Grammar grammar) {
SerializationContextMap<ISynAbsorberState> cached = cache.get(grammar);
if (cached != null)
return cached;
SerializationContextMap.Builder<ISynAbsorberState> builder = SerializationContextMap.builder();
SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = pdaProvider.getContextTypePDAs(grammar);
for (Entry<Pda<ISerState, RuleCall>> e : typePDAs.values()) {
Pda<ISerState, RuleCall> pda = e.getValue();
List<ISerializationContext> contexts = e.getContexts();
try {
EClass type = contexts.get(0).getType();
Map<ISerState, SynAbsorberState> absorbers = Maps.newLinkedHashMap();
Map<SynAbsorberState, Map<ISerState, SynState>> emitters = Maps.newLinkedHashMap();
SynAbsorberState state = createAbsorberState(pda.getStart(), absorbers, emitters, type);
builder.put(contexts, state);
} catch (Exception x) {
String ctxs = Joiner.on(", ").join(contexts);
LOG.error("Error creating PDA for syntactic sequencer for contexts: " + ctxs + ": " + x.getMessage(), x);
}
}
SerializationContextMap<ISynAbsorberState> result = builder.create();
cache.put(grammar, result);
return result;
}
use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.
the class AbstractSyntacticSequencer method acceptNode.
protected void acceptNode(INode node) {
Object ge = node.getGrammarElement();
if (ge instanceof Keyword)
acceptUnassignedKeyword((Keyword) ge, node.getText(), (ILeafNode) node);
else if (ge instanceof RuleCall) {
RuleCall rc = (RuleCall) ge;
if (rc.getRule() instanceof TerminalRule)
acceptUnassignedTerminal(rc, node.getText(), (ILeafNode) node);
else if (rc.getRule() instanceof ParserRule) {
StringBuilder text = new StringBuilder();
for (ILeafNode leaf : node.getLeafNodes()) if (text.length() > 0 || !leaf.isHidden())
text.append(leaf.getText());
acceptUnassignedDatatype(rc, text.toString(), (ICompositeNode) node);
} else if (rc.getRule() instanceof EnumRule)
acceptUnassignedEnum(rc, node.getText(), (ICompositeNode) node);
} else if (ge instanceof Action)
acceptUnassignedAction((Action) ge);
else
throw new RuntimeException("Unexpected grammar element: " + node.getGrammarElement());
}
Aggregations