Java Code Examples for org.antlr.v4.runtime.tree.TerminalNode#getSymbol()

The following examples show how to use org.antlr.v4.runtime.tree.TerminalNode#getSymbol() . 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: XmlParser.java    From manifold with Apache License 2.0 6 votes vote down vote up
@Override
public void visitTerminal( TerminalNode node )
{
  Token symbol = node.getSymbol();
  if( _attribute != null )
  {
    if( symbol.getType() == XMLLexer.STRING )
    {
      _attribute.setRawValue( new XmlTerminal( symbol, _attribute ) );
    }
  }
  else if( !_elements.isEmpty() )
  {
    if( symbol.getType() == XMLLexer.TEXT ||
        symbol.getType() == XMLLexer.STRING ||
        symbol.getType() == XMLLexer.CDATA )
    {
      XmlElement second = _elements.peek().getSecond();
      second.setRawContent( new XmlTerminal( symbol, second ) );
    }
  }
}
 
Example 2
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 6 votes vote down vote up
public static String unescapeStringLiteral(TerminalNode terminalNode) {
  Token token = terminalNode.getSymbol();
  if (token.getType() != BQLLexer.STRING_LITERAL) {
    throw new IllegalArgumentException();
  }

  String text = token.getText();
  char initialChar = text.charAt(0);
  if (text.charAt(text.length() - 1) != initialChar) {
    throw new IllegalArgumentException("malformed string literal");
  }

  text = text.substring(1, text.length() - 1);
  if (initialChar == '\'') {
    text = text.replace("''", "'");
  } else if (initialChar == '"') {
    text = text.replace("\"\"", "\"");
  } else {
    throw new UnsupportedOperationException("Not supported yet.");
  }
  return text;
}
 
Example 3
Source File: DefaultDataQLVisitor.java    From hasor with Apache License 2.0 6 votes vote down vote up
@Override
public T visitNameExprRoute(NameExprRouteContext ctx) {
    TerminalNode rouTerm = ctx.ROU();
    SpecialType special = specialType(rouTerm, SpecialType.Special_B);
    //
    Token enterToken = rouTerm != null ? rouTerm.getSymbol() : ctx.start;
    EnterRouteVariable enter = code(new EnterRouteVariable(RouteType.Expr, special), enterToken);
    if (ctx.DOT() != null) {
        StringToken stringToken = code(new StringToken(""), rouTerm);
        this.instStack.push(code(new NameRouteVariable(enter, stringToken), ctx));
    } else {
        this.instStack.push(enter);
    }
    //
    RouteNameSetContext routeNameSet = ctx.routeNameSet();
    if (routeNameSet != null) {
        routeNameSet.accept(this);
    }
    return null;
}
 
Example 4
Source File: DefaultDataQLVisitor.java    From hasor with Apache License 2.0 6 votes vote down vote up
@Override
public T visitSubExprRoute(SubExprRouteContext ctx) {
    TerminalNode rouTerm = ctx.ROU();
    SpecialType special = specialType(rouTerm, SpecialType.Special_B);
    //
    Token enterToken = rouTerm != null ? rouTerm.getSymbol() : ctx.start;
    EnterRouteVariable enter = code(new EnterRouteVariable(RouteType.Expr, special), enterToken);
    this.instStack.push(enter);
    //
    List<RouteSubscriptContext> subscriptContexts = ctx.routeSubscript();
    for (RouteSubscriptContext subContext : subscriptContexts) {
        subContext.accept(this);
    }
    //
    RouteNameSetContext routeNameSet = ctx.routeNameSet();
    if (routeNameSet != null) {
        routeNameSet.accept(this);
    }
    return null;
}
 
Example 5
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static List<TerminalNode> getAllRuleRefNodes(Parser parser, ParseTree tree, String ruleName) {
	List<TerminalNode> nodes = new ArrayList<TerminalNode>();
	Collection<ParseTree> ruleRefs;
	if ( Grammar.isTokenName(ruleName) ) {
		ruleRefs = XPath.findAll(tree, "//lexerRuleBlock//TOKEN_REF", parser);
	}
	else {
		ruleRefs = XPath.findAll(tree, "//ruleBlock//RULE_REF", parser);
	}
	for (ParseTree node : ruleRefs) {
		TerminalNode terminal = (TerminalNode)node;
		Token rrefToken = terminal.getSymbol();
		String r = rrefToken.getText();
		if ( r.equals(ruleName) ) {
			nodes.add(terminal);
		}
	}
	if ( nodes.size()==0 ) return null;
	return nodes;
}
 
Example 6
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static int getMatchingSymbolStartsLine(Corpus corpus,
	                                              InputDocument doc,
	                                              TerminalNode node)
	{
		TerminalNode matchingLeftNode = getMatchingLeftSymbol(corpus, doc, node);
		if ( matchingLeftNode != null ) {
			Token matchingLeftToken = matchingLeftNode.getSymbol();
			int i = matchingLeftToken.getTokenIndex();
			if ( i==0 ) return 1; // first token is considered first on line
			Token tokenBeforeMatchingToken = doc.tokens.getPreviousRealToken(i);
//			System.out.printf("doc=%s node=%s, pair=%s, before=%s\n",
//			                  new File(doc.fileName).getName(), node.getSymbol(), matchingLeftToken, tokenBeforeMatchingToken);
			if ( tokenBeforeMatchingToken!=null ) {
				return matchingLeftToken.getLine()>tokenBeforeMatchingToken.getLine() ? 1 : 0;
			}
			else { // matchingLeftToken must be first in file
				return 1;
			}
		}
		return NOT_PAIR;
	}
 
Example 7
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static int getMatchingSymbolEndsLine(Corpus corpus,
	                                            InputDocument doc,
	                                            TerminalNode node)
	{
		TerminalNode matchingLeftNode = getMatchingLeftSymbol(corpus, doc, node);
		if ( matchingLeftNode != null ) {
			Token matchingLeftToken = matchingLeftNode.getSymbol();
			int i = matchingLeftToken.getTokenIndex();
			Token tokenAfterMatchingToken = doc.tokens.getNextRealToken(i);
//			System.out.printf("doc=%s node=%s, pair=%s, after=%s\n",
//			                  new File(doc.fileName).getName(), node.getSymbol(), matchingLeftToken, tokenAfterMatchingToken);
			if ( tokenAfterMatchingToken!=null ) {
				if ( tokenAfterMatchingToken.getType()==Token.EOF ) {
					return 1;
				}
				return tokenAfterMatchingToken.getLine()>matchingLeftToken.getLine() ? 1 : 0;
			}
		}
		return NOT_PAIR;
	}
 
Example 8
Source File: Formatter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int[] getFeatures(InputDocument doc, int tokenIndexInStream) {
	Token prevToken = doc.tokens.getPreviousRealToken(tokenIndexInStream);
	Token prevPrevToken = prevToken!=null ? doc.tokens.getPreviousRealToken(prevToken.getTokenIndex()) : null;
	boolean prevTokenStartsLine = false;
	if ( prevToken!=null && prevPrevToken!=null ) {
		prevTokenStartsLine = prevToken.getLine()>prevPrevToken.getLine();
	}
	TerminalNode node = tokenToNodeMap.get(doc.tokens.get(tokenIndexInStream));
	if ( node==null ) {
		System.err.println("### No node associated with token "+doc.tokens.get(tokenIndexInStream));
		return null;
	}

	Token curToken = node.getSymbol();

	boolean curTokenStartsNewLine = false;
	if ( prevToken==null ) curTokenStartsNewLine = true; // we must be at start of file
	else if ( line > prevToken.getLine() ) curTokenStartsNewLine = true;

	int[] features = getContextFeatures(corpus, tokenToNodeMap, doc, tokenIndexInStream);

	setListInfoFeatures(tokenToListInfo, features, curToken);

	features[INDEX_PREV_FIRST_ON_LINE]         = prevTokenStartsLine ? 1 : 0;
	features[INDEX_FIRST_ON_LINE]              = curTokenStartsNewLine ? 1 : 0;

	return features;
}
 
Example 9
Source File: DefaultDataQLVisitor.java    From hasor with Apache License 2.0 5 votes vote down vote up
@Override
public T visitExprRoute(ExprRouteContext ctx) {
    TerminalNode rouTerm = ctx.ROU();
    SpecialType specialType = specialType(rouTerm, SpecialType.Special_A);
    //
    Token enterToken = rouTerm != null ? rouTerm.getSymbol() : ctx.start;
    EnterRouteVariable enter = code(new EnterRouteVariable(RouteType.Expr, specialType), enterToken);
    this.instStack.push(enter);
    //
    RouteNameSetContext routeNameSet = ctx.routeNameSet();
    if (routeNameSet != null) {
        routeNameSet.accept(this);
    }
    return null;
}
 
Example 10
Source File: DefaultDataQLVisitor.java    From hasor with Apache License 2.0 5 votes vote down vote up
@Override
public T visitParamRoute(ParamRouteContext ctx) {
    // .根
    TerminalNode rouTerm = ctx.ROU();
    SpecialType special = specialType(rouTerm, SpecialType.Special_B);
    TerminalNode identifier = ctx.IDENTIFIER();
    TerminalNode string = ctx.STRING();
    StringToken rouNameToken = null;
    //
    Token enterToken = rouTerm != null ? rouTerm.getSymbol() : ctx.start;
    EnterRouteVariable enter = code(new EnterRouteVariable(RouteType.Params, special), enterToken);
    if (identifier != null) {
        rouNameToken = code(new StringToken(fixIdentifier(identifier)), identifier);
    } else {
        rouNameToken = code(new StringToken(string.getText()), string);
    }
    this.instStack.push(code(new NameRouteVariable(enter, rouNameToken), rouNameToken));
    //
    // .x{} 后面的继续路由
    RouteSubscriptContext routeSubscript = ctx.routeSubscript();
    if (routeSubscript != null) {
        routeSubscript.accept(this);
    }
    RouteNameSetContext routeNameSet = ctx.routeNameSet();
    if (routeNameSet != null) {
        routeNameSet.accept(this);
    }
    return null;
}
 
Example 11
Source File: ParseTreeUtils.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
static Token verifyToken(final ParseTree parent, final int offset, final int expected) {
    final TerminalNode node = verifyTerminal(parent.getChild(offset));
    final Token ret = node.getSymbol();
    final int type = ret.getType();
    verify(type == expected, "Item %s has type %s, expected %s", node, type, expected);
    return ret;
}
 
Example 12
Source File: ExprGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] getBytes(TerminalNode rule) {
    Token token = rule.getSymbol();
    byte[] bytes = new byte[token.getStopIndex() - token.getStartIndex() - 1];
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);
    int j = 0;
    for (int i = 0; i < bytes.length; i++) {
        int ch = cs.LA(i + 1);
        if (ch == '\\') {
            i++;
            ch = cs.LA(i + 1);
            if (ch == '0') {
                ch = 0;
            }
            else if (ch == 'n') {
                ch = '\n';
            }
            else if (ch == 'r') {
                ch = '\r';
            }
            else if (ch == 'Z') {
                ch = '\032';
            }
        }
        bytes[j] = (byte) ch;
        j++;
    }
    cs.seek(pos);
    if (j != bytes.length) {
        // esacpe characters
        byte[] old = bytes;
        bytes = new byte[j];
        System.arraycopy(old, 0, bytes, 0, j);
    }
    return bytes;
}
 
Example 13
Source File: ExprGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
static String getString(TerminalNode rule, Decoder decoder) {
    Token token = rule.getSymbol();
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);
    int len = token.getStopIndex() - token.getStartIndex() - 1;
    IntSupplier supplier = new IntSupplier() {
        int i = 0;
        
        @Override
        public int getAsInt() {
            if (i >= len) {
                return -1;
            }
            int ch = cs.LA(i + 1);
            if (ch == '\\') {
                i++;
                ch = cs.LA(i + 1);
                if (ch == '0') {
                    ch = 0;
                }
                else if (ch == 'n') {
                    ch = '\n';
                }
                else if (ch == 'r') {
                    ch = '\r';
                }
                else if (ch == 'Z') {
                    ch = '\032';
                }
            }
            i++;
            return ch;
        }
    };
    String result = decoder.toString(supplier);
    cs.seek(pos);
    return result;
}
 
Example 14
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static TerminalNode getMatchingLeftSymbol(Corpus corpus,
                                                 InputDocument doc,
                                                 TerminalNode node)
{
	ParserRuleContext parent = (ParserRuleContext)node.getParent();
	int curTokensParentRuleIndex = parent.getRuleIndex();
	Token curToken = node.getSymbol();
	if (corpus.ruleToPairsBag != null) {
		String ruleName = doc.parser.getRuleNames()[curTokensParentRuleIndex];
		RuleAltKey ruleAltKey = new RuleAltKey(ruleName, parent.getAltNumber());
		List<Pair<Integer, Integer>> pairs = corpus.ruleToPairsBag.get(ruleAltKey);
		if ( pairs!=null ) {
			// Find appropriate pair given current token
			// If more than one pair (a,b) with b=current token pick first one
			// or if a common pair like ({,}), then give that one preference.
			// or if b is punctuation, prefer a that is punct
			List<Integer> viableMatchingLeftTokenTypes = viableLeftTokenTypes(parent, curToken, pairs);
			Vocabulary vocab = doc.parser.getVocabulary();
			if ( !viableMatchingLeftTokenTypes.isEmpty() ) {
				int matchingLeftTokenType =
					CollectTokenPairs.getMatchingLeftTokenType(curToken, viableMatchingLeftTokenTypes, vocab);
				List<TerminalNode> matchingLeftNodes = parent.getTokens(matchingLeftTokenType);
				// get matching left node by getting last node to left of current token
				List<TerminalNode> nodesToLeftOfCurrentToken =
					filter(matchingLeftNodes, n -> n.getSymbol().getTokenIndex()<curToken.getTokenIndex());
				TerminalNode matchingLeftNode = nodesToLeftOfCurrentToken.get(nodesToLeftOfCurrentToken.size()-1);
				if (matchingLeftNode == null) {
					System.err.println("can't find matching node for "+node.getSymbol());
				}
				return matchingLeftNode;
			}
		}
	}
	return null;
}
 
Example 15
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int[] getFeatures(int i)	{
	CodeBuffTokenStream tokens = doc.tokens;
	TerminalNode node = tokenToNodeMap.get(tokens.get(i));
	if ( node==null ) {
		System.err.println("### No node associated with token "+tokens.get(i));
		return null;
	}

	Token curToken = node.getSymbol();
	Token prevToken = tokens.getPreviousRealToken(i);
	Token prevPrevToken = prevToken!=null ? doc.tokens.getPreviousRealToken(prevToken.getTokenIndex()) : null;

	boolean prevTokenStartsLine = false;
	if ( prevToken!=null && prevPrevToken!=null ) {
		prevTokenStartsLine = prevToken.getLine()>prevPrevToken.getLine();
	}

	boolean curTokenStartsNewLine = false;
	if ( prevToken==null ) curTokenStartsNewLine = true; // we must be at start of file
	else if ( curToken.getLine() > prevToken.getLine() ) curTokenStartsNewLine = true;

	int[] features = getContextFeatures(corpus, tokenToNodeMap, doc, i);

	setListInfoFeatures(corpus.tokenToListInfo, features, curToken);

	features[INDEX_PREV_FIRST_ON_LINE]         = prevTokenStartsLine ? 1 : 0;
	features[INDEX_FIRST_ON_LINE]              = curTokenStartsNewLine ? 1 : 0;

	return features;
}
 
Example 16
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Walk upwards from node while p.stop == token; return immediate parent
 *  if there is no ancestor stopping at token. This is the earliest
 *  right ancestor. E.g, for '}' of a block, return parent up the chain from
 *  block stopping with '}'. For '{' of block, return just block as nothing
 *  stops with '{'. (block starts with it).
 */
public static ParserRuleContext earliestAncestorEndingWithToken(TerminalNode node) {
	Token token = node.getSymbol();
	ParserRuleContext p = (ParserRuleContext)node.getParent();
	ParserRuleContext prev = null;
	while (p!=null && p.getStop()==token) {
		prev = p;
		p = p.getParent();
	}
	if ( prev==null ) {
		return (ParserRuleContext)node.getParent();
	}
	return prev;
}
 
Example 17
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Walk upwards from node while p.start == token; return immediate parent
 *  if there is no ancestor starting at token. This is the earliest
 *  left ancestor. E.g, for '{' of a block, return parent up the chain from
 *  block starting with '{'. For '}' of block, return just block as nothing
 *  starts with '}'. (block stops with it).
 */
public static ParserRuleContext earliestAncestorStartingWithToken(TerminalNode node) {
	Token token = node.getSymbol();
	ParserRuleContext p = (ParserRuleContext)node.getParent();
	ParserRuleContext prev = null;
	while (p!=null && p.getStart()==token) {
		prev = p;
		p = p.getParent();
	}
	if ( prev==null ) {
		return (ParserRuleContext)node.getParent();
	}
	return prev;
}
 
Example 18
Source File: JavaFilePage.java    From jd-gui with GNU General Public License v3.0 4 votes vote down vote up
public String getInternalTypeName(ParseTree pt) {
    if (pt instanceof JavaParser.ExpressionContext) {

        if (pt.getChildCount() == 1) {
            JavaParser.PrimaryContext primary = ((JavaParser.ExpressionContext)pt).primary();
            TerminalNode identifier = primary.Identifier();

            if (identifier != null) {
                String name = identifier.getSymbol().getText();
                String descriptor = (currentContext == null) ? null : currentContext.getDescriptor(name);

                if (descriptor != null) {
                    // Is a local variable or a method parameter
                    if (descriptor.charAt(0) == 'L') {
                        return descriptor.substring(1, descriptor.length() - 1);
                    }
                } else if (currentInternalTypeName != null) {
                    String internalTypeName = searchInternalTypeNameForThisFieldName(currentInternalTypeName, name);

                    if (internalTypeName != null) {
                        // Is a field
                        return internalTypeName;
                    } else {
                        internalTypeName = resolveInternalTypeName(Collections.singletonList(identifier));

                        if (internalTypeName != null) {
                            // Is a type
                            return internalTypeName;
                        } else {
                            // Not found
                            return null;
                        }
                    }
                }
            } else {
                TerminalNode tn = primary.getChild(TerminalNode.class, 0);
                Token symbol = (tn == null) ? null : tn.getSymbol();

                if (symbol != null) {
                    switch (symbol.getType()) {
                        case JavaParser.THIS:
                            return currentInternalTypeName;
                        case JavaParser.SUPER:
                            DeclarationData data = declarations.get(currentInternalTypeName);

                            if (data instanceof TypeDeclarationData) {
                                return ((TypeDeclarationData)data).superTypeName;
                            } else {
                                return null;
                            }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 19
Source File: ValueNode.java    From gyro with Apache License 2.0 4 votes vote down vote up
public ValueNode(TerminalNode context) {
    super(context.getSymbol(), context.getSymbol());

    this.value = context.getText();
}
 
Example 20
Source File: ParseTreePrettyPrinter.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Override
public void visitTerminal(TerminalNode ctx) {
  String nodeText = BatfishCombinedParser.escape(ctx.getText());
  _ptSentences.getSentences().add("");
  for (int i = 0; i < _indent; i++) {
    _ptSentences.appendToLastSentence("  ");
  }
  Token t = ctx.getSymbol();
  int tokenType = t.getType();
  int modeAsInt = _combinedParser.getTokenMode(t);
  String mode;
  if (modeAsInt == -1) {
    mode = "<MANUAL/UNKNOWN>";
  } else {
    mode = _combinedParser.getLexer().getModeNames()[modeAsInt];
  }

  String tokenName = (tokenType == Token.EOF) ? "EOF" : _vocabulary.getSymbolicName(tokenType);

  // If the parent context has a named field pointing to the token, it is because the user
  // has a defined name. Add it to the output message.
  for (Field f : ctx.getParent().getClass().getFields()) {
    if (f.getName().equals("start")
        || f.getName().equals("stop")
        || f.getName().startsWith("_t")
        || f.getName().equals(tokenName)) {
      continue;
    }
    try {
      if (f.get(ctx.getParent()) == ctx.getSymbol()) {
        _ptSentences.appendToLastSentence(f.getName() + " = ");
      }
    } catch (Throwable thrown) {
      // Ignore the error and continue.
    }
  }

  if (tokenType == Token.EOF) {
    _ptSentences.appendToLastSentence(tokenName + ":" + nodeText);
  } else {
    _ptSentences.appendToLastSentence(tokenName + ":'" + nodeText + "'");
  }
  if (!mode.equals("DEFAULT_MODE")) {
    _ptSentences.appendToLastSentence("  <== mode:" + mode);
  }

  if (_printLineNumbers) {
    _ptSentences.appendToLastSentence(String.format(" line:%s", _combinedParser.getLine(t)));
  }
}