1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Simple annotation parsing
17 # This phase collects all the annotations found on AModuleDecl, AClassdef and
18 # APropdef and stores them in the related MEntity.
20 # Once the phase has been applied, annotations names are available in
21 # `AnnotatedMEntity::annotations`.
22 # One can then ask to the mentity if it holds the annnotation in its source code.
26 # fun is_annotated_with_foo(mentity: AnnotatedMEntity): Bool do
27 # return mentity.has_annotation("foo")
31 # Note that only the names of the annotations are stored, if one wants to access
32 # the annotations arguments, the traditional annotations framework is recommanded.
33 module parse_annotations
37 import modelize_property
38 private import annotation
40 redef class ToolContext
41 # Parse the annotations on modules, classdefs and propdefs
42 var parse_annotations_phase
: Phase = new ParseAnnotationsPhase(self,
43 [modelize_class_phase
, modelize_property_phase
])
46 # Parse annotations from modules, classdefs and propdefs
48 # Found annotations names are stored in `AnnotatedMEntity::annotations`.
49 private class ParseAnnotationsPhase
52 # Lookup for `nmodule` annotations
53 redef fun process_nmodule
(nmodule
) do
54 var mmodule
= nmodule
.mmodule
55 if mmodule
== null then return
57 var nmoduledecl
= nmodule
.n_moduledecl
58 if nmoduledecl
== null then return
60 var nannots
= nmoduledecl
.n_annotations
61 if nannots
== null then return
63 for nannot
in nannots
.n_items
do
64 mmodule
.annotations
.add nannot
.n_atid
.n_id
.text
68 # Lookup for `nclassdef` annotations
69 redef fun process_nclassdef
(nclassdef
) do
70 var mclassdef
= nclassdef
.mclassdef
71 if mclassdef
== null then return
73 for npropdef
in nclassdef
.n_propdefs
do
74 if not npropdef
isa AAnnotPropdef then continue
75 mclassdef
.annotations
.add npropdef
.n_atid
.n_id
.text
79 # Lookup for `npropdef` annotations
80 redef fun process_npropdef
(npropdef
) do
81 var mpropdef
= npropdef
.mpropdef
82 if mpropdef
== null then return
84 var nannots
= npropdef
.n_annotations
85 if nannots
== null then return
87 for nannot
in nannots
.n_items
do
88 mpropdef
.annotations
.add nannot
.n_atid
.n_id
.text
93 # A MEntity that can hold annotations from it's source code
95 # We do not introduce these services in MEntity to avoid semantics confusion.
96 # At this stage, the annotation concept is only relevant to source code related
97 # mentities such as MModules, MClassDefs and MPropdefs.
98 abstract class AnnotatedMEntity
100 # Names of the annotations found on `self` declaration
101 var annotations
: Set[String] = new HashSet[String]
103 # Does `self` contains `annotation` in its declaration?
104 fun has_annotation
(annotation
: String): Bool do return annotations
.has
(annotation
)
108 super AnnotatedMEntity
110 redef var is_test
is lazy
do return has_annotation
("test")
113 redef class MClassDef
114 super AnnotatedMEntity
116 redef var is_test
is lazy
do return has_annotation
("test")
120 super AnnotatedMEntity
122 redef var is_test
is lazy
do return has_annotation
("test")
124 redef var is_before
is lazy
do return has_annotation
("before")
126 redef var is_before_all
is lazy
do return has_annotation
("before_all")
128 redef var is_after
is lazy
do return has_annotation
("after")
130 redef var is_after_all
is lazy
do return has_annotation
("after_all")