Skip to content

Commit

Permalink
jython#10 Ellipsis (...) should be expr
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiah committed Apr 20, 2016
1 parent 0b33367 commit 6cfeac7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
3 changes: 2 additions & 1 deletion 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?
| Ellipsis

-- the following expression can appear in assignment context
| Attribute(expr value, identifier attr, expr_context ctx)
Expand All @@ -94,7 +95,7 @@ module Python version "$Revision$"

expr_context = Load | Store | Del | AugLoad | AugStore | Param

slice = Ellipsis | Slice(expr? lower, expr? upper, expr? step)
slice = Slice(expr? lower, expr? upper, expr? step)
| ExtSlice(slice* dims)
| Index(expr value)

Expand Down
14 changes: 7 additions & 7 deletions grammar/Python.g
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,10 @@ atom
{
etype = new Num($COMPLEX, actions.makeComplex($COMPLEX));
}
| d1=DOT DOT DOT
{
etype = new Ellipsis($d1);
}
| (S+=STRING)+
{
etype = new Str(actions.extractStringToken($S), actions.extractStrings($S, encoding, unicodeLiterals));
Expand Down Expand Up @@ -2054,17 +2058,13 @@ subscriptlist[Token begin]
}
;
//subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
//subscript: test | [test] ':' [test] [sliceop]
subscript
returns [slice sltype]
@after {
$subscript.tree = $sltype;
}
: d1=DOT DOT DOT
{
$sltype = new Ellipsis($d1);
}
| (test[null] COLON)
: (test[null] COLON)
=> lower=test[expr_contextType.Load] (c1=COLON (upper1=test[expr_contextType.Load])? (sliceop)?)?
{
$sltype = actions.makeSubscript($lower.tree, $c1, $upper1.tree, $sliceop.tree);
Expand Down Expand Up @@ -2391,7 +2391,7 @@ DEF : 'def' ;
DELETE : 'del' ;
ELIF : 'elif' ;
EXCEPT : 'except' ;
EXEC : 'exec' ;
EXEC : 'exec1' ;
FINALLY : 'finally' ;
FROM : 'from' ;
FOR : 'for' ;
Expand Down
7 changes: 5 additions & 2 deletions grammar/PythonPartial.g
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ atom
| LONGINT
| FLOAT
| COMPLEX
| ELLIPSIS
| (STRING)+
| TRISTRINGPART
| STRINGPART TRAILBACKSLASH
Expand Down Expand Up @@ -781,7 +782,7 @@ subscriptlist
//subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
subscript
: DOT DOT DOT
: ELLIPSIS
| (test COLON)
=> test (COLON (test)? (sliceop)?)?
| (COLON)
Expand Down Expand Up @@ -906,7 +907,7 @@ DEF : 'def' ;
DELETE : 'del' ;
ELIF : 'elif' ;
EXCEPT : 'except' ;
EXEC : 'exec' ;
EXEC : 'exec1' ;
FINALLY : 'finally' ;
FROM : 'from' ;
FOR : 'for' ;
Expand Down Expand Up @@ -1014,6 +1015,8 @@ DOUBLESLASHEQUAL : '//=' ;

DOT : '.' ;

ELLIPSIS : '...' ;

AT : '@' ;

AND : 'and' ;
Expand Down
47 changes: 43 additions & 4 deletions src/org/python/antlr/ast/Ellipsis.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
import java.io.IOException;
import java.util.ArrayList;

@ExposedType(name = "_ast.Ellipsis", base = slice.class)
public class Ellipsis extends slice {
@ExposedType(name = "_ast.Ellipsis", base = expr.class)
public class Ellipsis extends expr {
public static final PyType TYPE = PyType.fromClass(Ellipsis.class);

private final static PyString[] fields = new PyString[0];
@ExposedGet(name = "_fields")
public PyString[] get_fields() { return fields; }

private final static PyString[] attributes = new PyString[0];
private final static PyString[] attributes =
new PyString[] {new PyString("lineno"), new PyString("col_offset")};
@ExposedGet(name = "_attributes")
public PyString[] get_attributes() { return attributes; }

Expand All @@ -46,7 +47,17 @@ public Ellipsis(PyType subType) {
@ExposedMethod
public void Ellipsis___init__(PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("Ellipsis", args, keywords, new String[]
{}, 0, true);
{"lineno", "col_offset"}, 0, true);
int lin = ap.getInt(0, -1);
if (lin != -1) {
setLineno(lin);
}

int col = ap.getInt(1, -1);
if (col != -1) {
setLineno(col);
}

}

public Ellipsis() {
Expand Down Expand Up @@ -101,4 +112,32 @@ private void ensureDict() {
}
}

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;
}

}
12 changes: 6 additions & 6 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 visitEllipsis(Ellipsis node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}

public R visitAttribute(Attribute node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
Expand Down Expand Up @@ -337,12 +343,6 @@ public R visitTuple(Tuple node) throws Exception {
return ret;
}

public R visitEllipsis(Ellipsis node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}

public R visitSlice(Slice node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
Expand Down
2 changes: 1 addition & 1 deletion src/org/python/antlr/ast/VisitorIF.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ 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 visitEllipsis(Ellipsis node) throws Exception;
public R visitAttribute(Attribute node) throws Exception;
public R visitSubscript(Subscript node) throws Exception;
public R visitStarred(Starred node) throws Exception;
public R visitName(Name node) throws Exception;
public R visitList(List node) throws Exception;
public R visitTuple(Tuple node) throws Exception;
public R visitEllipsis(Ellipsis node) throws Exception;
public R visitSlice(Slice node) throws Exception;
public R visitExtSlice(ExtSlice node) throws Exception;
public R visitIndex(Index node) throws Exception;
Expand Down

0 comments on commit 6cfeac7

Please sign in to comment.