org.quasiliteral.antlr
Class AstroAST

java.lang.Object
  |
  +--antlr.BaseAST
        |
        +--org.quasiliteral.antlr.AstroAST
All Implemented Interfaces:
AST, Astro, AstroArg, Serializable

public class AstroAST
extends BaseAST
implements Astro

Untamed: AST node implementation which stores tokens explicitly.

This is handy if you'd rather derive information from tokens on an as-needed basis instead of snarfing data from a token as an AST is being built.

In keeping with the nature of Antlr ASTs, Astros are mutable. The corresponding immutable, Persistent, & PassByCopy type is the org.quasiliteral.term.Term.

Author:
Mark S. Miller, Based on Danfuzz Bornstein's TokenAST
See Also:
Serialized Form

Field Summary
private  AstroToken myOptToken
           
 
Fields inherited from class antlr.BaseAST
down, right
 
Constructor Summary
AstroAST()
          Enabled: Construct an instance which (at least initially) is not associated with a token.
AstroAST(Token optToken)
          Enabled: Construct an instance which is associated with the given token.
 
Method Summary
static Astro build(AST self, AstroBuilder builder)
          Enabled: Since not all ASTs are Astros, this static method provides the equivalent of the build/1 instance method for ASTs in general.
 Astro build(AstroBuilder builder)
          Enabled: Builds an equivalent of this AST using the building methods of 'builder'.
 ConstList getArgs()
          Enabled:
 Object getOptArgData()
          Enabled:
 Object getOptArgData(short tagCode)
          Enabled:
 String getOptArgString(short tagCode)
          Enabled:
 Object getOptData()
          Enabled:
 SourceSpan getOptSpan()
          Enabled:
 String getOptString()
          Enabled:
 short getOptTagCode()
          Enabled:
 AstroToken getOptToken()
          Enabled: Get the token associated with this instance.
 AstroTag getTag()
          Enabled:
 String getText()
          Enabled: Get the token text for this instance.
 int getType()
          Enabled: Get the token type for this instance.
 void initialize(AST ast)
          Enabled: Initialize this instance based on the given AST.
 void initialize(int type, String optText)
          Enabled: Initialize this instance with the given token type and text.
 void initialize(Token optToken)
          Enabled: Initialize this instance with the given non-composite token.
 void setOptToken(AstroToken optToken)
          Enabled: Set the token associated with this instance.
 void setText(String text)
          Enabled: Set the token text for this node.
 void setType(int type)
          Enabled: Set the token type for this node.
 String toString()
          Suppressed:
 Astro withArgs(ConstList args)
          Enabled:
 Astro withoutArgs()
          Enabled:
 
Methods inherited from class antlr.BaseAST
addChild, decode, encode, equals, equalsList, equalsListPartial, equalsTree, equalsTreePartial, findAll, findAllPartial, getFirstChild, getNextSibling, removeChildren, setFirstChild, setNextSibling, setVerboseStringConversion, toStringList, toStringTree, xmlSerialize, xmlSerializeNode, xmlSerializeRootClose, xmlSerializeRootOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

myOptToken

private AstroToken myOptToken
Constructor Detail

AstroAST

public AstroAST()
Enabled: Construct an instance which (at least initially) is not associated with a token.


AstroAST

public AstroAST(Token optToken)
Enabled: Construct an instance which is associated with the given token.

Parameters:
optToken - :AstroToken The (optional) token to associate this instance with
Method Detail

toString

public String toString()
Suppressed:

Specified by:
toString in interface AST
Overrides:
toString in class BaseAST
Returns:
a string representation of the object.

build

public Astro build(AstroBuilder builder)
Enabled: Builds an equivalent of this AST using the building methods of 'builder'.

Pattern-wise, the schema functions here as both a visitor and as a factory.

Specified by:
build in interface Astro
Returns:
:Node

build

public static Astro build(AST self,
                          AstroBuilder builder)
Enabled: Since not all ASTs are Astros, this static method provides the equivalent of the build/1 instance method for ASTs in general.

In the understanding of non-Astro ASTs used here, their functor is only according to the AST's type code, for the tag, the optSpan is null (since a generic AST doesn't provide an API for source position, and the implementation generally don't record it either), and the data is the AST's getText(). This choice for data will often be a mistake, but there's no generic way to make a better decision. For grammars where this is wrong, you should build a grammar-specific converter.

XXX This should probably be made into a sugar-instance-method of AST.


getText

public String getText()
Enabled: Get the token text for this instance. If there is no token associated with this instance, then this returns the empty string (""), not null.

Specified by:
getText in interface AST
Overrides:
getText in class BaseAST
Returns:
non-null; the token text

getType

public int getType()
Enabled: Get the token type for this instance. If there is no token associated with this instance, then this returns Token.INVALID_TYPE.

Specified by:
getType in interface AST
Overrides:
getType in class BaseAST
Returns:
the token type

getOptToken

public AstroToken getOptToken()
Enabled: Get the token associated with this instance. If there is no token associated with this instance, then this returns null.

Returns:
The token associated with this instance, or mull if there is no associated token

setOptToken

public void setOptToken(AstroToken optToken)
Enabled: Set the token associated with this instance.

Parameters:
optToken - The new token (or null) to associate with this instance.

initialize

public void initialize(Token optToken)
Enabled: Initialize this instance with the given non-composite token.

Specified by:
initialize in interface AST
Specified by:
initialize in class BaseAST
Parameters:
optToken - :AstroToken the non-composite token to associate with this instance

initialize

public void initialize(int type,
                       String optText)
Enabled: Initialize this instance with the given token type and text. This will construct a new CommonToken with the given parameters and associate this instance with it.

Specified by:
initialize in interface AST
Specified by:
initialize in class BaseAST
Parameters:
type - the token type
optText - The token text, or null

initialize

public void initialize(AST ast)
Enabled: Initialize this instance based on the given AST. If the given AST is in fact an instance of AstroAST, then this instance will be initialized to point at the same token as the given one. If not, then this instance will be initialized with the same token type and text as the given one.

Specified by:
initialize in interface AST
Specified by:
initialize in class BaseAST
Parameters:
ast - non-null; the AST to base this instance on

setText

public void setText(String text)
Enabled: Set the token text for this node. If this instance is already associated with a token, then that token is destructively modified by this operation. If not, then a new token is constructed with the type Token.INVALID_TYPE and the given text.

Specified by:
setText in interface AST
Overrides:
setText in class BaseAST
Parameters:
text - the new token text

setType

public void setType(int type)
Enabled: Set the token type for this node. If this instance is already associated with a token, then that token is destructively modified by this operation. If not, then a new token is constructed with the given type and an empty ("", not null) text string.

Specified by:
setType in interface AST
Overrides:
setType in class BaseAST
Parameters:
type - the new token type

getTag

public AstroTag getTag()
Enabled:

Specified by:
getTag in interface Astro

getOptTagCode

public short getOptTagCode()
Enabled:

Specified by:
getOptTagCode in interface Astro

getOptData

public Object getOptData()
Enabled:

Specified by:
getOptData in interface Astro

getOptString

public String getOptString()
Enabled:

Specified by:
getOptString in interface Astro

getOptArgData

public Object getOptArgData()
Enabled:

Specified by:
getOptArgData in interface Astro

getOptArgData

public Object getOptArgData(short tagCode)
Enabled:

Specified by:
getOptArgData in interface Astro

getOptArgString

public String getOptArgString(short tagCode)
Enabled:

Specified by:
getOptArgString in interface Astro

getOptSpan

public SourceSpan getOptSpan()
Enabled:

Specified by:
getOptSpan in interface AstroArg

getArgs

public ConstList getArgs()
Enabled:

Specified by:
getArgs in interface Astro

withoutArgs

public Astro withoutArgs()
Enabled:

Specified by:
withoutArgs in interface Astro

withArgs

public Astro withArgs(ConstList args)
Enabled:

Specified by:
withArgs in interface Astro


comments?