use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.
the class StreamMessagePrinter method print.
@Override
public void print(IMessage message, boolean pardoned) {
final StringBuilder sb = new StringBuilder();
final MessageSeverity severity = message.severity();
sb.append(severity);
if (severity != MessageSeverity.NOTE && pardoned) {
sb.append(" (pardoned)");
}
final FileObject source = message.source();
final ISourceRegion region = message.region();
if (source != null) {
sb.append(" in ");
sb.append(source.getName().getURI());
if (region != null) {
sb.append(":").append(region.startRow());
}
sb.append('\n');
}
if (printHighlight) {
try {
final String sourceText = sourceTextService.text(source);
if (region != null) {
final String affected = AffectedSourceHelper.affectedSourceText(message.region(), sourceText, " ");
if (affected != null) {
sb.append(affected);
}
}
} catch (IOException e) {
}
}
print(sb, message.message(), message.exception(), message.severity(), pardoned);
switch(severity) {
case NOTE:
++notes;
break;
case WARNING:
if (pardoned) {
++warningsPardoned;
} else {
++warnings;
}
break;
case ERROR:
if (pardoned) {
++errorsPardoned;
} else {
++errors;
}
break;
}
}
use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.
the class AbstractConstraintAnalyzer method message.
private IMessage message(ITerm originatingTerm, IMessageInfo messageInfo, MessageSeverity severity, IUnifier unifier, C context, FileObject defaultLocation) {
Optional<TermOrigin> maybeOrigin = TermOrigin.get(originatingTerm);
if (maybeOrigin.isPresent()) {
TermOrigin origin = maybeOrigin.get();
ISourceRegion region = JSGLRSourceRegionFactory.fromTokens(origin.getLeftToken(), origin.getRightToken());
FileObject resource = resourceService.resolve(context.location(), origin.getResource());
String message = messageInfo.getContent().apply(unifier::findRecursive).toString(prettyprint(context, resource(resource, context)));
return MessageFactory.newAnalysisMessage(resource, region, message, severity, null);
} else {
String message = messageInfo.getContent().apply(unifier::findRecursive).toString(prettyprint(context, null));
return MessageFactory.newAnalysisMessageAtTop(defaultLocation, message, severity, null);
}
}
use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.
the class JSGLRCompletionService method fromTokens.
// TODO: Do these strategies need to be specific for a language
@Nullable
protected ISourceLocation fromTokens(IStrategoTerm fragment, HybridInterpreter runtime, ITermFactory termFactory, int position, String languageName, Map<IStrategoTerm, Boolean> leftRecursiveTerms, Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
final FileObject resource = SourceAttachment.getResource(fragment, resourceService);
final IToken left = ImploderAttachment.getLeftToken(fragment);
final IToken right = ImploderAttachment.getRightToken(fragment);
ITokens tokenizer = ImploderAttachment.getTokenizer(fragment);
IToken leftmostValid = left;
IToken rightmostValid = right;
boolean isList = (fragment instanceof IStrategoList) ? true : false;
boolean isOptional = false;
String sort = ImploderAttachment.getSort(fragment);
IStrategoTerm input = termFactory.makeString(sort);
boolean isLeftRecursive = false;
if (fragment instanceof IStrategoAppl && position > right.getEndOffset()) {
try {
isLeftRecursive = strategoCommon.invoke(runtime, input, "is-left-recursive") != null;
} catch (MetaborgException e) {
logger.error("Failed to check recursivity for term {} of sort {} - syntactic completion not activated for this language, please import the completion stratego library", fragment, sort);
}
}
boolean isRightRecursive = false;
if (fragment instanceof IStrategoAppl && position <= left.getStartOffset()) {
try {
isRightRecursive = strategoCommon.invoke(runtime, input, "is-right-recursive") != null;
} catch (MetaborgException e) {
logger.error("Failed to check recursivity for term {} of sort {} - syntactic completion not activated for this language, please import the completion stratego library", fragment, sort);
}
}
if (isLeftRecursive) {
leftRecursiveTerms.put(fragment, true);
}
if (isRightRecursive) {
rightRecursiveTerms.put(fragment, true);
}
if (left == null || right == null) {
return null;
}
if (!isList && left == right && left.getEndOffset() < left.getStartOffset()) {
isOptional = true;
}
// if it's a list or a node that is empty make the element includes the surrounding layout tokens
if (left.getStartOffset() > right.getEndOffset() || isList || isOptional || (isLeftRecursive && isRightRecursive)) {
for (int i = left.getIndex() - 1; i >= 0; i--) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
leftmostValid = tokenizer.getTokenAt(i);
} else {
break;
}
}
for (int i = right.getIndex() + 1; i < tokenizer.getTokenCount(); i++) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
rightmostValid = tokenizer.getTokenAt(i);
} else {
break;
}
}
// if it is left recursive include the layout only on the right
} else if (isLeftRecursive) {
for (int i = left.getIndex(); i < right.getIndex(); i++) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
leftmostValid = tokenizer.getTokenAt(i + 1);
} else {
break;
}
}
for (int i = right.getIndex() + 1; i < tokenizer.getTokenCount(); i++) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
rightmostValid = tokenizer.getTokenAt(i);
} else {
break;
}
}
// if it is right recursive include the layout only on the left
} else if (isRightRecursive) {
for (int i = left.getIndex() - 1; i >= 0; i--) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
leftmostValid = tokenizer.getTokenAt(i);
} else {
break;
}
}
for (int i = right.getIndex(); i > left.getIndex(); i--) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
rightmostValid = tokenizer.getTokenAt(i - 1);
} else {
break;
}
}
} else // if not make it stripes the surrounding layout tokens
{
for (int i = left.getIndex(); i < right.getIndex(); i++) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
leftmostValid = tokenizer.getTokenAt(i + 1);
} else {
break;
}
}
for (int i = right.getIndex(); i > left.getIndex(); i--) {
if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
rightmostValid = tokenizer.getTokenAt(i - 1);
} else {
break;
}
}
}
final ISourceRegion region = JSGLRSourceRegionFactory.fromTokensLayout(leftmostValid, rightmostValid, (isOptional || isList || isLeftRecursive || isRightRecursive));
return new SourceLocation(region, resource);
}
use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.
the class AnalysisCommon method messages.
public Collection<IMessage> messages(FileObject resource, MessageSeverity severity, IStrategoTerm messagesTerm) {
final Collection<IMessage> messages = Lists.newArrayListWithExpectedSize(messagesTerm.getSubtermCount());
for (IStrategoTerm term : messagesTerm.getAllSubterms()) {
final IStrategoTerm originTerm;
final String message;
if (term.getSubtermCount() == 2) {
originTerm = term.getSubterm(0);
message = toString(term.getSubterm(1));
} else {
originTerm = term;
message = toString(term) + " (no tree node indicated)";
}
if (originTerm != null) {
final ISourceLocation location = tracingService.location(originTerm);
if (location != null) {
final ISourceRegion region = location.region();
messages.add(message(resource, region, message, severity));
} else {
messages.add(message(resource, message, severity));
}
} else {
messages.add(message(resource, message, severity));
}
}
return messages;
}
use of org.metaborg.core.source.ISourceRegion 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;
}
Aggregations