JavaScriptParsingLexer.java

1    /* 
2     * Copyright 2000-2005 JetBrains s.r.o. 
3     * 
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 
7     * 
8     * http://www.apache.org/licenses/LICENSE-2.0 
9     * 
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. 
15    */ 
16   package com.intellij.lang.javascript; 
17    
18   import com.intellij.lexer.FlexAdapter; 
19   import com.intellij.psi.tree.IElementType; 
20    
21   import java.io.Reader; 
22    
23   /** 
24    * @author max 
25    */ 
26   public class JavaScriptParsingLexer extends FlexAdapter { 
27     private boolean myOnBreakOrContinue = false; 
28     private boolean myOnSemanticLineFeed = false; 
29    
30     private final static int ON_BREAK_OR_CONTINUE = 3; 
31     private final static int ON_SEMANTIC_LF = 4; 
32    
33     public JavaScriptParsingLexer() { 
34       super(new _JavaScriptLexer((Reader)null)); 
35     } 
36    
37     @Override 
38     public void advance() { 
39       if (!myOnSemanticLineFeed) { 
40         super.advance(); 
41         final IElementType type = getTokenType(); 
42    
43         if (myOnBreakOrContinue && type == JSTokenTypes.WHITE_SPACE) { 
44           boolean hasLineFeed = false; 
45           for (int i = super.getTokenStart(); i < super.getTokenEnd(); i++) { 
46             if (getBuffer()[i] == '\n') { 
47               hasLineFeed = true; 
48               break; 
49             } 
50           } 
51    
52           if (hasLineFeed) { 
53             myOnSemanticLineFeed = true; 
54           } 
55         } 
56    
57         myOnBreakOrContinue = (type == JSTokenTypes.BREAK_KEYWORD || type == JSTokenTypes.CONTINUE_KEYWORD); 
58       } 
59       else { 
60         myOnSemanticLineFeed = false; 
61         myOnBreakOrContinue = false; 
62       } 
63     } 
64    
65     @Override 
66     public IElementType getTokenType() { 
67       return myOnSemanticLineFeed ? JSTokenTypes.SEMANTIC_LINEFEED : super.getTokenType(); 
68     } 
69    
70     @Override 
71     public int getTokenStart() { 
72       return super.getTokenStart(); 
73     } 
74    
75     @Override 
76     public int getTokenEnd() { 
77       return myOnSemanticLineFeed ? super.getTokenStart() : super.getTokenEnd(); 
78     } 
79    
80     @Override 
81     public int getState() { 
82       if (myOnSemanticLineFeed) return ON_SEMANTIC_LF; 
83       if (myOnBreakOrContinue) return ON_BREAK_OR_CONTINUE; 
84       return super.getState(); 
85     } 
86   } 
87