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 
33 namespace Antlr.Runtime
34 {
35     using NonSerialized = System.NonSerializedAttribute;
36     using Regex = System.Text.RegularExpressions.Regex;
37     using Serializable = System.SerializableAttribute;
38 
39     [Serializable]
40     public class CommonToken : IToken
41     {
42         int type;
43         int line;
44         int charPositionInLine = -1; // set to invalid position
45         int channel = TokenChannels.Default;
46         [NonSerialized]
47         ICharStream input;
48 
49         /** <summary>
50          *  We need to be able to change the text once in a while.  If
51          *  this is non-null, then getText should return this.  Note that
52          *  start/stop are not affected by changing this.
53          *  </summary>
54           */
55         string text;
56 
57         /** <summary>What token number is this from 0..n-1 tokens; &lt; 0 implies invalid index</summary> */
58         int index = -1;
59 
60         /** <summary>The char position into the input buffer where this token starts</summary> */
61         int start;
62 
63         /** <summary>The char position into the input buffer where this token stops</summary> */
64         int stop;
65 
CommonToken()66         public CommonToken()
67         {
68         }
69 
CommonToken( int type )70         public CommonToken( int type )
71         {
72             this.type = type;
73         }
74 
CommonToken( ICharStream input, int type, int channel, int start, int stop )75         public CommonToken( ICharStream input, int type, int channel, int start, int stop )
76         {
77             this.input = input;
78             this.type = type;
79             this.channel = channel;
80             this.start = start;
81             this.stop = stop;
82         }
83 
CommonToken( int type, string text )84         public CommonToken( int type, string text )
85         {
86             this.type = type;
87             this.channel = TokenChannels.Default;
88             this.text = text;
89         }
90 
CommonToken( IToken oldToken )91         public CommonToken( IToken oldToken )
92         {
93             text = oldToken.Text;
94             type = oldToken.Type;
95             line = oldToken.Line;
96             index = oldToken.TokenIndex;
97             charPositionInLine = oldToken.CharPositionInLine;
98             channel = oldToken.Channel;
99             input = oldToken.InputStream;
100             if ( oldToken is CommonToken )
101             {
102                 start = ( (CommonToken)oldToken ).start;
103                 stop = ( (CommonToken)oldToken ).stop;
104             }
105         }
106 
107         #region IToken Members
108         public string Text
109         {
110             get
111             {
112                 if ( text != null )
113                     return text;
114                 if ( input == null )
115                     return null;
116 
117                 if (start <= stop && stop < input.Count)
118                     return input.Substring( start, stop - start + 1 );
119 
120                 return "<EOF>";
121             }
122 
123             set
124             {
125                 /** Override the text for this token.  getText() will return this text
126                  *  rather than pulling from the buffer.  Note that this does not mean
127                  *  that start/stop indexes are not valid.  It means that that input
128                  *  was converted to a new string in the token object.
129                  */
130                 text = value;
131             }
132         }
133 
134         public int Type
135         {
136             get
137             {
138                 return type;
139             }
140             set
141             {
142                 type = value;
143             }
144         }
145 
146         public int Line
147         {
148             get
149             {
150                 return line;
151             }
152             set
153             {
154                 line = value;
155             }
156         }
157 
158         public int CharPositionInLine
159         {
160             get
161             {
162                 return charPositionInLine;
163             }
164             set
165             {
166                 charPositionInLine = value;
167             }
168         }
169 
170         public int Channel
171         {
172             get
173             {
174                 return channel;
175             }
176             set
177             {
178                 channel = value;
179             }
180         }
181 
182         public int StartIndex
183         {
184             get
185             {
186                 return start;
187             }
188             set
189             {
190                 start = value;
191             }
192         }
193 
194         public int StopIndex
195         {
196             get
197             {
198                 return stop;
199             }
200             set
201             {
202                 stop = value;
203             }
204         }
205 
206         public int TokenIndex
207         {
208             get
209             {
210                 return index;
211             }
212             set
213             {
214                 index = value;
215             }
216         }
217 
218         public ICharStream InputStream
219         {
220             get
221             {
222                 return input;
223             }
224             set
225             {
226                 input = value;
227             }
228         }
229 
230         #endregion
231 
ToString()232         public override string ToString()
233         {
234             string channelStr = "";
235             if ( channel > 0 )
236             {
237                 channelStr = ",channel=" + channel;
238             }
239             string txt = Text;
240             if ( txt != null )
241             {
242                 txt = Regex.Replace( txt, "\n", "\\\\n" );
243                 txt = Regex.Replace( txt, "\r", "\\\\r" );
244                 txt = Regex.Replace( txt, "\t", "\\\\t" );
245             }
246             else
247             {
248                 txt = "<no text>";
249             }
250             return "[@" + TokenIndex + "," + start + ":" + stop + "='" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + "]";
251         }
252 
253         [System.Runtime.Serialization.OnSerializing]
OnSerializing( System.Runtime.Serialization.StreamingContext context )254         internal void OnSerializing( System.Runtime.Serialization.StreamingContext context )
255         {
256             if ( text == null )
257                 text = Text;
258         }
259     }
260 }
261