KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PythonTagger.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: PythonTagger.java,v 1.1.1.1 2002/09/24 16:09:15 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 java.util.ArrayList JavaDoc;
25
26 public final class PythonTagger extends Tagger
27 {
28     // States.
29
private static final int STATE_NEUTRAL = 0;
30     private static final int STATE_CLASS_NAME = 1;
31     private static final int STATE_FUNCTION_NAME = 2;
32     private static final int STATE_SINGLE_QUOTE = 3;
33     private static final int STATE_DOUBLE_QUOTE = 4;
34     private static final int STATE_TRIPLE_SINGLE = 5;
35     private static final int STATE_TRIPLE_DOUBLE = 6;
36
37     private static final PythonMode mode = PythonMode.getMode();
38
39     public PythonTagger(SystemBuffer buffer)
40     {
41         super(buffer);
42     }
43
44     public void run()
45     {
46         ArrayList JavaDoc tags = new ArrayList JavaDoc();
47         Position pos = new Position(buffer.getFirstLine(), 0);
48         int state = STATE_NEUTRAL;
49         while (!pos.atEnd()) {
50             char c = pos.getChar();
51             if (Character.isWhitespace(c)) {
52                 pos.skipWhitespace();
53                 continue;
54             }
55             if (c == '\\') {
56                 // Escape.
57
if (pos.getOffset() < pos.getLineLength()-1) {
58                     pos.skip(2);
59                 } else {
60                     pos.setLine(pos.getNextLine());
61                     pos.setOffset(0);
62                 }
63                 continue;
64             }
65             if (state == STATE_SINGLE_QUOTE) {
66                 if (c == '\'')
67                     state = STATE_NEUTRAL;
68                 pos.next();
69                 continue;
70             }
71             if (state == STATE_DOUBLE_QUOTE) {
72                 if (c == '"')
73                     state = STATE_NEUTRAL;
74                 pos.next();
75                 continue;
76             }
77             if (state == STATE_TRIPLE_SINGLE) {
78                 if (c == '\'' && pos.lookingAt("'''")) {
79                     pos.skip(3);
80                     state = STATE_NEUTRAL;
81                 } else
82                     pos.next();
83                 continue;
84             }
85             if (state == STATE_TRIPLE_DOUBLE) {
86                 if (c == '"' && pos.lookingAt("\"\"\"")) {
87                     pos.skip(3);
88                     state = STATE_NEUTRAL;
89                 } else
90                     pos.next();
91                 continue;
92             }
93             if (c == '\'') {
94                 if (pos.lookingAt("'''")) {
95                     state = STATE_TRIPLE_SINGLE;
96                     pos.skip(3);
97                 } else {
98                     state = STATE_SINGLE_QUOTE;
99                     pos.next();
100                 }
101                 continue;
102             }
103             if (c == '"') {
104                 if (pos.lookingAt("\"\"\"")) {
105                     state = STATE_TRIPLE_DOUBLE;
106                     pos.skip(3);
107                 } else {
108                     state = STATE_DOUBLE_QUOTE;
109                     pos.next();
110                 }
111                 continue;
112             }
113             if (c == '#') {
114                 // Comment.
115
pos.setLine(pos.getNextLine());
116                 pos.setOffset(0);
117                 continue;
118             }
119             if (mode.isIdentifierStart(c)) {
120                 Position start = pos.copy();
121                 String JavaDoc token = gatherToken(pos);
122                 if (state == STATE_CLASS_NAME) {
123                     LocalTag tag = new PythonTag("class " + token, start, TAG_CLASS);
124                     tags.add(tag);
125                     state = STATE_NEUTRAL;
126                 } else if (state == STATE_FUNCTION_NAME) {
127                     LocalTag tag = new PythonTag(token, start, TAG_FUNCTION);
128                     tags.add(tag);
129                     state = STATE_NEUTRAL;
130                 } else if (token.equals("class")) {
131                     state = STATE_CLASS_NAME;
132                 } else if (token.equals("def")) {
133                     state = STATE_FUNCTION_NAME;
134                 }
135                 continue;
136             }
137             pos.next();
138         }
139         buffer.setTags(tags);
140     }
141
142     private static String JavaDoc gatherToken(Position pos)
143     {
144         FastStringBuffer sb = new FastStringBuffer();
145         char c;
146         while (mode.isIdentifierPart(c = pos.getChar())) {
147             sb.append(c);
148             if (!pos.next())
149                 break;
150         }
151         return sb.toString();
152     }
153 }
154
Popular Tags