2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.ide
.highlighter
.custom
;
19 import com
.intellij
.lexer
.LexerBase
;
20 import com
.intellij
.psi
.CustomHighlighterTokenType
;
21 import com
.intellij
.psi
.tree
.IElementType
;
27 public class SyntaxTableLexer
extends LexerBase
{
28 private final SyntaxTable table
;
30 private final PosBufferTokenizer tokenizer
;
32 private IElementType tokenType
;
33 private int tokenStart
;
36 private final String lineComment
;
37 private final String startComment
;
38 private final String endComment
;
40 private CharSequence buffer
;
41 private int startOffset
;
42 private int endOffset
;
44 private boolean firstCall
;
46 public SyntaxTableLexer(SyntaxTable table
) {
49 tokenizer
= new PosBufferTokenizer();
50 tokenizer
.setIgnoreCase(table
.isIgnoreCase());
52 tokenizer
.setHexPrefix(table
.getHexPrefix());
53 tokenizer
.setNumPostifxChars(table
.getNumPostfixChars());
55 tokenizer
.ordinaryChar('/');
56 tokenizer
.ordinaryChar('.');
57 tokenizer
.quoteChar('"');
58 tokenizer
.quoteChar('\'');
59 tokenizer
.wordChars('_', '_');
61 lineComment
= table
.getLineComment();
62 startComment
= table
.getStartComment();
63 endComment
= table
.getEndComment();
66 public void start(CharSequence buffer
, int startOffset
, int endOffset
,
69 this.startOffset
= startOffset
;
70 this.endOffset
= endOffset
;
74 tokenizer
.start(buffer
, startOffset
, endOffset
);
79 private void parseToken() {
82 int ttype
= tokenizer
.nextToken();
85 case PosBufferTokenizer
.TT_EOF
:
86 tokenStart
= endOffset
;
91 case PosBufferTokenizer
.TT_WHITESPACE
:
92 tokenType
= CustomHighlighterTokenType
.WHITESPACE
;
95 case PosBufferTokenizer
.TT_WORD
:
97 if (table
.getKeywords1().contains(st
)) {
98 tokenType
= CustomHighlighterTokenType
.KEYWORD_1
;
99 } else if (table
.getKeywords2().contains(st
)) {
100 tokenType
= CustomHighlighterTokenType
.KEYWORD_2
;
101 } else if (table
.getKeywords3().contains(st
)) {
102 tokenType
= CustomHighlighterTokenType
.KEYWORD_3
;
103 } else if (table
.getKeywords4().contains(st
)) {
104 tokenType
= CustomHighlighterTokenType
.KEYWORD_4
;
106 tokenType
= CustomHighlighterTokenType
.IDENTIFIER
;
110 case PosBufferTokenizer
.TT_NUMBER
:
111 tokenType
= CustomHighlighterTokenType
.NUMBER
;
114 case PosBufferTokenizer
.TT_QUOTE
:
115 tokenType
= CustomHighlighterTokenType
.STRING
;
120 if (lineComment
!= null && !"".equals(lineComment
.trim())) {
121 if (ttype
== lineComment
.charAt(0) && tokenizer
.matchString(lineComment
, 1)) {
122 tokenType
= CustomHighlighterTokenType
.LINE_COMMENT
;
123 tokenStart
= tokenizer
.getPos() - lineComment
.length();
124 tokenizer
.skipToEol();
125 tokenEnd
= tokenizer
.getPos();
131 if (startComment
!= null && !"".equals(startComment
.trim())) {
132 if (ttype
== startComment
.charAt(0) && tokenizer
.matchString(startComment
, 1)) {
133 tokenType
= CustomHighlighterTokenType
.MULTI_LINE_COMMENT
;
134 tokenStart
= tokenizer
.getPos() - startComment
.length();
135 tokenizer
.skipToStr(endComment
);
136 tokenEnd
= tokenizer
.getPos();
140 tokenType
= CustomHighlighterTokenType
.CHARACTER
;
143 tokenStart
= tokenizer
.startOffset
;
144 tokenEnd
= tokenizer
.endOffset
;
150 public IElementType
getTokenType() {
157 public int getTokenStart() {
161 public int getTokenEnd() {
165 public void advance() {
172 public CharSequence
getBufferSequence() {
176 public int getBufferEnd() {
180 public int getState() {