KickJava   Java API By Example, From Geeks To Geeks.

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


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

20
21 package org.armedbear.j;
22
23 import java.util.Vector JavaDoc;
24
25 public final class SchemeTagger extends Tagger
26 {
27     // States.
28
private static final int NEUTRAL = 0;
29     private static final int DEFUN = 1;
30
31     protected Position pos;
32     protected String JavaDoc token;
33     protected Position tokenStart;
34
35     private static final Mode schemeMode = SchemeMode.getMode();
36
37     public SchemeTagger(SystemBuffer buffer)
38     {
39         super(buffer);
40     }
41
42     public void run()
43     {
44         Vector JavaDoc tags = new Vector JavaDoc();
45         pos = new Position(buffer.getFirstLine(), 0);
46         token = null;
47         tokenStart = null;
48         int 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                     continue;
60                 }
61                 Line nextLine = pos.getNextLine();
62                 if (nextLine == null)
63                     break;
64                 pos.moveTo(nextLine, 0);
65                 continue;
66             }
67             if (c == '\"') {
68                 pos.skipQuote();
69                 continue;
70             }
71             if (c == ';') {
72                 // Comment.
73
Line nextLine = pos.getNextLine();
74                 if (nextLine == null)
75                     break;
76                 pos.moveTo(nextLine, 0);
77                 continue;
78             }
79             if (schemeMode.isIdentifierStart(c)) {
80                 gatherToken();
81                 if (state == DEFUN) {
82                     LocalTag tag = new LispTag(token, tokenStart);
83                     tags.add(tag);
84                     state = NEUTRAL;
85                 } else if (token.equals("define"))
86                     state = DEFUN;
87                 continue;
88             }
89             pos.next();
90         }
91         buffer.setTags(tags);
92     }
93
94     private void gatherToken()
95     {
96         tokenStart = new Position(pos);
97         FastStringBuffer sb = new FastStringBuffer();
98         char c;
99         while (schemeMode.isIdentifierPart(c = pos.getChar())) {
100             sb.append(c);
101             if (!pos.next())
102                 break;
103         }
104         token = sb.toString();
105     }
106 }
107
Popular Tags