use of org.metaborg.core.messages.IMessage in project spoofax by metaborg.
the class ConstraintSingleFileAnalyzer method analyzeAll.
@Override
protected ISpoofaxAnalyzeResults analyzeAll(Map<String, ISpoofaxParseUnit> changed, Set<String> removed, ISingleFileScopeGraphContext context, HybridInterpreter runtime, String strategy, IProgress progress, ICancel cancel) throws AnalysisException {
final NaBL2DebugConfig debugConfig = context.config().debug();
for (String input : removed) {
context.removeUnit(input);
}
final int n = changed.size();
progress.setWorkRemaining(n + 1);
if (debugConfig.analysis() || debugConfig.files()) {
logger.info("Analyzing {} files in {}.", n, context.location());
}
final Collection<ISpoofaxAnalyzeUnit> results = Lists.newArrayList();
try {
for (Map.Entry<String, ISpoofaxParseUnit> input : changed.entrySet()) {
final String source = input.getKey();
final ISpoofaxParseUnit parseUnit = input.getValue();
final ITerm ast = strategoTerms.fromStratego(parseUnit.ast());
if (debugConfig.files()) {
logger.info("Analyzing {}.", source);
}
final ISingleFileScopeGraphUnit unit = context.unit(source);
unit.clear();
try {
// initial
InitialResult initialResult;
final Optional<ITerm> customInitial;
{
if (debugConfig.collection()) {
logger.info("Collecting initial constraints of {}.", source);
}
ITerm initialResultTerm = doAction(strategy, Actions.analyzeInitial(source, ast), context, runtime).orElseThrow(() -> new AnalysisException(context, "No initial result."));
initialResult = InitialResult.matcher().match(initialResultTerm).orElseThrow(() -> new MetaborgException("Invalid initial results."));
customInitial = doCustomAction(strategy, Actions.customInitial(source, ast), context, runtime);
initialResult = initialResult.withCustomResult(customInitial);
if (debugConfig.collection()) {
logger.info("Collected {} initial constraints of {}.", initialResult.getConstraints().size(), source);
}
}
// unit
UnitResult unitResult;
final Optional<ITerm> customUnit;
{
if (debugConfig.collection()) {
logger.info("Collecting constraints of {}.", source);
}
final ITerm unitResultTerm = doAction(strategy, Actions.analyzeUnit(source, ast, initialResult.getArgs()), context, runtime).orElseThrow(() -> new AnalysisException(context, "No unit result."));
unitResult = UnitResult.matcher().match(unitResultTerm).orElseThrow(() -> new MetaborgException("Invalid unit results."));
final ITerm desugaredAST = unitResult.getAST();
customUnit = doCustomAction(strategy, Actions.customUnit(source, desugaredAST, customInitial.orElse(B.EMPTY_TUPLE)), context, runtime);
unitResult = unitResult.withCustomResult(customUnit);
unit.setUnitResult(unitResult);
if (debugConfig.collection()) {
logger.info("Collected {} constraints of {}.", unitResult.getConstraints().size(), source);
}
}
// solve
ISolution solution;
{
Set<IConstraint> constraints = Sets.union(initialResult.getConstraints(), unitResult.getConstraints());
if (debugConfig.resolution()) {
logger.info("Solving {} constraints of {}.", constraints.size(), source);
}
Function1<String, String> fresh = base -> context.unit(source).fresh().fresh(base);
final IProgress subprogress = progress.subProgress(1);
final SingleFileSolver solver = new SingleFileSolver(context.config().debug(), callExternal(runtime));
GraphSolution preSolution = solver.solveGraph(ImmutableBaseSolution.of(initialResult.getConfig(), constraints, PersistentUnifier.Immutable.of()), fresh, cancel, subprogress);
preSolution = solver.reportUnsolvedGraphConstraints(preSolution);
solution = solver.solve(preSolution, fresh, cancel, subprogress);
solution = solver.reportUnsolvedConstraints(solution);
if (!solution.flowSpecSolution().controlFlowGraph().isEmpty()) {
logger.debug("CFG is not empty: calling FlowSpec dataflow solver");
solution = new FixedPoint().entryPoint(solution, getFlowSpecTransferFunctions(context.language()));
}
unit.setSolution(solution);
if (debugConfig.resolution()) {
logger.info("Solved constraints of {}.", source);
}
}
// final
FinalResult finalResult;
final Optional<ITerm> customFinal;
{
if (debugConfig.files()) {
logger.info("Finalizing analysis of {}.", source);
}
ITerm finalResultTerm = doAction(strategy, Actions.analyzeFinal(source), context, runtime).orElseThrow(() -> new AnalysisException(context, "No final result."));
finalResult = FinalResult.matcher().match(finalResultTerm).orElseThrow(() -> new MetaborgException("Invalid final results."));
customFinal = doCustomAction(strategy, Actions.customFinal(source, customInitial.orElse(B.EMPTY_TUPLE), customUnit.map(cu -> Collections.singletonList(cu)).orElse(Collections.emptyList())), context, runtime);
finalResult = finalResult.withCustomResult(customFinal);
unit.setFinalResult(finalResult);
if (debugConfig.files()) {
logger.info("Finalized analysis of {}.", source);
}
}
final IStrategoTerm analyzedAST = strategoTerms.toStratego(unitResult.getAST());
Optional<CustomSolution> customSolution = customFinal.flatMap(CustomSolution.matcher()::match);
customSolution.ifPresent(cs -> unit.setCustomSolution(cs));
// errors
final boolean success;
{
if (debugConfig.files()) {
logger.info("Processing messages of {}.", source);
}
Messages.Transient messageBuilder = Messages.Transient.of();
messageBuilder.addAll(Messages.unsolvedErrors(solution.constraints()));
messageBuilder.addAll(solution.messages().getAll());
customSolution.map(CustomSolution::getMessages).map(IMessages::getAll).ifPresent(messageBuilder::addAll);
IMessages messages = messageBuilder.freeze();
success = messages.getErrors().isEmpty();
Iterable<IMessage> fileMessages = Iterables.concat(analysisCommon.ambiguityMessages(parseUnit.source(), parseUnit.ast()), messages(messages.getAll(), solution.unifier(), context, context.location()));
// result
results.add(unitService.analyzeUnit(parseUnit, new AnalyzeContrib(true, success, true, analyzedAST, fileMessages, -1), context));
if (debugConfig.analysis() || debugConfig.files()) {
logger.info("Analyzed {}: {} errors, {} warnings, {} notes.", source, messages.getErrors().size(), messages.getWarnings().size(), messages.getNotes().size());
}
}
} catch (MetaborgException | SolverException e) {
logger.warn("Analysis of " + source + " failed.", e);
Iterable<IMessage> messages = Iterables2.singleton(MessageFactory.newAnalysisErrorAtTop(parseUnit.source(), "File analysis failed.", e));
results.add(unitService.analyzeUnit(parseUnit, new AnalyzeContrib(false, false, true, parseUnit.ast(), messages, -1), context));
}
}
} catch (InterruptedException e) {
logger.debug("Analysis was interrupted.");
}
if (debugConfig.analysis()) {
logger.info("Analyzed {} files.", n);
}
return new SpoofaxAnalyzeResults(results, Collections.emptyList(), context);
}
use of org.metaborg.core.messages.IMessage in project spoofax by metaborg.
the class StrategoAnalyzer method resultNoAst.
private ISpoofaxAnalyzeUnit resultNoAst(IStrategoTerm result, ISpoofaxParseUnit input, IContext context, long duration) {
final FileObject source = input.source();
final Collection<IMessage> errors = analysisCommon.messages(source, MessageSeverity.ERROR, result.getSubterm(0));
final Collection<IMessage> warnings = analysisCommon.messages(source, MessageSeverity.WARNING, result.getSubterm(1));
final Collection<IMessage> notes = analysisCommon.messages(source, MessageSeverity.NOTE, result.getSubterm(2));
final Collection<IMessage> messages = Lists.newArrayListWithCapacity(errors.size() + warnings.size() + notes.size());
messages.addAll(errors);
messages.addAll(warnings);
messages.addAll(notes);
return unitService.analyzeUnit(input, new AnalyzeContrib(true, errors.isEmpty(), false, null, messages, duration), context);
}
use of org.metaborg.core.messages.IMessage in project spoofax by metaborg.
the class StrategoAnalyzer method result.
private ISpoofaxAnalyzeUnit result(IStrategoTerm result, ISpoofaxParseUnit input, IContext context, long duration) {
final IStrategoTerm ast = result.getSubterm(0);
final FileObject source = input.source();
final Collection<IMessage> errors = analysisCommon.messages(source, MessageSeverity.ERROR, result.getSubterm(1));
final Collection<IMessage> warnings = analysisCommon.messages(source, MessageSeverity.WARNING, result.getSubterm(2));
final Collection<IMessage> notes = analysisCommon.messages(source, MessageSeverity.NOTE, result.getSubterm(3));
final Collection<IMessage> ambiguities = analysisCommon.ambiguityMessages(source, ast);
final Collection<IMessage> messages = Lists.newArrayListWithCapacity(errors.size() + warnings.size() + notes.size() + ambiguities.size());
messages.addAll(errors);
messages.addAll(warnings);
messages.addAll(notes);
messages.addAll(ambiguities);
return unitService.analyzeUnit(input, new AnalyzeContrib(true, errors.isEmpty(), true, ast, messages, duration), context);
}
use of org.metaborg.core.messages.IMessage in project spoofax by metaborg.
the class TaskEngineAnalyzer method result.
@Nullable
private ISpoofaxAnalyzeUnit result(IStrategoTerm result, Map<String, ISpoofaxParseUnit> inputsPerSource, IContext context, long duration) {
final String sourceString = Tools.asJavaString(result.getSubterm(0));
final FileObject source;
try {
source = resourceService.resolve(sourceString);
} catch (MetaborgRuntimeException e) {
logger.error("Cannot find original source for {}, skipping result", e, sourceString);
return null;
}
final ISpoofaxParseUnit input = inputsPerSource.get(sourceString);
if (input == null) {
logger.error("Cannot find input parse result for {}, skipping result", sourceString);
return null;
}
final IStrategoTerm ast = result.getSubterm(2);
final Collection<IMessage> errors = analysisCommon.messages(source, MessageSeverity.ERROR, result.getSubterm(3));
final Collection<IMessage> warnings = analysisCommon.messages(source, MessageSeverity.WARNING, result.getSubterm(4));
final Collection<IMessage> notes = analysisCommon.messages(source, MessageSeverity.NOTE, result.getSubterm(5));
final Collection<IMessage> ambiguities = analysisCommon.ambiguityMessages(source, ast);
final Collection<IMessage> messages = Lists.newArrayListWithCapacity(errors.size() + warnings.size() + notes.size() + ambiguities.size());
messages.addAll(errors);
messages.addAll(warnings);
messages.addAll(notes);
messages.addAll(ambiguities);
return unitService.analyzeUnit(input, new AnalyzeContrib(true, errors.isEmpty(), true, ast, messages, duration), context);
}
use of org.metaborg.core.messages.IMessage 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;
}
Aggregations