Skip to content

Commit

Permalink
jython#7 None, True, False are NameConstant now
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiah committed Apr 22, 2016
1 parent 6b918f5 commit 0a448b1
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 2 deletions.
1 change: 1 addition & 0 deletions ast/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions grammar/Python.g
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1859,7 +1860,7 @@ power
// '[' [listmaker] ']' |
// '{' [dictorsetmaker] '}' |
// '`' testlist1 '`' |
// NAME | NUMBER | STRING+)
// NAME | NUMBER | STRING+ | '...' | 'True' | 'False' | 'None')
atom
returns [Token lparen = null]
@init {
Expand Down Expand Up @@ -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);
Expand All @@ -1928,7 +1933,7 @@ atom
{
etype = new Num($COMPLEX, actions.makeComplex($COMPLEX));
}
| d1=DOT DOT DOT
| d1=ELLIPSIS
{
etype = new Ellipsis($d1);
}
Expand Down Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions grammar/PythonPartial.g
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ atom
| FLOAT
| COMPLEX
| ELLIPSIS
| NAME_CONSTANT
| (STRING)+
| TRISTRINGPART
| STRINGPART TRAILBACKSLASH
Expand Down Expand Up @@ -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' )*
;
Expand Down
169 changes: 169 additions & 0 deletions src/org/python/antlr/ast/NameConstant.java
Original file line number Diff line number Diff line change
@@ -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> R accept(VisitorIF<R> 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;
}

}
6 changes: 6 additions & 0 deletions src/org/python/antlr/ast/VisitorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/org/python/antlr/ast/VisitorIF.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface VisitorIF<R> {
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;
Expand Down
7 changes: 7 additions & 0 deletions src/org/python/compiler/CodeCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0a448b1

Please sign in to comment.