| java.lang.Object xtc.type.AST
All known Subclasses: xtc.type.JavaAST,
AST | abstract public class AST (Code) | | Common type operations for Rats! ASTs.
This class supports two views on a grammar's generic AST. The
first view is dynamically typed, with all generic AST nodes
represented by the canonical node type. The second view is
statically typed, with all generic AST nodes represented by tuples
organized into variants. Either way, this class supports the
following types:
- The void type represented by
VoidT .
- The unit type represented by
UnitT .
- Characters represented by an
InternalT with name
"char".
- Strings represented by an
InternalT with name
"string".
- Tokens represented by an
InternalT with name
"token".
- Dynamically typed nodes represented by an
InternalT with name "node".
- Statically typed nodes represented by a
VariantT with
TupleT elements.
- Parse tree annotations represented by an
InternalT with name "formatting".
- Lists represented by an
InternalT with name
"list".
- Actions represented by an
InternalT with name
"action".
All node types have the
Constants.ATT_NODE attribute; the
dynamically typed node representing generic productions also has
the
Constants.ATT_GENERIC attribute.
In addition to generic ASTs, this class also supports
user-defined types, which must be represented by types that are not
listed above.
Concrete subclasses specify the mapping between strings and
types. For mapping internal representations back to strings, the
void type has name "void", the unit type has name "unit", and the
wildcard has name "?".
author: Robert Grimm version: $Revision: 1.36 $ |
Inner Class :public static class MetaData | |
Constructor Summary | |
public | AST() Create a new instance. |
Method Summary | |
public static Type | actionOf(Type element) Create a new action type.
Parameters: element - The element type. | public void | add(TupleT tuple, VariantT variant) Add the specified tuple type to the specified variant type. | public Type | combine(TupleT tuple1, TupleT tuple2, boolean flatten, boolean strict) Combine the specified tuple types into a consistent type. | public Type | concretize(Type type, Type concrete) Ensure that the specified type is concrete. | public void | concretizeTuples(VariantT variant, Type concrete) Concretize the specified variant type's tuples. | public String | extern(Type type) Convert the specified type to a string. | abstract protected String | externAction(Type type) Convert the specified action type to a string.
Parameters: type - The action type. | abstract protected String | externList(Type type) Convert the specified list type to a string.
Parameters: type - The list type. | abstract protected String | externUser(Type type) Convert the specified user-defined type to a string.
Parameters: type - The user-defined type. | public Type | flatten(TupleT tuple, boolean strict) Flatten the specified tuple type. | public static Type | getArgument(Type type) Get the specified instantiated type's only argument.
Parameters: type - The instantiated type. | public MetaData | getMetaData(VariantT variant) Determine the metadata for the specified variant.
Parameters: variant - The variant. | public FuzzyBoolean | hasLocation(Type type) Determine whether instances of the specified type have a source
location. | abstract protected FuzzyBoolean | hasLocationUser(Type type) Determine whether instances of the specified user-defined type
have a source location.
Parameters: type - The type. | public boolean | hasTuple(String name) Determine whether a tuple type with the specified name has been
created before.
Parameters: name - The name. | public boolean | hasVariant(String name) Determine whether a variant type with the specified name has been
created before. | public void | importModule(String module) Import the specified module. | public void | importType(String qualified, String simple) Import the specified type. | abstract public void | initialize(boolean hasNode, boolean hasToken, boolean hasFormatting, boolean hasAction) Initialize the mapping between external and internal
representations. | public Type | intern(String s) Convert the specified string representation of a type into the
type. | abstract protected Type | internAction(String s) Convert the specified string representation of an action type
into the type.
Parameters: s - The action type as a string. | abstract protected Type | internList(String s) Convert the specified string representation of a list type into
the type.
Parameters: s - The list type as a string. | abstract protected Type | internUser(String s) Convert the specified string representation of a user-defined
type into its internal representation.
Parameters: s - The user-defined type as a string. | public static boolean | isAction(Type type) Determine whether the specified type is an action.
Parameters: type - The type. | public static boolean | isAny(Type type) Determine whether the specified type is the any type.
Parameters: type - The type. | public static boolean | isChar(Type type) Determine whether the specified type is a character.
Parameters: type - The type. | public static boolean | isDynamicNode(Type type) Determine whether the specified type is a dynamically typed node.
Parameters: type - The type. | public static boolean | isFormatting(Type type) Determine whether the specified type is a formatting node.
Parameters: type - The type. | abstract public boolean | isGenericNode(String s) Determine whether the specified string represents the generic
node type.
Parameters: s - The type as a string. | public static boolean | isGenericNode(Type type) Determine whether the specified type is a generic node.
Parameters: type - The type. | public static boolean | isList(Type type) Determine whether the specified type is a list.
Parameters: type - The type. | public boolean | isMonomorphic(String name) Determine whether the tuple type with the specified name has been
created before and is monomorphic.
Parameters: name - The name. | public static boolean | isNode(Type type) Determine whether the specified type is a node.
Parameters: type - The type. | public static boolean | isNullNode(Type type) Determine whether the specified type is a null node.
Parameters: type - The type. | public static boolean | isOptional(Type type) Determine whether the specified type is optional.
Parameters: type - The type. | public static boolean | isStaticNode(Type type) Determine whether the specified type is a statically typed node.
Parameters: type - The type. | public static boolean | isString(Type type) Determine whether the specified type is a string.
Parameters: type - The type. | public static boolean | isToken(Type type) Determine whether the specified type is a token.
Parameters: type - The type. | public static boolean | isUser(Type type) Determine whether the specified type is user-defined.
Parameters: type - The type. | public static boolean | isVariable(Type type) Determine whether the specified type is variable.
Parameters: type - The type. | abstract public boolean | isVoid(String s) Determine whether the specified string represents the void type.
Parameters: s - The type as a string. | public static boolean | isVoid(Type type) Determine whether the specified type is the void type.
Parameters: type - The type. | public static Type | listOf(Type element) Create a new list type.
Parameters: element - The element type. | public static Type | markOptional(Type type) Mark the specified type as optional.
Parameters: type - The type. | public static Type | markVariable(Type type) Mark the specified type as variable.
Parameters: type - The type. | public boolean | overlap(VariantT v1, VariantT v2) Determine whether the specified variants overlap. | public void | print(Type type, Printer printer, boolean refIsDecl, boolean qualified, String module) Print the specified type. | public String | toOriginal(VariantT variant) Get the original name for the specified variant.
The specified variant must have been created with
AST.toVariant(String,boolean) .
Parameters: variant - The variant. | public TupleT | toTuple(String name) Get the tuple type with the specified name. | public TupleT | toTuple(VariantT variant) Get the polymorphic tuple for the specified variant.
The specified variant must have been created with
AST.toVariant(String,boolean) .
Parameters: variant - The variant. | public VariantT | toVariant(String name, boolean poly) Get the variant type with the specified name. | public String | toVariantName(String name) Convert the specified production's name into a variant name.
This method converts the name from Rats!' camel case
naming convention into ML's lower case with underscores naming
convention. | public List<VariantT> | toVariants(TupleT tuple) Get the specified tuple's variants.
The specified tuple must have been created with
AST.toTuple(String) or
AST.toTuple(VariantT) . | public Type | unify(Type t1, Type t2, boolean strict) Unify the specified types. | protected Type | unify(VariantT v1, VariantT v2) Unify the specified statically typed nodes. | abstract protected Type | unifyUser(Type t1, Type t2, boolean strict) Unify the specified user-defined types. |
ACTION | final public static Type ACTION(Code) | | The canonical parameterized action type.
|
ANY | final public static Type ANY(Code) | | The canonical any type.
|
CHAR | final public static Type CHAR(Code) | | The canonical character reference type.
|
FORMATTING | final public static Type FORMATTING(Code) | | The canonical formatting node type.
|
GENERIC | final public static Type GENERIC(Code) | | The canonical dynamically typed generic node type.
|
INTERNAL | final public static Set<String> INTERNAL(Code) | | The set of internal type names.
|
LIST | final public static Type LIST(Code) | | The canonical parameterized list type.
|
NODE | final public static Type NODE(Code) | | The canonical dynamically typed node type.
|
NULL_NODE | final public static Type NULL_NODE(Code) | | The canonical null node type.
|
STRING | final public static Type STRING(Code) | | The canonical string type.
|
TOKEN | final public static Type TOKEN(Code) | | The canonical token type.
|
WILD_ACTION | final public static Type WILD_ACTION(Code) | | The canonical action instantiated with a wildcard element type.
|
WILD_LIST | final public static Type WILD_LIST(Code) | | The canonical list instantiated with a wildcard element type.
|
externToIntern | final protected Map<String, Type> externToIntern(Code) | | The map from strings to type representations.
|
importedModules | final protected List<String> importedModules(Code) | | The list of imported module names. Each module name should end
with the separator necessary for creating a fully qualified type
name by appending a simple name.
|
importedTypes | final protected Map<String, String> importedTypes(Code) | | The map from simple type names to fully qualified type names.
|
internToExtern | final protected Map<String, String> internToExtern(Code) | | The map from internal type names to external types. Valid type
names are void, unit, any, char, string, token, node, formatting,
list, and action as well as ? for wildcards.
|
originalNames | final protected Map<String, String> originalNames(Code) | | The map from unqualified variant names to original names.
|
tupleVariants | final protected Map<String, List<VariantT>> tupleVariants(Code) | | The map from tuple names to variants containing the tuples.
|
actionOf | public static Type actionOf(Type element)(Code) | | Create a new action type.
Parameters: element - The element type. The corresponding action type. |
add | public void add(TupleT tuple, VariantT variant)(Code) | | Add the specified tuple type to the specified variant type. If
the tuple is not a member of the specified variant, this method
adds it, while also updating its internal state.
The specified tuple must have been created with
AST.toTuple(String) or
AST.toTuple(VariantT) . The specified
variant must have been created with
AST.toVariant(String,boolean) .
Parameters: tuple - The tuple. Parameters: variant - The variant. |
combine | public Type combine(TupleT tuple1, TupleT tuple2, boolean flatten, boolean strict)(Code) | | Combine the specified tuple types into a consistent type. The
types must be tuples with the same name.
Parameters: tuple1 - The first tuple. Parameters: tuple2 - The second tuple. Parameters: flatten - The flag for flattening lists. Parameters: strict - The flag for strict unification. The combined tuple type or ErrorT.TYPE if the twotuple types cannot be combined into a consistent type. |
concretize | public Type concretize(Type type, Type concrete)(Code) | | Ensure that the specified type is concrete. This method replaces
occurrences of the wildcard type with the specified replacement;
though it does not process variant types to avoid infinite
recursions. It assumes that list and action types are
instantiated.
See Also: AST.concretizeTuples(VariantT,Type) Parameters: type - The type. Parameters: concrete - The concrete replacement for wildcards. The concrete type. |
concretizeTuples | public void concretizeTuples(VariantT variant, Type concrete)(Code) | | Concretize the specified variant type's tuples. This method
updates any tuples in place.
See Also: AST.concretize(Type,Type) Parameters: variant - The variant. Parameters: concrete - The concrete replacement for wildcards. |
externAction | abstract protected String externAction(Type type)(Code) | | Convert the specified action type to a string.
Parameters: type - The action type. The type as a string. |
externList | abstract protected String externList(Type type)(Code) | | Convert the specified list type to a string.
Parameters: type - The list type. The type as a string. |
externUser | abstract protected String externUser(Type type)(Code) | | Convert the specified user-defined type to a string.
Parameters: type - The user-defined type. The type as a string. |
flatten | public Type flatten(TupleT tuple, boolean strict)(Code) | | Flatten the specified tuple type. If the specified tuple has a
list element, this method combines the first such list type with
all succeeding element types into a single list type, modifying
the specified tuple type.
Parameters: tuple - The tuple type. Parameters: strict - The flag for strict unification. The updated tuple type or ErrorT.TYPE if typescannot be unified. |
getArgument | public static Type getArgument(Type type)(Code) | | Get the specified instantiated type's only argument.
Parameters: type - The instantiated type. The argument type. |
getMetaData | public MetaData getMetaData(VariantT variant)(Code) | | Determine the metadata for the specified variant.
Parameters: variant - The variant. The corresponding metadata. |
hasLocation | public FuzzyBoolean hasLocation(Type type)(Code) | | Determine whether instances of the specified type have a source
location. This method defers to
AST.hasLocationUser(Type) for user-defined types.
Parameters: type - The type. The inexact answer. |
hasLocationUser | abstract protected FuzzyBoolean hasLocationUser(Type type)(Code) | | Determine whether instances of the specified user-defined type
have a source location.
Parameters: type - The type. The inexact answer. |
hasTuple | public boolean hasTuple(String name)(Code) | | Determine whether a tuple type with the specified name has been
created before.
Parameters: name - The name. true if a tuple type with the name exists. |
hasVariant | public boolean hasVariant(String name)(Code) | | Determine whether a variant type with the specified name has been
created before. This method first converts the name from
Rats!' camel case naming convention into ML's lower case
with underscores naming convention. It then checks whether a
variant type with that name has been returned by
AST.toVariant(String,boolean) before.
See Also: AST.toVariantName(String) Parameters: name - The name in camel case. true if a variant type with the name exists. |
importModule | public void importModule(String module)(Code) | | Import the specified module. This method adds the module to the
list of imported modules
AST.importedModules . The specified
module name must end with the appropriate separator.
Parameters: module - The module name. |
importType | public void importType(String qualified, String simple)(Code) | | Import the specified type. This method adds a mapping from the
specified simple type name to the specified qualified type name
to the imported types
AST.importedTypes .
Parameters: qualified - The fully qualified name. Parameters: simple - The simple name. |
initialize | abstract public void initialize(boolean hasNode, boolean hasToken, boolean hasFormatting, boolean hasAction)(Code) | | Initialize the mapping between external and internal
representations. This method fills the
AST.externToIntern and
AST.internToExtern data structures.
Parameters: hasNode - Flag to indicate use of built-in nodes. Parameters: hasToken - Flag to indicate use of tokens. Parameters: hasFormatting - Flag to indicate use of formatting. Parameters: hasAction - Flag to indicate use of actions. |
internAction | abstract protected Type internAction(String s)(Code) | | Convert the specified string representation of an action type
into the type.
Parameters: s - The action type as a string. The type or ErrorT.TYPE if the string does notrepresent an action. |
internList | abstract protected Type internList(String s)(Code) | | Convert the specified string representation of a list type into
the type.
Parameters: s - The list type as a string. The type or ErrorT.TYPE if the string does notrepresent a list. |
internUser | abstract protected Type internUser(String s)(Code) | | Convert the specified string representation of a user-defined
type into its internal representation.
Parameters: s - The user-defined type as a string. The type. |
isAction | public static boolean isAction(Type type)(Code) | | Determine whether the specified type is an action.
Parameters: type - The type. true if the type is an action. |
isAny | public static boolean isAny(Type type)(Code) | | Determine whether the specified type is the any type.
Parameters: type - The type. true if the type is the any type. |
isChar | public static boolean isChar(Type type)(Code) | | Determine whether the specified type is a character.
Parameters: type - The type. true if the type is a character. |
isDynamicNode | public static boolean isDynamicNode(Type type)(Code) | | Determine whether the specified type is a dynamically typed node.
Parameters: type - The type. true if the type is a dynamically typednode. |
isFormatting | public static boolean isFormatting(Type type)(Code) | | Determine whether the specified type is a formatting node.
Parameters: type - The type. true if the type is a formatting node. |
isGenericNode | abstract public boolean isGenericNode(String s)(Code) | | Determine whether the specified string represents the generic
node type.
Parameters: s - The type as a string. true if the string represents the genericnode type. |
isGenericNode | public static boolean isGenericNode(Type type)(Code) | | Determine whether the specified type is a generic node.
Parameters: type - The type. true if the type is a generic node. |
isList | public static boolean isList(Type type)(Code) | | Determine whether the specified type is a list.
Parameters: type - The type. true if the type is a list. |
isMonomorphic | public boolean isMonomorphic(String name)(Code) | | Determine whether the tuple type with the specified name has been
created before and is monomorphic.
Parameters: name - The name. true if a tuple type with the name existsand is monomorphic. |
isNode | public static boolean isNode(Type type)(Code) | | Determine whether the specified type is a node.
Parameters: type - The type. true if the type is a node. |
isNullNode | public static boolean isNullNode(Type type)(Code) | | Determine whether the specified type is a null node.
Parameters: type - The type. true if the type is a null node. |
isOptional | public static boolean isOptional(Type type)(Code) | | Determine whether the specified type is optional.
Parameters: type - The type. true if the specified type is optional. |
isStaticNode | public static boolean isStaticNode(Type type)(Code) | | Determine whether the specified type is a statically typed node.
Parameters: type - The type. true if the type is a statically typed node. |
isString | public static boolean isString(Type type)(Code) | | Determine whether the specified type is a string.
Parameters: type - The type. true if the type is a string. |
isToken | public static boolean isToken(Type type)(Code) | | Determine whether the specified type is a token.
Parameters: type - The type. true if the type is a token. |
isUser | public static boolean isUser(Type type)(Code) | | Determine whether the specified type is user-defined.
Parameters: type - The type. true if the type is a user-defined type. |
isVariable | public static boolean isVariable(Type type)(Code) | | Determine whether the specified type is variable.
Parameters: type - The type. true if the specified type is variable. |
isVoid | abstract public boolean isVoid(String s)(Code) | | Determine whether the specified string represents the void type.
Parameters: s - The type as a string. true if the string represents the void type. |
isVoid | public static boolean isVoid(Type type)(Code) | | Determine whether the specified type is the void type.
Parameters: type - The type. true if the type is the void type. |
listOf | public static Type listOf(Type element)(Code) | | Create a new list type.
Parameters: element - The element type. The corresponding list type. |
markOptional | public static Type markOptional(Type type)(Code) | | Mark the specified type as optional.
Parameters: type - The type. The optional type. |
markVariable | public static Type markVariable(Type type)(Code) | | Mark the specified type as variable.
Parameters: type - The type. The variable type. |
overlap | public boolean overlap(VariantT v1, VariantT v2)(Code) | | Determine whether the specified variants overlap. Two variants
overlap if they include tuples representing the same generic
node.
The specified variants must have been created with
AST.toVariant(String,boolean) .
Parameters: v1 - The first variant. Parameters: v2 - The second variant. true if the two variants overlap. |
print | public void print(Type type, Printer printer, boolean refIsDecl, boolean qualified, String module)(Code) | | Print the specified type.
Parameters: printer - The printer. Parameters: type - The type. Parameters: refIsDecl - The flag for whether a variant type referencealso is a declaration. Parameters: qualified - The flag for printing qualified names. Parameters: module - The current module name, which may be null . |
toOriginal | public String toOriginal(VariantT variant)(Code) | | Get the original name for the specified variant.
The specified variant must have been created with
AST.toVariant(String,boolean) .
Parameters: variant - The variant. The original name in Rats!' camel case. |
toTuple | public TupleT toTuple(String name)(Code) | | Get the tuple type with the specified name. If this method has
not been invoked on the specified name before, it returns a new
tuple type, which is incomplete. Otherwise, it simply returns
the previoulsy created tuple type.
Parameters: name - The name. The corresponding tuple type. |
toTuple | public TupleT toTuple(VariantT variant)(Code) | | Get the polymorphic tuple for the specified variant.
The specified variant must have been created with
AST.toVariant(String,boolean) .
Parameters: variant - The variant. The corresponding polymorphic tuple. |
toVariant | public VariantT toVariant(String name, boolean poly)(Code) | | Get the variant type with the specified name. This method first
converts the name from Rats!' camel case naming
convention into ML's lower case with underscores naming
convention. Then, if this method has not been invoked on the
specified name before, it returns a new variant type with an
empty list of tuples. Otherwise, it simply returns the
previously created variant type. The returned variant type has
the
Constants.ATT_NODE attribute.
See Also: AST.toVariantName(String) Parameters: name - The name in camel case. Parameters: poly - The flag for whether the variant is polymorphic. The corresponding variant type. |
toVariantName | public String toVariantName(String name)(Code) | | Convert the specified production's name into a variant name.
This method converts the name from Rats!' camel case
naming convention into ML's lower case with underscores naming
convention. It preserves any qualifier.
Parameters: name - The production's name. The corresponding variant name. |
unify | public Type unify(Type t1, Type t2, boolean strict)(Code) | | Unify the specified types. If the strict flag is set, statically
and dynamically typed nodes do not unify. If the flag is not
set, they do unify and otherwise incompatible types unify to the
any type. This method defers to
AST.unify(VariantT,VariantT) for statically typed nodes and to
AST.unifyUser(Type,Type,boolean) for user-defined types.
Parameters: t1 - The first type. Parameters: t2 - The second type. Parameters: strict - The flag for strict unification. The unified type or ErrorT.TYPE if the two typesdo not unify. |
unify | protected Type unify(VariantT v1, VariantT v2)(Code) | | Unify the specified statically typed nodes. Statically typed
nodes unify through polymorphic variant types, unless they
reference the same underlying AST node.
Parameters: v1 - The first variant. Parameters: v2 - The second variant. The unified variant. |
unifyUser | abstract protected Type unifyUser(Type t1, Type t2, boolean strict)(Code) | | Unify the specified user-defined types. Note that this method
need not handle instantiated types but must preserve parameterize
types.
Parameters: t1 - The first user-defined type. Parameters: t2 - The second user-defined type. Parameters: strict - The flag for strict unification. The unified type or ErrorT.TYPE if the two typesdo not unify. |
|
|