public class PythonTokenSource
extends java.lang.Object
implements org.antlr.runtime.TokenSource
a=1 if a>1: print a b=3Here the "b" token on the left edge signals that a DEDENT is needed after the "print a \n" and before the "b". The sequence should be
... 1 COLON NEWLINE INDENT PRINT a NEWLINE DEDENT b ASSIGN 3 ...For more examples, see the big comment at the bottom of this file. This TokenStream normally just passes tokens through to the parser. Upon NEWLINE token from the lexer, however, an INDENT or DEDENT token may need to be sent to the parser. The NEWLINE is the trigger for this class to do it's job. NEWLINE is saved and then the first token of the next line is examined. If non-leading-whitespace token, then check against stack for indent vs dedent. If LEADING_WS, then the column of the next non-whitespace token will dictate indent vs dedent. The column of the next real token is number of spaces in the LEADING_WS token + 1 (to move past the whitespace). The lexer grammar must set the text of the LEADING_WS token to be the proper number of spaces (and do tab conversion etc...). A stack of column numbers is tracked and used to detect changes in indent level from one token to the next. A queue of tokens is built up to hold multiple DEDENT tokens that are generated. Before asking the lexer for another token via nextToken(), the queue is flushed first one token at a time. Terence Parr and Loring Craymer February 2004
Modifier and Type | Field and Description |
---|---|
static int |
FIRST_CHAR_POSITION |
static int |
MAX_INDENTS |
Constructor and Description |
---|
PythonTokenSource(org.antlr.runtime.CommonTokenStream stream,
java.lang.String filename) |
PythonTokenSource(org.antlr.runtime.CommonTokenStream stream,
java.lang.String filename,
boolean single) |
PythonTokenSource(org.python.antlr.PythonLexer lexer) |
Modifier and Type | Method and Description |
---|---|
protected int |
findPreviousIndent(int i,
org.antlr.runtime.Token t)
Return the index on stack of previous indent level == i else -1
|
java.lang.String |
getSourceName() |
protected void |
insertImaginaryIndentDedentTokens() |
org.antlr.runtime.Token |
nextToken()
From http://www.python.org/doc/2.2.3/ref/indentation.html
"Before the first line of the file is read, a single zero is
pushed on the stack; this will never be popped off again.
|
protected int |
peek() |
protected int |
pop() |
protected void |
push(int i) |
java.lang.String |
stackString() |
public static final int MAX_INDENTS
public static final int FIRST_CHAR_POSITION
public PythonTokenSource(org.python.antlr.PythonLexer lexer)
public PythonTokenSource(org.antlr.runtime.CommonTokenStream stream, java.lang.String filename)
public PythonTokenSource(org.antlr.runtime.CommonTokenStream stream, java.lang.String filename, boolean single)
public org.antlr.runtime.Token nextToken()
nextToken
in interface org.antlr.runtime.TokenSource
protected void insertImaginaryIndentDedentTokens()
protected void push(int i)
protected int pop()
protected int peek()
protected int findPreviousIndent(int i, org.antlr.runtime.Token t)
public java.lang.String stackString()
public java.lang.String getSourceName()
getSourceName
in interface org.antlr.runtime.TokenSource