use of org.metaborg.core.MetaborgException in project spoofax by metaborg.
the class ASpoofaxPrimitive method call.
@Override
public boolean call(IContext context, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
final IStrategoTerm current = context.current();
final ITermFactory factory = context.getFactory();
try {
final IStrategoTerm newCurrent = call(current, svars, tvars, factory, context);
if (newCurrent != null) {
context.setCurrent(newCurrent);
return true;
}
} catch (MetaborgException | IOException e) {
throw new InterpreterException("Executing primitive " + name + " failed unexpectedly", e);
}
return false;
}
use of org.metaborg.core.MetaborgException in project spoofax by metaborg.
the class SimpleProjectService method create.
@Override
public IProject create(FileObject location) throws MetaborgException {
final FileName name = location.getName();
for (FileName projectName : projects.keySet()) {
if (name.equals(projectName) || name.isAncestor(projectName)) {
final String message = String.format("Location %s is equal to or nested in project %s", name, projectName);
throw new MetaborgException(message);
}
}
final ConfigRequest<? extends IProjectConfig> configRequest = projectConfigService.get(location);
if (!configRequest.valid()) {
logger.error("Errors occurred when retrieving project configuration from project directory {}", location);
configRequest.reportErrors(new StreamMessagePrinter(sourceTextService, false, false, logger));
}
final IProjectConfig config;
if (configRequest.config() != null) {
config = configRequest.config();
} else {
logger.debug("Using default configuration for project at {}", location);
config = projectConfigService.defaultConfig(location);
}
final IProject project = new Project(location, config);
if (projects.putIfAbsent(name, project) != null) {
final String message = String.format("Project with location %s already exists", name);
throw new MetaborgException(message);
}
return project;
}
use of org.metaborg.core.MetaborgException in project spoofax by metaborg.
the class LanguageSpecPpNamePrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
final FileObject location = context.location();
final IProject project = projectService.get(location);
if (project == null) {
return null;
}
if (languageSpecServiceProvider == null) {
// Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
// should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
logger.error("Language specification service is not available; static injection failed");
return null;
}
final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
if (!languageSpecService.available(project)) {
return null;
}
final ISpoofaxLanguageSpec languageSpec;
try {
languageSpec = languageSpecService.get(project);
} catch (ConfigException e) {
throw new MetaborgException("Unable to get language specification name for " + location, e);
}
if (languageSpec == null) {
return null;
}
return factory.makeString(languageSpec.config().prettyPrintLanguage());
}
use of org.metaborg.core.MetaborgException in project spoofax by metaborg.
the class StrategoAnalyzer method analyze.
private ISpoofaxAnalyzeUnit analyze(ISpoofaxParseUnit input, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
final FileObject source = input.source();
final IStrategoString contextPath = strategoCommon.locationTerm(context.location());
final IStrategoString resourcePath = strategoCommon.resourceTerm(source, context.location());
final IStrategoTuple inputTerm = termFactory.makeTuple(input.ast(), resourcePath, contextPath);
try {
logger.trace("Analysing {}", source);
final Timer timer = new Timer(true);
final IStrategoTerm resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
final long duration = timer.stop();
if (resultTerm == null) {
logger.trace("Analysis for {} failed", source);
return result(analysisCommon.analysisFailedMessage(runtime), input, context, null, duration);
} else if (!(resultTerm instanceof IStrategoTuple)) {
logger.trace("Analysis for {} has unexpected result, not a tuple", source);
final String message = logger.format("Unexpected results from analysis {}", resultTerm);
return result(message, input, context, null, duration);
} else if (resultTerm.getSubtermCount() == 4) {
logger.trace("Analysis for {} done", source);
return result(resultTerm, input, context, duration);
} else if (resultTerm.getSubtermCount() == 3) {
logger.trace("Analysis for {} done", source);
return resultNoAst(resultTerm, input, context, duration);
} else {
logger.trace("Analysis for {} has unexpected result; tuple with more than 4 or less than 2 elements", source);
final String message = logger.format("Unexpected results from analysis {}", resultTerm);
return result(message, input, context, null, duration);
}
} catch (MetaborgException e) {
final String message = logger.format("Analysis for {} failed", source);
logger.trace(message, e);
throw new AnalysisException(context, message, e);
}
}
use of org.metaborg.core.MetaborgException in project spoofax by metaborg.
the class TaskEngineAnalyzer method analyzeAll.
@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
cancel.throwIfCancelled();
final ILanguageImpl langImpl = context.language();
final ITermFactory termFactory = termFactoryService.getGeneric();
final FacetContribution<AnalysisFacet> facetContribution = langImpl.facetContribution(AnalysisFacet.class);
if (facetContribution == null) {
logger.debug("No analysis required for {}", langImpl);
return new SpoofaxAnalyzeResults(context);
}
final AnalysisFacet facet = facetContribution.facet;
cancel.throwIfCancelled();
final HybridInterpreter runtime;
try {
runtime = runtimeService.runtime(facetContribution.contributor, context, false);
} catch (MetaborgException e) {
throw new AnalysisException(context, "Failed to get Stratego runtime", e);
}
cancel.throwIfCancelled();
return analyzeAll(inputs, context, runtime, facet.strategyName, termFactory);
}
Aggregations