HierarchyBrowserManager is a service
[fedora-idea.git] / source / com / intellij / lexer / PrefixSuffixStripperLexer.java
blob150f3473240181d196534bde01287ec74956f5c4
1 /*
2 * @author max
3 */
4 package com.intellij.lexer;
6 import com.intellij.psi.tree.IElementType;
7 import com.intellij.util.text.CharArrayUtil;
8 import com.intellij.util.text.CharArrayCharSequence;
10 public class PrefixSuffixStripperLexer extends LexerBase {
11 private CharSequence myBuffer;
12 private char[] myBufferArray;
13 private int myTokenStart;
14 private int myTokenEnd;
15 private IElementType myTokenType;
16 private int myState;
17 private int myBufferEnd;
18 private String myPrefix;
19 private IElementType myPrefixType;
20 private String mySuffix;
21 private IElementType myMiddleTokenType;
22 private IElementType mySuffixType;
24 public PrefixSuffixStripperLexer(final String prefix,
25 final IElementType prefixType,
26 final String suffix,
27 final IElementType suffixType,
28 final IElementType middleTokenType) {
29 mySuffixType = suffixType;
30 myMiddleTokenType = middleTokenType;
31 mySuffix = suffix;
32 myPrefixType = prefixType;
33 myPrefix = prefix;
36 public void start(char[] buffer, int startOffset, int endOffset, int initialState) {
37 start(new CharArrayCharSequence(buffer),startOffset, endOffset, initialState);
40 public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) {
41 myBuffer = buffer;
42 myBufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer);
43 myTokenStart = startOffset;
44 myTokenEnd = startOffset;
45 myTokenType = null;
46 myState = initialState;
47 myBufferEnd = endOffset;
50 public IElementType getTokenType() {
51 locateToken();
52 return myTokenType;
55 public int getTokenStart() {
56 locateToken();
57 return myTokenStart;
60 public int getTokenEnd() {
61 locateToken();
62 return myTokenEnd;
65 public int getState() {
66 return myState;
69 public int getBufferEnd() {
70 return myBufferEnd;
73 public char[] getBuffer() {
74 return CharArrayUtil.fromSequence(myBuffer);
77 public CharSequence getBufferSequence() {
78 return myBuffer;
81 public void advance() {
82 myTokenType = null;
85 private void locateToken() {
86 if (myTokenType != null || myState == 3) return;
88 if (myState == 0) {
89 myTokenEnd = myTokenStart + myPrefix.length();
90 myTokenType = myPrefixType;
91 myState = myTokenEnd < myBufferEnd ? 1 : 3;
92 return;
95 if (myState == 1) {
96 myTokenStart = myTokenEnd;
97 final int suffixStart = myBufferEnd - mySuffix.length();
98 myTokenType = myMiddleTokenType;
99 if ( (myBufferArray != null && CharArrayUtil.regionMatches(myBufferArray, suffixStart, myBufferEnd, mySuffix)) ||
100 (myBufferArray == null && CharArrayUtil.regionMatches(myBuffer, suffixStart, myBufferEnd, mySuffix))
102 myTokenEnd = suffixStart;
103 if (myTokenStart < myTokenEnd) {
104 myState = 2;
106 else {
107 myState = 3;
108 myTokenType = mySuffixType;
109 myTokenEnd = myBufferEnd;
112 else {
113 myTokenEnd = myBufferEnd;
114 myState = 3;
117 return;
120 if (myState == 2) {
121 myTokenStart = myTokenEnd;
122 myTokenEnd = myBufferEnd;
123 myTokenType = mySuffixType;
124 myState = 3;