• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1org.antlr.runtime.CommonToken = function() {
2    var oldToken;
3
4    this.charPositionInLine = -1; // set to invalid position
5    this.channel = 0; // org.antlr.runtime.CommonToken.DEFAULT_CHANNEL
6    this.index = -1;
7
8    if (arguments.length == 1) {
9        if (org.antlr.lang.isNumber(arguments[0])) {
10            this.type = arguments[0];
11        } else {
12            oldToken = arguments[0];
13            this.text = oldToken.getText();
14            this.type = oldToken.getType();
15            this.line = oldToken.getLine();
16            this.index = oldToken.getTokenIndex();
17            this.charPositionInLine = oldToken.getCharPositionInLine();
18            this.channel = oldToken.getChannel();
19            if ( oldToken instanceof org.antlr.runtime.CommonToken ) {
20                this.start = oldToken.start;
21                this.stop = oldToken.stop;
22            }
23        }
24    } else if (arguments.length == 2) {
25        this.type = arguments[0];
26        this.text = arguments[1];
27        this.channel = 0; // org.antlr.runtime.CommonToken.DEFAULT_CHANNEL
28    } else if (arguments.length == 5) {
29        this.input = arguments[0];
30        this.type = arguments[1];
31        this.channel = arguments[2];
32        this.start = arguments[3];
33        this.stop = arguments[4];
34    }
35};
36
37org.antlr.lang.extend(org.antlr.runtime.CommonToken,
38                      org.antlr.runtime.Token,
39{
40    getType: function() {
41        return this.type;
42    },
43
44    setLine: function(line) {
45        this.line = line;
46    },
47
48    getText: function() {
49        if ( org.antlr.lang.isString(this.text) ) {
50            return this.text;
51        }
52        if ( !this.input ) {
53            return null;
54        }
55        this.text = this.input.substring(this.start,this.stop);
56        return this.text;
57    },
58
59    /** Override the text for this token.  getText() will return this text
60     *  rather than pulling from the buffer.  Note that this does not mean
61     *  that start/stop indexes are not valid.  It means that that input
62     *  was converted to a new string in the token object.
63     */
64    setText: function(text) {
65        this.text = text;
66    },
67
68    getLine: function() {
69        return this.line;
70    },
71
72    getCharPositionInLine: function() {
73        return this.charPositionInLine;
74    },
75
76    setCharPositionInLine: function(charPositionInLine) {
77        this.charPositionInLine = charPositionInLine;
78    },
79
80    getChannel: function() {
81        return this.channel;
82    },
83
84    setChannel: function(channel) {
85        this.channel = channel;
86    },
87
88    setType: function(type) {
89        this.type = type;
90    },
91
92    getStartIndex: function() {
93        return this.start;
94    },
95
96    setStartIndex: function(start) {
97        this.start = start;
98    },
99
100    getStopIndex: function() {
101        return this.stop;
102    },
103
104    setStopIndex: function(stop) {
105        this.stop = stop;
106    },
107
108    getTokenIndex: function() {
109        return this.index;
110    },
111
112    setTokenIndex: function(index) {
113        this.index = index;
114    },
115
116    getInputStream: function() {
117        return this.input;
118    },
119
120    setInputStream: function(input) {
121        this.input = input;
122    },
123
124    toString: function() {
125        var channelStr = "";
126        if ( this.channel>0 ) {
127            channelStr=",channel="+this.channel;
128        }
129        var txt = this.getText();
130        if ( !org.antlr.lang.isNull(txt) ) {
131            txt = txt.replace(/\n/g,"\\\\n");
132            txt = txt.replace(/\r/g,"\\\\r");
133            txt = txt.replace(/\t/g,"\\\\t");
134        }
135        else {
136            txt = "<no text>";
137        }
138        return "[@"+this.getTokenIndex()+","+this.start+":"+this.stop+"='"+txt+"',<"+this.type+">"+channelStr+","+this.line+":"+this.getCharPositionInLine()+"]";
139    }
140});
141
142/* Monkey patch Token static vars that depend on CommonToken. */
143org.antlr.lang.augmentObject(org.antlr.runtime.Token, {
144    EOF_TOKEN: new org.antlr.runtime.CommonToken(org.antlr.runtime.CharStream.EOF),
145    INVALID_TOKEN: new org.antlr.runtime.CommonToken(0),
146    SKIP_TOKEN: new org.antlr.runtime.CommonToken(0)
147}, true);
148