Search in sources :

Example 1 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.

the class OutlineService method toOutlineNode.

@Nullable
private IOutlineNode toOutlineNode(IStrategoTerm term, @Nullable IOutlineNode parent, FileObject location) {
    if (!(term instanceof IStrategoAppl)) {
        return null;
    }
    final IStrategoAppl appl = (IStrategoAppl) term;
    if (!Tools.hasConstructor(appl, "Node", 2)) {
        return null;
    }
    final IStrategoTerm labelTerm = appl.getSubterm(0);
    final String label = label(labelTerm);
    final FileObject icon = icon(labelTerm, location);
    final ISourceRegion region = region(labelTerm);
    final OutlineNode node = new OutlineNode(label, icon, region, parent);
    final IStrategoTerm nodesTerm = appl.getSubterm(1);
    for (IStrategoTerm nodeTerm : nodesTerm) {
        final IOutlineNode childNode = toOutlineNode(nodeTerm, node, location);
        if (childNode != null) {
            node.addChild(childNode);
        }
    }
    return node;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceRegion(org.metaborg.core.source.ISourceRegion) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) OutlineNode(org.metaborg.core.outline.OutlineNode) IOutlineNode(org.metaborg.core.outline.IOutlineNode) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject) IOutlineNode(org.metaborg.core.outline.IOutlineNode) Nullable(javax.annotation.Nullable)

Example 2 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.

the class CategorizerValidator method validate.

public static <T> Iterable<IRegionCategory<T>> validate(Iterable<IRegionCategory<T>> categorization) {
    int offset = -1;
    final Collection<IRegionCategory<T>> validated = Lists.newLinkedList();
    for (IRegionCategory<T> regionCategory : categorization) {
        final ISourceRegion region = regionCategory.region();
        if (offset >= region.startOffset()) {
            logger.warn("Invalid {}, starting offset is greater than offset in previous regions, " + "region category will be skipped", regionCategory);
        } else if (offset >= region.endOffset()) {
            logger.warn("Invalid {}, ending offset is greater than offset in previous regions, " + "region category will be skipped", regionCategory);
        } else if (region.startOffset() > region.endOffset()) {
            logger.warn("Invalid {}, starting offset is greater than ending offset, " + "region category will be skipped", regionCategory);
        } else {
            validated.add(regionCategory);
            offset = regionCategory.region().endOffset();
        }
    }
    return validated;
}
Also used : ISourceRegion(org.metaborg.core.source.ISourceRegion) IRegionCategory(org.metaborg.core.style.IRegionCategory)

Example 3 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.

the class HoverService method hover.

private Hover hover(@Nullable TermWithRegion tuple) {
    if (tuple == null) {
        return null;
    }
    final IStrategoTerm output = tuple.term;
    final ISourceRegion offsetRegion = tuple.region;
    final String text;
    if (output.getTermType() == IStrategoTerm.STRING) {
        text = Tools.asJavaString(output);
    } else {
        text = output.toString();
    }
    final String massagedText = text.replace("\\\"", "\"").replace("\\n", "");
    return new Hover(offsetRegion, massagedText);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) Hover(org.metaborg.core.tracing.Hover) ISourceRegion(org.metaborg.core.source.ISourceRegion)

Example 4 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.

the class ResolverService method resolve.

private Resolution resolve(@Nullable TermWithRegion tuple) {
    if (tuple == null) {
        return null;
    }
    final IStrategoTerm output = tuple.term;
    final ISourceRegion offsetRegion = tuple.region;
    final Collection<ISourceLocation> targets = Lists.newLinkedList();
    if (output.getTermType() == IStrategoTerm.LIST) {
        for (IStrategoTerm subterm : output) {
            final ISourceLocation targetLocation = common.getTargetLocation(subterm);
            if (targetLocation == null) {
                logger.debug("Cannot get target location for {}", subterm);
                continue;
            }
            targets.add(targetLocation);
        }
    } else {
        final ISourceLocation targetLocation = common.getTargetLocation(output);
        if (targetLocation == null) {
            logger.debug("Reference resolution failed, cannot get target location for {}", output);
            return null;
        }
        targets.add(targetLocation);
    }
    if (targets.isEmpty()) {
        logger.debug("Reference resolution failed, cannot get target locations for {}", output);
        return null;
    }
    return new Resolution(offsetRegion, targets);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceRegion(org.metaborg.core.source.ISourceRegion) ISourceLocation(org.metaborg.core.source.ISourceLocation) Resolution(org.metaborg.core.tracing.Resolution)

Example 5 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.

the class TracingService method tokenLocation.

private ISourceLocation tokenLocation(IStrategoTerm fragment) {
    final IToken left = ImploderAttachment.getLeftToken(fragment);
    final IToken right = ImploderAttachment.getRightToken(fragment);
    if (left == null || right == null) {
        return null;
    }
    final ISourceRegion region = JSGLRSourceRegionFactory.fromTokens(left, right);
    final FileObject resource = SourceAttachment.getResource(fragment, resourceService);
    return new SourceLocation(region, resource);
}
Also used : SourceLocation(org.metaborg.core.source.SourceLocation) ISourceLocation(org.metaborg.core.source.ISourceLocation) IToken(org.spoofax.jsglr.client.imploder.IToken) ISourceRegion(org.metaborg.core.source.ISourceRegion) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

ISourceRegion (org.metaborg.core.source.ISourceRegion)12 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)7 FileObject (org.apache.commons.vfs2.FileObject)5 ISourceLocation (org.metaborg.core.source.ISourceLocation)5 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)4 IToken (org.spoofax.jsglr.client.imploder.IToken)4 Nullable (javax.annotation.Nullable)2 IMessage (org.metaborg.core.messages.IMessage)2 SourceLocation (org.metaborg.core.source.SourceLocation)2 IRegionCategory (org.metaborg.core.style.IRegionCategory)2 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 ITokens (org.spoofax.jsglr.client.imploder.ITokens)2 IOException (java.io.IOException)1 TermOrigin (mb.nabl2.stratego.TermOrigin)1 MetaborgException (org.metaborg.core.MetaborgException)1 IOutlineNode (org.metaborg.core.outline.IOutlineNode)1 OutlineNode (org.metaborg.core.outline.OutlineNode)1 ICategory (org.metaborg.core.style.ICategory)1 Hover (org.metaborg.core.tracing.Hover)1 Resolution (org.metaborg.core.tracing.Resolution)1