update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / highlighter / custom / AbstractCustomLexer.java
blobbd4b8ed2bfa94a86bef45ae9ba1db34dcbc13a81
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.ide.highlighter.custom.tokens.TokenInfo;
20 import com.intellij.ide.highlighter.custom.tokens.TokenParser;
21 import com.intellij.lexer.LexerBase;
22 import com.intellij.psi.CustomHighlighterTokenType;
23 import com.intellij.psi.tree.IElementType;
24 import com.intellij.util.ArrayUtil;
26 /**
27 * @author dsl
29 public abstract class AbstractCustomLexer extends LexerBase {
30 protected CharSequence myBuffer = ArrayUtil.EMPTY_CHAR_SEQUENCE;
31 protected int myStartOffset = 0;
32 protected int myEndOffset = 0;
33 private final TokenParser[] myTokenParsers;
34 private TokenInfo myCurrentToken;
35 private int myPosition;
37 public AbstractCustomLexer(TokenParser[] tokenParsers) {
38 myTokenParsers = tokenParsers;
40 int smartUpdateShift = 0;
41 for (TokenParser tokenParser : myTokenParsers) {
42 smartUpdateShift = Math.max(smartUpdateShift, tokenParser.getSmartUpdateShift());
46 public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) {
47 myBuffer = buffer;
48 myStartOffset = startOffset;
49 myEndOffset = endOffset;
50 myPosition = myStartOffset;
51 myCurrentToken = new TokenInfo();
52 for (TokenParser tokenParser : myTokenParsers) {
53 tokenParser.setBuffer(myBuffer, myStartOffset, myEndOffset);
55 advance();
58 public int getState() {
59 return 0;
62 public IElementType getTokenType() {
63 return myCurrentToken.getType();
66 public int getTokenStart() {
67 return myCurrentToken.getStart();
70 public int getTokenEnd() {
71 return myCurrentToken.getEnd();
74 public void advance() {
75 if (myPosition >= myEndOffset) {
76 myCurrentToken.updateData(myPosition, myPosition, null);
77 return;
79 boolean tokenFound = false;
80 for (TokenParser tokenParser : myTokenParsers) {
81 if (tokenParser.hasToken(myPosition)) {
82 tokenParser.getTokenInfo(myCurrentToken);
83 tokenFound = true;
84 break;
88 if (!tokenFound) {
89 myCurrentToken.updateData(myPosition, myPosition + 1, CustomHighlighterTokenType.CHARACTER);
91 myPosition = myCurrentToken.getEnd();
94 public CharSequence getBufferSequence() {
95 return myBuffer;
98 public int getBufferEnd() {
99 return myEndOffset;