diff --git a/ast/Python.asdl b/ast/Python.asdl index 98753fa7b..6cc030039 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? + | Ellipsis -- the following expression can appear in assignment context | Attribute(expr value, identifier attr, expr_context ctx) @@ -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) diff --git a/grammar/Python.g b/grammar/Python.g index 56dbb3f03..6b918b617 100644 --- a/grammar/Python.g +++ b/grammar/Python.g @@ -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)); @@ -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); @@ -2391,7 +2391,7 @@ DEF : 'def' ; DELETE : 'del' ; ELIF : 'elif' ; EXCEPT : 'except' ; -EXEC : 'exec' ; +EXEC : 'exec1' ; FINALLY : 'finally' ; FROM : 'from' ; FOR : 'for' ; diff --git a/grammar/PythonPartial.g b/grammar/PythonPartial.g index 022e82d11..9f5063dd3 100644 --- a/grammar/PythonPartial.g +++ b/grammar/PythonPartial.g @@ -735,6 +735,7 @@ atom | LONGINT | FLOAT | COMPLEX + | ELLIPSIS | (STRING)+ | TRISTRINGPART | STRINGPART TRAILBACKSLASH @@ -781,7 +782,7 @@ subscriptlist //subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] subscript - : DOT DOT DOT + : ELLIPSIS | (test COLON) => test (COLON (test)? (sliceop)?)? | (COLON) @@ -906,7 +907,7 @@ DEF : 'def' ; DELETE : 'del' ; ELIF : 'elif' ; EXCEPT : 'except' ; -EXEC : 'exec' ; +EXEC : 'exec1' ; FINALLY : 'finally' ; FROM : 'from' ; FOR : 'for' ; @@ -1014,6 +1015,8 @@ DOUBLESLASHEQUAL : '//=' ; DOT : '.' ; +ELLIPSIS : '...' ; + AT : '@' ; AND : 'and' ; diff --git a/src/org/python/antlr/ast/Ellipsis.java b/src/org/python/antlr/ast/Ellipsis.java index 0224d901e..74f91fe34 100644 --- a/src/org/python/antlr/ast/Ellipsis.java +++ b/src/org/python/antlr/ast/Ellipsis.java @@ -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; } @@ -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() { @@ -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; + } + } diff --git a/src/org/python/antlr/ast/VisitorBase.java b/src/org/python/antlr/ast/VisitorBase.java index 7970a826e..f916d68a1 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 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); @@ -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); diff --git a/src/org/python/antlr/ast/VisitorIF.java b/src/org/python/antlr/ast/VisitorIF.java index 2b96dca5a..cda7c9995 100644 --- a/src/org/python/antlr/ast/VisitorIF.java +++ b/src/org/python/antlr/ast/VisitorIF.java @@ -48,13 +48,13 @@ 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 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;