Kaydet (Commit) dde00289 authored tarafından Georg Brandl's avatar Georg Brandl

Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.

üst 428f0641
...@@ -107,7 +107,7 @@ power: atom trailer* ['**' factor] ...@@ -107,7 +107,7 @@ power: atom trailer* ['**' factor]
atom: ('(' [yield_expr|testlist_gexp] ')' | atom: ('(' [yield_expr|testlist_gexp] ')' |
'[' [listmaker] ']' | '[' [listmaker] ']' |
'{' [dictsetmaker] '}' | '{' [dictsetmaker] '}' |
NAME | NUMBER | STRING+ | '.' '.' '.') NAME | NUMBER | STRING+ | '...')
listmaker: test ( list_for | (',' test)* [','] ) listmaker: test ( list_for | (',' test)* [','] )
testlist_gexp: test ( gen_for | (',' test)* [','] ) testlist_gexp: test ( gen_for | (',' test)* [','] )
lambdef: 'lambda' [varargslist] ':' test lambdef: 'lambda' [varargslist] ':' test
......
...@@ -59,10 +59,11 @@ extern "C" { ...@@ -59,10 +59,11 @@ extern "C" {
#define DOUBLESLASHEQUAL 49 #define DOUBLESLASHEQUAL 49
#define AT 50 #define AT 50
#define RARROW 51 #define RARROW 51
#define ELLIPSIS 52
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */ /* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
#define OP 52 #define OP 53
#define ERRORTOKEN 53 #define ERRORTOKEN 54
#define N_TOKENS 54 #define N_TOKENS 55
/* Special definitions for cooperation with parser */ /* Special definitions for cooperation with parser */
......
...@@ -113,7 +113,7 @@ class Transformer: ...@@ -113,7 +113,7 @@ class Transformer:
token.LBRACE: self.atom_lbrace, token.LBRACE: self.atom_lbrace,
token.NUMBER: self.atom_number, token.NUMBER: self.atom_number,
token.STRING: self.atom_string, token.STRING: self.atom_string,
token.DOT: self.atom_ellipsis, token.ELLIPSIS: self.atom_ellipsis,
token.NAME: self.atom_name, token.NAME: self.atom_name,
} }
self.encoding = None self.encoding = None
......
...@@ -121,6 +121,7 @@ the \'lazy\' dog.\n\ ...@@ -121,6 +121,7 @@ the \'lazy\' dog.\n\
def testEllipsis(self): def testEllipsis(self):
x = ... x = ...
self.assert_(x is Ellipsis) self.assert_(x is Ellipsis)
self.assertRaises(SyntaxError, eval, ".. .")
class GrammarTests(unittest.TestCase): class GrammarTests(unittest.TestCase):
......
...@@ -61,9 +61,10 @@ DOUBLESLASH = 48 ...@@ -61,9 +61,10 @@ DOUBLESLASH = 48
DOUBLESLASHEQUAL = 49 DOUBLESLASHEQUAL = 49
AT = 50 AT = 50
RARROW = 51 RARROW = 51
OP = 52 ELLIPSIS = 52
ERRORTOKEN = 53 OP = 53
N_TOKENS = 54 ERRORTOKEN = 54
N_TOKENS = 55
NT_OFFSET = 256 NT_OFFSET = 256
#--end constants-- #--end constants--
......
...@@ -83,7 +83,7 @@ Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"!=", ...@@ -83,7 +83,7 @@ Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"!=",
r"~") r"~")
Bracket = '[][(){}]' Bracket = '[][(){}]'
Special = group(r'\r?\n', r'[:;.,@]') Special = group(r'\r?\n', r'\.\.\.', r'[:;.,@]')
Funny = group(Operator, Bracket, Special) Funny = group(Operator, Bracket, Special)
PlainToken = group(Number, Funny, String, Name) PlainToken = group(Number, Funny, String, Name)
...@@ -334,8 +334,8 @@ def generate_tokens(readline): ...@@ -334,8 +334,8 @@ def generate_tokens(readline):
spos, epos, pos = (lnum, start), (lnum, end), end spos, epos, pos = (lnum, start), (lnum, end), end
token, initial = line[start:end], line[start] token, initial = line[start:end], line[start]
if initial in numchars or \ if (initial in numchars or # ordinary number
(initial == '.' and token != '.'): # ordinary number (initial == '.' and token != '.' and token != '...')):
yield (NUMBER, token, spos, epos, line) yield (NUMBER, token, spos, epos, line)
elif initial in '\r\n': elif initial in '\r\n':
yield (NL if parenlev > 0 else NEWLINE, yield (NL if parenlev > 0 else NEWLINE,
......
...@@ -93,6 +93,7 @@ char *_PyParser_TokenNames[] = { ...@@ -93,6 +93,7 @@ char *_PyParser_TokenNames[] = {
"DOUBLESLASHEQUAL", "DOUBLESLASHEQUAL",
"AT", "AT",
"RARROW", "RARROW",
"ELLIPSIS",
/* This table must match the #defines in token.h! */ /* This table must match the #defines in token.h! */
"OP", "OP",
"<ERRORTOKEN>", "<ERRORTOKEN>",
...@@ -1082,6 +1083,16 @@ PyToken_ThreeChars(int c1, int c2, int c3) ...@@ -1082,6 +1083,16 @@ PyToken_ThreeChars(int c1, int c2, int c3)
break; break;
} }
break; break;
case '.':
switch (c2) {
case '.':
switch (c3) {
case '.':
return ELLIPSIS;
}
break;
}
break;
} }
return OP; return OP;
} }
...@@ -1278,13 +1289,22 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end) ...@@ -1278,13 +1289,22 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end)
c = tok_nextc(tok); c = tok_nextc(tok);
if (isdigit(c)) { if (isdigit(c)) {
goto fraction; goto fraction;
} } else if (c == '.') {
else { c = tok_nextc(tok);
if (c == '.') {
*p_start = tok->start;
*p_end = tok->cur;
return ELLIPSIS;
} else {
tok_backup(tok, c);
}
tok_backup(tok, '.');
} else {
tok_backup(tok, c); tok_backup(tok, c);
*p_start = tok->start;
*p_end = tok->cur;
return DOT;
} }
*p_start = tok->start;
*p_end = tok->cur;
return DOT;
} }
/* Number */ /* Number */
......
...@@ -1410,7 +1410,7 @@ ast_for_atom(struct compiling *c, const node *n) ...@@ -1410,7 +1410,7 @@ ast_for_atom(struct compiling *c, const node *n)
PyArena_AddPyObject(c->c_arena, pynum); PyArena_AddPyObject(c->c_arena, pynum);
return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
} }
case DOT: /* Ellipsis */ case ELLIPSIS: /* Ellipsis */
return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
case LPAR: /* some parenthesized expressions */ case LPAR: /* some parenthesized expressions */
ch = CHILD(n, 1); ch = CHILD(n, 1);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment