• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package org.jf.smalidea;
33 
34 import com.intellij.lexer.LexerBase;
35 import com.intellij.psi.TokenType;
36 import com.intellij.psi.tree.IElementType;
37 import org.antlr.runtime.CommonToken;
38 import org.jetbrains.annotations.NotNull;
39 import org.jf.smali.smaliFlexLexer;
40 import org.jf.smali.smaliParser;
41 import org.jf.util.BlankReader;
42 
43 public class SmaliLexer extends LexerBase {
44     private final smaliFlexLexer lexer = new smaliFlexLexer(BlankReader.INSTANCE);
45     private CommonToken token = null;
46     private int state = 0;
47     private int endOffset;
48     private CharSequence text;
49 
SmaliLexer()50     public SmaliLexer() {
51         super();
52         lexer.setSuppressErrors(true);
53     }
54 
start(@otNull CharSequence buffer, int startOffset, int endOffset, int initialState)55     @Override public void start(@NotNull CharSequence buffer, int startOffset, int endOffset, int initialState) {
56         text = buffer;
57         lexer.reset(buffer, startOffset, endOffset, initialState);
58         this.endOffset = endOffset;
59         this.token = null;
60         this.state = 0;
61     }
62 
getTokenSequence()63     @NotNull @Override public CharSequence getTokenSequence() {
64         return getTokenText();
65     }
66 
getTokenText()67     @NotNull @Override public String getTokenText() {
68         ensureToken();
69         return token.getText();
70     }
71 
72     @Override
getState()73     public int getState() {
74         ensureToken();
75         return state;
76     }
77 
78     @Override
getTokenType()79     public IElementType getTokenType() {
80         ensureToken();
81         return mapTokenTypeToElementType(token.getType());
82     }
83 
mapTokenTypeToElementType(int tokenType)84     private IElementType mapTokenTypeToElementType(int tokenType) {
85         if (tokenType == smaliParser.WHITE_SPACE) {
86             return TokenType.WHITE_SPACE;
87         }
88         if (tokenType == smaliParser.INVALID_TOKEN) {
89             return TokenType.BAD_CHARACTER;
90         }
91         if (tokenType == smaliParser.EOF) {
92             return null;
93         }
94         return SmaliTokens.getElementType(tokenType);
95     }
96 
97     @Override
getTokenStart()98     public int getTokenStart() {
99         ensureToken();
100         return token.getStartIndex();
101     }
102 
103     @Override
getTokenEnd()104     public int getTokenEnd() {
105         ensureToken();
106         return token.getStopIndex()+1;
107     }
108 
109     @Override
advance()110     public void advance() {
111         token = null;
112         state = 0;
113     }
114 
getBufferSequence()115     @NotNull @Override public CharSequence getBufferSequence() {
116         return text;
117     }
118 
119     @Override
getBufferEnd()120     public int getBufferEnd() {
121         return endOffset;
122     }
123 
ensureToken()124     private void ensureToken() {
125         if (token == null) {
126             token = (CommonToken)lexer.nextToken();
127             state = lexer.yystate();
128         }
129         assert token != null;
130     }
131 }
132