Skip to content

Commit

Permalink
Merge pull request #10 from trinnguyen/fix-linker
Browse files Browse the repository at this point in the history
Fix issue #1 : New approach to load standard library in global scope
  • Loading branch information
eyip002 authored Jul 27, 2021
2 parents f2be896 + 3930c5b commit d27189e
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 175 deletions.
4 changes: 2 additions & 2 deletions src/build-gradle.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
cd "$(dirname "$0")" || exit 1
./gradlew de.uniba.swt.dsl:build || exit 1
./gradlew de.uniba.swt.dsl.ide:build || exit 1
./gradlew de.uniba.swt.dsl:clean de.uniba.swt.dsl:build || exit 1
./gradlew de.uniba.swt.dsl.ide:clean de.uniba.swt.dsl.ide:build || exit 1

# build folder
mkdir build
Expand Down
2 changes: 1 addition & 1 deletion src/build-test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e
cd "$(dirname "$0")"
./gradlew de.uniba.swt.dsl.test:test de.uniba.swt.dsl.test:jacocoTestReport || exit 1
./gradlew clean de.uniba.swt.dsl.test:test de.uniba.swt.dsl.test:jacocoTestReport || exit 1

# build folder
mkdir -p build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,6 @@ public void testFormatDriveRoute() throws Exception {
assertEquals("drive_route", BahnUtil.getDecls(resource).get(0).getName());
}

@Test
public void testFormatStandardLib() throws Exception {
var input = testHelper.parseValid("");
StandardLibHelper.loadStandardLibResource(validator, input.getResourceSet());

// find the standard lib resource
Resource standardRes = null;
for (Resource resource : input.getResourceSet().getResources()) {
if (resource != input) {
standardRes = resource;
break;
}
}

// format
if (standardRes != null) {
var out = formatResource(standardRes);
assertTrue(out != null && !out.isEmpty(), "Formatted standard lib must not be empty");
} else {
fail("Failed to load the standard library");
}
}

/**
* BahnFormatter is called using Serializer
* @param src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ private BahnUiSnippet() {}
" points master\n" +
" end\n" +
"\n" +
" peripherals master\n" +
" end\n" +
"\n" +
" blocks\n" +
" end\n" +
"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
*/
package de.uniba.swt.dsl;

import org.eclipse.xtext.linking.ILinker;
import de.uniba.swt.dsl.linker.BahnImportURIGlobalScopeProvider;
import org.eclipse.xtext.scoping.IGlobalScopeProvider;

/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
public class BahnRuntimeModule extends AbstractBahnRuntimeModule {

@Override
public Class<? extends ILinker> bindILinker() {
return StandardLibLazyLinker.class;
public Class<? extends IGlobalScopeProvider> bindIGlobalScopeProvider() {
return BahnImportURIGlobalScopeProvider.class;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void generateExpr(PrimaryExpr expression) {
// ValuedReferenceExpr
if (expression instanceof ValuedReferenceExpr) {
ValuedReferenceExpr referenceExpr = (ValuedReferenceExpr) expression;
append(((ValuedReferenceExpr) expression).getDecl().getName());
append(referenceExpr.getDecl().getName());

// index
if (referenceExpr.getIndexExpr() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ public static List<FuncDecl> getDecls(ResourceSet set) {
public static List<FuncDecl> getDecls(Resource resource) {
var bahnModel = getBahnModel(resource);
if (bahnModel != null) {
List<FuncDecl> decls = new ArrayList<>();
for (Component component : bahnModel.getComponents()) {
if (component instanceof FuncDecl) {
decls.add((FuncDecl)component);
}
}
return decls;
return getDecls(bahnModel);
}

return null;
}

public static List<FuncDecl> getDecls(BahnModel bahnModel) {
List<FuncDecl> decls = new ArrayList<>();
for (Component component : bahnModel.getComponents()) {
if (component instanceof FuncDecl) {
decls.add((FuncDecl)component);
}
}
return decls;
}

public static BooleanLiteral createBooleanLiteral(boolean val) {
var liter = BahnFactory.eINSTANCE.createBooleanLiteral();
liter.setBoolValue(val);
Expand Down Expand Up @@ -288,4 +292,12 @@ public static boolean isMacOS() {
public static boolean isWindows() {
return getOsName().startsWith("win");
}

public static String getUri(BahnModel bahnModel) {
if (bahnModel.eResource() != null && bahnModel.eResource().getURI() != null) {
return bahnModel.eResource().getURI().toString();
}

return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import de.uniba.swt.dsl.generator.externals.EmbeddedSccLowLevelCodeExternalGenerator;
import de.uniba.swt.dsl.generator.externals.JavaCliRuntimeExecutor;
import de.uniba.swt.dsl.generator.externals.LibraryExternalGenerator;
import de.uniba.swt.dsl.linker.BahnImportURIGlobalScopeProvider;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
Expand All @@ -50,6 +52,8 @@
import java.nio.file.Paths;
import java.util.List;

import static de.uniba.swt.dsl.generator.StandardLibHelper.getStandardLibPlatformUri;

public class StandaloneApp {

public static final String MODE_DEFAULT = "default";
Expand Down Expand Up @@ -97,6 +101,11 @@ public boolean runGenerator(Resource resource, String filePath, AbstractFileSyst
}
fsa.setOutputPath(out);

// Validate the standardlib
Resource standardlibResource = resource.getResourceSet().getResource(getStandardLibPlatformUri(), true);
if (validateTheResource(standardlibResource))
return false;

// Validate the resource
if (validateTheResource(resource))
return false;
Expand Down Expand Up @@ -159,6 +168,9 @@ private Resource loadResource(String filePath) {
return null;

// load resource
return resourceSetProvider.get().getResource(URI.createFileURI(filePath), true);
Resource resource = resourceSetProvider.get().getResource(URI.createFileURI(filePath), true);
BahnImportURIGlobalScopeProvider.setXtextResourceSetOptions(resource);

return resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,82 +24,13 @@

package de.uniba.swt.dsl.generator;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class StandardLibHelper {
private StandardLibHelper() {

}

private static final Logger logger = Logger.getLogger(StandardLibHelper.class);

private static final String RESOURCES_FOLDER_NAME = "resources";

public static final String FILE_NAME = "standardlib.bahn";

public static void loadStandardLibResource(IResourceValidator validator, ResourceSet resourceSet) {
logger.debug("Start loading standard resource");

var resource = loadPluginResource(resourceSet);
logger.debug("Failed to load resource from plugin. Attempt to load from embedded resource");
if (resource == null ) {
resource = loadEmbeddedResource(resourceSet);
}

if (resource != null) {
List<Issue> issues = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
if (issues != null && issues.size() > 0) {
logger.error("Error on parsing built-in standard library");
for (var error : issues) {
logger.error(error);
}
} else {
logger.debug("Success loading standard library");
}
}
}

/**
* Load stream from embedded resource of Java application
* Used in cli compiler and ide server for visual studio code extension
* @return
*/
private static Resource loadEmbeddedResource(ResourceSet resourceSet) {
try (var stream = StandardLibHelper.class.getClassLoader().getResourceAsStream(FILE_NAME)) {
if (stream == null)
return null;

URI uri = URI.createURI(FILE_NAME);
var resource = resourceSet.createResource(uri);
resource.load(stream, resourceSet.getLoadOptions());
return resource;
} catch (IOException e) {
logger.error("Failed to load embedded stream: " + e.getMessage());
}

return null;
public static URI getStandardLibPlatformUri() {
return URI.createURI("classpath:/" + FILE_NAME);
}

/**
* Load resource from URI using Eclipse plugin
* Run with Eclipse-based IDE only
* @param resourceSet
* @return
*/
private static Resource loadPluginResource(ResourceSet resourceSet) {
URI uri = URI.createURI("platform:/plugin/de.uniba.swt.dsl/" + RESOURCES_FOLDER_NAME + "/" + FILE_NAME);
return resourceSet.getResource(uri, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.uniba.swt.dsl.linker;

import de.uniba.swt.dsl.generator.StandardLibHelper;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.scoping.impl.ImportUriGlobalScopeProvider;

import java.util.LinkedHashSet;

public class BahnImportURIGlobalScopeProvider extends ImportUriGlobalScopeProvider {

@Override
protected LinkedHashSet<URI> getImportedUris(Resource resource) {
BahnImportURIGlobalScopeProvider.setXtextResourceSetOptions(resource);

LinkedHashSet<URI> set = super.getImportedUris(resource);
var uri = StandardLibHelper.getStandardLibPlatformUri();
set.add(uri);

return set;
}

public static void setXtextResourceSetOptions(Resource resource) {
if (resource.getResourceSet() instanceof XtextResourceSet) {
XtextResourceSet resourceSet = (XtextResourceSet) resource.getResourceSet();
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
resourceSet.setClasspathUriResolver(new CustomClassPathUriResolver());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.uniba.swt.dsl.linker;

import de.uniba.swt.dsl.generator.StandaloneApp;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.resource.ClassloaderClasspathUriResolver;

public class CustomClassPathUriResolver extends ClassloaderClasspathUriResolver {
@Override
public URI resolve(Object context, URI classpathUri) {
var ctx = context != null ? context : StandaloneApp.class.getClassLoader();
return super.resolve(ctx, classpathUri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void prepare() {
// convert syntactic sugar foreach to while iteration
normalizers.add(foreachNormalizer);

// convert string comparision expression using extern C function
// convert string comparison expression using extern C function
normalizers.add(stringEqualNormalizer);

// convert all getter/setter for configuration and track state
Expand Down
Loading

0 comments on commit d27189e

Please sign in to comment.