Skip to content

Commit a4ede46

Browse files
author
whitequark
committedMay 5, 2015
75% grammar coverage.
1 parent f3d451d commit a4ede46

File tree

3 files changed

+364
-56
lines changed

3 files changed

+364
-56
lines changed
 

Diff for: ‎pyparser/ast.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,10 @@ class ClassDef(keywordloc, stmt, ast.ClassDef):
450450
:ivar name_loc: location of name
451451
:ivar lparen_loc: location of ``(``, if any
452452
:ivar rparen_loc: location of ``)``, if any
453+
:ivar colon_loc: location of ``:``
453454
:ivar at_locs: locations of decorator ``@``
454455
"""
455-
_locs = keywordloc._locs + ('name_loc', 'lparen_loc', 'rparen_loc', 'at_loc')
456+
_locs = keywordloc._locs + ('name_loc', 'lparen_loc', 'rparen_loc', 'colon_loc', 'at_locs')
456457
class Continue(keywordloc, stmt, ast.Continue):
457458
"""The ``continue`` statement."""
458459
class Delete(keywordloc, stmt, ast.Delete):
@@ -478,7 +479,7 @@ class Expr(stmt, ast.Expr):
478479
479480
:ivar value: (:class:`expr`) value
480481
"""
481-
class For(keywordloc, stmt, ast.While):
482+
class For(keywordloc, stmt, ast.For):
482483
"""
483484
The ``for x in y:· z·else:· t`` statement.
484485
@@ -506,13 +507,15 @@ class FunctionDef(keywordloc, stmt, ast.FunctionDef):
506507
:ivar colon_loc: location of ``:``, if any
507508
:ivar at_locs: locations of decorator ``@``
508509
"""
509-
_locs = keywordloc._locs + ('keyword_loc' 'name_loc', 'colon_loc', 'at_loc')
510+
_locs = keywordloc._locs + ('name_loc', 'colon_loc', 'at_loc')
510511
class Global(keywordloc, stmt, ast.Global):
511512
"""
512513
The ``global x, y`` statement.
513514
514-
:ivar names: (list of :class:`Name`) names
515+
:ivar names: (list of string) names
516+
:ivar name_locs: locations of names
515517
"""
518+
_locs = keywordloc._locs + ('name_locs',)
516519
class If(keywordloc, stmt, ast.If):
517520
"""
518521
The ``if x:· y·else:· z`` or ``if x:· y·elif: z· t`` statement.
@@ -603,7 +606,7 @@ class While(keywordloc, stmt, ast.While):
603606
:ivar else_loc: location of ``else``, if any
604607
:ivar else_colon_loc: location of colon after ``else``, if any
605608
"""
606-
_locs = keywordloc._locs + ('while_loc', 'while_colon_loc', 'else_loc', 'else_colon_loc')
609+
_locs = keywordloc._locs + ('while_colon_loc', 'else_loc', 'else_colon_loc')
607610
class With(keywordloc, stmt, ast.With):
608611
"""
609612
The ``with x as y:· z`` statement.
@@ -615,7 +618,7 @@ class With(keywordloc, stmt, ast.With):
615618
:ivar as_loc: location of ``as``, if any
616619
:ivar colon_loc: location of ``:``
617620
"""
618-
_locs = keywordloc._locs + ('colon_loc',)
621+
_locs = keywordloc._locs + ('as_loc', 'colon_loc')
619622

620623
class unaryop(commonloc):
621624
"""Base class for unary numeric and boolean operators."""

Diff for: ‎pyparser/parser.py

+87-49
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ def rule(parser):
228228
while True:
229229
result = separator_rule(parser)
230230
if result is unmatched:
231-
results.trailing_comma = False
231+
results.trailing_comma = None
232232
return results
233233

234-
result = inner_rule(parser)
235-
if result is unmatched:
236-
results.trailing_comma = True
234+
result_1 = inner_rule(parser)
235+
if result_1 is unmatched:
236+
results.trailing_comma = result
237237
return results
238238
else:
239-
results.append(result)
239+
results.append(result_1)
240240
return rule
241241

242242
# Python AST specific parser combinators
@@ -365,7 +365,7 @@ def decorated(self, decorators, classfuncdef):
365365
return classfuncdef
366366

367367
@action(Seq(Loc('def'), Tok('ident'), Rule('parameters'), Loc(':'), Rule('suite')))
368-
def funcdef(def_loc, ident_tok, args, colon_loc, suite):
368+
def funcdef(self, def_loc, ident_tok, args, colon_loc, suite):
369369
"""funcdef: 'def' NAME parameters ':' suite"""
370370
return ast.FunctionDef(name=ident_tok.value, args=args, body=suite, decorator_list=[],
371371
keyword_loc=def_loc, name_loc=ident_tok.value, colon_loc=colon_loc)
@@ -457,13 +457,17 @@ def expr_stmt(self, lhs, rhs):
457457

458458
@action(List(Rule('test'), ',', trailing=True))
459459
def print_stmt_1(self, values):
460-
return ast.Print(dest=None, values=values, nl=values.trailing,
461-
dest_loc=None)
460+
loc = values.trailing_comma.loc if values.trailing_comma else values[-1].loc
461+
nl = True if values.trailing_comma else False
462+
return ast.Print(dest=None, values=values, nl=nl,
463+
dest_loc=None, loc=loc)
462464

463465
@action(Seq(Loc('>>'), Rule('test'), Tok(','), List(Rule('test'), ',', trailing=True)))
464466
def print_stmt_2(self, dest_loc, dest, comma_tok, values):
465-
return ast.Print(dest=dest, values=values, nl=values.trailing,
466-
dest_loc=dest_loc)
467+
loc = values.trailing_comma.loc if values.trailing_comma else values[-1].loc
468+
nl = True if values.trailing_comma else False
469+
return ast.Print(dest=dest, values=values, nl=nl,
470+
dest_loc=dest_loc, loc=loc)
467471

468472
@action(Seq(Loc('print'), Alt(print_stmt_1, print_stmt_2)))
469473
def print_stmt(self, print_loc, stmt):
@@ -473,12 +477,13 @@ def print_stmt(self, print_loc, stmt):
473477
'>>' test [ (',' test)+ [','] ] )
474478
"""
475479
stmt.keyword_loc = print_loc
480+
stmt.loc = print_loc.join(stmt.loc)
476481
return stmt
477482

478483
@action(Seq(Loc('del'), Rule('exprlist')))
479484
def del_stmt(self, stmt_loc, exprs):
480485
"""del_stmt: 'del' exprlist"""
481-
return ast.Delete(targets=list(map(self._assignable, exprs)),
486+
return ast.Delete(targets=self._assignable(exprs),
482487
loc=stmt_loc.join(exprs.loc), keyword_loc=stmt_loc)
483488

484489
@action(Loc('pass'))
@@ -503,23 +508,34 @@ def continue_stmt(self, stmt_loc):
503508
@action(Seq(Loc('return'), Opt(Rule('testlist'))))
504509
def return_stmt(self, stmt_loc, values):
505510
"""return_stmt: 'return' [testlist]"""
506-
return ast.Return(loc=stmt_loc.join(values.loc), keyword_loc=stmt_loc)
511+
loc = stmt_loc
512+
if values:
513+
loc = loc.join(values.loc)
514+
return ast.Return(value=values,
515+
loc=loc, keyword_loc=stmt_loc)
507516

508-
yield_stmt = Rule('yield_expr')
509-
"""yield_stmt: yield_expr"""
517+
@action(Rule('yield_expr'))
518+
def yield_stmt(self, expr):
519+
"""yield_stmt: yield_expr"""
520+
return ast.Expr(value=expr, loc=expr.loc)
510521

511522
@action(Seq(Loc('raise'), Opt(Seq(Rule('test'),
512523
Opt(Seq(Tok(','), Rule('test'),
513524
Opt(SeqN(1, Tok(','), Rule('test')))))))))
514525
def raise_stmt(self, raise_loc, type_opt):
515526
"""raise_stmt: 'raise' [test [',' test [',' test]]]"""
516527
type_ = inst = tback = None
528+
loc = raise_loc
517529
if type_opt:
518-
_, type_, inst_opt = type_opt
530+
type_, inst_opt = type_opt
531+
loc = loc.join(type_.loc)
519532
if inst_opt:
520-
inst, tback = inst_opt
533+
_, inst, tback = inst_opt
534+
loc = loc.join(inst.loc)
535+
if tback:
536+
loc = loc.join(tback.loc)
521537
return ast.Raise(type=type_, inst=inst, tback=tback,
522-
raise_loc=raise_loc)
538+
keyword_loc=raise_loc, loc=loc)
523539

524540
import_stmt = Alt(Rule('import_name'), Rule('import_from'))
525541
"""import_stmt: import_name | import_from"""
@@ -593,27 +609,36 @@ def dotted_name(self, idents):
593609
'.'.join(list(map(lambda x: x.value, idents)))
594610

595611
@action(Seq(Loc('global'), List(Tok('ident'), ',', trailing=False)))
596-
def global_stmt(self, keyword_loc, names):
612+
def global_stmt(self, global_loc, names):
597613
"""global_stmt: 'global' NAME (',' NAME)*"""
598-
return ast.Global(names=list(map(ast.Name, names)),
599-
keyword_loc=keyword_loc)
614+
return ast.Global(names=list(map(lambda x: x.value, names)),
615+
name_locs=list(map(lambda x: x.loc, names)),
616+
keyword_loc=global_loc, loc=global_loc.join(names[-1].loc))
600617

601618
@action(Seq(Loc('exec'), Rule('expr'),
602619
Opt(Seq(Loc('in'), Rule('test'),
603620
Opt(SeqN(1, Loc(','), Rule('test')))))))
604-
def exec_stmt(self, keyword_loc, body, in_opt):
621+
def exec_stmt(self, exec_loc, body, in_opt):
605622
"""exec_stmt: 'exec' expr ['in' test [',' test]]"""
606-
in_loc, locals, globals_opt = None, None, None
623+
in_loc, locals, globals = None, None, None
624+
loc = exec_loc.join(body.loc)
607625
if in_opt:
608-
in_loc, locals, globals_opt = in_opt
626+
in_loc, locals, globals = in_opt
627+
if globals:
628+
loc = loc.join(globals.loc)
629+
else:
630+
loc = loc.join(locals.loc)
609631
return ast.Exec(body=body, locals=locals, globals=globals,
610-
keyword_loc=keyword_loc, in_loc=in_loc)
632+
loc=loc, keyword_loc=exec_loc, in_loc=in_loc)
611633

612634
@action(Seq(Loc('assert'), Rule('test'), Opt(SeqN(1, Tok(','), Rule('test')))))
613-
def assert_stmt(self, keyword_loc, test, msg):
635+
def assert_stmt(self, assert_loc, test, msg):
614636
"""assert_stmt: 'assert' test [',' test]"""
637+
loc = assert_loc.join(test.loc)
638+
if msg:
639+
loc = loc.join(msg.loc)
615640
return ast.Assert(test=test, msg=msg,
616-
keyword_loc=keyword_loc)
641+
loc=loc, keyword_loc=assert_loc)
617642

618643
@action(Alt(Rule('if_stmt'), Rule('while_stmt'), Rule('for_stmt'),
619644
Rule('try_stmt'), Rule('with_stmt'), Rule('funcdef'),
@@ -637,38 +662,42 @@ def if_stmt(self, if_loc, test, if_colon_loc, body, elifs, else_opt):
637662
for elif_ in elifs:
638663
stmt.keyword_loc, stmt.test, stmt.if_colon_loc, stmt.body = elif_
639664
stmt.loc = stmt.keyword_loc.join(stmt.body[-1].loc)
640-
if stmt.orelse: stmt.loc = stmt.loc.join(stmt.orelse[-1])
665+
if stmt.orelse: stmt.loc = stmt.loc.join(stmt.orelse[-1].loc)
641666
stmt = ast.If(orelse=[stmt],
642667
else_loc=None, else_colon_loc=None)
643668

644669
stmt.keyword_loc, stmt.test, stmt.if_colon_loc, stmt.body = \
645670
if_loc, test, if_colon_loc, body
646671
stmt.loc = stmt.keyword_loc.join(stmt.body[-1].loc)
647-
if stmt.orelse: stmt.loc = stmt.loc.join(stmt.orelse[-1])
672+
if stmt.orelse: stmt.loc = stmt.loc.join(stmt.orelse[-1].loc)
648673
return stmt
649674

650675
@action(Seq(Loc('while'), Rule('test'), Loc(':'), Rule('suite'),
651676
Opt(Seq(Loc('else'), Loc(':'), Rule('suite')))))
652-
def while_stmt(while_loc, test, while_colon_loc, body, else_opt):
677+
def while_stmt(self, while_loc, test, while_colon_loc, body, else_opt):
653678
"""while_stmt: 'while' test ':' suite ['else' ':' suite]"""
654679
stmt = ast.While(test=test, body=body, orelse=[],
655-
while_loc=while_loc, while_colon_loc=while_colon_loc,
656-
else_loc=None, else_colon_loc=None)
680+
keyword_loc=while_loc, while_colon_loc=while_colon_loc,
681+
else_loc=None, else_colon_loc=None,
682+
loc=while_loc.join(body[-1].loc))
657683
if else_opt:
658-
stmt.else_loc, stmt.else_colon_loc, orelse = else_opt
684+
stmt.else_loc, stmt.else_colon_loc, stmt.orelse = else_opt
685+
stmt.loc = stmt.loc.join(stmt.orelse[-1].loc)
659686

660687
return stmt
661688

662689
@action(Seq(Loc('for'), Rule('exprlist'), Loc('in'), Rule('testlist'),
663690
Loc(':'), Rule('suite'),
664691
Opt(Seq(Loc('else'), Loc(':'), Rule('suite')))))
665-
def for_stmt(for_loc, target, in_loc, iter, for_colon_loc, body, else_opt):
692+
def for_stmt(self, for_loc, target, in_loc, iter, for_colon_loc, body, else_opt):
666693
"""for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]"""
667694
stmt = ast.For(target=self._assignable(target), iter=iter, body=body, orelse=[],
668-
for_loc=for_loc, in_loc=in_loc, for_colon_loc=for_colon_loc,
669-
else_loc=None, else_colon_loc=None)
695+
keyword_loc=for_loc, in_loc=in_loc, for_colon_loc=for_colon_loc,
696+
else_loc=None, else_colon_loc=None,
697+
loc=for_loc.join(body[-1].loc))
670698
if else_opt:
671-
stmt.else_loc, stmt.else_colon_loc, orelse = else_opt
699+
stmt.else_loc, stmt.else_colon_loc, stmt.orelse = else_opt
700+
stmt.loc = stmt.loc.join(stmt.orelse[-1].loc)
672701

673702
return stmt
674703

@@ -716,19 +745,22 @@ def try_stmt(self, try_loc, try_colon_loc, body, stmt):
716745
return stmt
717746

718747
@action(Seq(Loc('with'), Rule('test'), Opt(Rule('with_var')), Loc(':'), Rule('suite')))
719-
def with_stmt(with_loc, context, with_var, colon_loc, body):
748+
def with_stmt(self, with_loc, context, with_var, colon_loc, body):
720749
"""with_stmt: 'with' test [ with_var ] ':' suite"""
721-
as_loc, optional_vars = with_var
722-
return ast.With(context=context, optional_vars=optional_vars, body=body,
723-
keyword_loc=with_loc, as_loc=as_loc, colon_loc=colon_loc)
750+
as_loc = optional_vars = None
751+
if with_var:
752+
as_loc, optional_vars = with_var
753+
return ast.With(context_expr=context, optional_vars=optional_vars, body=body,
754+
keyword_loc=with_loc, as_loc=as_loc, colon_loc=colon_loc,
755+
loc=with_loc.join(body[-1].loc))
724756

725757
with_var = Seq(Loc('as'), Rule('expr'))
726758
"""with_var: 'as' expr"""
727759

728760
@action(Seq(Loc('except'),
729761
Opt(Seq(Rule('test'),
730762
Opt(Seq(Alt(Loc('as'), Loc(',')), Rule('test')))))))
731-
def except_clause(except_loc, exc_opt):
763+
def except_clause(self, except_loc, exc_opt):
732764
"""except_clause: 'except' [test [('as' | ',') test]]"""
733765
type_ = name = as_loc = None
734766
if exc_opt:
@@ -738,8 +770,12 @@ def except_clause(except_loc, exc_opt):
738770
return ast.ExceptHandler(type=type_, name=name,
739771
except_loc=except_loc, as_loc=as_loc)
740772

773+
@action(Plus(Rule('stmt')))
774+
def suite_1(self, stmts):
775+
return reduce(list.__add__, stmts, [])
776+
741777
suite = Alt(Rule('simple_stmt'),
742-
SeqN(2, Tok('newline'), Tok('indent'), Plus(Rule('stmt')), Tok('dedent')))
778+
SeqN(2, Tok('newline'), Tok('indent'), suite_1, Tok('dedent')))
743779
"""suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT"""
744780

745781
# 2.x-only backwards compatibility start
@@ -1031,17 +1067,19 @@ def dictmaker(self, elts):
10311067
loc=None)
10321068

10331069
@action(Seq(Loc('class'), Tok('ident'),
1034-
Opt(BeginEnd('(', Rule('testlist'), ')')),
1070+
Opt(Seq(Loc('('), List(Rule('test'), ',', trailing=True), Loc(')'))),
10351071
Loc(':'), Rule('suite')))
1036-
def classdef(class_loc, name_tok, bases_opt, colon_loc, body):
1072+
def classdef(self, class_loc, name_tok, bases_opt, colon_loc, body):
10371073
"""classdef: 'class' NAME ['(' [testlist] ')'] ':' suite"""
1038-
bases = lparen_loc = rparen_loc = None
1074+
bases, lparen_loc, rparen_loc = [], None, None
10391075
if bases_opt:
1040-
lparen_loc, bases, rparen_loc = \
1041-
bases_opt.begin_loc, bases_opt.elts, bases_opt.end_loc
1076+
lparen_loc, bases, rparen_loc = bases_opt
10421077

1043-
return ast.ClassDef(name=name_tok.value, bases=bases, body=body, decorator_list=[],
1044-
keyword_loc=class_loc, lparen_loc=lparen_loc, rparen_loc=rparen_loc)
1078+
return ast.ClassDef(name=name_tok.value, bases=bases, body=body,
1079+
decorator_list=[], at_locs=[],
1080+
keyword_loc=class_loc, lparen_loc=lparen_loc, rparen_loc=rparen_loc,
1081+
name_loc=name_tok.loc, colon_loc=colon_loc,
1082+
loc=class_loc.join(body[-1].loc))
10451083

10461084
@action(Seq(Loc('*'), Rule('test'), Star(SeqN(1, Tok(','), Rule('argument'))),
10471085
Opt(Seq(Tok(','), Loc('**'), Rule('test')))))

Diff for: ‎pyparser/test/test_parser.py

+268-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def assertDiagnosesUnexpected(self, code, err_token, loc):
136136
ast_2 = {'ty': 'Num', 'n': 2}
137137
ast_3 = {'ty': 'Num', 'n': 3}
138138

139+
ast_expr_1 = {'ty': 'Expr', 'value': {'ty': 'Num', 'n': 1}}
140+
ast_expr_2 = {'ty': 'Expr', 'value': {'ty': 'Num', 'n': 2}}
141+
ast_expr_3 = {'ty': 'Expr', 'value': {'ty': 'Num', 'n': 3}}
142+
139143
ast_x = {'ty': 'Name', 'id': 'x', 'ctx': None}
140144
ast_y = {'ty': 'Name', 'id': 'y', 'ctx': None}
141145
ast_z = {'ty': 'Name', 'id': 'z', 'ctx': None}
@@ -649,6 +653,13 @@ def test_subscript(self):
649653
" ~~~~~ slice.loc"
650654
"~~~~~~~~ loc")
651655

656+
self.assertParsesExpr(
657+
{'ty': 'Subscript', 'value': self.ast_x, 'ctx': None,
658+
'slice': {'ty': 'Ellipsis'}},
659+
"x[...]",
660+
" ~~~ slice.loc"
661+
"~~~~~~ loc")
662+
652663
def test_attribute(self):
653664
self.assertParsesExpr(
654665
{'ty': 'Attribute', 'value': self.ast_x, 'attr': 'zz', 'ctx': None},
@@ -658,7 +669,7 @@ def test_attribute(self):
658669
"~~~~ loc")
659670

660671
#
661-
# STATEMENTS
672+
# SIMPLE STATEMENTS
662673
#
663674

664675
def test_assign(self):
@@ -675,6 +686,12 @@ def test_assign(self):
675686
" ^ 0.op_locs.0"
676687
" ^ 0.op_locs.1")
677688

689+
self.assertParsesSuite(
690+
[{'ty': 'Assign', 'targets': [self.ast_x], 'value':
691+
{'ty': 'Yield', 'value': self.ast_y}}],
692+
"x = yield y",
693+
"~~~~~~~~~~~ 0.loc")
694+
678695
def test_augassign(self):
679696
self.assertParsesSuite(
680697
[{'ty': 'AugAssign', 'op': {'ty': 'Add'}, 'target': self.ast_x, 'value': self.ast_1}],
@@ -748,6 +765,256 @@ def test_augassign(self):
748765
"~~~~~~ 0.loc"
749766
" ~~ 0.op.loc")
750767

768+
self.assertParsesSuite(
769+
[{'ty': 'AugAssign', 'op': {'ty': 'Add'}, 'target': self.ast_x, 'value':
770+
{'ty': 'Yield', 'value': self.ast_y}}],
771+
"x += yield y",
772+
"~~~~~~~~~~~~ 0.loc")
773+
774+
def test_print(self):
775+
self.assertParsesSuite(
776+
[{'ty': 'Print', 'dest': None, 'values': [self.ast_1], 'nl': False}],
777+
"print 1",
778+
"~~~~~ 0.keyword_loc"
779+
"~~~~~~~ 0.loc")
780+
781+
self.assertParsesSuite(
782+
[{'ty': 'Print', 'dest': None, 'values': [self.ast_1], 'nl': True}],
783+
"print 1,",
784+
"~~~~~ 0.keyword_loc"
785+
"~~~~~~~~ 0.loc")
786+
787+
self.assertParsesSuite(
788+
[{'ty': 'Print', 'dest': self.ast_2, 'values': [self.ast_1], 'nl': False}],
789+
"print >>2, 1",
790+
"~~~~~ 0.keyword_loc"
791+
" ~~ 0.dest_loc"
792+
"~~~~~~~~~~~~ 0.loc")
793+
794+
def test_del(self):
795+
self.assertParsesSuite(
796+
[{'ty': 'Delete', 'targets': self.ast_x}],
797+
"del x",
798+
"~~~ 0.keyword_loc"
799+
"~~~~~ 0.loc")
800+
801+
def test_pass(self):
802+
self.assertParsesSuite(
803+
[{'ty': 'Pass'}],
804+
"pass",
805+
"~~~~ 0.keyword_loc"
806+
"~~~~ 0.loc")
807+
808+
def test_break(self):
809+
self.assertParsesSuite(
810+
[{'ty': 'Break'}],
811+
"break",
812+
"~~~~~ 0.keyword_loc"
813+
"~~~~~ 0.loc")
814+
815+
def test_continue(self):
816+
self.assertParsesSuite(
817+
[{'ty': 'Continue'}],
818+
"continue",
819+
"~~~~~~~~ 0.keyword_loc"
820+
"~~~~~~~~ 0.loc")
821+
822+
def test_return(self):
823+
self.assertParsesSuite(
824+
[{'ty': 'Return', 'value': None}],
825+
"return",
826+
"~~~~~~ 0.keyword_loc"
827+
"~~~~~~ 0.loc")
828+
829+
self.assertParsesSuite(
830+
[{'ty': 'Return', 'value': self.ast_x}],
831+
"return x",
832+
"~~~~~~ 0.keyword_loc"
833+
"~~~~~~~~ 0.loc")
834+
835+
def test_yield(self):
836+
self.assertParsesSuite(
837+
[{'ty': 'Expr', 'value': {'ty': 'Yield', 'value': self.ast_x}}],
838+
"yield x",
839+
"~~~~~ 0.value.keyword_loc"
840+
"~~~~~~~ 0.value.loc"
841+
"~~~~~~~ 0.loc")
842+
843+
def test_raise(self):
844+
self.assertParsesSuite(
845+
[{'ty': 'Raise', 'type': None, 'inst': None, 'tback': None}],
846+
"raise",
847+
"~~~~~ 0.keyword_loc"
848+
"~~~~~ 0.loc")
849+
850+
self.assertParsesSuite(
851+
[{'ty': 'Raise', 'type': self.ast_x, 'inst': None, 'tback': None}],
852+
"raise x",
853+
"~~~~~ 0.keyword_loc"
854+
"~~~~~~~ 0.loc")
855+
856+
self.assertParsesSuite(
857+
[{'ty': 'Raise', 'type': self.ast_x, 'inst': self.ast_y, 'tback': None}],
858+
"raise x, y",
859+
"~~~~~ 0.keyword_loc"
860+
"~~~~~~~~~~ 0.loc")
861+
862+
self.assertParsesSuite(
863+
[{'ty': 'Raise', 'type': self.ast_x, 'inst': self.ast_y, 'tback': self.ast_z}],
864+
"raise x, y, z",
865+
"~~~~~ 0.keyword_loc"
866+
"~~~~~~~~~~~~~ 0.loc")
867+
868+
def test_global(self):
869+
self.assertParsesSuite(
870+
[{'ty': 'Global', 'names': ['x', 'y']}],
871+
"global x, y",
872+
"~~~~~~ 0.keyword_loc"
873+
" ^ 0.name_locs.0"
874+
" ^ 0.name_locs.1"
875+
"~~~~~~~~~~~ 0.loc")
876+
877+
def test_exec(self):
878+
self.assertParsesSuite(
879+
[{'ty': 'Exec', 'body': self.ast_1, 'locals': None, 'globals': None}],
880+
"exec 1",
881+
"~~~~ 0.keyword_loc"
882+
"~~~~~~ 0.loc")
883+
884+
self.assertParsesSuite(
885+
[{'ty': 'Exec', 'body': self.ast_1, 'locals': self.ast_2, 'globals': None}],
886+
"exec 1 in 2",
887+
"~~~~ 0.keyword_loc"
888+
" ~~ 0.in_loc"
889+
"~~~~~~~~~~~ 0.loc")
890+
891+
self.assertParsesSuite(
892+
[{'ty': 'Exec', 'body': self.ast_1, 'locals': self.ast_2, 'globals': self.ast_3}],
893+
"exec 1 in 2, 3",
894+
"~~~~ 0.keyword_loc"
895+
" ~~ 0.in_loc"
896+
"~~~~~~~~~~~~~~ 0.loc")
897+
898+
def test_assert(self):
899+
self.assertParsesSuite(
900+
[{'ty': 'Assert', 'test': self.ast_1, 'msg': None}],
901+
"assert 1",
902+
"~~~~~~ 0.keyword_loc"
903+
"~~~~~~~~ 0.loc")
904+
905+
self.assertParsesSuite(
906+
[{'ty': 'Assert', 'test': self.ast_1, 'msg': self.ast_2}],
907+
"assert 1, 2",
908+
"~~~~~~ 0.keyword_loc"
909+
"~~~~~~~~~~~ 0.loc")
910+
911+
#
912+
# COMPOUND STATEMENTS
913+
#
914+
915+
def test_if(self):
916+
self.assertParsesSuite(
917+
[{'ty': 'If', 'test': self.ast_x, 'body': [self.ast_expr_1], 'orelse': []}],
918+
"if x:· 1",
919+
"^^ 0.keyword_loc"
920+
" ^ 0.if_colon_loc"
921+
"~~~~~~~~~ 0.loc")
922+
923+
self.assertParsesSuite(
924+
[{'ty': 'If', 'test': self.ast_x,
925+
'body': [self.ast_expr_1], 'orelse': [self.ast_expr_2] }],
926+
"if x:· 1·else:· 2",
927+
"^^ 0.keyword_loc"
928+
" ^ 0.if_colon_loc"
929+
" ^^^^ 0.else_loc"
930+
" ^ 0.else_colon_loc"
931+
"~~~~~~~~~~~~~~~~~~~ 0.loc")
932+
933+
self.assertParsesSuite(
934+
[{'ty': 'If', 'test': self.ast_x, 'body': [self.ast_expr_1], 'orelse': [
935+
{'ty': 'If', 'test': self.ast_y, 'body': [self.ast_expr_2],
936+
'orelse': [self.ast_expr_3]}
937+
]}],
938+
"if x:· 1·elif y:· 2·else:· 3",
939+
"^^ 0.keyword_loc"
940+
" ^ 0.if_colon_loc"
941+
" ~~~~ 0.orelse.0.keyword_loc"
942+
" ^ 0.orelse.0.if_colon_loc"
943+
" ~~~~ 0.orelse.0.else_loc"
944+
" ^ 0.orelse.0.else_colon_loc"
945+
" ~~~~~~~~~~~~~~~~~~~~~ 0.orelse.0.loc"
946+
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 0.loc")
947+
948+
def test_while(self):
949+
self.assertParsesSuite(
950+
[{'ty': 'While', 'test': self.ast_x, 'body': [self.ast_expr_1], 'orelse': []}],
951+
"while x:· 1",
952+
"~~~~~ 0.keyword_loc"
953+
" ^ 0.while_colon_loc"
954+
"~~~~~~~~~~~~ 0.loc")
955+
956+
self.assertParsesSuite(
957+
[{'ty': 'While', 'test': self.ast_x, 'body': [self.ast_expr_1],
958+
'orelse': [self.ast_expr_2]}],
959+
"while x:· 1·else:· 2",
960+
"~~~~~ 0.keyword_loc"
961+
" ^ 0.while_colon_loc"
962+
" ~~~~ 0.else_loc"
963+
" ^ 0.else_colon_loc"
964+
"~~~~~~~~~~~~~~~~~~~~~~ 0.loc")
965+
966+
def test_for(self):
967+
self.assertParsesSuite(
968+
[{'ty': 'For', 'target': self.ast_x, 'iter': self.ast_y,
969+
'body': [self.ast_expr_1], 'orelse': []}],
970+
"for x in y:· 1",
971+
"~~~ 0.keyword_loc"
972+
" ~~ 0.in_loc"
973+
" ^ 0.for_colon_loc"
974+
"~~~~~~~~~~~~~~~ 0.loc")
975+
976+
self.assertParsesSuite(
977+
[{'ty': 'For', 'target': self.ast_x, 'iter': self.ast_y,
978+
'body': [self.ast_expr_1], 'orelse': [self.ast_expr_2]}],
979+
"for x in y:· 1·else:· 2",
980+
" ~~~~ 0.else_loc"
981+
" ^ 0.else_colon_loc"
982+
"~~~~~~~~~~~~~~~~~~~~~~~~~ 0.loc")
983+
984+
def test_with(self):
985+
self.assertParsesSuite(
986+
[{'ty': 'With', 'context_expr': self.ast_x, 'optional_vars': None,
987+
'body': [self.ast_expr_1]}],
988+
"with x:· 1",
989+
"~~~~ 0.keyword_loc"
990+
" ^ 0.colon_loc"
991+
"~~~~~~~~~~~ 0.loc")
992+
993+
self.assertParsesSuite(
994+
[{'ty': 'With', 'context_expr': self.ast_x, 'optional_vars': self.ast_y,
995+
'body': [self.ast_expr_1]}],
996+
"with x as y:· 1",
997+
" ~~ 0.as_loc"
998+
"~~~~~~~~~~~~~~~~ 0.loc")
999+
1000+
def test_class(self):
1001+
self.assertParsesSuite(
1002+
[{'ty': 'ClassDef', 'name': 'x', 'bases': [],
1003+
'body': [{'ty': 'Pass'}], 'decorator_list': []}],
1004+
"class x:· pass",
1005+
"~~~~~ 0.keyword_loc"
1006+
" ^ 0.name_loc"
1007+
" ^ 0.colon_loc"
1008+
"~~~~~~~~~~~~~~~ 0.loc")
1009+
1010+
self.assertParsesSuite(
1011+
[{'ty': 'ClassDef', 'name': 'x', 'bases': [self.ast_y, self.ast_z],
1012+
'body': [{'ty': 'Pass'}], 'decorator_list': []}],
1013+
"class x(y, z):· pass",
1014+
" ^ 0.lparen_loc"
1015+
" ^ 0.rparen_loc"
1016+
"~~~~~~~~~~~~~~~~~~~~~ 0.loc")
1017+
7511018
#
7521019
# PARSING MODES
7531020
#

0 commit comments

Comments
 (0)
Please sign in to comment.