mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
Merge pull request #1638 from eclipse/cd_x2j_225
[eclipse/xtext#1897] converted xtend code 2 java
This commit is contained in:
commit
fe8e7cd250
6 changed files with 281 additions and 602 deletions
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator.serializer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.xtext.Action;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.GrammarUtil;
|
||||
import org.eclipse.xtext.ParserRule;
|
||||
import org.eclipse.xtext.serializer.ISerializationContext;
|
||||
import org.eclipse.xtext.serializer.analysis.SerializationContextMap;
|
||||
|
||||
/**
|
||||
* @author Moritz Eysholdt - Initial contribution and API
|
||||
*/
|
||||
public class NamedSerializationContextProvider {
|
||||
private final Map<ParserRule, Integer> rules;
|
||||
|
||||
public NamedSerializationContextProvider(Grammar grammar) {
|
||||
rules = new HashMap<>();
|
||||
int index = 0;
|
||||
for (ParserRule rule : GrammarUtil.allParserRules(grammar)) {
|
||||
rules.put(rule, index++);
|
||||
}
|
||||
}
|
||||
|
||||
public <T extends Object> List<NamedSerializationContexts<T>> getNamedContexts(SerializationContextMap<T> map) {
|
||||
ArrayList<NamedSerializationContexts<T>> result = new ArrayList<>();
|
||||
HashMap<String, Integer> names = new HashMap<>();
|
||||
for (SerializationContextMap.Entry<T> e : map.values()) {
|
||||
for (EClass t : e.getTypes()) {
|
||||
List<ISerializationContext> ctx = e.getContexts(t);
|
||||
String namePrefix = (t == null) ? "" : t.getName();
|
||||
String name = namePrefix + "_" + getSignificantGrammarElement(ctx);
|
||||
Integer dup = names.get(name);
|
||||
String unique = null;
|
||||
if (dup == null) {
|
||||
names.put(name, 1);
|
||||
unique = name;
|
||||
} else {
|
||||
names.put(name, dup.intValue() + 1);
|
||||
unique = name + "_" + dup;
|
||||
}
|
||||
result.add(new NamedSerializationContexts<T>(unique, t, ctx, e.getValue()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getSignificantGrammarElement(Iterable<ISerializationContext> contexts) {
|
||||
ParserRule rule = null;
|
||||
int index = Integer.MAX_VALUE;
|
||||
for (ISerializationContext ctx : contexts) {
|
||||
ParserRule pr = ctx.getParserRule();
|
||||
if (pr == null) {
|
||||
Action action = ctx.getAssignedAction();
|
||||
if (action != null) {
|
||||
pr = GrammarUtil.containingParserRule(action);
|
||||
}
|
||||
}
|
||||
if (pr != null) {
|
||||
Integer i = rules.get(pr);
|
||||
if (i.intValue() < index) {
|
||||
index = i.intValue();
|
||||
rule = pr;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rule != null) {
|
||||
return rule.getName();
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.generator.serializer
|
||||
|
||||
import java.util.List
|
||||
import java.util.Map
|
||||
import org.eclipse.xtext.Grammar
|
||||
import org.eclipse.xtext.GrammarUtil
|
||||
import org.eclipse.xtext.ParserRule
|
||||
import org.eclipse.xtext.serializer.ISerializationContext
|
||||
import org.eclipse.xtext.serializer.analysis.SerializationContextMap
|
||||
|
||||
/**
|
||||
* @author Moritz Eysholdt - Initial contribution and API
|
||||
*/
|
||||
class NamedSerializationContextProvider {
|
||||
val Map<ParserRule, Integer> rules
|
||||
|
||||
new(Grammar grammar) {
|
||||
rules = newHashMap(GrammarUtil.allParserRules(grammar).indexed.map[value -> key])
|
||||
}
|
||||
|
||||
def <T> List<NamedSerializationContexts<T>> getNamedContexts(SerializationContextMap<T> map) {
|
||||
val result = <NamedSerializationContexts<T>>newArrayList()
|
||||
val names = <String, Integer>newHashMap()
|
||||
for (e : map.values) {
|
||||
for (t : e.types) {
|
||||
val ctx = e.getContexts(t)
|
||||
val name = (if(t === null) "" else t.name) + "_" + getSignificantGrammarElement(ctx)
|
||||
val dup = names.get(name)
|
||||
val unique = if (dup === null) {
|
||||
names.put(name, 1)
|
||||
name
|
||||
} else {
|
||||
names.put(name, dup + 1)
|
||||
name + "_" + dup
|
||||
}
|
||||
result += new NamedSerializationContexts(unique, t, ctx, e.value)
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
def String getSignificantGrammarElement(Iterable<ISerializationContext> contexts) {
|
||||
var ParserRule rule = null
|
||||
var index = Integer.MAX_VALUE;
|
||||
for (ISerializationContext ctx : contexts) {
|
||||
var pr = ctx.parserRule
|
||||
if (pr === null) {
|
||||
val action = ctx.assignedAction
|
||||
if (action !== null) {
|
||||
pr = GrammarUtil.containingParserRule(action)
|
||||
}
|
||||
}
|
||||
if (pr !== null) {
|
||||
val i = rules.get(pr)
|
||||
if (i < index) {
|
||||
index = i
|
||||
rule = pr
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rule !== null) {
|
||||
return rule.name
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,196 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator.serializer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.emf.common.notify.impl.AdapterImpl;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.serializer.ISerializationContext;
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider;
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.ConstraintElementType;
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint;
|
||||
import org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState;
|
||||
import org.eclipse.xtext.serializer.analysis.SerializationContextMap;
|
||||
import org.eclipse.xtext.util.formallang.Nfa;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class SemanticSequencerExtensions {
|
||||
protected static class SuperGrammar extends AdapterImpl {
|
||||
private final Grammar grammar;
|
||||
|
||||
@Override
|
||||
public boolean isAdapterForType(Object type) {
|
||||
return type == SuperGrammar.class;
|
||||
}
|
||||
|
||||
public SuperGrammar(Grammar grammar) {
|
||||
this.grammar = grammar;
|
||||
}
|
||||
|
||||
public Grammar getGrammar() {
|
||||
return grammar;
|
||||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
private IGrammarConstraintProvider gcp;
|
||||
|
||||
public Map<IConstraint, List<ISerializationContext>> getGrammarConstraints(Grammar grammar, EClass clazz) {
|
||||
Map<IConstraint, List<ISerializationContext>> result = new LinkedHashMap<>();
|
||||
SerializationContextMap<IConstraint> constraints = gcp.getConstraints(grammar);
|
||||
for (SerializationContextMap.Entry<IConstraint> e : constraints.values()) {
|
||||
IConstraint constraint = e.getValue();
|
||||
if (constraint.getType() == clazz) {
|
||||
List<ISerializationContext> contexts = result.get(constraint);
|
||||
if (contexts == null) {
|
||||
contexts = new ArrayList<>();
|
||||
result.put(constraint, contexts);
|
||||
}
|
||||
contexts.addAll(e.getContexts(clazz));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected ResourceSet cloneResourceSet(ResourceSet rs) {
|
||||
XtextResourceSet result = new XtextResourceSet();
|
||||
result.setPackageRegistry(rs.getPackageRegistry());
|
||||
result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry());
|
||||
result.setURIConverter(rs.getURIConverter());
|
||||
if (rs instanceof XtextResourceSet) {
|
||||
result.setClasspathURIContext(((XtextResourceSet) rs).getClasspathURIContext());
|
||||
result.setClasspathUriResolver(((XtextResourceSet) rs).getClasspathUriResolver());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Grammar getSuperGrammar(Grammar grammar) {
|
||||
if (grammar.getUsedGrammars().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
SuperGrammar sg = (SuperGrammar) EcoreUtil.getExistingAdapter(grammar, SuperGrammar.class);
|
||||
if (sg != null) {
|
||||
return sg.grammar;
|
||||
}
|
||||
URI uri = Iterables.getFirst(grammar.getUsedGrammars(), null).eResource().getURI();
|
||||
Resource resource = cloneResourceSet(grammar.eResource().getResourceSet()).getResource(uri, true);
|
||||
Grammar result = (Grammar) Iterables.getFirst(resource.getContents(), null);
|
||||
grammar.eAdapters().add(new SuperGrammar(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
public Collection<IConstraint> getGrammarConstraints(Grammar grammar) {
|
||||
if (grammar == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return Lists.transform(gcp.getConstraints(grammar).values(), SerializationContextMap.Entry::getValue);
|
||||
}
|
||||
|
||||
public List<ISemState> getLinearListOfMandatoryAssignments(IConstraint constraint) {
|
||||
Nfa<ISemState> nfa = constraint.getNfa();
|
||||
ISemState state = nfa.getStart();
|
||||
List<ISemState> result = new ArrayList<>();
|
||||
Set<EStructuralFeature> features = new HashSet<>();
|
||||
while (state != null) {
|
||||
if (state == nfa.getStop()) {
|
||||
if (result.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
if (state.getFollowers().size() != 1) {
|
||||
return null;
|
||||
}
|
||||
if (state != nfa.getStart()) {
|
||||
EStructuralFeature feature = state.getFeature();
|
||||
if (feature == null || feature.isMany() || !features.add(feature)) {
|
||||
return null;
|
||||
}
|
||||
result.add(state);
|
||||
}
|
||||
state = Iterables.getFirst(state.getFollowers(), null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toAcceptMethod(ConstraintElementType type) {
|
||||
if (type != null) {
|
||||
switch (type) {
|
||||
case ASSIGNED_ACTION_CALL:
|
||||
return "acceptAssignedAction";
|
||||
case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL:
|
||||
return "acceptAssignedCrossRefDatatype";
|
||||
case ASSIGNED_CROSSREF_ENUM_RULE_CALL:
|
||||
return "acceptAssignedCrossRefEnum";
|
||||
case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL:
|
||||
return "acceptAssignedCrossRefTerminal";
|
||||
case ASSIGNED_CROSSREF_KEYWORD:
|
||||
return "acceptAssignedCrossRefKeyword";
|
||||
case ASSIGNED_DATATYPE_RULE_CALL:
|
||||
return "acceptAssignedDatatype";
|
||||
case ASSIGNED_ENUM_RULE_CALL:
|
||||
return "acceptAssignedEnum";
|
||||
case ASSIGNED_KEYWORD:
|
||||
return "acceptAssignedKeyword";
|
||||
case ASSIGNED_PARSER_RULE_CALL:
|
||||
return "acceptAssignedParserRuleCall";
|
||||
case ASSIGNED_TERMINAL_RULE_CALL:
|
||||
return "acceptAssignedTerminal";
|
||||
default:
|
||||
return "<error, unknown type '" + type + "'>";
|
||||
}
|
||||
} else {
|
||||
return "<error, unknown type '" + type + "'>";
|
||||
}
|
||||
}
|
||||
|
||||
public String toNodeType(ConstraintElementType type) {
|
||||
if (type != null) {
|
||||
switch (type) {
|
||||
case ASSIGNED_ACTION_CALL:
|
||||
case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL:
|
||||
case ASSIGNED_CROSSREF_ENUM_RULE_CALL:
|
||||
case ASSIGNED_CROSSREF_KEYWORD:
|
||||
case ASSIGNED_DATATYPE_RULE_CALL:
|
||||
case ASSIGNED_ENUM_RULE_CALL:
|
||||
case ASSIGNED_PARSER_RULE_CALL:
|
||||
return ICompositeNode.class.getSimpleName();
|
||||
case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL:
|
||||
case ASSIGNED_KEYWORD:
|
||||
case ASSIGNED_TERMINAL_RULE_CALL:
|
||||
return ILeafNode.class.getSimpleName();
|
||||
default:
|
||||
return "<error, unknown type '" + type + "'>";
|
||||
}
|
||||
} else {
|
||||
return "<error, unknown type '" + type + "'>";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,167 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015, 2016 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.generator.serializer
|
||||
|
||||
import com.google.inject.Inject
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
import java.util.Map
|
||||
import java.util.Set
|
||||
import org.eclipse.emf.common.notify.impl.AdapterImpl
|
||||
import org.eclipse.emf.ecore.EClass
|
||||
import org.eclipse.emf.ecore.EStructuralFeature
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
import org.eclipse.xtext.Grammar
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode
|
||||
import org.eclipse.xtext.resource.XtextResourceSet
|
||||
import org.eclipse.xtext.serializer.ISerializationContext
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.ConstraintElementType
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint
|
||||
import org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState
|
||||
|
||||
class SemanticSequencerExtensions {
|
||||
|
||||
@Inject IGrammarConstraintProvider gcp
|
||||
|
||||
def Map<IConstraint, List<ISerializationContext>> getGrammarConstraints(Grammar grammar, EClass clazz) {
|
||||
val Map<IConstraint, List<ISerializationContext>> result = newLinkedHashMap
|
||||
val constraints = gcp.getConstraints(grammar)
|
||||
for (e : constraints.values) {
|
||||
val constraint = e.value
|
||||
if (constraint.type === clazz) {
|
||||
var contexts = result.get(constraint)
|
||||
if (contexts === null) {
|
||||
contexts = newArrayList
|
||||
result.put(constraint, contexts)
|
||||
}
|
||||
contexts.addAll(e.getContexts(clazz))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// def List<IConstraintContext> getGrammarConstraintContexts(Grammar grammar) {
|
||||
// return gcp.getConstraints(grammar)
|
||||
// }
|
||||
|
||||
@FinalFieldsConstructor
|
||||
@Accessors
|
||||
protected static class SuperGrammar extends AdapterImpl {
|
||||
|
||||
val Grammar grammar
|
||||
|
||||
override boolean isAdapterForType(Object type) {
|
||||
return type === SuperGrammar
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def protected ResourceSet cloneResourceSet(ResourceSet rs) {
|
||||
val result = new XtextResourceSet
|
||||
result.packageRegistry = rs.packageRegistry
|
||||
result.resourceFactoryRegistry = rs.resourceFactoryRegistry
|
||||
result.URIConverter = rs.URIConverter
|
||||
if (rs instanceof XtextResourceSet) {
|
||||
result.classpathURIContext = rs.classpathURIContext
|
||||
result.classpathUriResolver = rs.classpathUriResolver
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
def Grammar getSuperGrammar(Grammar grammar) {
|
||||
if (grammar.usedGrammars.isEmpty)
|
||||
return null
|
||||
var sg = EcoreUtil.getExistingAdapter(grammar, SuperGrammar) as SuperGrammar
|
||||
if (sg !== null)
|
||||
return sg.grammar
|
||||
val uri = grammar.usedGrammars.head.eResource.URI
|
||||
val resource = cloneResourceSet(grammar.eResource.resourceSet).getResource(uri, true)
|
||||
val result = resource.contents.head as Grammar
|
||||
grammar.eAdapters.add(new SuperGrammar(result))
|
||||
return result
|
||||
}
|
||||
|
||||
def Collection<IConstraint> getGrammarConstraints(Grammar grammar) {
|
||||
if (grammar === null)
|
||||
return emptySet
|
||||
return gcp.getConstraints(grammar).values.map[value]
|
||||
}
|
||||
|
||||
def List<ISemState> getLinearListOfMandatoryAssignments(IConstraint constraint) {
|
||||
val nfa = constraint.nfa
|
||||
var state = nfa.start
|
||||
val List<ISemState> result = newArrayList
|
||||
val Set<EStructuralFeature> features = newHashSet
|
||||
while (state !== null) {
|
||||
if (state === nfa.stop)
|
||||
return if (result.empty) null else result
|
||||
if (state.followers.size !== 1)
|
||||
return null
|
||||
if (state !== nfa.start) {
|
||||
val feature = state.feature
|
||||
if (feature === null || feature.isMany || !features.add(feature))
|
||||
return null
|
||||
result.add(state)
|
||||
}
|
||||
state = state.followers.head
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
def String toAcceptMethod(ConstraintElementType type) {
|
||||
switch (type) {
|
||||
case ASSIGNED_ACTION_CALL:
|
||||
'acceptAssignedAction'
|
||||
case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL:
|
||||
'acceptAssignedCrossRefDatatype'
|
||||
case ASSIGNED_CROSSREF_ENUM_RULE_CALL:
|
||||
'acceptAssignedCrossRefEnum'
|
||||
case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL:
|
||||
'acceptAssignedCrossRefTerminal'
|
||||
case ASSIGNED_CROSSREF_KEYWORD:
|
||||
'acceptAssignedCrossRefKeyword'
|
||||
case ASSIGNED_DATATYPE_RULE_CALL:
|
||||
'acceptAssignedDatatype'
|
||||
case ASSIGNED_ENUM_RULE_CALL:
|
||||
'acceptAssignedEnum'
|
||||
case ASSIGNED_KEYWORD:
|
||||
'acceptAssignedKeyword'
|
||||
case ASSIGNED_PARSER_RULE_CALL:
|
||||
'acceptAssignedParserRuleCall'
|
||||
case ASSIGNED_TERMINAL_RULE_CALL:
|
||||
'acceptAssignedTerminal'
|
||||
default:
|
||||
'<error, unknown type \'' + type + '\'>'
|
||||
}
|
||||
}
|
||||
|
||||
def String toNodeType(ConstraintElementType type) {
|
||||
switch (type) {
|
||||
case ASSIGNED_ACTION_CALL,
|
||||
case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL,
|
||||
case ASSIGNED_CROSSREF_ENUM_RULE_CALL,
|
||||
case ASSIGNED_CROSSREF_KEYWORD,
|
||||
case ASSIGNED_DATATYPE_RULE_CALL,
|
||||
case ASSIGNED_ENUM_RULE_CALL,
|
||||
case ASSIGNED_PARSER_RULE_CALL:
|
||||
ICompositeNode.simpleName
|
||||
case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL,
|
||||
case ASSIGNED_KEYWORD,
|
||||
case ASSIGNED_TERMINAL_RULE_CALL:
|
||||
ILeafNode.simpleName
|
||||
default:
|
||||
'<error, unknown type \'' + type + '\'>'
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,117 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator.serializer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.xtext.Action;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.GrammarUtil;
|
||||
import org.eclipse.xtext.ParserRule;
|
||||
import org.eclipse.xtext.serializer.ISerializationContext;
|
||||
import org.eclipse.xtext.serializer.analysis.SerializationContextMap;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Conversions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pair;
|
||||
import org.eclipse.xtext.xtext.generator.serializer.NamedSerializationContexts;
|
||||
|
||||
/**
|
||||
* @author Moritz Eysholdt - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class NamedSerializationContextProvider {
|
||||
private final Map<ParserRule, Integer> rules;
|
||||
|
||||
public NamedSerializationContextProvider(final Grammar grammar) {
|
||||
final Function1<Pair<Integer, ParserRule>, Pair<ParserRule, Integer>> _function = (Pair<Integer, ParserRule> it) -> {
|
||||
ParserRule _value = it.getValue();
|
||||
Integer _key = it.getKey();
|
||||
return Pair.<ParserRule, Integer>of(_value, _key);
|
||||
};
|
||||
this.rules = CollectionLiterals.<ParserRule, Integer>newHashMap(((Pair<? extends ParserRule, ? extends Integer>[])Conversions.unwrapArray(IterableExtensions.<Pair<Integer, ParserRule>, Pair<ParserRule, Integer>>map(IterableExtensions.<ParserRule>indexed(GrammarUtil.allParserRules(grammar)), _function), Pair.class)));
|
||||
}
|
||||
|
||||
public <T extends Object> List<NamedSerializationContexts<T>> getNamedContexts(final SerializationContextMap<T> map) {
|
||||
final ArrayList<NamedSerializationContexts<T>> result = CollectionLiterals.<NamedSerializationContexts<T>>newArrayList();
|
||||
final HashMap<String, Integer> names = CollectionLiterals.<String, Integer>newHashMap();
|
||||
List<SerializationContextMap.Entry<T>> _values = map.values();
|
||||
for (final SerializationContextMap.Entry<T> e : _values) {
|
||||
Set<EClass> _types = e.getTypes();
|
||||
for (final EClass t : _types) {
|
||||
{
|
||||
final List<ISerializationContext> ctx = e.getContexts(t);
|
||||
String _xifexpression = null;
|
||||
if ((t == null)) {
|
||||
_xifexpression = "";
|
||||
} else {
|
||||
_xifexpression = t.getName();
|
||||
}
|
||||
String _plus = (_xifexpression + "_");
|
||||
String _significantGrammarElement = this.getSignificantGrammarElement(ctx);
|
||||
final String name = (_plus + _significantGrammarElement);
|
||||
final Integer dup = names.get(name);
|
||||
String _xifexpression_1 = null;
|
||||
if ((dup == null)) {
|
||||
String _xblockexpression = null;
|
||||
{
|
||||
names.put(name, Integer.valueOf(1));
|
||||
_xblockexpression = name;
|
||||
}
|
||||
_xifexpression_1 = _xblockexpression;
|
||||
} else {
|
||||
String _xblockexpression_1 = null;
|
||||
{
|
||||
names.put(name, Integer.valueOf(((dup).intValue() + 1)));
|
||||
_xblockexpression_1 = ((name + "_") + dup);
|
||||
}
|
||||
_xifexpression_1 = _xblockexpression_1;
|
||||
}
|
||||
final String unique = _xifexpression_1;
|
||||
T _value = e.getValue();
|
||||
NamedSerializationContexts<T> _namedSerializationContexts = new NamedSerializationContexts<T>(unique, t, ctx, _value);
|
||||
result.add(_namedSerializationContexts);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getSignificantGrammarElement(final Iterable<ISerializationContext> contexts) {
|
||||
ParserRule rule = null;
|
||||
int index = Integer.MAX_VALUE;
|
||||
for (final ISerializationContext ctx : contexts) {
|
||||
{
|
||||
ParserRule pr = ctx.getParserRule();
|
||||
if ((pr == null)) {
|
||||
final Action action = ctx.getAssignedAction();
|
||||
if ((action != null)) {
|
||||
pr = GrammarUtil.containingParserRule(action);
|
||||
}
|
||||
}
|
||||
if ((pr != null)) {
|
||||
final Integer i = this.rules.get(pr);
|
||||
if (((i).intValue() < index)) {
|
||||
index = (i).intValue();
|
||||
rule = pr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((rule != null)) {
|
||||
return rule.getName();
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
}
|
|
@ -1,243 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2016 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator.serializer;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.eclipse.emf.common.notify.Adapter;
|
||||
import org.eclipse.emf.common.notify.impl.AdapterImpl;
|
||||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.serializer.ISerializationContext;
|
||||
import org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider;
|
||||
import org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider;
|
||||
import org.eclipse.xtext.serializer.analysis.SerializationContextMap;
|
||||
import org.eclipse.xtext.util.formallang.Nfa;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class SemanticSequencerExtensions {
|
||||
@FinalFieldsConstructor
|
||||
@Accessors
|
||||
protected static class SuperGrammar extends AdapterImpl {
|
||||
private final Grammar grammar;
|
||||
|
||||
@Override
|
||||
public boolean isAdapterForType(final Object type) {
|
||||
return (type == SemanticSequencerExtensions.SuperGrammar.class);
|
||||
}
|
||||
|
||||
public SuperGrammar(final Grammar grammar) {
|
||||
super();
|
||||
this.grammar = grammar;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public Grammar getGrammar() {
|
||||
return this.grammar;
|
||||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
private IGrammarConstraintProvider gcp;
|
||||
|
||||
public Map<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>> getGrammarConstraints(final Grammar grammar, final EClass clazz) {
|
||||
final Map<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>> result = CollectionLiterals.<IGrammarConstraintProvider.IConstraint, List<ISerializationContext>>newLinkedHashMap();
|
||||
final SerializationContextMap<IGrammarConstraintProvider.IConstraint> constraints = this.gcp.getConstraints(grammar);
|
||||
List<SerializationContextMap.Entry<IGrammarConstraintProvider.IConstraint>> _values = constraints.values();
|
||||
for (final SerializationContextMap.Entry<IGrammarConstraintProvider.IConstraint> e : _values) {
|
||||
{
|
||||
final IGrammarConstraintProvider.IConstraint constraint = e.getValue();
|
||||
EClass _type = constraint.getType();
|
||||
boolean _tripleEquals = (_type == clazz);
|
||||
if (_tripleEquals) {
|
||||
List<ISerializationContext> contexts = result.get(constraint);
|
||||
if ((contexts == null)) {
|
||||
contexts = CollectionLiterals.<ISerializationContext>newArrayList();
|
||||
result.put(constraint, contexts);
|
||||
}
|
||||
contexts.addAll(e.getContexts(clazz));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected ResourceSet cloneResourceSet(final ResourceSet rs) {
|
||||
final XtextResourceSet result = new XtextResourceSet();
|
||||
result.setPackageRegistry(rs.getPackageRegistry());
|
||||
result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry());
|
||||
result.setURIConverter(rs.getURIConverter());
|
||||
if ((rs instanceof XtextResourceSet)) {
|
||||
result.setClasspathURIContext(((XtextResourceSet)rs).getClasspathURIContext());
|
||||
result.setClasspathUriResolver(((XtextResourceSet)rs).getClasspathUriResolver());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Grammar getSuperGrammar(final Grammar grammar) {
|
||||
boolean _isEmpty = grammar.getUsedGrammars().isEmpty();
|
||||
if (_isEmpty) {
|
||||
return null;
|
||||
}
|
||||
Adapter _existingAdapter = EcoreUtil.getExistingAdapter(grammar, SemanticSequencerExtensions.SuperGrammar.class);
|
||||
SemanticSequencerExtensions.SuperGrammar sg = ((SemanticSequencerExtensions.SuperGrammar) _existingAdapter);
|
||||
if ((sg != null)) {
|
||||
return sg.grammar;
|
||||
}
|
||||
final URI uri = IterableExtensions.<Grammar>head(grammar.getUsedGrammars()).eResource().getURI();
|
||||
final Resource resource = this.cloneResourceSet(grammar.eResource().getResourceSet()).getResource(uri, true);
|
||||
EObject _head = IterableExtensions.<EObject>head(resource.getContents());
|
||||
final Grammar result = ((Grammar) _head);
|
||||
EList<Adapter> _eAdapters = grammar.eAdapters();
|
||||
SemanticSequencerExtensions.SuperGrammar _superGrammar = new SemanticSequencerExtensions.SuperGrammar(result);
|
||||
_eAdapters.add(_superGrammar);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Collection<IGrammarConstraintProvider.IConstraint> getGrammarConstraints(final Grammar grammar) {
|
||||
if ((grammar == null)) {
|
||||
return CollectionLiterals.<IGrammarConstraintProvider.IConstraint>emptySet();
|
||||
}
|
||||
final Function1<SerializationContextMap.Entry<IGrammarConstraintProvider.IConstraint>, IGrammarConstraintProvider.IConstraint> _function = (SerializationContextMap.Entry<IGrammarConstraintProvider.IConstraint> it) -> {
|
||||
return it.getValue();
|
||||
};
|
||||
return ListExtensions.<SerializationContextMap.Entry<IGrammarConstraintProvider.IConstraint>, IGrammarConstraintProvider.IConstraint>map(this.gcp.getConstraints(grammar).values(), _function);
|
||||
}
|
||||
|
||||
public List<ISemanticSequencerNfaProvider.ISemState> getLinearListOfMandatoryAssignments(final IGrammarConstraintProvider.IConstraint constraint) {
|
||||
final Nfa<ISemanticSequencerNfaProvider.ISemState> nfa = constraint.getNfa();
|
||||
ISemanticSequencerNfaProvider.ISemState state = nfa.getStart();
|
||||
final List<ISemanticSequencerNfaProvider.ISemState> result = CollectionLiterals.<ISemanticSequencerNfaProvider.ISemState>newArrayList();
|
||||
final Set<EStructuralFeature> features = CollectionLiterals.<EStructuralFeature>newHashSet();
|
||||
while ((state != null)) {
|
||||
{
|
||||
ISemanticSequencerNfaProvider.ISemState _stop = nfa.getStop();
|
||||
boolean _tripleEquals = (state == _stop);
|
||||
if (_tripleEquals) {
|
||||
List<ISemanticSequencerNfaProvider.ISemState> _xifexpression = null;
|
||||
boolean _isEmpty = result.isEmpty();
|
||||
if (_isEmpty) {
|
||||
_xifexpression = null;
|
||||
} else {
|
||||
_xifexpression = result;
|
||||
}
|
||||
return _xifexpression;
|
||||
}
|
||||
int _size = state.getFollowers().size();
|
||||
boolean _tripleNotEquals = (_size != 1);
|
||||
if (_tripleNotEquals) {
|
||||
return null;
|
||||
}
|
||||
ISemanticSequencerNfaProvider.ISemState _start = nfa.getStart();
|
||||
boolean _tripleNotEquals_1 = (state != _start);
|
||||
if (_tripleNotEquals_1) {
|
||||
final EStructuralFeature feature = state.getFeature();
|
||||
if ((((feature == null) || feature.isMany()) || (!features.add(feature)))) {
|
||||
return null;
|
||||
}
|
||||
result.add(state);
|
||||
}
|
||||
state = IterableExtensions.<ISemanticSequencerNfaProvider.ISemState>head(state.getFollowers());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toAcceptMethod(final IGrammarConstraintProvider.ConstraintElementType type) {
|
||||
String _switchResult = null;
|
||||
if (type != null) {
|
||||
switch (type) {
|
||||
case ASSIGNED_ACTION_CALL:
|
||||
_switchResult = "acceptAssignedAction";
|
||||
break;
|
||||
case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL:
|
||||
_switchResult = "acceptAssignedCrossRefDatatype";
|
||||
break;
|
||||
case ASSIGNED_CROSSREF_ENUM_RULE_CALL:
|
||||
_switchResult = "acceptAssignedCrossRefEnum";
|
||||
break;
|
||||
case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL:
|
||||
_switchResult = "acceptAssignedCrossRefTerminal";
|
||||
break;
|
||||
case ASSIGNED_CROSSREF_KEYWORD:
|
||||
_switchResult = "acceptAssignedCrossRefKeyword";
|
||||
break;
|
||||
case ASSIGNED_DATATYPE_RULE_CALL:
|
||||
_switchResult = "acceptAssignedDatatype";
|
||||
break;
|
||||
case ASSIGNED_ENUM_RULE_CALL:
|
||||
_switchResult = "acceptAssignedEnum";
|
||||
break;
|
||||
case ASSIGNED_KEYWORD:
|
||||
_switchResult = "acceptAssignedKeyword";
|
||||
break;
|
||||
case ASSIGNED_PARSER_RULE_CALL:
|
||||
_switchResult = "acceptAssignedParserRuleCall";
|
||||
break;
|
||||
case ASSIGNED_TERMINAL_RULE_CALL:
|
||||
_switchResult = "acceptAssignedTerminal";
|
||||
break;
|
||||
default:
|
||||
_switchResult = (("<error, unknown type \'" + type) + "\'>");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
_switchResult = (("<error, unknown type \'" + type) + "\'>");
|
||||
}
|
||||
return _switchResult;
|
||||
}
|
||||
|
||||
public String toNodeType(final IGrammarConstraintProvider.ConstraintElementType type) {
|
||||
String _switchResult = null;
|
||||
if (type != null) {
|
||||
switch (type) {
|
||||
case ASSIGNED_ACTION_CALL:
|
||||
case ASSIGNED_CROSSREF_DATATYPE_RULE_CALL:
|
||||
case ASSIGNED_CROSSREF_ENUM_RULE_CALL:
|
||||
case ASSIGNED_CROSSREF_KEYWORD:
|
||||
case ASSIGNED_DATATYPE_RULE_CALL:
|
||||
case ASSIGNED_ENUM_RULE_CALL:
|
||||
case ASSIGNED_PARSER_RULE_CALL:
|
||||
_switchResult = ICompositeNode.class.getSimpleName();
|
||||
break;
|
||||
case ASSIGNED_CROSSREF_TERMINAL_RULE_CALL:
|
||||
case ASSIGNED_KEYWORD:
|
||||
case ASSIGNED_TERMINAL_RULE_CALL:
|
||||
_switchResult = ILeafNode.class.getSimpleName();
|
||||
break;
|
||||
default:
|
||||
_switchResult = (("<error, unknown type \'" + type) + "\'>");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
_switchResult = (("<error, unknown type \'" + type) + "\'>");
|
||||
}
|
||||
return _switchResult;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue