diff options
Diffstat (limited to 'src/cuchaz/enigma/analysis/Lexer.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/Lexer.java | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/cuchaz/enigma/analysis/Lexer.java b/src/cuchaz/enigma/analysis/Lexer.java index acb52bf..602e3a9 100644 --- a/src/cuchaz/enigma/analysis/Lexer.java +++ b/src/cuchaz/enigma/analysis/Lexer.java | |||
| @@ -14,6 +14,7 @@ import java.util.Iterator; | |||
| 14 | 14 | ||
| 15 | import jsyntaxpane.SyntaxDocument; | 15 | import jsyntaxpane.SyntaxDocument; |
| 16 | import jsyntaxpane.Token; | 16 | import jsyntaxpane.Token; |
| 17 | import jsyntaxpane.TokenType; | ||
| 17 | import jsyntaxpane.lexers.JavaLexer; | 18 | import jsyntaxpane.lexers.JavaLexer; |
| 18 | 19 | ||
| 19 | public class Lexer implements Iterable<Token> | 20 | public class Lexer implements Iterable<Token> |
| @@ -21,10 +22,10 @@ public class Lexer implements Iterable<Token> | |||
| 21 | private SyntaxDocument m_doc; | 22 | private SyntaxDocument m_doc; |
| 22 | private Iterator<Token> m_iter; | 23 | private Iterator<Token> m_iter; |
| 23 | 24 | ||
| 24 | public Lexer( String source ) | 25 | public Lexer( CharSequence source ) |
| 25 | { | 26 | { |
| 26 | m_doc = new SyntaxDocument( new JavaLexer() ); | 27 | m_doc = new SyntaxDocument( new JavaLexer() ); |
| 27 | m_doc.append( source ); | 28 | m_doc.append( source.toString() ); |
| 28 | m_iter = m_doc.getTokens( 0, m_doc.getLength() ); | 29 | m_iter = m_doc.getTokens( 0, m_doc.getLength() ); |
| 29 | } | 30 | } |
| 30 | 31 | ||
| @@ -38,4 +39,41 @@ public class Lexer implements Iterable<Token> | |||
| 38 | { | 39 | { |
| 39 | return token.getString( m_doc ); | 40 | return token.getString( m_doc ); |
| 40 | } | 41 | } |
| 42 | |||
| 43 | public Token getFirstIdentifier( ) | ||
| 44 | { | ||
| 45 | for( Token token : this ) | ||
| 46 | { | ||
| 47 | if( token.type == TokenType.IDENTIFIER ) | ||
| 48 | { | ||
| 49 | return token; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | return null; | ||
| 53 | } | ||
| 54 | |||
| 55 | public Token getFirstIdentifierMatching( CharSequence val ) | ||
| 56 | { | ||
| 57 | for( Token token : this ) | ||
| 58 | { | ||
| 59 | if( token.type == TokenType.IDENTIFIER && getText( token ).equals( val.toString() ) ) | ||
| 60 | { | ||
| 61 | return token; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return null; | ||
| 65 | } | ||
| 66 | |||
| 67 | public Token getLastIdentifier( ) | ||
| 68 | { | ||
| 69 | Token lastToken = null; | ||
| 70 | for( Token token : this ) | ||
| 71 | { | ||
| 72 | if( token.type == TokenType.IDENTIFIER ) | ||
| 73 | { | ||
| 74 | lastToken = token; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | return lastToken; | ||
| 78 | } | ||
| 41 | } | 79 | } |