Java Code Examples for org.antlr.runtime.Token#getCharPositionInLine()

The following examples show how to use org.antlr.runtime.Token#getCharPositionInLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ANTLRUtil.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public static void debugNodeStream(BufferedTreeNodeStream nodes, PrintStream out) {
	Iterator<Object> iterator = nodes.iterator();
	int indent = 0;
	while (iterator.hasNext()) {
		Object object = iterator.next();
		CommonTree node = (CommonTree) object;
		Token token = node.token;
		if (token != null) {
			if (token.getType() == 2) {
				++indent;
				continue;
			}
			else if (token.getType() == 3) {
				--indent;
				continue;
			}
		}
		String line =
			token == null ? "no pos" : token.getLine() + ":" + token.getCharPositionInLine();
		out.println(indent(indent) + "'" + object + "'     (" + line + ")");
	}
}
 
Example 2
Source File: ParserHelper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
void setEnd( BaseDescr descr ) {
    Token last = input.LT( -1 );
    if ( descr != null && last != null ) {
        int endLocation = last.getText() != null ? last.getCharPositionInLine() + last.getText().length() - 1 : last.getCharPositionInLine();
        descr.setEndCharacter( ((CommonToken) last).getStopIndex() + 1 );
        descr.setEndLocation( last.getLine(),
                              endLocation );
    }
}
 
Example 3
Source File: ParserHelper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
void setEnd( DescrBuilder< ? , ? > db ) {
    Token last = input.LT( -1 );
    if ( db != null && last != null ) {
        int endLocation = last.getText() != null ? last.getCharPositionInLine() + last.getText().length() - 1 : last.getCharPositionInLine();
        db.endCharacter( ((CommonToken) last).getStopIndex() + 1 ).endLocation( last.getLine(),
                                                                                endLocation );
    }
}
 
Example 4
Source File: SemanticException.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, Token token, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = token;
    this.index = ((CommonToken)token).getStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 5
Source File: SemanticException.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, Token token, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = token;
    this.index = ((CommonToken)token).getStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 6
Source File: GrammarSyntaxMessage.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public GrammarSyntaxMessage(ErrorType etype,
							String fileName,
							Token offendingToken,
							RecognitionException antlrException,
							Object... args)
{
	super(etype, antlrException, offendingToken, args);
	this.fileName = fileName;
	this.offendingToken = offendingToken;
	if ( offendingToken!=null ) {
		line = offendingToken.getLine();
		charPosition = offendingToken.getCharPositionInLine();
	}
}
 
Example 7
Source File: GrammarSemanticsMessage.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public GrammarSemanticsMessage(ErrorType etype,
                                 String fileName,
                                 Token offendingToken,
                                 Object... args)
  {
      super(etype,offendingToken,args);
      this.fileName = fileName;
if ( offendingToken!=null ) {
          line = offendingToken.getLine();
          charPosition = offendingToken.getCharPositionInLine();
      }
  }
 
Example 8
Source File: SemanticException.java    From AppTroy with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, Token token, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = token;
    this.index = ((CommonToken)token).getStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 9
Source File: SemanticException.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, Token token, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = token;
    this.index = ((CommonToken)token).getStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 10
Source File: ErrorCollector.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Appends a query snippet to the message to help the user to understand the problem.
 *
 * @param from the first token to include within the snippet
 * @param to the last token to include within the snippet
 * @param offending the token which is responsible for the error
 */
final void appendSnippet(StringBuilder builder,
                         Token from,
                         Token to,
                         Token offending)
{
    if (!areTokensValid(from, to, offending))
        return;

    String[] lines = query.split("\n");

    boolean includeQueryStart = (from.getLine() == 1) && (from.getCharPositionInLine() == 0);
    boolean includeQueryEnd = (to.getLine() == lines.length)
            && (getLastCharPositionInLine(to) == lines[lines.length - 1].length());

    builder.append(" (");

    if (!includeQueryStart)
        builder.append("...");

    lines[lineIndex(to)] = lines[lineIndex(to)].substring(0, getLastCharPositionInLine(to));
    lines[lineIndex(offending)] = highlightToken(lines[lineIndex(offending)], offending);
    lines[lineIndex(from)] = lines[lineIndex(from)].substring(from.getCharPositionInLine());

    for (int i = lineIndex(from), m = lineIndex(to); i <= m; i++)
        builder.append(lines[i]);

    if (!includeQueryEnd)
        builder.append("...");

    builder.append(")");
}
 
Example 11
Source File: SemanticException.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
SemanticException(IntStream input, Token token, String errorMessage, Object... messageArguments) {
    super();
    this.input = input;
    this.token = token;
    this.index = ((CommonToken)token).getStartIndex();
    this.line = token.getLine();
 this.charPositionInLine = token.getCharPositionInLine();
    this.errorMessage = String.format(errorMessage, messageArguments);
}
 
Example 12
Source File: CFParsedStatement.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
protected CFParsedStatement( Token t ) {
	this(t.getLine(), t.getCharPositionInLine());
}
 
Example 13
Source File: CFParsedStatement.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public CFParsedStatement( Token t ) {
	this(t.getLine(), t.getCharPositionInLine());
}
 
Example 14
Source File: ErrorCollector.java    From stratio-cassandra with Apache License 2.0 2 votes vote down vote up
/**
 * Checks that the specified token is valid.
 *
 * @param token the token to check
 * @return <code>true</code> if it is considered as valid, <code>false</code> otherwise.
 */
private static boolean isTokenValid(Token token)
{
    return token.getLine() > 0 && token.getCharPositionInLine() >= 0;
}
 
Example 15
Source File: ErrorCollector.java    From stratio-cassandra with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the index of the last character relative to the beginning of the line 0..n-1
 *
 * @param token the token
 * @return the index of the last character relative to the beginning of the line 0..n-1
 */
private static int getLastCharPositionInLine(Token token)
{
    return token.getCharPositionInLine() + getLength(token);
}