update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / highlighter / custom / SyntaxTableLexer.java
blob233131282dd1a28b575f9916a728d4fdab5ccdb7
1 /*
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;
23 /**
24 * @author Yura Cangea
25 * @version 1.0
27 public class SyntaxTableLexer extends LexerBase {
28 private final SyntaxTable table;
30 private final PosBufferTokenizer tokenizer;
32 private IElementType tokenType;
33 private int tokenStart;
34 private int tokenEnd;
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) {
47 this.table = 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,
67 int initialState) {
68 this.buffer = buffer;
69 this.startOffset = startOffset;
70 this.endOffset = endOffset;
72 tokenType = null;
74 tokenizer.start(buffer, startOffset, endOffset);
76 firstCall = true;
79 private void parseToken() {
80 String st = null;
81 while (true) {
82 int ttype = tokenizer.nextToken();
84 switch (ttype) {
85 case PosBufferTokenizer.TT_EOF:
86 tokenStart = endOffset;
87 tokenEnd = endOffset;
88 tokenType = null;
89 return;
91 case PosBufferTokenizer.TT_WHITESPACE:
92 tokenType = CustomHighlighterTokenType.WHITESPACE;
93 break;
95 case PosBufferTokenizer.TT_WORD:
96 st = tokenizer.sval;
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;
105 } else {
106 tokenType = CustomHighlighterTokenType.IDENTIFIER;
108 break;
110 case PosBufferTokenizer.TT_NUMBER:
111 tokenType = CustomHighlighterTokenType.NUMBER;
112 break;
114 case PosBufferTokenizer.TT_QUOTE:
115 tokenType = CustomHighlighterTokenType.STRING;
116 break;
118 default:
119 // Line comment
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();
126 return;
130 // Block comment
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();
137 return;
140 tokenType = CustomHighlighterTokenType.CHARACTER;
141 break;
143 tokenStart = tokenizer.startOffset;
144 tokenEnd = tokenizer.endOffset;
145 return;
150 public IElementType getTokenType() {
151 if (firstCall) {
152 advance();
154 return tokenType;
157 public int getTokenStart() {
158 return tokenStart;
161 public int getTokenEnd() {
162 return tokenEnd;
165 public void advance() {
166 if (firstCall) {
167 firstCall = false;
169 parseToken();
172 public CharSequence getBufferSequence() {
173 return buffer;
176 public int getBufferEnd() {
177 return endOffset;
180 public int getState() {
181 // No state.
182 return 0;