Search in sources :

Example 76 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class CategorizerService method categorize.

@Override
public Iterable<IRegionCategory<IStrategoTerm>> categorize(ILanguageImpl language, ISpoofaxParseUnit parseResult) {
    final List<IRegionCategory<IStrategoTerm>> regionCategories = Lists.newLinkedList();
    if (!parseResult.valid()) {
        return regionCategories;
    }
    final StylerFacet facet = language.facet(StylerFacet.class);
    if (facet == null) {
        logger.error("Cannot categorize input of {}, it does not have a styler facet", language);
        // GTODO: throw exception instead
        return regionCategories;
    }
    final ImploderAttachment rootImploderAttachment = ImploderAttachment.get(parseResult.ast());
    if (rootImploderAttachment == null) {
        logger.error("Cannot categorize input {} of {}, it does not have an imploder attachment", parseResult, language);
        // GTODO: throw exception instead
        return regionCategories;
    }
    final ITokens tokenizer = rootImploderAttachment.getLeftToken().getTokenizer();
    if (tokenizer == null) {
        logger.error("Cannot categorize input {} of {}, it does not have a tokenizer", parseResult, language);
        // GTODO: throw exception instead
        return regionCategories;
    }
    final int tokenCount = tokenizer.getTokenCount();
    int offset = -1;
    for (int i = 0; i < tokenCount; ++i) {
        final IToken token = tokenizer.getTokenAt(i);
        if (tokenizer.isAmbigous() && token.getStartOffset() < offset) {
            // In case of ambiguities, tokens inside the ambiguity are duplicated, ignore.
            continue;
        }
        if (token.getStartOffset() > token.getEndOffset()) {
            // Indicates an invalid region. Empty lists have regions like this.
            continue;
        }
        if (offset >= token.getStartOffset()) {
            // Duplicate region, skip.
            continue;
        }
        offset = token.getEndOffset();
        final ICategory category = category(facet, token);
        if (category != null) {
            final ISourceRegion region = JSGLRSourceRegionFactory.fromToken(token);
            final IStrategoTerm term = (IStrategoTerm) token.getAstNode();
            regionCategories.add(new RegionCategory<>(region, category, term));
        }
    }
    return regionCategories;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) ICategory(org.metaborg.core.style.ICategory) ISourceRegion(org.metaborg.core.source.ISourceRegion) IRegionCategory(org.metaborg.core.style.IRegionCategory) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ITokens(org.spoofax.jsglr.client.imploder.ITokens)

Example 77 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class CategorizerService method category.

private ICategory category(StylerFacet facet, IToken token) {
    IStrategoTerm term = (IStrategoTerm) token.getAstNode();
    if (term == null) {
        return tokenCategory(token);
    }
    final int termType = term.getTermType();
    if (termType != IStrategoTerm.APPL && termType != IStrategoTerm.TUPLE && termType != IStrategoTerm.LIST) {
        // Try to use the parent of terminal nodes, mimicking behavior of old Spoofax/IMP runtime.
        final IStrategoTerm parentTerm = ParentAttachment.getParent(term);
        if (parentTerm != null) {
            final ICategory category = sortConsCategory(facet, parentTerm);
            if (category != null) {
                return category;
            }
        }
    }
    final ICategory category = sortConsCategory(facet, term);
    if (category != null) {
        return category;
    }
    return tokenCategory(token);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ICategory(org.metaborg.core.style.ICategory)

Example 78 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class JSGLR1I method parse.

public ParseContrib parse(@Nullable JSGLRParserConfiguration parserConfig) throws IOException {
    if (parserConfig == null) {
        parserConfig = new JSGLRParserConfiguration();
    }
    final String fileName = resource != null ? resource.getName().getURI() : null;
    final JSGLRParseErrorHandler errorHandler = new JSGLRParseErrorHandler(this, resource, getParseTable(config.getParseTableProvider()).hasRecovers());
    final Timer timer = new Timer(true);
    SGLRParseResult result;
    try {
        // should throw a fatal, or return a non-null result
        result = actuallyParse(input, fileName, parserConfig);
        assert result != null;
    } catch (SGLRException | InterruptedException e) {
        result = null;
        errorHandler.setRecoveryFailed(parserConfig.recovery);
        errorHandler.processFatalException(new NullTokenizer(input, fileName), e);
    }
    final long duration = timer.stop();
    final IStrategoTerm ast;
    if (result != null) {
        // No fatals occurred, so either parsing succeeded or recovery succeeded
        ast = (IStrategoTerm) result.output;
        if (ast == null) {
            // so we have nothing to do
            assert parser.getTreeBuilder() instanceof NullTreeBuilder;
        } else {
            // in case recovery was required, collect the recoverable errors
            errorHandler.setRecoveryFailed(false);
            errorHandler.gatherNonFatalErrors(ast);
            if (resource != null) {
                SourceAttachment.putSource(ast, resource);
            }
        }
    } else {
        ast = null;
    }
    final boolean hasAst = ast != null;
    final Iterable<IMessage> messages = errorHandler.messages();
    final boolean hasErrors = MessageUtils.containsSeverity(messages, MessageSeverity.ERROR);
    return new ParseContrib(hasAst, hasAst && !hasErrors, ast, messages, duration);
}
Also used : ParseContrib(org.metaborg.spoofax.core.unit.ParseContrib) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IMessage(org.metaborg.core.messages.IMessage) NullTokenizer(org.spoofax.jsglr.client.imploder.NullTokenizer) SGLRParseResult(org.spoofax.jsglr.client.SGLRParseResult) Timer(org.metaborg.util.time.Timer) NullTreeBuilder(org.spoofax.jsglr.client.NullTreeBuilder) SGLRException(org.spoofax.jsglr.shared.SGLRException)

Example 79 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class JSGLR2I method parse.

public ParseContrib parse(@Nullable JSGLRParserConfiguration parserConfig) throws IOException {
    if (parserConfig == null) {
        parserConfig = new JSGLRParserConfiguration();
    }
    final String fileName = resource != null ? resource.getName().getURI() : null;
    String startSymbol = getOrDefaultStartSymbol(parserConfig);
    final Timer timer = new Timer(true);
    final IStrategoTerm ast = parser.parse(input, fileName, startSymbol);
    final long duration = timer.stop();
    final boolean hasAst = ast != null;
    final boolean hasErrors = ast == null;
    final Iterable<IMessage> messages;
    if (hasErrors)
        messages = Collections.singletonList(MessageFactory.newParseErrorAtTop(resource, "Invalid syntax", null));
    else
        messages = Collections.emptyList();
    return new ParseContrib(hasAst, hasAst && !hasErrors, ast, messages, duration);
}
Also used : ParseContrib(org.metaborg.spoofax.core.unit.ParseContrib) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) Timer(org.metaborg.util.time.Timer) IMessage(org.metaborg.core.messages.IMessage)

Example 80 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project nabl by metaborg.

the class ScopeGraphContextPrimitive method call.

@Override
public final boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    final Object contextObj = env.contextObject();
    if (contextObj == null) {
        logger.warn("Context is null.");
        return false;
    }
    if (!(contextObj instanceof IScopeGraphContext)) {
        throw new InterpreterException("Context does not implement IScopeGraphContext");
    }
    final IScopeGraphContext<?> context = (IScopeGraphContext<?>) env.contextObject();
    List<IStrategoTerm> termArgs = Arrays.asList(tvars);
    Optional<? extends IStrategoTerm> result;
    try (IClosableLock lock = context.guard()) {
        result = call(context, env.current(), termArgs, env.getFactory());
    }
    return result.map(t -> {
        env.setCurrent(t);
        return true;
    }).orElse(false);
}
Also used : Arrays(java.util.Arrays) ITerm(mb.nabl2.terms.ITerm) StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) Collectors(java.util.stream.Collectors) IContext(org.spoofax.interpreter.core.IContext) List(java.util.List) Strategy(org.spoofax.interpreter.stratego.Strategy) InterpreterException(org.spoofax.interpreter.core.InterpreterException) ConstraintTerms(mb.nabl2.stratego.ConstraintTerms) Optional(java.util.Optional) LoggerUtils(org.metaborg.util.log.LoggerUtils) AbstractPrimitive(org.spoofax.interpreter.library.AbstractPrimitive) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ILogger(org.metaborg.util.log.ILogger) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7