use of org.metaborg.spoofax.core.unit.ISpoofaxInputUnit in project spoofax by metaborg.
the class ParsePrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, org.spoofax.interpreter.core.IContext strategoContext) throws MetaborgException, IOException {
// Determine what to parse.
if (!(current instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, input string or file " + current + " is not a string");
}
final String stringOrFile = ((IStrategoString) current).stringValue();
final String text;
@Nullable final FileObject file;
final IStrategoTerm isFileTerm = tvars[0];
if (!(isFileTerm instanceof IStrategoInt)) {
throw new MetaborgException("Cannot parse, input kind " + isFileTerm + " is not an integer");
}
if (((IStrategoInt) isFileTerm).intValue() == 1) {
file = resourceService.resolve(stringOrFile);
if (!file.exists() || !file.isFile()) {
throw new MetaborgException("Cannot parse, input file " + file + " does not exist or is not a file");
}
text = sourceTextService.text(file);
} else {
file = null;
text = stringOrFile;
}
// Determine which language to parse it with.
final ILanguageImpl langImpl;
final IStrategoTerm nameOrGroupIdTerm = tvars[1];
final IStrategoTerm idTerm = tvars[2];
final IStrategoTerm versionTerm = tvars[3];
if (nameOrGroupIdTerm instanceof IStrategoTuple) {
// No name, groupId, id, and version was set, auto detect language to parse with.
if (file == null) {
throw new MetaborgException("Cannot parse a string, no language to parse it with was given");
}
final IContext context = metaborgContext(strategoContext);
if (context != null) {
langImpl = languageIdentifierService.identify(file, context.project());
} else {
langImpl = languageIdentifierService.identify(file);
}
if (langImpl == null) {
throw new MetaborgException("Cannot parse, language for " + file + " could not be identified");
}
} else if (idTerm instanceof IStrategoTuple) {
// No id was set, name is set.
if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language name " + nameOrGroupIdTerm + " is not a string");
}
final String name = ((IStrategoString) nameOrGroupIdTerm).stringValue();
final ILanguage lang = languageService.getLanguage(name);
if (lang == null) {
throw new MetaborgException("Cannot parse, language " + nameOrGroupIdTerm + " does not exist");
}
langImpl = lang.activeImpl();
if (langImpl == null) {
throw new MetaborgException("Cannot parse, language " + lang + " has no implementation loaded");
}
} else {
// A groupId, id, and version is set.
if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language groupId " + nameOrGroupIdTerm + " is not a string");
}
final String groupId = ((IStrategoString) nameOrGroupIdTerm).stringValue();
if (!(idTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language id " + idTerm + " is not a string");
}
final String id = ((IStrategoString) idTerm).stringValue();
if (!(versionTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language version " + versionTerm + " is not a string");
}
final String versionStr = ((IStrategoString) versionTerm).stringValue();
final LanguageVersion version = LanguageVersion.parse(versionStr);
final LanguageIdentifier langId = new LanguageIdentifier(groupId, id, version);
langImpl = languageService.getImpl(langId);
if (langImpl == null) {
throw new MetaborgException("Cannot parse, language implementation " + langId + " does not exist");
}
}
// Parse the text.
final ISpoofaxInputUnit input;
if (file != null) {
@Nullable ILanguageImpl dialect;
try {
final IdentifiedDialect identifierDialect = dialectIdentifier.identify(file);
if (identifierDialect != null) {
dialect = identifierDialect.dialect;
} else {
dialect = null;
}
} catch (MetaborgException | MetaborgRuntimeException e) {
// Ignore
dialect = null;
}
input = unitService.inputUnit(file, text, langImpl, dialect);
} else {
input = unitService.inputUnit(text, langImpl, null);
}
final ISpoofaxParseUnit result = syntaxService.parse(input);
if (result.valid() && result.success()) {
return result.ast();
} else {
return null;
}
}
use of org.metaborg.spoofax.core.unit.ISpoofaxInputUnit in project spoofax by metaborg.
the class ParseFileStrategy method invoke.
@Override
public IStrategoTerm invoke(Context context, IStrategoTerm current) {
if (!Tools.isTermString(current))
return null;
try {
final String path = Tools.asJavaString(current);
final FileObject resource = resourceService.resolve(path);
if (resource.getType() != FileType.FILE) {
return null;
}
final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
if (identifiedResource == null) {
return null;
}
final String text = sourceTextService.text(resource);
final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
final ISpoofaxParseUnit result = syntaxService.parse(input);
return result.ast();
} catch (ParseException | IOException e) {
throw new StrategoException("Parsing failed unexpectedly", e);
}
}
use of org.metaborg.spoofax.core.unit.ISpoofaxInputUnit in project spoofax by metaborg.
the class SpoofaxContext method parse.
@Nullable
public IStrategoTerm parse(File file) throws IOException, ParseException {
final FileObject resource = resourceService.resolve(file);
final ILanguageImpl language = languageIdentifierService.identify(resource);
if (language == null) {
return null;
}
final String text = sourceTextService.text(resource);
final ISpoofaxInputUnit inputUnit = unitService.inputUnit(resource, text, language, null);
final ISpoofaxParseUnit result = syntaxService.parse(inputUnit);
if (!result.valid() || !result.success()) {
return null;
}
final IStrategoTerm term = result.ast();
return term;
}
use of org.metaborg.spoofax.core.unit.ISpoofaxInputUnit in project spoofax by metaborg.
the class JSGLRCompletionService method get.
@Override
public Iterable<ICompletion> get(int position, ISpoofaxParseUnit parseInput, boolean nested) throws MetaborgException {
ISpoofaxParseUnit completionParseResult = null;
if (!nested && !parseInput.success()) {
final JSGLRParserConfiguration config = new JSGLRParserConfiguration(true, true, true, 3000, position);
final ISpoofaxInputUnit input = parseInput.input();
final ISpoofaxInputUnit modifiedInput = unitService.inputUnit(input.source(), input.text(), input.langImpl(), input.dialect(), config);
completionParseResult = syntaxService.parse(modifiedInput);
}
Collection<ICompletion> completions = Lists.newLinkedList();
// Completion in case of empty input
String inputText = parseInput.input().text();
if (inputText.trim().isEmpty()) {
final ILanguageImpl language = parseInput.input().langImpl();
final FileObject location = parseInput.source();
final Iterable<String> startSymbols = language.facet(SyntaxFacet.class).startSymbols;
completions.addAll(completionEmptyProgram(startSymbols, inputText.length(), language, location));
return completions;
}
if (completionParseResult != null && completionParseResult.ast() == null) {
return completions;
}
Collection<IStrategoTerm> nestedCompletionTerms = getNestedCompletionTermsFromAST(completionParseResult);
Collection<IStrategoTerm> completionTerms = getCompletionTermsFromAST(completionParseResult);
boolean blankLineCompletion = isCompletionBlankLine(position, parseInput.input().text());
if (!completionTerms.isEmpty()) {
completions.addAll(completionErroneousPrograms(position, completionTerms, completionParseResult));
}
if (!nestedCompletionTerms.isEmpty()) {
completions.addAll(completionErroneousProgramsNested(position, nestedCompletionTerms, completionParseResult));
}
if (completionTerms.isEmpty() && nestedCompletionTerms.isEmpty()) {
completions.addAll(completionCorrectPrograms(position, blankLineCompletion, parseInput));
}
return completions;
}
use of org.metaborg.spoofax.core.unit.ISpoofaxInputUnit in project spoofax by metaborg.
the class ParseFile method build.
@Override
protected Out<IStrategoTerm> build(Input input) throws Throwable {
requireBuild(input.requiredUnits);
require(input.file);
if (!FileCommands.exists(input.file)) {
return null;
}
final FileObject resource = context.resourceService().resolve(input.file);
final ILanguageImpl language = context.languageIdentifierService().identify(resource);
if (language == null) {
return null;
}
final String text = context.sourceTextService().text(resource);
final ISpoofaxInputUnit inputUnit = context.unitService().inputUnit(resource, text, language, null);
final ISpoofaxParseUnit result = context.syntaxService().parse(inputUnit);
if (!result.valid()) {
return null;
}
return input.persistResult ? OutputPersisted.of(result.ast()) : OutputTransient.of(result.ast());
}
Aggregations