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

The following examples show how to use org.antlr.v4.runtime.Token#getChannel() . 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: SqlParser.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void exitNonReserved(SqlBaseParser.NonReservedContext context)
{
    // we can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal.
    // Otherwise, ANTLR gets confused an fires spurious notifications.
    if (!(context.getChild(0) instanceof TerminalNode)) {
        int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex();
        throw new AssertionError("nonReserved can only contain tokens. Found nested rule: " + ruleNames.get(rule));
    }

    // replace nonReserved words with IDENT tokens
    context.getParent().removeLastChild();

    Token token = (Token) context.getChild(0).getPayload();
    Token newToken = new CommonToken(
            new Pair<>(token.getTokenSource(), token.getInputStream()),
            SqlBaseLexer.IDENTIFIER,
            token.getChannel(),
            token.getStartIndex(),
            token.getStopIndex());

    context.getParent().addChild(parser.createTerminalNode(context.getParent(), newToken));
}
 
Example 2
Source File: DocCommentManager.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Prints warnings for unused documentation comments.
 */
public void printWarnings()
{
    if (currentTokenStream == null)
        return;

    for (int i = 0; i < currentTokenStream.size(); ++i)
    {
        final Token token = currentTokenStream.get(i);
        if (token.getChannel() == ZserioLexer.DOC)
        {
            if (!currentUsedComments.contains(token))
            {
                ZserioToolPrinter.printWarning(new AstLocation(token),
                        "Documentation comment is not used!");
            }
        }
    }
}
 
Example 3
Source File: CumulusPortsBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _lastTokenType = token.getType();
  }
}
 
Example 4
Source File: StatementSplitter.java    From presto with Apache License 2.0 5 votes vote down vote up
public static boolean isEmptyStatement(String sql)
{
    TokenSource tokens = getLexer(sql, ImmutableSet.of());
    while (true) {
        Token token = tokens.nextToken();
        if (token.getType() == Token.EOF) {
            return true;
        }
        if (token.getChannel() != Token.HIDDEN_CHANNEL) {
            return false;
        }
    }
}
 
Example 5
Source File: CiscoXrBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 6
Source File: GroupTreeBuilder.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public void exitS_groups_named(S_groups_namedContext ctx) {
  String groupName = unquote(ctx.name.getText());
  HierarchyTree tree = _hierarchy.getTree(groupName);
  if (tree == null) {
    tree = _hierarchy.newTree(groupName);
  }
  StatementContext statement = ctx.s_groups_tail().statement();
  if (statement == null) {
    return;
  }
  Interval interval = ctx.s_groups_tail().getSourceInterval();
  List<Token> unfilteredTokens = _combinedParser.getTokens().getTokens(interval.a, interval.b);
  HierarchyPath path = new HierarchyPath();
  for (Token currentToken : unfilteredTokens) {
    if (currentToken.getChannel() != Lexer.HIDDEN) {
      String text = currentToken.getText();
      int line = currentToken.getLine();
      if (currentToken.getType() == FlatJuniperLexer.WILDCARD) {
        path.addWildcardNode(text, line);
      } else {
        path.addNode(text, line);
      }
    }
  }
  path.setStatement(statement);
  tree.addPath(path, _currentSetLine, null);
}
 
Example 7
Source File: FlatJuniperBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 8
Source File: CiscoNxosBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 9
Source File: F5BigipStructuredBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 10
Source File: BatfishParserErrorListener.java    From batfish with Apache License 2.0 5 votes vote down vote up
private String printToken(Token token) {
  int modeAsInt = _combinedParser.getTokenMode(token);
  String mode = _combinedParser.getLexer().getModeNames()[modeAsInt];
  String rawTokenText = token.getText();
  String tokenText = BatfishCombinedParser.escape(rawTokenText);
  int tokenType = token.getType();
  String channel = token.getChannel() == Lexer.HIDDEN ? "(HIDDEN) " : "";
  String tokenName;
  int line = token.getLine();
  int col = token.getCharPositionInLine();
  if (tokenType == -1) {
    tokenName = "EOF";
  } else {
    tokenName = _combinedParser.getParser().getVocabulary().getSymbolicName(tokenType);
    tokenText = "'" + tokenText + "'";
  }
  return " line "
      + line
      + ":"
      + col
      + " "
      + channel
      + " "
      + tokenName
      + ":"
      + tokenText
      + (!"DEFAULT_MODE".equals(mode) ? "  <== mode:" + mode : "");
}
 
Example 11
Source File: CiscoBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 12
Source File: CumulusFrrBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 13
Source File: CodeBuffTokenStream.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<Token> getRealTokens(int from, int to) {
	List<Token> real = new ArrayList<Token>();
	for (int i=from; i<=to; i++) {
		Token t = tokens.get(i);
		if ( t.getChannel()==Lexer.DEFAULT_TOKEN_CHANNEL ) real.add(t);
	}
	if ( real.size()==0 ) return null;
	return real;
}
 
Example 14
Source File: CumulusInterfacesBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _lastTokenType = token.getType();
  }
}
 
Example 15
Source File: JavaScriptBaseLexer.java    From MSPaintIDE with MIT License 5 votes vote down vote up
/**
 * Return the next token from the character stream and records this last
 * token in case it resides on the default channel. This recorded token
 * is used to determine when the lexer could possibly match a regex
 * literal. Also changes scopeStrictModes stack if tokenize special
 * string 'use strict';
 *
 * @return the next token from the character stream.
 */
@Override
public Token nextToken() {
    Token next = super.nextToken();

    if (next.getChannel() == Token.DEFAULT_CHANNEL) {
        // Keep track of the last token on the default channel.
        this.lastToken = next;
    }

    return next;
}
 
Example 16
Source File: AristaBaseLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public final void emit(Token token) {
  super.emit(token);
  if (token.getChannel() != HIDDEN) {
    _secondToLastTokenType = _lastTokenType;
    _lastTokenType = token.getType();
  }
}
 
Example 17
Source File: ECMAScriptLexer.java    From rockscript with Apache License 2.0 5 votes vote down vote up
/**
 * Return the next token from the character stream and records this last
 * token in case it resides on the default channel. This recorded token
 * is used to determine when the lexer could possibly match a regex
 * literal.
 *
 * @return the next token from the character stream.
 */
@Override
public Token nextToken() {

    // Get the next token.
    Token next = super.nextToken();

    if (next.getChannel() == Token.DEFAULT_CHANNEL) {
        // Keep track of the last token on the default channel.
        this.lastToken = next;
    }

    return next;
}
 
Example 18
Source File: StatementSplitter.java    From rainbow with Apache License 2.0 5 votes vote down vote up
public static boolean isEmptyStatement(String sql)
{
    TokenSource tokens = getLexer(sql, ImmutableSet.of());
    while (true) {
        Token token = tokens.nextToken();
        if (token.getType() == Token.EOF) {
            return true;
        }
        if (token.getChannel() != Token.HIDDEN_CHANNEL) {
            return false;
        }
    }
}
 
Example 19
Source File: ProtoParseListener.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
private boolean isWhitespace(Token token) {
    return token.getChannel() == ProtoLexer.HIDDEN
            && token.getType() != ProtoLexer.LINE_COMMENT;
}
 
Example 20
Source File: JavaScriptBaseParser.java    From MSPaintIDE with MIT License 3 votes vote down vote up
/**
 * Returns {@code true} iff on the current index of the parser's
 * token stream a token of the given {@code type} exists on the
 * {@code HIDDEN} channel.
 *
 * @param type the type of the token on the {@code HIDDEN} channel
 *             to check.
 * @return {@code true} iff on the current index of the parser's
 * token stream a token of the given {@code type} exists on the
 * {@code HIDDEN} channel.
 */
private boolean here(final int type) {

    // Get the token ahead of the current index.
    int possibleIndexEosToken = this.getCurrentToken().getTokenIndex() - 1;
    Token ahead = _input.get(possibleIndexEosToken);

    // Check if the token resides on the HIDDEN channel and if it's of the
    // provided type.
    return (ahead.getChannel() == Lexer.HIDDEN) && (ahead.getType() == type);
}