This phase collects all the annotations found on AModuleDecl, AClassdef and APropdef and stores them in the related MEntity.
Once the phase has been applied, annotations names are available in
AnnotatedMEntity::annotations
.
One can then ask to the mentity if it holds the annnotation in its source code.
Example:
fun is_annotated_with_foo(mentity: AnnotatedMEntity): Bool do
return mentity.has_annotation("foo")
end
Note that only the names of the annotations are stored, if one wants to access the annotations arguments, the traditional annotations framework is recommanded.
nitc :: parse_annotations $ MClassDef
A definition (an introduction or a refinement) of a class in a modulenitc :: parse_annotations $ MModule
A Nit module is usually associated with a Nit source file.nitc :: parse_annotations $ MPropDef
A definition of a property (local property)nitc :: parse_annotations $ MClassDef
A definition (an introduction or a refinement) of a class in a modulenitc :: parse_annotations $ MModule
A Nit module is usually associated with a Nit source file.nitc :: parse_annotations $ MPropDef
A definition of a property (local property)Serializable::inspect
to show more useful information
nitc :: modelbuilder
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsnitc :: toolcontext
Common command-line tool infrastructure than handle options and error messagescore :: union_find
union–find algorithm using an efficient disjoint-set data structurenitc :: api_metrics
nitc :: commands_ini
nitc :: light_only
Compiler support for the light FFI only, detects unsupported usage of callbacksnitc
.
nitc :: nitmetrics
A program that collects various metrics on nit programs and librariesnitc :: nitrestful
Tool generating boilerplate code linking RESTful actions to Nit methodsnitc :: separate_erasure_compiler
Separate compilation of a Nit program with generic type erasureclone
method of the astbuilder tool
nitc :: uml_module
Services for generation of a UML package diagram based on aModel
# Simple annotation parsing
#
# This phase collects all the annotations found on AModuleDecl, AClassdef and
# APropdef and stores them in the related MEntity.
#
# Once the phase has been applied, annotations names are available in
# `AnnotatedMEntity::annotations`.
# One can then ask to the mentity if it holds the annnotation in its source code.
#
# Example:
# ~~~nitish
# fun is_annotated_with_foo(mentity: AnnotatedMEntity): Bool do
# return mentity.has_annotation("foo")
# end
# ~~~
#
# Note that only the names of the annotations are stored, if one wants to access
# the annotations arguments, the traditional annotations framework is recommanded.
module parse_annotations
import phase
import modelize_class
import modelize_property
private import annotation
redef class ToolContext
# Parse the annotations on modules, classdefs and propdefs
var parse_annotations_phase: Phase = new ParseAnnotationsPhase(self,
[modelize_class_phase, modelize_property_phase])
end
# Parse annotations from modules, classdefs and propdefs
#
# Found annotations names are stored in `AnnotatedMEntity::annotations`.
private class ParseAnnotationsPhase
super Phase
# Lookup for `nmodule` annotations
redef fun process_nmodule(nmodule) do
var mmodule = nmodule.mmodule
if mmodule == null then return
var nmoduledecl = nmodule.n_moduledecl
if nmoduledecl == null then return
var nannots = nmoduledecl.n_annotations
if nannots == null then return
for nannot in nannots.n_items do
mmodule.annotations.add nannot.n_atid.n_id.text
end
end
# Lookup for `nclassdef` annotations
redef fun process_nclassdef(nclassdef) do
var mclassdef = nclassdef.mclassdef
if mclassdef == null then return
for npropdef in nclassdef.n_propdefs do
if not npropdef isa AAnnotPropdef then continue
mclassdef.annotations.add npropdef.n_atid.n_id.text
end
end
# Lookup for `npropdef` annotations
redef fun process_npropdef(npropdef) do
var mpropdef = npropdef.mpropdef
if mpropdef == null then return
var nannots = npropdef.n_annotations
if nannots == null then return
for nannot in nannots.n_items do
mpropdef.annotations.add nannot.n_atid.n_id.text
end
end
end
# A MEntity that can hold annotations from it's source code
#
# We do not introduce these services in MEntity to avoid semantics confusion.
# At this stage, the annotation concept is only relevant to source code related
# mentities such as MModules, MClassDefs and MPropdefs.
abstract class AnnotatedMEntity
# Names of the annotations found on `self` declaration
var annotations: Set[String] = new HashSet[String]
# Does `self` contains `annotation` in its declaration?
fun has_annotation(annotation: String): Bool do return annotations.has(annotation)
end
redef class MModule
super AnnotatedMEntity
redef var is_test is lazy do return has_annotation("test")
end
redef class MClassDef
super AnnotatedMEntity
redef var is_test is lazy do return has_annotation("test")
end
redef class MPropDef
super AnnotatedMEntity
redef var is_test is lazy do return has_annotation("test")
redef var is_before is lazy do return has_annotation("before")
redef var is_before_all is lazy do return has_annotation("before_all")
redef var is_after is lazy do return has_annotation("after")
redef var is_after_all is lazy do return has_annotation("after_all")
end
src/frontend/parse_annotations.nit:15,1--131,3