Search in sources :

Example 1 with ParseError

use of org.spoofax.terms.ParseError in project spoofax by metaborg.

the class DialectIdentifier method identify.

@Override
public IdentifiedDialect identify(FileObject resource) throws MetaborgException {
    final ILanguage strategoLanguage = languageService.getLanguage(SpoofaxConstants.LANG_STRATEGO_NAME);
    if (strategoLanguage == null) {
        final String message = logger.format("Could not find Stratego language, Stratego dialects cannot be identified for resource: {}", resource);
        throw new MetaborgRuntimeException(message);
    }
    // GTODO: use identifier service instead, but that introduces a cyclic dependency. Could use a provider.
    final ILanguageImpl strategoImpl = strategoLanguage.activeImpl();
    if (strategoImpl == null) {
        return null;
    }
    // HACK: assuming single identification facet
    final IdentificationFacet facet = strategoImpl.facet(IdentificationFacet.class);
    if (facet == null || !facet.identify(resource)) {
        return null;
    }
    try {
        final FileObject metaResource = metaResource(resource);
        if (metaResource == null) {
            return null;
        }
        final TermReader termReader = new TermReader(termFactoryService.getGeneric());
        final IStrategoTerm term = termReader.parseFromStream(metaResource.getContent().getInputStream());
        final String name = getSyntaxName(term.getSubterm(0));
        if (name == null) {
            return null;
        }
        final ILanguageImpl dialect = dialectService.getDialect(name);
        if (dialect == null) {
            final String message = String.format("Resource %s requires dialect %s, but that dialect does not exist", resource, name);
            throw new MetaborgException(message);
        }
        final ILanguageImpl base = dialectService.getBase(dialect);
        return new IdentifiedDialect(dialect, base);
    } catch (ParseError | IOException e) {
        throw new MetaborgException("Unable to open or parse .meta file", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ILanguage(org.metaborg.core.language.ILanguage) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ParseError(org.spoofax.terms.ParseError) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedDialect(org.metaborg.core.language.dialect.IdentifiedDialect) TermReader(org.spoofax.terms.io.binary.TermReader)

Example 2 with ParseError

use of org.spoofax.terms.ParseError in project spoofax by metaborg.

the class AbstractConstraintAnalyzer method getFlowSpecTransferFunctions.

protected Optional<TFFileInfo> getFlowSpecTransferFunctions(ILanguageComponent component) {
    TFFileInfo transferFunctions = flowSpecTransferFunctionCache.get(component);
    if (transferFunctions != null) {
        return Optional.of(transferFunctions);
    }
    FileObject tfs = resourceService.resolve(component.location(), TRANSFER_FUNCTIONS_FILE);
    try {
        IStrategoTerm sTerm = termFactory.parseFromString(IOUtils.toString(tfs.getContent().getInputStream(), StandardCharsets.UTF_8));
        ITerm term = strategoTerms.fromStratego(sTerm);
        transferFunctions = TFFileInfo.match().match(term, PersistentUnifier.Immutable.of()).orElseThrow(() -> new ParseException("Parse error on reading the transfer function file"));
    } catch (ParseError | ParseException | IOException e) {
        logger.error("Could not read transfer functions file for {}", component);
        return Optional.empty();
    }
    logger.debug("Caching flowspec transfer functions for language {}", component);
    flowSpecTransferFunctionCache.put(component, transferFunctions);
    return Optional.of(transferFunctions);
}
Also used : TFFileInfo(mb.flowspec.runtime.solver.TFFileInfo) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ITerm(mb.nabl2.terms.ITerm) ParseError(org.spoofax.terms.ParseError) FileObject(org.apache.commons.vfs2.FileObject) ParseException(mb.flowspec.runtime.solver.ParseException) IOException(java.io.IOException)

Example 3 with ParseError

use of org.spoofax.terms.ParseError in project spoofax by metaborg.

the class LanguageComponentFactory method request.

private IComponentCreationConfigRequest request(FileObject root) throws MetaborgException {
    final Collection<String> errors = Lists.newLinkedList();
    final Collection<Throwable> exceptions = Lists.newLinkedList();
    final ConfigRequest<ILanguageComponentConfig> configRequest = componentConfigService.get(root);
    if (!configRequest.valid()) {
        for (IMessage message : configRequest.errors()) {
            errors.add(message.message());
            final Throwable exception = message.exception();
            if (exception != null) {
                exceptions.add(exception);
            }
        }
    }
    final ILanguageComponentConfig config = configRequest.config();
    if (config == null) {
        final String message = logger.format("Cannot retrieve language component configuration at {}", root);
        errors.add(message);
        return new ComponentFactoryRequest(root, errors, exceptions);
    }
    final IStrategoAppl esvTerm;
    try {
        final FileObject esvFile = root.resolveFile("target/metaborg/editor.esv.af");
        if (!esvFile.exists()) {
            esvTerm = null;
        } else {
            esvTerm = esvTerm(root, esvFile);
        }
    } catch (ParseError | IOException | MetaborgException e) {
        exceptions.add(e);
        return new ComponentFactoryRequest(root, errors, exceptions);
    }
    SyntaxFacet syntaxFacet = null;
    StrategoRuntimeFacet strategoRuntimeFacet = null;
    if (esvTerm != null) {
        try {
            syntaxFacet = SyntaxFacetFromESV.create(esvTerm, root);
            if (syntaxFacet != null) {
                Iterables.addAll(errors, syntaxFacet.available());
            }
        } catch (FileSystemException e) {
            exceptions.add(e);
        }
        try {
            strategoRuntimeFacet = StrategoRuntimeFacetFromESV.create(esvTerm, root);
            if (strategoRuntimeFacet != null) {
                Iterables.addAll(errors, strategoRuntimeFacet.available(resourceService));
            }
        } catch (IOException e) {
            exceptions.add(e);
        }
    }
    final ComponentFactoryRequest request;
    if (errors.isEmpty() && exceptions.isEmpty()) {
        request = new ComponentFactoryRequest(root, config, esvTerm, syntaxFacet, strategoRuntimeFacet);
    } else {
        request = new ComponentFactoryRequest(root, errors, exceptions);
    }
    return request;
}
Also used : SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) IMessage(org.metaborg.core.messages.IMessage) MetaborgException(org.metaborg.core.MetaborgException) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IOException(java.io.IOException) FileSystemException(org.apache.commons.vfs2.FileSystemException) ParseError(org.spoofax.terms.ParseError) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponentConfig(org.metaborg.core.config.ILanguageComponentConfig) StrategoRuntimeFacet(org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet)

Aggregations

IOException (java.io.IOException)3 FileObject (org.apache.commons.vfs2.FileObject)3 ParseError (org.spoofax.terms.ParseError)3 MetaborgException (org.metaborg.core.MetaborgException)2 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)2 ParseException (mb.flowspec.runtime.solver.ParseException)1 TFFileInfo (mb.flowspec.runtime.solver.TFFileInfo)1 ITerm (mb.nabl2.terms.ITerm)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 ILanguageComponentConfig (org.metaborg.core.config.ILanguageComponentConfig)1 ILanguage (org.metaborg.core.language.ILanguage)1 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)1 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)1 IdentifiedDialect (org.metaborg.core.language.dialect.IdentifiedDialect)1 IMessage (org.metaborg.core.messages.IMessage)1 StrategoRuntimeFacet (org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet)1 SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 TermReader (org.spoofax.terms.io.binary.TermReader)1