grammar: introduce AAnnotPropdef for annotation in classes
authorJean Privat <jean@pryen.org>
Tue, 24 Feb 2015 16:01:29 +0000 (23:01 +0700)
committerJean Privat <jean@pryen.org>
Tue, 24 Feb 2015 16:05:34 +0000 (23:05 +0700)
Signed-off-by: Jean Privat <jean@pryen.org>

src/parser/nit.sablecc3xx
src/parser/parser_nodes.nit
src/phase.nit

index 8d67fe8..7d92ae8 100644 (file)
@@ -277,6 +277,7 @@ propdef~toplevel {-> propdef}
 !toplevel| {init} [doc]:no redef visibility kwinit qmethid? signature annotation_noend? kwdo stmtso kwend_o {-> New propdef.meth(doc.doc, redef.kwredef, visibility, Null, kwinit, Null, qmethid.methid, signature, annotation_noend.annotations, Null, Null, stmtso.expr)}
 !toplevel| {type} [doc]:no redef visibility kwtype classid typing annotation_withend {-> New propdef.type(doc.doc, redef.kwredef, visibility, kwtype, classid, typing.type, annotation_withend.annotations)}
 !toplevel| {extern_init_implicit} [doc]:no redef visibility kwnew qmethid? signature annotation_noend? extern_calls extern_code_block {-> New propdef.meth(doc.doc, redef.kwredef, visibility, Null, Null, kwnew, qmethid.methid, signature, annotation_noend.annotations, extern_calls, extern_code_block, Null)}
+!toplevel| {annot} line_annotation_forclass {-> line_annotation_forclass.propdef}
 !toplevel| {super} superclass {-> superclass.propdef}
        ;
 annotation_withend~nonull {-> annotations?}
@@ -665,13 +666,10 @@ line_annotation {-> annotation}
        | {args} [doc]:no redef visibility atid opar no at_args cpar annotations? n1 {-> New annotation(doc.doc, redef.kwredef, visibility, atid.atid, opar, [at_args.expr], cpar, annotations)}
        | {nopar} [doc]:no redef visibility atid at_args_nopar n1 {-> New annotation(doc.doc, redef.kwredef, visibility, atid.atid, Null, [at_args_nopar.expr], Null, Null)}
        ;
-line_annotations_forclass {-> annotations}
-       = line_annotation_forclass+ {-> New annotations(Null, Null, [line_annotation_forclass.annotation], Null) }
-       ;
-line_annotation_forclass {-> annotation}
-       = [doc]:no atid_forclass annotations? n1 {-> New annotation(doc.doc, Null, Null, atid_forclass.atid, Null, [], Null, annotations)}
-       | {args} [doc]:no atid_forclass opar no at_args cpar annotations? n1 {-> New annotation(doc.doc, Null, Null, atid_forclass.atid, opar, [at_args.expr], cpar, annotations)}
-       | {nopar} [doc]:no atid_forclass at_args_nopar n1 {-> New annotation(doc.doc, Null, Null, atid_forclass.atid, Null, [at_args_nopar.expr], Null, Null)}
+line_annotation_forclass {-> propdef}
+       = [doc]:no atid_forclass annotations? {-> New propdef.annot(doc.doc, Null, Null, atid_forclass.atid, Null, [], Null, annotations)}
+       | {args} [doc]:no atid_forclass opar no at_args cpar annotations? {-> New propdef.annot(doc.doc, Null, Null, atid_forclass.atid, opar, [at_args.expr], cpar, annotations)}
+       | {nopar} [doc]:no atid_forclass at_args_nopar {-> New propdef.annot(doc.doc, Null, Null, atid_forclass.atid, Null, [at_args_nopar.expr], Null, Null)}
        ;
 
 at_args~nopar {-> expr* }
@@ -820,6 +818,7 @@ propdef = {attr} doc? kwredef? visibility kwvar [id2]:id type? expr? annotations
        | {type} doc? kwredef? visibility kwtype [id]:classid type annotations?
        | {meth} doc? kwredef? visibility kwmeth? kwinit? kwnew? methid? signature annotations? extern_calls? extern_code_block? [block]:expr?
        | {super} doc? kwredef? visibility kwsuper type annotations?
+       | {annot} doc? kwredef? visibility? atid opar? [args]:expr* cpar? annotations?
        ;
 
 methid = {id} id | {plus} plus | {minus} minus | {star} star | {starstar} starstar | {slash} slash | {percent} percent | {eq} eq | {ne} ne | {le} le | {ge} ge | {lt} lt | {gt} gt |  {ll} ll | {gg} gg | {bra} obra cbra | {starship} starship | {assign} id assign | {braassign} obra cbra assign;
index 93a26e2..63913fe 100644 (file)
@@ -315,11 +315,17 @@ abstract class Prod
        do
                var res = new Array[AAnnotation]
                var nas = n_annotations
-               if nas == null then return res
-               for na in nas.n_items do
+               if nas != null then for na in nas.n_items do
                        if na.name != name then continue
                        res.add(na)
                end
+               if self isa AClassdef then for na in n_propdefs do
+                       if na isa AAnnotPropdef then
+                               if na.name != name then continue
+                               res.add na
+                       end
+               end
+
                return res
        end
 
@@ -1190,6 +1196,11 @@ class AMainMethPropdef
        super AMethPropdef
 end
 
+class AAnnotPropdef
+       super APropdef
+       super AAnnotation
+end
+
 # A super-class. eg `super X`
 class ASuperPropdef
        super APropdef
index 1a10cf2..0a32484 100644 (file)
@@ -132,7 +132,10 @@ redef class ToolContext
                                        break
                                end
                                for na in vannot.annotations do
-                                       phase.process_annotated_node(na.parent.parent.as(not null), na)
+                                       var p = na.parent
+                                       if p isa AAnnotations then p = p.parent
+                                       assert p != null
+                                       phase.process_annotated_node(p, na)
                                end
                                if errcount != self.error_count then
                                        self.check_errors