PageRenderTime 138ms CodeModel.GetById 129ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/structure/RubyToken.java

#
Java | 89 lines | 71 code | 15 blank | 3 comment | 24 complexity | ef2d1bab3baecab8939d6269d16fd62c MD5 | raw file
 1package org.jedit.ruby.structure;
 2
 3import org.gjt.sp.jedit.syntax.Token;
 4
 5/**
 6 * @author robmckinnon at users,sourceforge,net
 7 */
 8public final class RubyToken extends Token {
 9    private final Token firstToken;
10    private RubyToken previousToken;
11    private RubyToken nextToken;
12
13    public RubyToken(Token token, Token firstToken) {
14        super(token.id, token.offset, token.length, token.rules);
15        this.next = token.next;
16        this.firstToken = firstToken;
17    }
18
19    public final RubyToken getPreviousToken() {
20        if (previousToken == null) {
21            Token token = firstToken;
22            Token previous = null;
23
24            while (token != null && token.next != null) {
25                if (isEqual(token.next)) {
26                    previous = token;
27                    break;
28                }
29                token = token.next;
30            }
31
32            previousToken = previous == null ? null : new RubyToken(previous, firstToken);
33        }
34        return previousToken;
35    }
36
37    public final RubyToken getNextToken() {
38        if (nextToken == null) {
39            nextToken = next == null ? null : new RubyToken(next, firstToken);
40        }
41        return nextToken;
42    }
43
44    public final boolean isNextLiteral() {
45        return getNextToken() != null && getNextToken().isLiteral();
46    }
47
48    public final boolean isPreviousLiteral() {
49        return getPreviousToken() != null && getPreviousToken().isLiteral();
50    }
51
52    public final boolean isOperator() {
53        return id == Token.OPERATOR;
54    }
55
56    public final boolean isLiteral() {
57        switch (id) {
58            case Token.LITERAL1:
59            case Token.LITERAL2:
60            case Token.LITERAL3:
61            case Token.LITERAL4:
62                return true;
63            default:
64                return false;
65        }
66    }
67
68    public final boolean isComment() {
69        switch(id) {
70            case Token.COMMENT1:
71            case Token.COMMENT2:
72            case Token.COMMENT3:
73            case Token.COMMENT4:
74                return true;
75            default:
76                return false;
77        }
78    }
79
80    public final String toString() {
81        String id = this.id < Token.TOKEN_TYPES.length ? Token.TOKEN_TYPES[this.id] : String.valueOf(this.id);
82        return id + '['+offset+','+(offset+length)+']';
83    }
84
85    private boolean isEqual(Token token) {
86        return token != null && token.id == id && token.offset == offset && token.length == length;
87    }
88
89}