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