[antlr-interest] add new support methods in BaseTree?

Terence Parr parrt at cs.usfca.edu
Wed Oct 22 16:09:41 PDT 2008


On Oct 22, 2008, at 3:46 PM, Terence Parr wrote:

>
> On Oct 22, 2008, at 2:10 PM, Gerald Rosenberg wrote:
>> Yes, this would be quite helpful.  What other functions are you
>> considering?
>
> I'm wide open to suggestions.
>
> I'm thinking we need a general:
>
> public boolean inContext(String ctx);
>
> where we could use contexts of "CLASS ... METHOD" that says I'm
> directly below a METHOD which is a descendent of a CLASS somewhere
> above. "..." would really be a sort of .* reg ex.  Perhaps an xpath
> [shudder] like thing too where "/CLASS ..." means root must be  
> CLASS...
>
> Also hasAncestor() should have getAncestor() variant too I guess.
>
> Perhaps getAncestors()?  Would return a list of nodes on path to root.

ok, all of that is done and easy. except the context one.  CLASS and  
METHOD etc... are token names, which are meaningless to a general tree  
routine...we need something to convert CLASS and METHOD strings to  
token types...perhaps we put in BaseRecognizer and then leverage  
getTokenNames.  It returns an array like this:

     public static final String[] tokenNames = new String[] {
         "<invalid>", "<EOR>", "<DOWN>", "<UP>", "UNIT", "SLIST",  
"ELIST", "CLOSURE", "LIST", "SET", "ARGS", "ARG", "FILTER", "DICT",  
"TUPLE", "COMPREHENSION", "APPLY", "EXPR", "PIPE", "RANGE", "PROGRAM",  
"METHOD", "FIELD", "CALL", "INDEX",
...};

as we walk up tree, we compare tokenNames[ttype] to CLASS and  
METHOD...cool!

ok, in BaseRecog it goes.  Actually...in TreeParser.  only place it's  
useful.

Hmm...i knew this was familiar.  TreeWizard has:

	/** Compute a Map<String, Integer> that is an inverted index of
	 *  tokenNames (which maps int token types to names).
	 */
	public Map computeTokenTypes(String[] tokenNames) {...}

	/** Using the map of token names to token types, return the type. */
	public int getTokenType(String tokenName) {...}

Maybe it belongs in there?

Ter


More information about the antlr-interest mailing list