ClassicToken.cs revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2005-2008 Terence Parr
4 * All rights reserved.
5 *
6 * Conversion to C#:
7 * Copyright (c) 2008-2009 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
34{
35    /** <summary>
36     *  A Token object like we'd use in ANTLR 2.x; has an actual string created
37     *  and associated with this object.  These objects are needed for imaginary
38     *  tree nodes that have payload objects.  We need to create a Token object
39     *  that has a string; the tree node will point at this token.  CommonToken
40     *  has indexes into a char stream and hence cannot be used to introduce
41     *  new strings.
42     *  </summary>
43     */
44    [System.Serializable]
45    public class ClassicToken : IToken
46    {
47        string text;
48        int type;
49        int line;
50        int charPositionInLine;
51        int channel = TokenChannels.Default;
52
53        /** <summary>What token number is this from 0..n-1 tokens</summary> */
54        int index;
55
56        public ClassicToken( int type )
57        {
58            this.type = type;
59        }
60
61        public ClassicToken( IToken oldToken )
62        {
63            text = oldToken.Text;
64            type = oldToken.Type;
65            line = oldToken.Line;
66            charPositionInLine = oldToken.CharPositionInLine;
67            channel = oldToken.Channel;
68        }
69
70        public ClassicToken( int type, string text )
71        {
72            this.type = type;
73            this.text = text;
74        }
75
76        public ClassicToken( int type, string text, int channel )
77        {
78            this.type = type;
79            this.text = text;
80            this.channel = channel;
81        }
82
83        #region IToken Members
84        public string Text
85        {
86            get
87            {
88                return text;
89            }
90            set
91            {
92                text = value;
93            }
94        }
95
96        public int Type
97        {
98            get
99            {
100                return type;
101            }
102            set
103            {
104                type = value;
105            }
106        }
107
108        public int Line
109        {
110            get
111            {
112                return line;
113            }
114            set
115            {
116                line = value;
117            }
118        }
119
120        public int CharPositionInLine
121        {
122            get
123            {
124                return charPositionInLine;
125            }
126            set
127            {
128                charPositionInLine = value;
129            }
130        }
131
132        public int Channel
133        {
134            get
135            {
136                return channel;
137            }
138            set
139            {
140                channel = value;
141            }
142        }
143
144        public int StartIndex
145        {
146            get
147            {
148                return -1;
149            }
150            set
151            {
152            }
153        }
154
155        public int StopIndex
156        {
157            get
158            {
159                return -1;
160            }
161            set
162            {
163            }
164        }
165
166        public int TokenIndex
167        {
168            get
169            {
170                return index;
171            }
172            set
173            {
174                index = value;
175            }
176        }
177
178        public ICharStream InputStream
179        {
180            get
181            {
182                return null;
183            }
184            set
185            {
186            }
187        }
188
189        #endregion
190
191        public override string ToString()
192        {
193            string channelStr = "";
194            if ( channel > 0 )
195            {
196                channelStr = ",channel=" + channel;
197            }
198            string txt = Text;
199            if ( txt != null )
200            {
201                txt = txt.Replace( "\n", "\\\\n" );
202                txt = txt.Replace( "\r", "\\\\r" );
203                txt = txt.Replace( "\t", "\\\\t" );
204            }
205            else
206            {
207                txt = "<no text>";
208            }
209            return "[@" + TokenIndex + ",'" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + "]";
210        }
211    }
212}
213