@@ -105,6 +105,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
105
105
%token <string> TK_COMMENT
106
106
%token <string> TK_EOLCOMMENT
107
107
%token <string> TK_TYPE_COMMENT
108
+ %token <string> TK_TYPE_IGNORE
108
109
%token TK_POW " **"
109
110
%token TK_FLOOR_DIV " //"
110
111
%token TK_RIGHTSHIFT " >>"
@@ -245,6 +246,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
245
246
%type <ast> while_statement
246
247
%type <vec_ast> sep
247
248
%type <ast> sep_one
249
+ %type <string> type_comment
248
250
%type <ast> string
249
251
%type <ast> ternary_if_statement
250
252
/* %type <ast> list_comprehension */
@@ -421,7 +423,7 @@ assignment_statement
421
423
: target_list expr { $$ = ASSIGNMENT($1, $2, @$); }
422
424
| target_list expr_list "," expr {
423
425
$$ = ASSIGNMENT($1, TUPLE_01(TUPLE_($2, $4), @$), @$); }
424
- | target_list expr TK_TYPE_COMMENT { $$ = ASSIGNMENT2($1, $2, $3, @$); }
426
+ | target_list expr type_comment { $$ = ASSIGNMENT2($1, $2, $3, @$); }
425
427
;
426
428
427
429
augassign_statement
@@ -486,7 +488,8 @@ dot_list
486
488
487
489
import_statement
488
490
: KW_IMPORT module_item_list { $$ = IMPORT_01($2, @$); }
489
- | KW_IMPORT module_item_list TK_TYPE_COMMENT { $$ = IMPORT_01($2, @$); }
491
+ | KW_IMPORT module_item_list TK_TYPE_IGNORE { $$ = IMPORT_01($2, @$);
492
+ extract_type_comment(p, @$, $3); }
490
493
| KW_FROM module KW_IMPORT module_item_list {
491
494
$$ = IMPORT_02($2, $4, @$); }
492
495
| KW_FROM module KW_IMPORT "(" module_item_list comma_opt ")" {
@@ -541,9 +544,9 @@ for_statement
541
544
$$ = FOR_01($2, TUPLE_03(A2LIST(p.m_a, $4), @$), $7, @$); }
542
545
| KW_FOR for_target_list KW_IN expr ":" body_stmts KW_ELSE ":"
543
546
body_stmts { $$ = FOR_02($2, $4, $6, $9, @$); }
544
- | KW_FOR for_target_list KW_IN expr ":" TK_TYPE_COMMENT TK_NEWLINE
547
+ | KW_FOR for_target_list KW_IN expr ":" type_comment TK_NEWLINE
545
548
statements { $$ = FOR_03($2, $4, $6, $8, @$); }
546
- | KW_FOR for_target_list KW_IN expr ":" TK_TYPE_COMMENT TK_NEWLINE
549
+ | KW_FOR for_target_list KW_IN expr ":" type_comment TK_NEWLINE
547
550
statements KW_ELSE ":" body_stmts {
548
551
$$ = FOR_04($2, $4, $8, $11, $6, @$); }
549
552
;
@@ -585,9 +588,9 @@ with_as_items
585
588
with_statement
586
589
: KW_WITH expr_list ":" body_stmts { $$ = WITH($2, $4, @$); }
587
590
| KW_WITH with_as_items ":" body_stmts { $$ = WITH_02($2, $4, @$); }
588
- | KW_WITH expr_list ":" TK_TYPE_COMMENT TK_NEWLINE
591
+ | KW_WITH expr_list ":" type_comment TK_NEWLINE
589
592
statements { $$ = WITH_01($2, $6, $4, @$); }
590
- | KW_WITH with_as_items ":" TK_TYPE_COMMENT TK_NEWLINE
593
+ | KW_WITH with_as_items ":" type_comment TK_NEWLINE
591
594
statements { $$ = WITH_03($2, $6, $4, @$); }
592
595
;
593
596
@@ -599,6 +602,14 @@ decorators_opt
599
602
decorators
600
603
: decorators "@" expr sep { $$ = $1; LIST_ADD($$, $3); }
601
604
| "@" expr sep { LIST_NEW($$); LIST_ADD($$, $2); }
605
+ | decorators "@" expr TK_TYPE_IGNORE TK_NEWLINE { $$ = $1;
606
+ LIST_ADD($$, $3); extract_type_comment(p, @$, $4); }
607
+ | "@" expr TK_TYPE_IGNORE TK_NEWLINE { LIST_NEW($$); LIST_ADD($$, $2);
608
+ extract_type_comment(p, @$, $3); }
609
+ | decorators "@" expr TK_TYPE_IGNORE TK_NEWLINE sep { $$ = $1;
610
+ LIST_ADD($$, $3); extract_type_comment(p, @$, $4); }
611
+ | "@" expr TK_TYPE_IGNORE TK_NEWLINE sep { LIST_NEW($$); LIST_ADD($$, $2);
612
+ extract_type_comment(p, @$, $3); }
602
613
;
603
614
604
615
parameter
@@ -611,9 +622,9 @@ parameter
611
622
defparameter_list
612
623
: defparameter_list "," parameter { $$ = $1; LIST_ADD($$, $3); }
613
624
| parameter { LIST_NEW($$); LIST_ADD($$, $1); }
614
- | defparameter_list "," TK_TYPE_COMMENT parameter { $$ = $1;
625
+ | defparameter_list "," type_comment parameter { $$ = $1;
615
626
LIST_ADD($$, $4); ADD_TYPE_COMMENT($1, $3, @$); }
616
- | defparameter_list "," TK_TYPE_COMMENT {
627
+ | defparameter_list "," type_comment {
617
628
$$ = $1; ADD_TYPE_COMMENT($1, $3, @$); }
618
629
;
619
630
@@ -663,16 +674,16 @@ function_def
663
674
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
664
675
body_stmts { $$ = FUNCTION_02($1, $3, $5, $8, $10, @$); }
665
676
| decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
666
- TK_TYPE_COMMENT TK_NEWLINE statements {
677
+ type_comment TK_NEWLINE statements {
667
678
$$ = FUNCTION_03($1, $3, $5, $10, $8, @$); }
668
679
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
669
- TK_TYPE_COMMENT TK_NEWLINE statements {
680
+ type_comment TK_NEWLINE statements {
670
681
$$ = FUNCTION_04($1, $3, $5, $8, $12, $10, @$); }
671
682
| decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
672
- TK_NEWLINE TK_TYPE_COMMENT TK_NEWLINE statements {
683
+ TK_NEWLINE type_comment TK_NEWLINE statements {
673
684
$$ = FUNCTION_03($1, $3, $5, $11, $9, @$); }
674
685
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
675
- TK_NEWLINE TK_TYPE_COMMENT TK_NEWLINE statements {
686
+ TK_NEWLINE type_comment TK_NEWLINE statements {
676
687
$$ = FUNCTION_04($1, $3, $5, $8, $13, $11, @$); }
677
688
;
678
689
@@ -688,16 +699,16 @@ class_def
688
699
| decorators_opt KW_CLASS id "(" keyword_items ")" ":" body_stmts {
689
700
$$ = CLASS_04($1, $3, $5, $8, @$); }
690
701
| decorators_opt KW_CLASS id "(" expr_list_opt ")" ":"
691
- TK_TYPE_COMMENT TK_NEWLINE statements {
702
+ type_comment TK_NEWLINE statements {
692
703
$$ = CLASS_05($1, $3, $5, $8, $10, @$); }
693
704
| decorators_opt KW_CLASS id "(" expr_list "," keyword_items ")"
694
- ":" TK_TYPE_COMMENT TK_NEWLINE statements {
705
+ ":" type_comment TK_NEWLINE statements {
695
706
$$ = CLASS_06($1, $3, $5, $7, $10, $12, @$); }
696
707
| decorators_opt KW_CLASS id "(" keyword_items "," expr_list ")"
697
- ":" TK_TYPE_COMMENT TK_NEWLINE statements {
708
+ ":" type_comment TK_NEWLINE statements {
698
709
$$ = CLASS_06($1, $3, $7, $5, $10, $12, @$); }
699
710
| decorators_opt KW_CLASS id "(" keyword_items ")" ":"
700
- TK_TYPE_COMMENT TK_NEWLINE statements {
711
+ type_comment TK_NEWLINE statements {
701
712
$$ = CLASS_07($1, $3, $5, $8, $10, @$); }
702
713
;
703
714
@@ -711,16 +722,16 @@ async_func_def
711
722
| KW_ASYNC KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
712
723
body_stmts { $$ = ASYNC_FUNCTION_04($3, $5, $8, $10, @$); }
713
724
| decorators KW_ASYNC KW_DEF id "(" parameter_list_opt ")" ":"
714
- TK_TYPE_COMMENT TK_NEWLINE statements {
725
+ type_comment TK_NEWLINE statements {
715
726
$$ = ASYNC_FUNCTION_05($1, $4, $6, $11, $9, @$); }
716
727
| decorators KW_ASYNC KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
717
- TK_TYPE_COMMENT TK_NEWLINE statements {
728
+ type_comment TK_NEWLINE statements {
718
729
$$ = ASYNC_FUNCTION_06($1, $4, $6, $9, $13, $11, @$); }
719
730
| KW_ASYNC KW_DEF id "(" parameter_list_opt ")" ":"
720
- TK_TYPE_COMMENT TK_NEWLINE statements {
731
+ type_comment TK_NEWLINE statements {
721
732
$$ = ASYNC_FUNCTION_07($3, $5, $10, $8, @$); }
722
733
| KW_ASYNC KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
723
- TK_TYPE_COMMENT TK_NEWLINE statements {
734
+ type_comment TK_NEWLINE statements {
724
735
$$ = ASYNC_FUNCTION_08($3, $5, $8, $12, $10, @$); }
725
736
;
726
737
@@ -731,10 +742,10 @@ async_for_stmt
731
742
KW_ELSE ":" body_stmts {
732
743
$$ = ASYNC_FOR_02($3, $5, $7, $10, @$); }
733
744
| KW_ASYNC KW_FOR expr KW_IN expr ":"
734
- TK_TYPE_COMMENT TK_NEWLINE statements {
745
+ type_comment TK_NEWLINE statements {
735
746
$$ = ASYNC_FOR_03($3, $5, $9, $7, @$); }
736
747
| KW_ASYNC KW_FOR expr KW_IN expr ":"
737
- TK_TYPE_COMMENT TK_NEWLINE statements KW_ELSE ":" body_stmts {
748
+ type_comment TK_NEWLINE statements KW_ELSE ":" body_stmts {
738
749
$$ = ASYNC_FOR_04($3, $5, $9, $12, $7, @$); }
739
750
;
740
751
@@ -743,9 +754,9 @@ async_with_stmt
743
754
$$ = ASYNC_WITH($3, $5, @$); }
744
755
| KW_ASYNC KW_WITH with_as_items ":" body_stmts {
745
756
$$ = ASYNC_WITH_02($3, $5, @$); }
746
- | KW_ASYNC KW_WITH expr_list ":" TK_TYPE_COMMENT
757
+ | KW_ASYNC KW_WITH expr_list ":" type_comment
747
758
TK_NEWLINE statements { $$ = ASYNC_WITH_01($3, $7, $5, @$); }
748
- | KW_ASYNC KW_WITH with_as_items ":" TK_TYPE_COMMENT
759
+ | KW_ASYNC KW_WITH with_as_items ":" type_comment
749
760
TK_NEWLINE statements { $$ = ASYNC_WITH_03($3, $7, $5, @$); }
750
761
;
751
762
@@ -787,7 +798,7 @@ expr_list
787
798
788
799
dict
789
800
: expr ":" expr { $$ = DICT_EXPR_01($1, $3, @$); }
790
- | expr ":" TK_TYPE_COMMENT expr { $$ = DICT_EXPR_02($1, $3, $4, @$); }
801
+ | expr ":" type_comment expr { $$ = DICT_EXPR_02($1, $3, $4, @$); }
791
802
;
792
803
793
804
dict_list
@@ -1015,6 +1026,11 @@ id
1015
1026
: TK_NAME { $$ = SYMBOL($1, @$); }
1016
1027
;
1017
1028
1029
+ type_comment
1030
+ : TK_TYPE_COMMENT { $$ = $1; }
1031
+ | TK_TYPE_IGNORE { $$ = $1; }
1032
+ ;
1033
+
1018
1034
sep
1019
1035
: sep sep_one { $$ = $1; LIST_ADD($$, $2); }
1020
1036
| sep_one { LIST_NEW($$); LIST_ADD($$, $1); }
0 commit comments