update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / lexer / EscapedJavaLexer.java
blobbadc086e01c5cf501c5fb430c305cc4b9fe17597
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.
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;
22 /**
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) {
50 myBuffer = buffer;
51 myCurOffset = startOffset;
52 myTokenEnd = startOffset;
53 myBufferEnd = endOffset;
54 myTokenType = null;
57 public CharSequence getBufferSequence() {
58 return myBuffer;
61 public int getState() {
62 return 0;
65 public IElementType getTokenType() {
66 locateToken();
67 return myTokenType;
70 public final int getTokenStart(){
71 locateToken();
72 return myCurOffset;
75 public final int getTokenEnd(){
76 locateToken();
77 return myTokenEnd;
80 public final void advance(){
81 locateToken();
82 myTokenType = null;
83 myCurOffset = myTokenEnd;
86 public final int getBufferEnd(){
87 return myBufferEnd;
90 private void locateToken() {
91 if (myTokenType != null) return;
92 if (myCurOffset >= myBufferEnd) return;
94 boolean esc = false;
95 int offset = myCurOffset;
96 int state = 0; // 0 -- start/end
97 // 1 -- inside string
98 // 2 -- after escape (/) in string literal
100 char literalStarter = 0;
101 do {
102 if (offset >= myBufferEnd) break;
104 char c = myBuffer.charAt(offset);
105 boolean wasEsc = esc;
106 esc = false;
107 if (c == '\\') {
108 if (!wasEsc) {
109 esc = true;
111 else {
112 state = 2;
115 else if (state == 0) {
116 if (c == '\'' || c == '\"') {
117 literalStarter = c;
118 state = 1;
121 else if (state == 1) {
122 if (c == literalStarter) {
123 state = 0;
124 offset++;
125 break;
128 else if (state == 2) {
129 state = 1;
132 if (!esc && state == 0) {
133 break;
136 offset++;
138 while (true);
140 if(offset >= myBufferEnd - 1) state = 0;
141 switch (state){
142 case 0:
143 if(offset == myCurOffset){
144 myJavaLexer.start(myBuffer, myCurOffset, myBufferEnd);
145 myTokenType = myJavaLexer.getTokenType();
146 myTokenEnd = myJavaLexer.getTokenEnd();
148 else {
149 myTokenType = literalStarter == '\"' ? JavaTokenType.STRING_LITERAL : JavaTokenType.CHARACTER_LITERAL;
150 myTokenEnd = offset;
152 break;
154 case 1:
155 myTokenType = literalStarter == '\"' ? JavaTokenType.STRING_LITERAL : JavaTokenType.CHARACTER_LITERAL;
156 myTokenEnd = offset;
157 break;
159 default:
160 myTokenType = JavaTokenType.BAD_CHARACTER;
161 myTokenEnd = offset;
162 break;