Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10 Ellipsis (...) should be expr #15

Merged
merged 2 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1062,7 +1062,7 @@ import_name
}
;

//import_from: ('from' ('.'* dotted_name | '.'+)
//import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
// 'import' ('*' | '(' import_as_names ')' | import_as_names))
import_from
@init {
Expand Down Expand Up @@ -1928,6 +1928,10 @@ atom
{
etype = new Num($COMPLEX, actions.makeComplex($COMPLEX));
}
| d=DOT DOT DOT
{
etype = new Ellipsis($d);
}
| (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
8 changes: 4 additions & 4 deletions grammar/PythonPartial.g
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ import_name
: IMPORT dotted_as_names
;

//import_from: ('from' ('.'* dotted_name | '.'+)
//import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
// 'import' ('*' | '(' import_as_names ')' | import_as_names))
import_from
: FROM (DOT* dotted_name | DOT+) IMPORT
Expand Down Expand Up @@ -735,6 +735,7 @@ atom
| LONGINT
| FLOAT
| COMPLEX
| DOT DOT DOT
| (STRING)+
| TRISTRINGPART
| STRINGPART TRAILBACKSLASH
Expand Down Expand Up @@ -779,10 +780,9 @@ subscriptlist
: subscript (options {greedy=true;}:COMMA subscript)* (COMMA)?
;

//subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
//subscript: test | [test] ':' [test] [sliceop]
subscript
: DOT DOT DOT
| (test COLON)
: (test COLON)
=> test (COLON (test)? (sliceop)?)?
| (COLON)
=> COLON (test)? (sliceop)?
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