Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   [The "BSD license"]
   Copyright (c) 2005-2009 Terence Parr
   All rights reserved.
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
      derived from this software without specific prior written permission.
 package org.antlr.runtime.tree;
A parser for a stream of tree nodes. "tree grammars" result in a subclass of this. All the error reporting and recovery is shared with Parser via the BaseRecognizer superclass.
 public class TreeParser extends BaseRecognizer {
 	public static final int DOWN = .;
 	public static final int UP = .;
     // precompiled regex used by inContext
     static String dotdot = ".*[^.]\\.\\.[^.].*";
     static String doubleEtc = ".*\\.\\.\\.\\s+\\.\\.\\..*";
     static Pattern dotdotPattern = Pattern.compile();
     static Pattern doubleEtcPattern = Pattern.compile();
 	protected TreeNodeStream input;
 	public TreeParser(TreeNodeStream input) {
 		super(); // highlight that we go to super to set state object
 	public TreeParser(TreeNodeStream inputRecognizerSharedState state) {
 		super(state); // share the state object with another parser
 	public void reset() {
 		super.reset(); // reset all recognizer state variables
 		if ( !=null ) {
 			.seek(0); // rewind the input

Set the input stream
 	public void setTreeNodeStream(TreeNodeStream input) {
 		this. = input;
 		return ;
 	public String getSourceName() {
 		return .getSourceName();
 	protected Object getCurrentInputSymbol(IntStream input) {
 		return ((TreeNodeStream)input).LT(1);
 	protected Object getMissingSymbol(IntStream input,
 									  int expectedTokenType,
 									  BitSet follow)
 		String tokenText =
 			"<missing "+getTokenNames()[expectedTokenType]+">";
         TreeAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
         return adaptor.create(new CommonToken(expectedTokenTypetokenText));

Match '.' in tree parser has special meaning. Skip node or entire tree if node has children. If children, scan until corresponding UP node.
	public void matchAny(IntStream ignore) { // ignore stream, copy of input
		. = false;
		Object look = .LT(1);
		if ( .getTreeAdaptor().getChildCount(look)==0 ) {
			.consume(); // not subtree, consume 1 node and return
		// current node is a subtree, skip to corresponding UP.
		// must count nesting level to get right UP
		int level=0;
		int tokenType = .getTreeAdaptor().getType(look);
		while ( tokenType!=. && !(tokenType== && level==0) ) {
			look = .LT(1);
			tokenType = .getTreeAdaptor().getType(look);
			if ( tokenType ==  ) {
			else if ( tokenType ==  ) {
		.consume(); // consume UP

We have DOWN/UP nodes in the stream that have no line info; override. plus we want to alter the exception type. Don't try to recover from tree parser errors inline...
    protected Object recoverFromMismatchedToken(IntStream input,
                                                int ttype,
                                                BitSet follow)
        throws RecognitionException
        throw new MismatchedTreeNodeException(ttype, (TreeNodeStream)input);

Prefix error message with the grammar name because message is always intended for the programmer because the parser built the input tree not the user.
		return getGrammarFileName()+": node from "+
			   (e.approximateLineInfo?"after ":"")+"line "+e.line+":"+e.charPositionInLine;

Tree parsers parse nodes they usually have a token object as payload. Set the exception token and do the default behavior.
	public String getErrorMessage(RecognitionException eString[] tokenNames) {
		if ( this instanceof TreeParser ) {
			TreeAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
			e.token = adaptor.getToken(e.node);
			if ( e.token==null ) { // could be an UP/DOWN node
				e.token = new CommonToken(adaptor.getType(e.node),
		return super.getErrorMessage(etokenNames);
	public void traceIn(String ruleNameint ruleIndex)  {
	public void traceOut(String ruleNameint ruleIndex)  {
New to GrepCode? Check out our FAQ X