001: /*
002: * SchemeTagger.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package org.armedbear.j;
022:
023: import java.util.Vector;
024:
025: public final class SchemeTagger extends Tagger {
026: // States.
027: private static final int NEUTRAL = 0;
028: private static final int DEFUN = 1;
029:
030: protected Position pos;
031: protected String token;
032: protected Position tokenStart;
033:
034: private static final Mode schemeMode = SchemeMode.getMode();
035:
036: public SchemeTagger(SystemBuffer buffer) {
037: super (buffer);
038: }
039:
040: public void run() {
041: Vector tags = new Vector();
042: pos = new Position(buffer.getFirstLine(), 0);
043: token = null;
044: tokenStart = null;
045: int state = NEUTRAL;
046: while (!pos.atEnd()) {
047: char c = pos.getChar();
048: if (Character.isWhitespace(c)) {
049: pos.skipWhitespace();
050: continue;
051: }
052: if (c == '\\') {
053: // Escape.
054: if (pos.getOffset() < pos.getLineLength() - 1) {
055: pos.skip(2);
056: continue;
057: }
058: Line nextLine = pos.getNextLine();
059: if (nextLine == null)
060: break;
061: pos.moveTo(nextLine, 0);
062: continue;
063: }
064: if (c == '\"') {
065: pos.skipQuote();
066: continue;
067: }
068: if (c == ';') {
069: // Comment.
070: Line nextLine = pos.getNextLine();
071: if (nextLine == null)
072: break;
073: pos.moveTo(nextLine, 0);
074: continue;
075: }
076: if (schemeMode.isIdentifierStart(c)) {
077: gatherToken();
078: if (state == DEFUN) {
079: LocalTag tag = new LispTag(token, tokenStart);
080: tags.add(tag);
081: state = NEUTRAL;
082: } else if (token.equals("define"))
083: state = DEFUN;
084: continue;
085: }
086: pos.next();
087: }
088: buffer.setTags(tags);
089: }
090:
091: private void gatherToken() {
092: tokenStart = new Position(pos);
093: FastStringBuffer sb = new FastStringBuffer();
094: char c;
095: while (schemeMode.isIdentifierPart(c = pos.getChar())) {
096: sb.append(c);
097: if (!pos.next())
098: break;
099: }
100: token = sb.toString();
101: }
102: }
|