diff options
Diffstat (limited to 'src/Python3LexerBase.java')
-rw-r--r-- | src/Python3LexerBase.java | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/Python3LexerBase.java b/src/Python3LexerBase.java index 4041747..407e624 100644 --- a/src/Python3LexerBase.java +++ b/src/Python3LexerBase.java @@ -1,7 +1,6 @@ -import org.antlr.v4.runtime.*; - import java.util.ArrayDeque; import java.util.Deque; +import org.antlr.v4.runtime.*; abstract class Python3LexerBase extends Lexer { // A queue where extra tokens are pushed on (see the NEWLINE lexer rule). @@ -73,10 +72,10 @@ abstract class Python3LexerBase extends Lexer { // following rules into account: // // "Tabs are replaced (from left to right) by one to eight spaces - // such that the total number of characters up to and including - // the replacement is a multiple of eight [...]" + // such that the total number of characters up to and including + // the replacement is a multiple of eight [...]" // - // -- https://docs.python.org/3.1/reference/lexical_analysis.html#indentation + // -- https://docs.python.org/3.1/reference/lexical_analysis.html#indentation static int getIndentationCount(String spaces) { int count = 0; for (char ch : spaces.toCharArray()) { @@ -97,42 +96,41 @@ abstract class Python3LexerBase extends Lexer { return super.getCharPositionInLine() == 0 && super.getLine() == 1; } - void openBrace(){ + void openBrace() { this.opened++; } - void closeBrace(){ + void closeBrace() { this.opened--; } - void onNewLine(){ + void onNewLine() { String newLine = getText().replaceAll("[^\r\n\f]+", ""); String spaces = getText().replaceAll("[\r\n\f]+", ""); - // Strip newlines inside open clauses except if we are near EOF. We keep NEWLINEs near EOF to + // Strip newlines inside open clauses except if we are near EOF. We keep + // NEWLINEs near EOF to // satisfy the final newline needed by the single_put rule used by the REPL. int next = _input.LA(1); int nextnext = _input.LA(2); - if (opened > 0 || (nextnext != -1 && (next == '\r' || next == '\n' || next == '\f' || next == '#'))) { + if (opened > 0 + || (nextnext != -1 && (next == '\r' || next == '\n' || next == '\f' || next == '#'))) { // If we're inside a list or on a blank line, ignore all indents, // dedents and line breaks. skip(); - } - else { + } else { emit(commonToken(Python3Lexer.NEWLINE, newLine)); int indent = getIndentationCount(spaces); int previous = indents.isEmpty() ? 0 : indents.peek(); if (indent == previous) { // skip indents of the same size as the present indent-size skip(); - } - else if (indent > previous) { + } else if (indent > previous) { indents.push(indent); emit(commonToken(Python3Lexer.INDENT, spaces)); - } - else { + } else { // Possibly emit more than 1 DEDENT token. - while(!indents.isEmpty() && indents.peek() > indent) { + while (!indents.isEmpty() && indents.peek() > indent) { this.emit(createDedent()); indents.pop(); } @@ -141,12 +139,11 @@ abstract class Python3LexerBase extends Lexer { } @Override - public void reset() - { + public void reset() { tokens = new java.util.LinkedList<>(); indents = new ArrayDeque<>(); opened = 0; lastToken = null; super.reset(); } -}
\ No newline at end of file +} |