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.
16 package com
.intellij
.lexer
;
18 import com
.intellij
.pom
.java
.LanguageLevel
;
19 import com
.intellij
.psi
.JavaTokenType
;
20 import com
.intellij
.psi
.tree
.IElementType
;
23 * Used to process scriptlet code in JSP attribute values like this:
24 * attribute="<%=texts.get(\"Blabla\")%>"
26 public class EscapedJavaLexer
extends LexerBase
{
27 private char mySurroundingQuote
;
28 private final JavaLexer myJavaLexer
;
30 private CharSequence myBuffer
;
31 private int myBufferEnd
;
32 private int myCurOffset
;
33 private IElementType myTokenType
= null;
34 private int myTokenEnd
;
36 public EscapedJavaLexer(char surroundingQuote
, LanguageLevel languageLevel
) {
37 mySurroundingQuote
= surroundingQuote
;
38 myJavaLexer
= new JavaLexer(languageLevel
);
41 public char getSurroundingQuote() {
42 return mySurroundingQuote
;
45 public void setSurroundingQuote(char surroundingQuote
) {
46 mySurroundingQuote
= surroundingQuote
;
49 public void start(CharSequence buffer
, int startOffset
, int endOffset
, int state
) {
51 myCurOffset
= startOffset
;
52 myTokenEnd
= startOffset
;
53 myBufferEnd
= endOffset
;
57 public CharSequence
getBufferSequence() {
61 public int getState() {
65 public IElementType
getTokenType() {
70 public final int getTokenStart(){
75 public final int getTokenEnd(){
80 public final void advance(){
83 myCurOffset
= myTokenEnd
;
86 public final int getBufferEnd(){
90 private void locateToken() {
91 if (myTokenType
!= null) return;
92 if (myCurOffset
>= myBufferEnd
) return;
95 int offset
= myCurOffset
;
96 int state
= 0; // 0 -- start/end
98 // 2 -- after escape (/) in string literal
100 char literalStarter
= 0;
102 if (offset
>= myBufferEnd
) break;
104 char c
= myBuffer
.charAt(offset
);
105 boolean wasEsc
= esc
;
115 else if (state
== 0) {
116 if (c
== '\'' || c
== '\"') {
121 else if (state
== 1) {
122 if (c
== literalStarter
) {
128 else if (state
== 2) {
132 if (!esc
&& state
== 0) {
140 if(offset
>= myBufferEnd
- 1) state
= 0;
143 if(offset
== myCurOffset
){
144 myJavaLexer
.start(myBuffer
, myCurOffset
, myBufferEnd
);
145 myTokenType
= myJavaLexer
.getTokenType();
146 myTokenEnd
= myJavaLexer
.getTokenEnd();
149 myTokenType
= literalStarter
== '\"' ? JavaTokenType
.STRING_LITERAL
: JavaTokenType
.CHARACTER_LITERAL
;
155 myTokenType
= literalStarter
== '\"' ? JavaTokenType
.STRING_LITERAL
: JavaTokenType
.CHARACTER_LITERAL
;
160 myTokenType
= JavaTokenType
.BAD_CHARACTER
;