Skip to content

Commit 4900bd9

Browse files
Merge pull request #997 from Thirumalai-Shaktivel/type_ignore
2 parents 46bede2 + 27f419a commit 4900bd9

File tree

5 files changed

+90
-64
lines changed

5 files changed

+90
-64
lines changed

src/lpython/parser/parser.yy

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
180180
%type <ast> id
181181
%type <ast> expr
182182
%type <vec_ast> expr_list
183+
%type <vec_ast> func_call_expr_list
183184
%type <vec_ast> expr_list_opt
184185
%type <ast> tuple_list
185186
%type <ast> statement
@@ -314,6 +315,7 @@ statements
314315

315316
sep_statements
316317
: sep statements { $$ = $2; }
318+
| type_ignore_sep statements { $$ = $2; }
317319
;
318320

319321
body_stmts
@@ -345,10 +347,17 @@ single_line_multi_statements_opt
345347
| single_line_statement ";" { LIST_NEW($$); LIST_ADD($$, $1); }
346348
;
347349

350+
type_ignore_sep
351+
: TK_TYPE_IGNORE sep { extract_type_comment(p, @$, $1); }
352+
| sep TK_TYPE_IGNORE sep { extract_type_comment(p, @$, $2); }
353+
;
354+
348355
statement
349356
: single_line_statement sep { $$ = $1; }
357+
| single_line_statement type_ignore_sep { $$ = $1; }
350358
| multi_line_statement
351359
| multi_line_statement sep { $$ = $1; }
360+
| multi_line_statement type_ignore_sep { $$ = $1; }
352361
;
353362

354363
single_line_statement
@@ -419,7 +428,8 @@ target_list
419428

420429
assignment_statement
421430
: target_list tuple_list { $$ = ASSIGNMENT($1, $2, @$); }
422-
| target_list expr type_comment { $$ = ASSIGNMENT2($1, $2, $3, @$); }
431+
| target_list tuple_list TK_TYPE_COMMENT {
432+
$$ = ASSIGNMENT2($1, $2, $3, @$); }
423433
;
424434

425435
augassign_statement
@@ -469,6 +479,8 @@ module_as_id
469479

470480
module_item_list
471481
: module_item_list "," module_as_id { $$ = $1; PLIST_ADD($$, $3); }
482+
| module_item_list "," TK_TYPE_IGNORE module_as_id { $$ = $1;
483+
PLIST_ADD($$, $4); extract_type_comment(p, @$, $3); }
472484
| module_as_id { LIST_NEW($$); PLIST_ADD($$, $1); }
473485
;
474486

@@ -479,22 +491,26 @@ dot_list
479491
| "..." { DOT_COUNT_02(); }
480492
;
481493

494+
type_ignore_opt
495+
: TK_TYPE_IGNORE { extract_type_comment(p, @$, $1); }
496+
| "," TK_TYPE_IGNORE { extract_type_comment(p, @$, $2); }
497+
| comma_opt
498+
;
499+
482500
import_statement
483501
: KW_IMPORT module_item_list { $$ = IMPORT_01($2, @$); }
484-
| KW_IMPORT module_item_list TK_TYPE_IGNORE { $$ = IMPORT_01($2, @$);
485-
extract_type_comment(p, @$, $3); }
486502
| KW_FROM module KW_IMPORT module_item_list {
487503
$$ = IMPORT_02($2, $4, @$); }
488-
| KW_FROM module KW_IMPORT "(" module_item_list comma_opt ")" {
504+
| KW_FROM module KW_IMPORT "(" module_item_list type_ignore_opt ")" {
489505
$$ = IMPORT_02($2, $5, @$); }
490506
| KW_FROM dot_list KW_IMPORT module_item_list {
491507
$$ = IMPORT_03($4, @$); }
492508
| KW_FROM dot_list module KW_IMPORT module_item_list {
493509
$$ = IMPORT_04($3, $5, @$); }
494-
| KW_FROM dot_list KW_IMPORT "(" module_item_list comma_opt ")" {
510+
| KW_FROM dot_list KW_IMPORT "(" module_item_list type_ignore_opt ")" {
495511
$$ = IMPORT_03($5, @$); }
496-
| KW_FROM dot_list module KW_IMPORT "(" module_item_list comma_opt ")" {
497-
$$ = IMPORT_04($3, $6, @$); }
512+
| KW_FROM dot_list module KW_IMPORT "(" module_item_list type_ignore_opt ")"
513+
{ $$ = IMPORT_04($3, $6, @$); }
498514
;
499515

500516
global_statement
@@ -542,9 +558,9 @@ for_statement
542558
$$ = FOR_01($2, $4, $6, @$); }
543559
| KW_FOR for_target_list KW_IN tuple_list ":" body_stmts KW_ELSE ":"
544560
body_stmts { $$ = FOR_02($2, $4, $6, $9, @$); }
545-
| KW_FOR for_target_list KW_IN tuple_list ":" type_comment TK_NEWLINE
561+
| KW_FOR for_target_list KW_IN tuple_list ":" TK_TYPE_COMMENT sep
546562
statements { $$ = FOR_03($2, $4, $6, $8, @$); }
547-
| KW_FOR for_target_list KW_IN tuple_list ":" type_comment TK_NEWLINE
563+
| KW_FOR for_target_list KW_IN tuple_list ":" TK_TYPE_COMMENT sep
548564
statements KW_ELSE ":" body_stmts {
549565
$$ = FOR_04($2, $4, $8, $11, $6, @$); }
550566
;
@@ -591,10 +607,10 @@ with_as_items
591607
with_statement
592608
: KW_WITH expr_list ":" body_stmts { $$ = WITH($2, $4, @$); }
593609
| KW_WITH with_as_items ":" body_stmts { $$ = WITH_02($2, $4, @$); }
594-
| KW_WITH expr_list ":" type_comment TK_NEWLINE
595-
statements { $$ = WITH_01($2, $6, $4, @$); }
596-
| KW_WITH with_as_items ":" type_comment TK_NEWLINE
597-
statements { $$ = WITH_03($2, $6, $4, @$); }
610+
| KW_WITH expr_list ":" TK_TYPE_COMMENT sep statements {
611+
$$ = WITH_01($2, $6, $4, @$); }
612+
| KW_WITH with_as_items ":" TK_TYPE_COMMENT sep statements {
613+
$$ = WITH_03($2, $6, $4, @$); }
598614
;
599615

600616
decorators_opt
@@ -605,13 +621,9 @@ decorators_opt
605621
decorators
606622
: decorators "@" expr sep { $$ = $1; LIST_ADD($$, $3); }
607623
| "@" expr sep { LIST_NEW($$); LIST_ADD($$, $2); }
608-
| decorators "@" expr TK_TYPE_IGNORE TK_NEWLINE { $$ = $1;
609-
LIST_ADD($$, $3); extract_type_comment(p, @$, $4); }
610-
| "@" expr TK_TYPE_IGNORE TK_NEWLINE { LIST_NEW($$); LIST_ADD($$, $2);
611-
extract_type_comment(p, @$, $3); }
612-
| decorators "@" expr TK_TYPE_IGNORE TK_NEWLINE sep { $$ = $1;
624+
| decorators "@" expr TK_TYPE_IGNORE sep { $$ = $1;
613625
LIST_ADD($$, $3); extract_type_comment(p, @$, $4); }
614-
| "@" expr TK_TYPE_IGNORE TK_NEWLINE sep { LIST_NEW($$); LIST_ADD($$, $2);
626+
| "@" expr TK_TYPE_IGNORE sep { LIST_NEW($$); LIST_ADD($$, $2);
615627
extract_type_comment(p, @$, $3); }
616628
;
617629

@@ -677,16 +689,16 @@ function_def
677689
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
678690
body_stmts { $$ = FUNCTION_02($1, $3, $5, $8, $10, @$); }
679691
| decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
680-
type_comment TK_NEWLINE statements {
692+
TK_TYPE_COMMENT sep statements {
681693
$$ = FUNCTION_03($1, $3, $5, $10, $8, @$); }
682694
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
683-
type_comment TK_NEWLINE statements {
695+
TK_TYPE_COMMENT sep statements {
684696
$$ = FUNCTION_04($1, $3, $5, $8, $12, $10, @$); }
685697
| decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
686-
TK_NEWLINE type_comment TK_NEWLINE statements {
698+
sep TK_TYPE_COMMENT sep statements {
687699
$$ = FUNCTION_03($1, $3, $5, $11, $9, @$); }
688700
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
689-
TK_NEWLINE type_comment TK_NEWLINE statements {
701+
sep TK_TYPE_COMMENT sep statements {
690702
$$ = FUNCTION_04($1, $3, $5, $8, $13, $11, @$); }
691703
;
692704

@@ -701,18 +713,6 @@ class_def
701713
":" body_stmts { $$ = CLASS_03($1, $3, $7, $5, $10, @$); }
702714
| decorators_opt KW_CLASS id "(" keyword_items ")" ":" body_stmts {
703715
$$ = CLASS_04($1, $3, $5, $8, @$); }
704-
| decorators_opt KW_CLASS id "(" expr_list_opt ")" ":"
705-
type_comment TK_NEWLINE statements {
706-
$$ = CLASS_05($1, $3, $5, $8, $10, @$); }
707-
| decorators_opt KW_CLASS id "(" expr_list "," keyword_items ")"
708-
":" type_comment TK_NEWLINE statements {
709-
$$ = CLASS_06($1, $3, $5, $7, $10, $12, @$); }
710-
| decorators_opt KW_CLASS id "(" keyword_items "," expr_list ")"
711-
":" type_comment TK_NEWLINE statements {
712-
$$ = CLASS_06($1, $3, $7, $5, $10, $12, @$); }
713-
| decorators_opt KW_CLASS id "(" keyword_items ")" ":"
714-
type_comment TK_NEWLINE statements {
715-
$$ = CLASS_07($1, $3, $5, $8, $10, @$); }
716716
;
717717

718718
async_func_def
@@ -725,16 +725,16 @@ async_func_def
725725
| KW_ASYNC KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
726726
body_stmts { $$ = ASYNC_FUNCTION_04($3, $5, $8, $10, @$); }
727727
| decorators KW_ASYNC KW_DEF id "(" parameter_list_opt ")" ":"
728-
type_comment TK_NEWLINE statements {
728+
TK_TYPE_COMMENT sep statements {
729729
$$ = ASYNC_FUNCTION_05($1, $4, $6, $11, $9, @$); }
730730
| decorators KW_ASYNC KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
731-
type_comment TK_NEWLINE statements {
731+
TK_TYPE_COMMENT sep statements {
732732
$$ = ASYNC_FUNCTION_06($1, $4, $6, $9, $13, $11, @$); }
733733
| KW_ASYNC KW_DEF id "(" parameter_list_opt ")" ":"
734-
type_comment TK_NEWLINE statements {
734+
TK_TYPE_COMMENT sep statements {
735735
$$ = ASYNC_FUNCTION_07($3, $5, $10, $8, @$); }
736736
| KW_ASYNC KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
737-
type_comment TK_NEWLINE statements {
737+
TK_TYPE_COMMENT sep statements {
738738
$$ = ASYNC_FUNCTION_08($3, $5, $8, $12, $10, @$); }
739739
;
740740

@@ -745,10 +745,10 @@ async_for_stmt
745745
KW_ELSE ":" body_stmts {
746746
$$ = ASYNC_FOR_02($3, $5, $7, $10, @$); }
747747
| KW_ASYNC KW_FOR expr KW_IN expr ":"
748-
type_comment TK_NEWLINE statements {
748+
TK_TYPE_COMMENT sep statements {
749749
$$ = ASYNC_FOR_03($3, $5, $9, $7, @$); }
750750
| KW_ASYNC KW_FOR expr KW_IN expr ":"
751-
type_comment TK_NEWLINE statements KW_ELSE ":" body_stmts {
751+
TK_TYPE_COMMENT sep statements KW_ELSE ":" body_stmts {
752752
$$ = ASYNC_FOR_04($3, $5, $9, $12, $7, @$); }
753753
;
754754

@@ -757,10 +757,10 @@ async_with_stmt
757757
$$ = ASYNC_WITH($3, $5, @$); }
758758
| KW_ASYNC KW_WITH with_as_items ":" body_stmts {
759759
$$ = ASYNC_WITH_02($3, $5, @$); }
760-
| KW_ASYNC KW_WITH expr_list ":" type_comment
761-
TK_NEWLINE statements { $$ = ASYNC_WITH_01($3, $7, $5, @$); }
762-
| KW_ASYNC KW_WITH with_as_items ":" type_comment
763-
TK_NEWLINE statements { $$ = ASYNC_WITH_03($3, $7, $5, @$); }
760+
| KW_ASYNC KW_WITH expr_list ":" TK_TYPE_COMMENT sep statements {
761+
$$ = ASYNC_WITH_01($3, $7, $5, @$); }
762+
| KW_ASYNC KW_WITH with_as_items ":" TK_TYPE_COMMENT sep statements {
763+
$$ = ASYNC_WITH_03($3, $7, $5, @$); }
764764
;
765765

766766
while_statement
@@ -840,31 +840,40 @@ comp_for_items
840840
| comp_for { LIST_NEW($$); PLIST_ADD($$, $1); }
841841
;
842842

843+
func_call_expr_list
844+
: func_call_expr_list "," expr { $$ = $1; LIST_ADD($$, $3); }
845+
| func_call_expr_list "," TK_TYPE_IGNORE expr { $$ = $1; LIST_ADD($$, $4);
846+
extract_type_comment(p, @$, $3); }
847+
| expr { LIST_NEW($$); LIST_ADD($$, $1); }
848+
;
849+
843850
call_arguement_list
844-
: expr_list_opt { $$ = $1; }
845-
| expr_list "," { $$ = $1; }
851+
: %empty { LIST_NEW($$); }
852+
| func_call_expr_list type_ignore_opt { $$ = $1; }
846853
| expr comp_for_items { $$ = A2LIST(p.m_a, GENERATOR_EXPR($1, $2, @$)); }
847854
;
848855

849856
function_call
850857
: primary "(" call_arguement_list ")" { $$ = CALL_01($1, $3, @$); }
851-
| primary "(" expr_list "," keyword_items comma_opt ")" {
858+
| primary "(" func_call_expr_list "," keyword_items type_ignore_opt ")" {
852859
$$ = CALL_02($1, $3, $5, @$); }
853-
| primary "(" keyword_items "," expr_list comma_opt ")" {
860+
| primary "(" keyword_items "," func_call_expr_list type_ignore_opt ")" {
854861
$$ = CALL_02($1, $5, $3, @$); }
855-
| primary "(" keyword_items comma_opt ")" { $$ = CALL_03($1, $3, @$); }
862+
| primary "(" keyword_items type_ignore_opt ")" { $$ = CALL_03($1, $3, @$); }
856863
| function_call "(" call_arguement_list ")" { $$ = CALL_01($1, $3, @$); }
857-
| function_call "(" expr_list "," keyword_items comma_opt ")" {
858-
$$ = CALL_02($1, $3, $5, @$); }
859-
| function_call "(" keyword_items "," expr_list comma_opt ")" {
860-
$$ = CALL_02($1, $5, $3, @$); }
861-
| function_call "(" keyword_items comma_opt ")" { $$ = CALL_03($1, $3, @$); }
864+
| function_call "(" func_call_expr_list "," keyword_items type_ignore_opt ")"
865+
{ $$ = CALL_02($1, $3, $5, @$); }
866+
| function_call "(" keyword_items "," func_call_expr_list type_ignore_opt ")"
867+
{ $$ = CALL_02($1, $5, $3, @$); }
868+
| function_call "(" keyword_items type_ignore_opt ")" {
869+
$$ = CALL_03($1, $3, @$); }
862870
| subscript "(" call_arguement_list ")" { $$ = CALL_01($1, $3, @$); }
863-
| subscript "(" expr_list "," keyword_items comma_opt ")" {
871+
| subscript "(" func_call_expr_list "," keyword_items type_ignore_opt ")" {
864872
$$ = CALL_02($1, $3, $5, @$); }
865-
| subscript "(" keyword_items "," expr_list comma_opt ")" {
873+
| subscript "(" keyword_items "," func_call_expr_list type_ignore_opt ")" {
866874
$$ = CALL_02($1, $5, $3, @$); }
867-
| subscript "(" keyword_items comma_opt ")" { $$ = CALL_03($1, $3, @$); }
875+
| subscript "(" keyword_items type_ignore_opt ")" {
876+
$$ = CALL_03($1, $3, @$); }
868877
| "(" expr ")" "(" call_arguement_list ")" { $$ = CALL_01($2, $5, @$); }
869878
;
870879

src/lpython/parser/tokenizer.re

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,17 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
468468
indent = true;
469469
}
470470
token(yylval.string);
471-
RET(TK_TYPE_IGNORE);
471+
last_token=yytokentype::TK_NEWLINE;
472+
return yytokentype::TK_TYPE_IGNORE;
472473
}
473-
474+
474475
type_comment {
475476
if (last_token == yytokentype::TK_COLON && !parenlevel) {
476477
indent = true;
477478
}
478479
token(yylval.string);
479-
RET(TK_TYPE_COMMENT);
480+
last_token=yytokentype::TK_NEWLINE;
481+
return yytokentype::TK_TYPE_COMMENT;
480482
}
481483
482484
comment {

tests/parser/type_comment1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ def ndarray_func(x):
1717
def test(x):
1818
# type: (np.ndarray) -> np.ndarray
1919
return x
20+
21+
def test(): # type: ignore
22+
# Comment
23+
...
24+
25+
def main():
26+
27+
# type: ignore
28+
pass
29+
30+
x (x, # type: ignore
31+
y)
32+
33+
from sympy.simplify import (collect, powsimp, # type: ignore
34+
separatevars, simplify)

tests/reference/ast_new-type_comment1-710ea6c.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-type_comment1-710ea6c",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/type_comment1.py",
5-
"infile_hash": "9c79bc041758b5401f4431a53b6b333999a4e7dfe9dfabac13d83178",
5+
"infile_hash": "c4c669232bd7137cb068b0d6aaa9f4a998a0d6244b4da03ce174ea10",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-type_comment1-710ea6c.stdout",
9-
"stdout_hash": "c7019449158ebe30677a0808ad2fd8f93aebd2eee6cd90914c44cd98",
9+
"stdout_hash": "9e22c0795da6142ec862c25e6b57dd3446f11cb35082caf318ba8f2c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(Import [(pytest ())]) (FunctionDef ndarray_func ([] [(x () ())] [] [] [] [] []) [(Return (Name x Load))] [] () "(np.ndarray) -> np.ndarray") (FunctionDef test ([] [(x () ())] [] [] [] [] []) [(Return (Name x Load))] [(Name decorator1 Load) (Name decorator2 Load) (Name decorator3 Load)] () "(np.ndarray) -> np.ndarray")] [(TypeIgnore 0 "") (TypeIgnore 0 "") (TypeIgnore 0 "")])
1+
(Module [(Import [(pytest ())]) (FunctionDef ndarray_func ([] [(x () ())] [] [] [] [] []) [(Return (Name x Load))] [] () "(np.ndarray) -> np.ndarray") (FunctionDef test ([] [(x () ())] [] [] [] [] []) [(Return (Name x Load))] [(Name decorator1 Load) (Name decorator2 Load) (Name decorator3 Load)] () "(np.ndarray) -> np.ndarray") (FunctionDef test ([] [] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef main ([] [] [] [] [] [] []) [(Pass)] [] () ()) (Expr (Call (Name x Load) [(Name x Load) (Name y Load)] [])) (ImportFrom sympy.simplify [(collect ()) (powsimp ()) (separatevars ()) (simplify ())] 0)] [(TypeIgnore 0 "") (TypeIgnore 0 "") (TypeIgnore 0 "") (TypeIgnore 0 "") (TypeIgnore 0 "") (TypeIgnore 0 "") (TypeIgnore 0 "")])

0 commit comments

Comments
 (0)