1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2011 Terence Parr
4 * All rights reserved.
5 *
6 * Conversion to C#:
7 * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33namespace Antlr.Runtime.Tree
34{
35
36    /** <summary>
37     *  A TreeAdaptor that works with any Tree implementation.  It provides
38     *  really just factory methods; all the work is done by BaseTreeAdaptor.
39     *  If you would like to have different tokens created than ClassicToken
40     *  objects, you need to override this and then set the parser tree adaptor to
41     *  use your subclass.
42     *  </summary>
43     *
44     *  <remarks>
45     *  To get your parser to build nodes of a different type, override
46     *  create(Token), errorNode(), and to be safe, YourTreeClass.dupNode().
47     *  dupNode is called to duplicate nodes during rewrite operations.
48     *  </remarks>
49     */
50    public class CommonTreeAdaptor : BaseTreeAdaptor
51    {
52        public override object Create( IToken payload )
53        {
54            return new CommonTree( payload );
55        }
56
57        /** <summary>
58         *  Tell me how to create a token for use with imaginary token nodes.
59         *  For example, there is probably no input symbol associated with imaginary
60         *  token DECL, but you need to create it as a payload or whatever for
61         *  the DECL node as in ^(DECL type ID).
62         *  </summary>
63         *
64         *  <remarks>
65         *  If you care what the token payload objects' type is, you should
66         *  override this method and any other createToken variant.
67         *  </remarks>
68         */
69        public override IToken CreateToken( int tokenType, string text )
70        {
71            return new CommonToken( tokenType, text );
72        }
73
74        /** <summary>
75         *  Tell me how to create a token for use with imaginary token nodes.
76         *  For example, there is probably no input symbol associated with imaginary
77         *  token DECL, but you need to create it as a payload or whatever for
78         *  the DECL node as in ^(DECL type ID).
79         *  </summary>
80         *
81         *  <remarks>
82         *  This is a variant of createToken where the new token is derived from
83         *  an actual real input token.  Typically this is for converting '{'
84         *  tokens to BLOCK etc...  You'll see
85         *
86         *    r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;
87         *
88         *  If you care what the token payload objects' type is, you should
89         *  override this method and any other createToken variant.
90         *  </remarks>
91         */
92        public override IToken CreateToken( IToken fromToken )
93        {
94            return new CommonToken( fromToken );
95        }
96
97        /** <summary>
98         *  What is the Token associated with this node?  If
99         *  you are not using CommonTree, then you must
100         *  override this in your own adaptor.
101         *  </summary>
102         */
103        public override IToken GetToken( object t )
104        {
105            if ( t is CommonTree )
106            {
107                return ( (CommonTree)t ).Token;
108            }
109            return null; // no idea what to do
110        }
111    }
112}
113