Search in sources :

Example 16 with IMessage

use of org.metaborg.core.messages.IMessage in project spoofax by metaborg.

the class AnalysisCommon method ambiguityMessages.

public Collection<IMessage> ambiguityMessages(final FileObject resource, IStrategoTerm ast) {
    final Collection<IMessage> messages = Lists.newLinkedList();
    final TermVisitor termVisitor = new TermVisitor() {

        private IStrategoTerm ambStart;

        @Override
        public void preVisit(IStrategoTerm term) {
            if (ambStart == null && "amb".equals(Term.tryGetName(term))) {
                final String text = "Fragment is ambiguous: " + ambToString(term);
                final ISourceLocation location = tracingService.location(term);
                if (location != null) {
                    final ISourceRegion region = location.region();
                    messages.add(message(resource, region, text, MessageSeverity.WARNING));
                } else {
                    messages.add(message(resource, text, MessageSeverity.WARNING));
                }
                ambStart = term;
            }
        }

        @Override
        public void postVisit(IStrategoTerm term) {
            if (term == ambStart) {
                ambStart = null;
            }
        }

        private String ambToString(IStrategoTerm amb) {
            final String result = amb.toString();
            return result.length() > 5000 ? result.substring(0, 5000) + "..." : result;
        }
    };
    termVisitor.visit(ast);
    return messages;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IMessage(org.metaborg.core.messages.IMessage) ISourceRegion(org.metaborg.core.source.ISourceRegion) TermVisitor(org.spoofax.terms.TermVisitor) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Example 17 with IMessage

use of org.metaborg.core.messages.IMessage in project spoofax by metaborg.

the class LanguageSpecConfigService method toConfig.

@Override
protected ConfigRequest<ILanguageSpecConfig> toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) {
    final ProjectConfig projectConfig = new ProjectConfig(config);
    final LanguageSpecConfig languageSpecConfig = new LanguageSpecConfig(config, projectConfig);
    final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile);
    final Collection<IMessage> messages = languageSpecConfig.validate(mb);
    return new ConfigRequest<>(languageSpecConfig, messages);
}
Also used : ProjectConfig(org.metaborg.core.config.ProjectConfig) MessageBuilder(org.metaborg.core.messages.MessageBuilder) ConfigRequest(org.metaborg.core.config.ConfigRequest) IMessage(org.metaborg.core.messages.IMessage)

Example 18 with IMessage

use of org.metaborg.core.messages.IMessage 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 19 with IMessage

use of org.metaborg.core.messages.IMessage 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)

Aggregations

IMessage (org.metaborg.core.messages.IMessage)19 FileObject (org.apache.commons.vfs2.FileObject)8 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)8 AnalyzeContrib (org.metaborg.spoofax.core.unit.AnalyzeContrib)6 MessageBuilder (org.metaborg.core.messages.MessageBuilder)5 MetaborgException (org.metaborg.core.MetaborgException)3 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)3 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 Inject (com.google.inject.Inject)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Optional (java.util.Optional)2 FixedPoint (mb.flowspec.runtime.solver.FixedPoint)2 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)2 ISolution (mb.nabl2.solver.ISolution)2 SolverException (mb.nabl2.solver.SolverException)2 IMessages (mb.nabl2.solver.messages.IMessages)2