com.regnosys.rosetta.tests.util.ExpressionParser Maven / Gradle / Ivy
The newest version!
package com.regnosys.rosetta.tests.util;
import com.google.common.collect.Iterables;
import com.regnosys.rosetta.rosetta.RosettaModel;
import com.regnosys.rosetta.rosetta.expression.RosettaExpression;
import com.regnosys.rosetta.rosetta.simple.Attribute;
import com.regnosys.rosetta.scoping.RosettaScopeProvider;
import com.regnosys.rosetta.services.RosettaGrammarAccess;
import java.io.StringReader;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.diagnostics.IDiagnosticConsumer;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.linking.ILinkingService;
import org.eclipse.xtext.linking.impl.DefaultLinkingService;
import org.eclipse.xtext.linking.lazy.LazyLinker;
import org.eclipse.xtext.linking.lazy.LazyLinkingResource;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.impl.ListBasedDiagnosticConsumer;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.scoping.impl.ImportNormalizer;
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.ListExtensions;
import org.junit.jupiter.api.Assertions;
@SuppressWarnings("all")
public class ExpressionParser {
private static class RosettaContextBasedScopeProvider extends RosettaScopeProvider {
private List context = CollectionLiterals.emptyList();
public void setContext(final List context) {
this.context = context;
}
@Override
protected List getImplicitImports(final boolean ignoreCase) {
List _implicitImports = super.getImplicitImports(ignoreCase);
final Function1 _function = (RosettaModel it) -> {
return it.getName();
};
final Function1 _function_1 = (String it) -> {
return this.createImportedNamespaceResolver((it + ".*"), ignoreCase);
};
Iterable _map = IterableExtensions.map(IterableExtensions.toSet(ListExtensions.map(this.context, _function)), _function_1);
return IterableExtensions.toList(Iterables.concat(_implicitImports, _map));
}
}
private static class RosettaStaticLinker extends LazyLinker {
@Inject
private ExpressionParser.RosettaContextBasedScopeProvider scopeProvider;
private IScope staticScope = IScope.NULLSCOPE;
public void setStateForNextLink(final List context, final Collection extends EObject> globals) {
this.scopeProvider.setContext(context);
this.staticScope = Scopes.scopeFor(globals);
}
private void clearState() {
this.scopeProvider.setContext(CollectionLiterals.emptyList());
this.staticScope = IScope.NULLSCOPE;
}
@Override
protected void doLinkModel(final EObject root, final IDiagnosticConsumer consumer) {
Resource _eResource = root.eResource();
ILinkingService _linkingService = ((LazyLinkingResource) _eResource).getLinkingService();
((DefaultLinkingService) _linkingService).setScopeProvider(this.scopeProvider);
super.doLinkModel(root, consumer);
EcoreUtil2.resolveAll(root);
this.clearState();
}
@Override
protected void createAndSetProxy(final EObject obj, final INode node, final EReference eRef) {
final String varName = NodeModelUtils.getTokenText(node);
final IEObjectDescription staticElement = this.staticScope.getSingleElement(QualifiedName.create(varName));
if ((staticElement != null)) {
final EObject resolved = staticElement.getEObjectOrProxy();
boolean _isMany = eRef.isMany();
if (_isMany) {
Object _eGet = obj.eGet(eRef, false);
((InternalEList) _eGet).addUnique(resolved);
} else {
obj.eSet(eRef, resolved);
}
} else {
super.createAndSetProxy(obj, node, eRef);
}
}
}
@Inject
private IParser parser;
@Inject
private RosettaGrammarAccess grammar;
@Inject
private ModelHelper modelHelper;
@Inject
private Provider resourceProvider;
@Inject
private ExpressionParser.RosettaStaticLinker linker;
public RosettaExpression parseExpression(final CharSequence expr) {
return this.parseExpression(expr, CollectionLiterals.emptyList());
}
public RosettaExpression parseExpression(final CharSequence expr, final Collection extends CharSequence> attrs) {
return this.parseExpression(expr, this.defaultContext(), attrs);
}
public RosettaExpression parseExpression(final CharSequence expr, final List context, final Collection extends CharSequence> attrs) {
final Function1 _function = (CharSequence it) -> {
return this.parseAttribute(it, context);
};
final List attributes = IterableExtensions.toList(IterableExtensions.map(attrs, _function));
return this.parseExpression(expr, context, ((Attribute[])Conversions.unwrapArray(attributes, Attribute.class)));
}
public RosettaExpression parseExpression(final CharSequence expr, final Attribute... attributes) {
return this.parseExpression(expr, this.defaultContext(), attributes);
}
public RosettaExpression parseExpression(final CharSequence expr, final List context, final Attribute... attributes) {
ParserRule _rosettaCalcExpressionRule = this.grammar.getRosettaCalcExpressionRule();
String _string = expr.toString();
StringReader _stringReader = new StringReader(_string);
final IParseResult result = this.parser.parse(_rosettaCalcExpressionRule, _stringReader);
Assertions.assertFalse(result.hasSyntaxErrors());
EObject _rootASTElement = result.getRootASTElement();
final RosettaExpression expression = ((RosettaExpression) _rootASTElement);
this.createResource("expr", expression, context);
this.link(expression, context, ((Collection extends EObject>)Conversions.doWrapArray(attributes)));
return expression;
}
public Attribute parseAttribute(final CharSequence attr) {
return this.parseAttribute(attr, this.defaultContext());
}
public Attribute parseAttribute(final CharSequence attr, final List context) {
ParserRule _attributeRule = this.grammar.getAttributeRule();
String _string = attr.toString();
StringReader _stringReader = new StringReader(_string);
final IParseResult result = this.parser.parse(_attributeRule, _stringReader);
Assertions.assertFalse(result.hasSyntaxErrors());
EObject _rootASTElement = result.getRootASTElement();
final Attribute attribute = ((Attribute) _rootASTElement);
this.createResource("attribute", attribute, context);
this.link(attribute, context, CollectionLiterals.emptyList());
return attribute;
}
private Resource createResource(final String name, final EObject content, final List context) {
XtextResource _xblockexpression = null;
{
final ResourceSet resourceSet = IterableExtensions.head(context).eResource().getResourceSet();
int nr = 0;
int _plusPlus = nr++;
String _plus = (("synthetic://" + name) + Integer.valueOf(_plusPlus));
URI uniqueURI = URI.createURI(_plus);
while ((resourceSet.getResource(uniqueURI, false) != null)) {
int _plusPlus_1 = nr++;
String _plus_1 = (("synthetic://" + name) + Integer.valueOf(_plusPlus_1));
uniqueURI = URI.createURI(_plus_1);
}
final XtextResource resource = this.resourceProvider.get();
resource.setURI(uniqueURI);
resource.getContents().add(content);
resourceSet.getResources().add(resource);
_xblockexpression = resource;
}
return _xblockexpression;
}
private List defaultContext() {
final Function1 _function = (Resource it) -> {
EObject _head = IterableExtensions.head(it.getContents());
return ((RosettaModel) _head);
};
return CollectionLiterals.newArrayList(((RosettaModel[])Conversions.unwrapArray(ListExtensions.map(this.modelHelper.testResourceSet().getResources(), _function), RosettaModel.class)));
}
private void link(final EObject obj, final List context, final Collection extends EObject> globals) {
this.linker.setStateForNextLink(context, globals);
final ListBasedDiagnosticConsumer consumer = new ListBasedDiagnosticConsumer();
this.linker.linkModel(obj, consumer);
obj.eResource().getErrors().addAll(consumer.getResult(Severity.ERROR));
}
}