Skip to content

Commit 1cdae49

Browse files
committed
[GR-18396] Don't create parse tree in ANTLR parser.
PullRequest: graalpython/665
2 parents d009a6c + b62fb27 commit 1cdae49

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private static Python3NewParser getPython3NewParser(Source source, ParserErrorCa
7171
lexer.removeErrorListeners();
7272
lexer.addErrorListener(Builder.ERROR_LISTENER);
7373
Python3NewParser parser = new Python3NewParser(new CommonTokenStream(lexer));
74+
parser.setBuildParseTree(false);
7475
parser.factory = new PythonNodeFactory(errors.getLanguage(), source);
7576
parser.removeErrorListeners();
7677
parser.addErrorListener(Builder.ERROR_LISTENER);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/BuilderNew.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static Python3NewParser createParser(CharStream input) {
5757
lexer.removeErrorListeners();
5858
lexer.addErrorListener(Builder.ERROR_LISTENER);
5959
Python3NewParser parser = new Python3NewParser(new CommonTokenStream(lexer));
60+
parser.setBuildParseTree(false);
6061
parser.removeErrorListeners();
6162
parser.addErrorListener(Builder.ERROR_LISTENER);
6263
return parser;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3New.g4

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ import java.util.Arrays;
269269
// We don't have to have new lines in the source section
270270
int tokenIndex = token.getTokenIndex();
271271
Token tmp = token;
272-
while(tmp.getType() == NEWLINE && tokenIndex > -1) {
272+
while(tmp.getType() == NEWLINE && tokenIndex > 0) {
273273
tmp = getTokenStream().get(--tokenIndex);
274274
}
275275
stopIndex = tmp.getStopIndex();
@@ -280,17 +280,8 @@ import java.util.Arrays;
280280

281281
/** Get the last offset of the context */
282282
private int getLastIndex(ParserRuleContext ctx) {
283-
if (ctx.getStop() != null) {
284-
return getStopIndex(ctx);
285-
}
286-
ParseTree pt = ctx.getChild(ctx.getChildCount() - 1);
287-
if (pt instanceof TerminalNode) {
288-
return getStopIndex(((TerminalNode) pt).getSymbol());
289-
}
290-
if (pt instanceof RuleNode) {
291-
return getStopIndex((RuleNode) pt);
292-
}
293-
return -1;
283+
// ignores ctx
284+
return getStopIndex(this._input.get(this._input.index() - 1));
294285
}
295286
}
296287

@@ -385,12 +376,12 @@ decorated:
385376
async_funcdef: ASYNC funcdef;
386377
funcdef
387378
:
388-
'def' NAME parameters
379+
'def' n=NAME parameters
389380
(
390381
'->' test
391382
)? ':'
392383
{
393-
String name = _localctx.NAME().getText();
384+
String name = $n.getText();
394385
ScopeInfo enclosingScope = factory.getCurrentScope();
395386
String enclosingClassName = enclosingScope.isInClassScope() ? enclosingScope.getScopeId() : null;
396387
ScopeInfo functionScope = factory.createScope(name, ScopeInfo.ScopeKind.Function);
@@ -1395,10 +1386,11 @@ setlisttuplemaker [PythonBuiltinClassType type, PythonBuiltinClassType compType]
13951386
star_expr { push($star_expr.result); }
13961387
)
13971388
)*
1398-
','?
1389+
{ boolean comma = false; }
1390+
(',' { comma = true; } )?
13991391
{
14001392
SSTNode[] items = getArray(start, SSTNode[].class);
1401-
if ($type == PythonBuiltinClassType.PTuple && items.length == 1 && !$ctx.getText().endsWith(",")) {
1393+
if ($type == PythonBuiltinClassType.PTuple && items.length == 1 && !comma) {
14021394
$result = items[0];
14031395
} else {
14041396
$result = new CollectionSSTNode(items, $type, -1, -1);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/BlockSSTNode.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141

4242
package com.oracle.graal.python.parser.sst;
4343

44-
/**
45-
*
46-
* @author petr
47-
*/
4844
public class BlockSSTNode extends SSTNode {
4945
protected final SSTNode[] statements;
5046

0 commit comments

Comments
 (0)