Search in sources :

Example 11 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class EcmaScriptSubGenerator method getCompileResultAsText.

// note: following method is only used for testing
@Override
public String getCompileResultAsText(Script root, GeneratorOption[] options) {
    final Resource resource = root.eResource();
    if (!(resource instanceof N4JSResource)) {
        throw new IllegalArgumentException("given script must be contained in an N4JSResource");
    }
    final N4JSResource resourceCasted = (N4JSResource) resource;
    final Writer buffCode = new StringWriter();
    getTranspiler().transpile(resourceCasted, options, buffCode, Optional.absent());
    return buffCode.toString();
}
Also used : StringWriter(java.io.StringWriter) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 12 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class PrettyPrinter method print.

/**
 * Serialize the intermediate model in the given transpiler state to <code>outCode</code> and emit source maps to
 * <code>optSourceMapData</code>.
 *
 * @param optPreamble
 *            an optional preamble that will be prepended to the output code. Use '\n' as line separator. If absent,
 *            no preamble will be prepended.<br>
 *            If present, a single line break will be used to separate the preamble from the main output, but
 *            additional line feed characters may be added at the end of the preamble string if empty lines are
 *            desired between preamble and main output.
 */
public void print(TranspilerState state, Writer outCode, Optional<String> optPreamble, Optional<SourceMapInfo> optSourceMapInfo) throws IOException {
    final boolean emitSourceMaps = optSourceMapInfo.isPresent();
    final SourceMapAwareAppendable out = new SourceMapAwareAppendable(outCode, INDENT, emitSourceMaps);
    if (optPreamble.isPresent()) {
        // #append(CharSequence) will convert '\n' to correct line separator
        out.append(optPreamble.get());
        out.newLine();
    }
    PrettyPrinterSwitch.append(out, state);
    if (emitSourceMaps) {
        final SourceMapInfo sourceMapInfo = optSourceMapInfo.get();
        final SourceMapGenerator generator = new SourceMapGeneratorDummy();
        // append link to source maps to outCode
        out.newLine();
        out.append("//# sourceMappingURL=" + sourceMapInfo.simpleSourceMapFileName);
        out.newLine();
        // get the mappings collected by SourceMapAwareAppendable
        final List<SourceOutputMapping> mappings = new ArrayList<>(out.getSourceMapData());
        // perform some tweaks on the mappings (TEMPORARY)
        removeCatchAllMapping(mappings);
        sortMappings(mappings);
        // Convert the source/output mappings produced by SourceMapAwareAppendable to the API of the
        // Google Closure compiler source map library and add them to our SourceMapGenerator 'generator'
        final PositionProvider positionProvider = PositionProvider.from(state.resource);
        for (SourceOutputMapping m : mappings) {
            final EObject originalASTNode = state.tracer.getOriginalASTNode(m.elementInIM);
            if (// it's ok if this is null
            originalASTNode != null && originalASTNode.eResource() instanceof N4JSResource) {
                final ITextRegion region = locationInFileProvider.getSignificantTextRegion(originalASTNode);
                // get the resource and compute the path.
                final String path = sourceMapInfo.resolve((N4JSResource) originalASTNode.eResource());
                final FilePosition sourceStartPosition = positionProvider.toPosition(region.getOffset());
                generator.addMapping(path, // TODO source maps: support for original symbol name
                null, sourceStartPosition, m.outputStart, m.outputEnd);
            }
        }
        // append actual source maps to the buffer passed in via 'sourceMapInfo'
        generator.appendTo(sourceMapInfo.sourceMapBuff, sourceMapInfo.simpleCompiledFileName);
    }
}
Also used : SourceMapGeneratorDummy(org.eclipse.n4js.transpiler.sourcemap.SourceMapGeneratorDummy) ArrayList(java.util.ArrayList) FilePosition(org.eclipse.n4js.transpiler.sourcemap.FilePosition) SourceMapInfo(org.eclipse.n4js.transpiler.AbstractTranspiler.SourceMapInfo) SourceOutputMapping(org.eclipse.n4js.transpiler.print.SourceMapAwareAppendable.SourceOutputMapping) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) N4JSResource(org.eclipse.n4js.resource.N4JSResource) SourceMapGenerator(org.eclipse.n4js.transpiler.sourcemap.SourceMapGenerator)

Example 13 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class PreparationStep method prepare.

/**
 * Creates and initializes the transpiler state. In particular, this will create the intermediate model as a copy of
 * the original AST with some modifications (esp. rewiring of cross-references to ensure intermediate model is
 * self-contained).
 *
 * @param options
 *            the {@link GeneratorOption generator options} to use during generation.
 */
public TranspilerState prepare(Script script, GeneratorOption[] options) {
    final N4JSResource resource = (N4JSResource) script.eResource();
    final ContainerTypesHelper.MemberCollector memberCollector = containerTypesHelper.fromContext(resource);
    final Tracer tracer = new Tracer();
    final InformationRegistry info = new InformationRegistry();
    final STECache steCache = createIM(script, tracer, info);
    return new TranspilerState(resource, options, memberCollector, steCache.im, steCache, tracer, info);
}
Also used : ContainerTypesHelper(org.eclipse.n4js.utils.ContainerTypesHelper) STECache(org.eclipse.n4js.transpiler.TranspilerState.STECache) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Example 14 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class N4IDEXpectFileSetup method createThisResource.

/**
 * Creates {@link N4JSResource} in new {@link ResourceSet}. Created resource has uri of processed xt file and its
 * context. During creation resource factory is obtained dynamically to preserve bindings created by XPECT (see
 * {@link org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup#configure})
 */
@Creates(ThisResource.class)
public XtextResource createThisResource() throws IOException, CoreException {
    Entry<IFile, IProject> file2project = findTestResources();
    IFile xpectFile = file2project.getKey();
    IProject userProject = file2project.getValue();
    ResourceSet resourceSet = resourceSetProvider.get(userProject);
    URI xpectFilePlatformURI = URI.createPlatformResourceURI(xpectFile.getFullPath().toString(), ENCODE_PLATFORM_RESOURCE_URIS);
    Injector injector = IXtInjectorProvider.INSTANCE.getInjector(ctx.get(XpectJavaModel.class), xpectFilePlatformURI);
    Resource resource = injector.getInstance(IResourceFactory.class).createResource(xpectFilePlatformURI);
    resourceSet.getResources().add(resource);
    InputStream input = xpectFile.getContents();
    try {
        resource.load(input, null);
    } finally {
        if (input != null)
            input.close();
    }
    return (XtextResource) resource;
}
Also used : IFile(org.eclipse.core.resources.IFile) Injector(com.google.inject.Injector) XpectJavaModel(org.eclipse.xpect.XpectJavaModel) InputStream(java.io.InputStream) ThisResource(org.eclipse.xpect.xtext.lib.setup.ThisResource) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IResourceFactory(org.eclipse.xtext.resource.IResourceFactory) IProject(org.eclipse.core.resources.IProject) Creates(org.eclipse.xpect.state.Creates)

Example 15 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class ConcreteSyntaxAwareReferenceFinder method findReferences.

@Override
public void findReferences(Predicate<URI> targetURIs, Resource resource, Acceptor acceptor, IProgressMonitor monitor) {
    // make sure data is present
    keys.getData((TargetURIs) targetURIs, new SimpleResourceAccess(resource.getResourceSet()));
    EList<EObject> astContents;
    if (resource instanceof N4JSResource) {
        // In case of N4JSResource, we search only in the AST but NOT in TModule tree!
        Script script = (Script) ((N4JSResource) resource).getContents().get(0);
        astContents = new BasicEList<>();
        astContents.add(script);
    } else {
        astContents = resource.getContents();
    }
    for (EObject content : astContents) {
        findReferences(targetURIs, content, acceptor, monitor);
    }
}
Also used : Script(org.eclipse.n4js.n4JS.Script) SimpleResourceAccess(org.eclipse.n4js.ts.findReferences.SimpleResourceAccess) EObject(org.eclipse.emf.ecore.EObject) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Aggregations

N4JSResource (org.eclipse.n4js.resource.N4JSResource)21 Resource (org.eclipse.emf.ecore.resource.Resource)10 URI (org.eclipse.emf.common.util.URI)8 EObject (org.eclipse.emf.ecore.EObject)6 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 Script (org.eclipse.n4js.n4JS.Script)4 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)3 TModule (org.eclipse.n4js.ts.types.TModule)3 Optional (com.google.common.base.Optional)2 Iterables (com.google.common.collect.Iterables)2 File (java.io.File)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2