From 0a448b12020f659e651d2f65688a8887b73090e6 Mon Sep 17 00:00:00 2001 From: Isaiah Peng Date: Fri, 22 Apr 2016 12:17:39 +0200 Subject: [PATCH] #7 None, True, False are NameConstant now --- ast/Python.asdl | 1 + grammar/Python.g | 11 +- grammar/PythonPartial.g | 3 + src/org/python/antlr/ast/NameConstant.java | 169 +++++++++++++++++++++ src/org/python/antlr/ast/VisitorBase.java | 6 + src/org/python/antlr/ast/VisitorIF.java | 1 + src/org/python/compiler/CodeCompiler.java | 7 + 7 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/org/python/antlr/ast/NameConstant.java diff --git a/ast/Python.asdl b/ast/Python.asdl index 6cc030039..e8aeb6fee 100644 --- a/ast/Python.asdl +++ b/ast/Python.asdl @@ -80,6 +80,7 @@ module Python version "$Revision$" | Num(object n) -- a number as a PyObject. | Str(string s) -- need to specify raw, unicode, etc? -- other literals? bools? + | NameConstant(identifier value) | Ellipsis -- the following expression can appear in assignment context diff --git a/grammar/Python.g b/grammar/Python.g index 6b918b617..ffa0cc2ef 100644 --- a/grammar/Python.g +++ b/grammar/Python.g @@ -125,6 +125,7 @@ import org.python.antlr.ast.ListComp; import org.python.antlr.ast.Lambda; import org.python.antlr.ast.Module; import org.python.antlr.ast.Name; +import org.python.antlr.ast.NameConstant; import org.python.antlr.ast.Nonlocal; import org.python.antlr.ast.Num; import org.python.antlr.ast.operatorType; @@ -1859,7 +1860,7 @@ power // '[' [listmaker] ']' | // '{' [dictorsetmaker] '}' | // '`' testlist1 '`' | -// NAME | NUMBER | STRING+) +// NAME | NUMBER | STRING+ | '...' | 'True' | 'False' | 'None') atom returns [Token lparen = null] @init { @@ -1908,6 +1909,10 @@ atom { etype = new Repr($lb, actions.castExpr($testlist.tree)); } + | NAME_CONSTANT + { + etype = new NameConstant($NAME_CONSTANT.tree); + } | name_or_print { etype = new Name($name_or_print.start, $name_or_print.text, $expr::ctype); @@ -1928,7 +1933,7 @@ atom { etype = new Num($COMPLEX, actions.makeComplex($COMPLEX)); } - | d1=DOT DOT DOT + | d1=ELLIPSIS { etype = new Ellipsis($d1); } @@ -2550,6 +2555,8 @@ NAME: ( 'a' .. 'z' | 'A' .. 'Z' | '_') ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* ; +NAME_CONSTANT: 'None' | 'True' | 'False' ; + /** Match various string types. Note that greedy=false implies ''' * should make us exit loop not continue. */ diff --git a/grammar/PythonPartial.g b/grammar/PythonPartial.g index 9f5063dd3..e86e6394b 100644 --- a/grammar/PythonPartial.g +++ b/grammar/PythonPartial.g @@ -736,6 +736,7 @@ atom | FLOAT | COMPLEX | ELLIPSIS + | NAME_CONSTANT | (STRING)+ | TRISTRINGPART | STRINGPART TRAILBACKSLASH @@ -1055,6 +1056,8 @@ COMPLEX fragment DIGITS : ( '0' .. '9' )+ ; +NAME_CONSTANT: 'None' | 'True' | 'False' ; + NAME: ( 'a' .. 'z' | 'A' .. 'Z' | '_') ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* ; diff --git a/src/org/python/antlr/ast/NameConstant.java b/src/org/python/antlr/ast/NameConstant.java new file mode 100644 index 000000000..639023553 --- /dev/null +++ b/src/org/python/antlr/ast/NameConstant.java @@ -0,0 +1,169 @@ +// Autogenerated AST node +package org.python.antlr.ast; +import org.antlr.runtime.CommonToken; +import org.antlr.runtime.Token; +import org.python.antlr.AST; +import org.python.antlr.PythonTree; +import org.python.antlr.adapter.AstAdapters; +import org.python.antlr.base.excepthandler; +import org.python.antlr.base.expr; +import org.python.antlr.base.mod; +import org.python.antlr.base.slice; +import org.python.antlr.base.stmt; +import org.python.core.ArgParser; +import org.python.core.AstList; +import org.python.core.Py; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyStringMap; +import org.python.core.PyType; +import org.python.core.Visitproc; +import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedSet; +import org.python.expose.ExposedType; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +@ExposedType(name = "_ast.NameConstant", base = expr.class) +public class NameConstant extends expr { +public static final PyType TYPE = PyType.fromClass(NameConstant.class); + private String value; + public String getInternalValue() { + return value; + } + @ExposedGet(name = "value") + public PyObject getValue() { + if (value == null) return Py.None; + return new PyString(value); + } + @ExposedSet(name = "value") + public void setValue(PyObject value) { + this.value = AstAdapters.py2identifier(value); + } + + + private final static PyString[] fields = + new PyString[] {new PyString("value")}; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + + private final static PyString[] attributes = + new PyString[] {new PyString("lineno"), new PyString("col_offset")}; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + + public NameConstant(PyType subType) { + super(subType); + } + public NameConstant() { + this(TYPE); + } + @ExposedNew + @ExposedMethod + public void NameConstant___init__(PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("NameConstant", args, keywords, new String[] + {"value", "lineno", "col_offset"}, 1, true); + setValue(ap.getPyObject(0, Py.None)); + int lin = ap.getInt(1, -1); + if (lin != -1) { + setLineno(lin); + } + + int col = ap.getInt(2, -1); + if (col != -1) { + setLineno(col); + } + + } + + public NameConstant(PyObject value) { + setValue(value); + } + + public NameConstant(Token token, String value) { + super(token); + this.value = value; + } + + public NameConstant(Integer ttype, Token token, String value) { + super(ttype, token); + this.value = value; + } + + public NameConstant(PythonTree tree, String value) { + super(tree); + this.value = value; + } + + @ExposedGet(name = "repr") + public String toString() { + return "NameConstant"; + } + + public String toStringTree() { + StringBuffer sb = new StringBuffer("NameConstant("); + sb.append("value="); + sb.append(dumpThis(value)); + sb.append(","); + sb.append(")"); + return sb.toString(); + } + + public R accept(VisitorIF visitor) throws Exception { + return visitor.visitNameConstant(this); + } + + public void traverse(VisitorIF visitor) throws Exception { + } + + public PyObject __dict__; + + @Override + public PyObject fastGetDict() { + ensureDict(); + return __dict__; + } + + @ExposedGet(name = "__dict__") + public PyObject getDict() { + return fastGetDict(); + } + + private void ensureDict() { + if (__dict__ == null) { + __dict__ = new PyStringMap(); + } + } + + private int lineno = -1; + @ExposedGet(name = "lineno") + public int getLineno() { + if (lineno != -1) { + return lineno; + } + return getLine(); + } + + @ExposedSet(name = "lineno") + public void setLineno(int num) { + lineno = num; + } + + private int col_offset = -1; + @ExposedGet(name = "col_offset") + public int getCol_offset() { + if (col_offset != -1) { + return col_offset; + } + return getCharPositionInLine(); + } + + @ExposedSet(name = "col_offset") + public void setCol_offset(int num) { + col_offset = num; + } + +} diff --git a/src/org/python/antlr/ast/VisitorBase.java b/src/org/python/antlr/ast/VisitorBase.java index f916d68a1..a2b6a0870 100644 --- a/src/org/python/antlr/ast/VisitorBase.java +++ b/src/org/python/antlr/ast/VisitorBase.java @@ -301,6 +301,12 @@ public R visitStr(Str node) throws Exception { return ret; } + public R visitNameConstant(NameConstant node) throws Exception { + R ret = unhandled_node(node); + traverse(node); + return ret; + } + public R visitEllipsis(Ellipsis node) throws Exception { R ret = unhandled_node(node); traverse(node); diff --git a/src/org/python/antlr/ast/VisitorIF.java b/src/org/python/antlr/ast/VisitorIF.java index cda7c9995..953477d4e 100644 --- a/src/org/python/antlr/ast/VisitorIF.java +++ b/src/org/python/antlr/ast/VisitorIF.java @@ -48,6 +48,7 @@ public interface VisitorIF { public R visitRepr(Repr node) throws Exception; public R visitNum(Num node) throws Exception; public R visitStr(Str node) throws Exception; + public R visitNameConstant(NameConstant node) throws Exception; public R visitEllipsis(Ellipsis node) throws Exception; public R visitAttribute(Attribute node) throws Exception; public R visitSubscript(Subscript node) throws Exception; diff --git a/src/org/python/compiler/CodeCompiler.java b/src/org/python/compiler/CodeCompiler.java index 36c9b5ac5..a16131a4b 100644 --- a/src/org/python/compiler/CodeCompiler.java +++ b/src/org/python/compiler/CodeCompiler.java @@ -2495,6 +2495,13 @@ public Object visitStarred(Starred node) throws Exception { return null; } + @Override + public Object visitNameConstant(NameConstant node) throws Exception { + String name = node.getInternalValue(); + code.getstatic(p(Py.class), name, ci(PyObject.class)); + return null; + } + @Override public Object visitName(Name node) throws Exception { String name;