KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > syntax > misc > ATEToken


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.ate.syntax.misc;
33
34 public class ATEToken implements Comparable JavaDoc {
35
36     public int type;
37
38     public int startLineNumber; // starting line number
39
public int endLineNumber; // ending line number
40
public int startLineIndex; // starting line character index
41
public int endLineIndex; // ending line character index
42
protected String JavaDoc text; // the text containing this token
43

44     /** These two fiels are public because ATEPanel needs to access them
45      * really quickly without using accessors. If anything needs to be changed,
46      * modify the adjustTokens() method of ATEPanel.
47      */

48     public int start;
49     public int end;
50
51     protected String JavaDoc attribute; // the portion of text covered by this token (subset of text)
52

53     public int index; // index inside the tokens list
54
public boolean modified; // true if the token has been modified in the text editor
55
public ATEScope scope;
56
57     public ATEToken(int type, int start, int end,
58                     int startLineNumber, int endLineNumber,
59                     int startLineIndex, int endLineIndex,
60                     String JavaDoc text)
61     {
62         this.type = type;
63
64         this.start = start;
65         this.end = end;
66
67         this.startLineNumber = startLineNumber;
68         this.endLineNumber = endLineNumber;
69         this.startLineIndex = startLineIndex;
70         this.endLineIndex = endLineIndex;
71
72         this.text = text;
73         this.attribute = text.substring(start, end);
74     }
75
76     public String JavaDoc getText() {
77         return text;
78     }
79
80     public String JavaDoc getAttribute() {
81         return attribute;
82     }
83
84     public int getStartLineIndex() {
85         return startLineIndex;
86     }
87
88     public int getStartIndex() {
89         return start;
90     }
91
92     public int getEndIndex() {
93         return end;
94     }
95
96     public boolean containsIndex(int index) {
97         return index >= getStartIndex() && index <= getEndIndex();
98     }
99
100     public boolean equals(Object JavaDoc otherObject) {
101         if(otherObject == null) {
102             return false;
103         }
104
105         if(otherObject instanceof ATEToken) {
106             ATEToken otherToken = (ATEToken)otherObject;
107             return type == otherToken.type &&
108                     start == otherToken.start &&
109                     end == otherToken.end;
110         } else {
111             return false;
112         }
113     }
114
115     public int compareTo(Object JavaDoc o) {
116         if(o instanceof ATEToken) {
117             ATEToken otherToken = (ATEToken)o;
118             return this.getAttribute().compareTo(otherToken.getAttribute());
119         } else {
120             return 1;
121         }
122     }
123
124     public String JavaDoc toString() {
125         return getAttribute()+" <type="+type+", start="+start+", end="+end+">";
126     }
127
128     public static boolean isLexerName(String JavaDoc name) {
129         if(name == null || name.length() < 1)
130             return false;
131         else
132             return name.charAt(0) == name.toUpperCase().charAt(0);
133     }
134 }
135
Popular Tags