KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ObjCTagger


1 /*
2  * CTagger.java
3  *
4  * Copyright (C) 2003 Peter Graves
5  * $Id: ObjCTagger.java,v 1.2 2003/12/30 19:25:17 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.UncheckedRE;
26 import java.util.ArrayList JavaDoc;
27
28 public final class ObjCTagger extends JavaTagger
29 {
30     // States.
31
private static final int NEUTRAL = 0;
32     private static final int METHOD_PROLOG = 1;
33     private static final int METHOD_NAME = 2;
34     private static final int PARAMETER_LIST = 3;
35
36     private Mode mode = ObjCMode.getMode();
37
38     public ObjCTagger(SystemBuffer buffer)
39     {
40         super(buffer);
41     }
42
43     public void run()
44     {
45         ArrayList JavaDoc tags = new ArrayList JavaDoc();
46         pos = new Position(buffer.getFirstLine(), 0);
47         token = null;
48         tokenStart = null;
49         int state = NEUTRAL;
50         while (!pos.atEnd()) {
51             char c = pos.getChar();
52             if (Character.isWhitespace(c)) {
53                 pos.skipWhitespace();
54                 continue;
55             }
56             if (c == '\'' || c == '"') {
57                 pos.skipQuote();
58                 continue;
59             }
60             if (pos.lookingAt("/*")) {
61                 skipComment(pos);
62                 continue;
63             }
64             if (pos.lookingAt("//")) {
65                 skipSingleLineComment(pos);
66                 continue;
67             }
68             if (c == '#' && pos.getOffset() == 0) {
69                 skipPreprocessor(pos);
70                 continue;
71             }
72             if (state == METHOD_NAME) {
73                 if (c == '{') {
74                     if (token != null && !mode.isKeyword(token))
75                         tags.add(new CTag(token, tokenStart));
76                     skipBrace();
77                     state = NEUTRAL;
78                 } else if (isIdentifierStart(c)) {
79                     state = PARAMETER_LIST;
80                     pos.next();
81                 } else {
82                     state = NEUTRAL;
83                     pos.next();
84                 }
85                 continue;
86             }
87             if (state == PARAMETER_LIST) {
88                 if (c == '{') {
89                     if (token != null && !mode.isKeyword(token))
90                         tags.add(new CTag(token, tokenStart));
91                     skipBrace();
92                     state = NEUTRAL;
93                     continue;
94                 } else if (c == '(') {
95                     state = NEUTRAL;
96                     skipParen();
97                     continue;
98                 } else {
99                     pos.next();
100                     continue;
101                 }
102             }
103             if (state == METHOD_PROLOG) {
104                 if (c == '{') {
105                     if (token != null && !mode.isKeyword(token))
106                         tags.add(new ObjCTag(token, tokenStart));
107                     skipBrace();
108                     state = NEUTRAL;
109                     continue;
110                 } else if (c == '(') {
111                     skipParen();
112                     continue;
113                 } else if (isIdentifierStart(c)) {
114                     if (token == null) {
115                         tokenStart = pos.copy();
116                         token = gatherToken(pos);
117                         continue;
118                     } else {
119                         String JavaDoc s = gatherToken(pos);
120                         if (s.endsWith(":"))
121                             token = token.concat(s);
122                         continue;
123                     }
124                 }
125                 pos.next();
126                 continue;
127             }
128             if (c == '}') {
129                 pos.next();
130                 continue;
131             }
132             if (pos.getOffset() == 0 && (c == '+' || c == '-')) {
133                 token = null;
134                 state = METHOD_PROLOG;
135                 continue;
136             }
137             if (isIdentifierStart(c)) {
138                 tokenStart = pos.copy();
139                 token = gatherToken(pos);
140                 continue;
141             }
142             if (c == '(') {
143                 skipParen();
144                 state = METHOD_NAME;
145                 continue;
146             }
147             pos.next();
148         }
149         buffer.setTags(tags);
150     }
151
152     private String JavaDoc gatherToken(Position pos)
153     {
154         FastStringBuffer sb = new FastStringBuffer();
155         char c;
156         while (isIdentifierPart(c = pos.getChar()) || c == ':') {
157             sb.append(c);
158             if (!pos.next())
159                 break;
160         }
161         return sb.toString();
162     }
163
164     private static final boolean isIdentifierStart(char c)
165     {
166         return CMode.getMode().isIdentifierStart(c);
167     }
168
169     private static final boolean isIdentifierPart(char c)
170     {
171         return CMode.getMode().isIdentifierPart(c);
172     }
173 }
174
Popular Tags