A creator of AST node instances.

This class and it's sub-classes exists primarily as an optimization of the reflection-based mechanism(s) previously used exclusively to create instances of AST node objects.

Parsers and TreeParsers already use the ASTFactory class in ANTLR whenever they need to create an AST node objeect. What this class does is to support performant extensibility of the basic ASTFactory. The ASTFactory can now be extnded as run-time to support more new AST node types without using needing to use reflection.

Ejemplo n.º 1
0
        /// <summary>
        /// Register an ASTNodeCreator for a given Token type ID.
        /// </summary>
        /// <param name="NodeType">The Token type ID.</param>
        /// <param name="creator">The creater to register.</param>
        public void setTokenTypeASTNodeCreator(int NodeType, ASTNodeCreator creator)
        {
            // check validity of arguments...
            if (NodeType < Token.MIN_USER_TYPE)
            {
                throw new ANTLRException("Internal parser error: Cannot change AST Node Type for Token ID '" + NodeType + "'");
            }

            // resize up to and including 'type' and initialize any gaps to default
            // factory.
            if (NodeType > (heteroList_.Length + 1))
            {
                setMaxNodeType(NodeType);
            }
            // And add new thing..
            if (heteroList_[NodeType] == null)
            {
                heteroList_[NodeType] = new FactoryEntry(creator);
            }
            else
            {
                heteroList_[NodeType].Creator = creator;
            }

            //typename2creator_[NodeType.ToString()]		= creator;
            typename2creator_[creator.ASTNodeTypeName] = creator;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sets the global default AST Node Type for this ASTFactory instance.
 /// This method also attempts to load the <see cref="System.Type"/> instance
 /// for the specified typename.
 /// </summary>
 /// <param name="t">Fully qualified AST Node Type name.</param>
 public virtual void  setASTNodeType(string t)
 {
     if (defaultCreator_ != null)
     {
         if (t != defaultCreator_.ASTNodeTypeName)
         {
             defaultCreator_ = null;
         }
     }
     defaultASTNodeTypeObject_ = loadNodeTypeObject(t);
 }
Ejemplo n.º 3
0
        private AST createFromNodeName(string nodeTypeName)
        {
            AST newNode = null;

            ASTNodeCreator creator = (ASTNodeCreator)typename2creator_[nodeTypeName];

            if (creator != null)
            {
                newNode = creator.Create();
                if (newNode == null)
                {
                    throw new ArgumentException("Unable to create AST Node Type: '" + nodeTypeName + "'");
                }
            }
            else
            {
                newNode = createFromNodeTypeObject(loadNodeTypeObject(nodeTypeName));
            }
            return(newNode);
        }
Ejemplo n.º 4
0
        private AST createFromAST(AST node)
        {
            AST  newNode       = null;
            Type nodeAsTypeObj = node.GetType();

            ASTNodeCreator creator = (ASTNodeCreator)typename2creator_[nodeAsTypeObj.FullName];

            if (creator != null)
            {
                newNode = creator.Create();
                if (newNode == null)
                {
                    throw new ArgumentException("Unable to create AST Node Type: '" + nodeAsTypeObj.FullName + "'");
                }
            }
            else
            {
                newNode = createFromNodeTypeObject(nodeAsTypeObj);
            }
            return(newNode);
        }
Ejemplo n.º 5
0
			public FactoryEntry(ASTNodeCreator creator)
			{
				Creator			= creator;
			}
Ejemplo n.º 6
0
			public FactoryEntry(Type typeObj, ASTNodeCreator creator)
			{
				NodeTypeObject	= typeObj;
				Creator			= creator;
			}
Ejemplo n.º 7
0
		/// <summary>
		/// Sets the global default AST Node Type for this ASTFactory instance.
		/// This method also attempts to load the <see cref="System.Type"/> instance
		/// for the specified typename.
		/// </summary>
		/// <param name="t">Fully qualified AST Node Type name.</param>
		public virtual void  setASTNodeType(string t)
		{
			if (defaultCreator_ != null)
			{
				if (t != defaultCreator_.ASTNodeTypeName)
				{
					defaultCreator_ = null;
				}
			}
			defaultASTNodeTypeObject_ = loadNodeTypeObject(t);
		}
Ejemplo n.º 8
0
		/// <summary>
		/// Register an ASTNodeCreator to be used for creating node by default.
		/// </summary>
		/// <param name="creator">The ASTNodeCreator.</param>
		public void setASTNodeCreator(ASTNodeCreator creator)
		{
			defaultCreator_ = creator;
		}
Ejemplo n.º 9
0
		/// <summary>
		/// Register an ASTNodeCreator for a given Token type ID.
		/// </summary>
		/// <param name="NodeType">The Token type ID.</param>
		/// <param name="creator">The creater to register.</param>
		public void setTokenTypeASTNodeCreator(int NodeType, ASTNodeCreator creator)
		{
			// check validity of arguments...
			if( NodeType < Token.MIN_USER_TYPE )
				throw new ANTLRException("Internal parser error: Cannot change AST Node Type for Token ID '" + NodeType + "'");

			// resize up to and including 'type' and initialize any gaps to default
			// factory.
			if (NodeType > (heteroList_.Length+1))
				setMaxNodeType(NodeType);
			// And add new thing..
			if (heteroList_[NodeType] == null)
				heteroList_[NodeType] = new FactoryEntry(creator);
			else
				heteroList_[NodeType].Creator = creator;

			//typename2creator_[NodeType.ToString()]		= creator;
			typename2creator_[creator.ASTNodeTypeName]	= creator;
		}
Ejemplo n.º 10
0
            static SpringASTFactory()
            {
                BASENODE_TYPE = typeof (SpringAST);

                Typename2Creator = new Hashtable();
                foreach (Type type in typeof(SpringASTFactory).Assembly.GetTypes())
                {
                    if (BASENODE_TYPE.IsAssignableFrom(type))
                    {
                        ConstructorInfo ctor = type.GetConstructor(new Type[0]);
                        if (ctor != null)
                        {
                            ASTNodeCreator creator = new ASTNodeCreator(ctor);
                            Typename2Creator[creator.ASTNodeTypeName] = creator;
                        }
                    }
                }
                Typename2Creator[BASENODE_TYPE.FullName] = SpringAST.Creator;
            }
Ejemplo n.º 11
0
 public FactoryEntry(ASTNodeCreator creator)
 {
     Creator = creator;
 }
Ejemplo n.º 12
0
 public FactoryEntry(Type typeObj, ASTNodeCreator creator)
 {
     NodeTypeObject = typeObj;
     Creator        = creator;
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Register an ASTNodeCreator to be used for creating node by default.
 /// </summary>
 /// <param name="creator">The ASTNodeCreator.</param>
 public void setASTNodeCreator(ASTNodeCreator creator)
 {
     defaultCreator_ = creator;
 }