Changeset 184


Ignore:
Timestamp:
10/30/02 17:46:35 (21 years ago)
Author:
piso
Message:

Work in progress.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/org/armedbear/j/LispTagger.java

    r170 r184  
    2121package org.armedbear.j;
    2222
    23 import java.util.Vector;
     23import java.util.ArrayList;
    2424
    2525public final class LispTagger extends Tagger
    2626{
    2727    // States.
    28     private static final int NEUTRAL = 0;
    29     private static final int DEFUN   = 1;
     28    private static final int NEUTRAL    = 0;
     29    private static final int OPEN_PAREN = 1;
     30    private static final int DEFUN      = 2;
    3031
    31     protected Position pos;
    32     protected String token;
    33     protected Position tokenStart;
    34 
    35     private static final Mode lispMode = LispMode.getMode();
     32    private static final Mode mode = LispMode.getMode();
    3633
    3734    public LispTagger(SystemBuffer buffer)
     
    4239    public void run()
    4340    {
    44         Vector tags = new Vector();
    45         pos = new Position(buffer.getFirstLine(), 0);
    46         token = null;
    47         tokenStart = null;
     41        ArrayList tags = new ArrayList();
     42        Position pos = new Position(buffer.getFirstLine(), 0);
    4843        int state = NEUTRAL;
    4944        while (!pos.atEnd()) {
     
    7772                continue;
    7873            }
    79             if (lispMode.isIdentifierStart(c)) {
    80                 gatherToken();
     74            if (c == '(') {
     75                state = OPEN_PAREN;
     76                pos.next();
     77                continue;
     78            }
     79            if (mode.isIdentifierStart(c)) {
    8180                if (state == DEFUN) {
     81                    Position tokenStart = pos.copy();
     82                    String token = gatherToken(pos);
    8283                    LocalTag tag = new LispTag(token, tokenStart);
    8384                    tags.add(tag);
    8485                    state = NEUTRAL;
    85                 } else if (token.startsWith("def")) {
    86                     if (token.equals("defun") ||
    87                         token.equals("defvar") ||
    88                         token.equals("defmacro") ||
    89                         token.equals("defcustom") ||
    90                         token.equals("defgroup") ||
    91                         token.equals("defsubst"))
     86                    continue;
     87                }
     88                if (state == OPEN_PAREN) {
     89                    String preceding =
     90                        pos.getLine().substring(0, pos.getOffset()).trim();
     91                    if (!preceding.equals("(")) {
     92                        state = NEUTRAL;
     93                        continue;
     94                    }
     95                    String token = gatherToken(pos);
     96                    if (isDefinitionStart(token, pos))
    9297                        state = DEFUN;
     98                    else
     99                        state = NEUTRAL;
     100                    continue;
    93101                }
     102                skipToken(pos);
    94103                continue;
    95104            }
     105            state = NEUTRAL;
    96106            pos.next();
    97107        }
    98108        buffer.setTags(tags);
    99109    }
     110   
     111    private static final String[] tokens = new String[] {
     112        "defun", "defvar", "defmacro", "defcustom", "defgroup", "defsubst"
     113    };
     114   
     115    // pos points to first char after token.
     116    private boolean isDefinitionStart(String token, Position pos)
     117    {
     118        if (token.startsWith("def"))
     119            if (Utilities.isOneOf(token, tokens))
     120                return true;
     121       
     122        return false;
     123    }
    100124
    101     private void gatherToken()
     125    // Advances pos past token.
     126    private String gatherToken(Position pos)
    102127    {
    103         tokenStart = new Position(pos);
    104128        FastStringBuffer sb = new FastStringBuffer();
    105129        char c;
    106         while (lispMode.isIdentifierPart(c = pos.getChar())) {
     130        while (mode.isIdentifierPart(c = pos.getChar())) {
    107131            sb.append(c);
    108132            if (!pos.next())
    109133                break;
    110134        }
    111         token = sb.toString();
     135        return sb.toString();
     136    }
     137   
     138    // Advances pos past token.
     139    private void skipToken(Position pos)
     140    {
     141        while (mode.isIdentifierPart(pos.getChar())) {
     142            if (!pos.next())
     143                return;
     144        }
    112145    }
    113146}
Note: See TracChangeset for help on using the changeset viewer.