annotation: add `get_single_annotation` and `get_annotations`
[nit.git] / src / annotation.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Management and utilities on annotations
16 module annotation
17
18 import parser
19 import modelbuilder
20 import literal
21
22 redef class Prod
23 super ANode
24
25 # Try to get its single annotation with a given name
26 # If there is no such an annotation, null is returned.
27 # If there is more than one annotation, a error message is printed and the first annotation is returned
28 fun get_single_annotation(name: String, modelbuilder: ModelBuilder): nullable AAnnotation
29 do
30 var res = get_annotations(name)
31 if res.is_empty then return null
32 if res.length > 1 then
33 modelbuilder.error(res[1], "Error: multiple annotation `{name}`. A previous one is defined line {res[0].location.line_start}")
34 end
35 return res.first
36 end
37
38 # Return all its annotations of a given name in the order of their declaration
39 # Retun an empty array if no such an annotation.
40 fun get_annotations(name: String): Array[AAnnotation]
41 do
42 var res = new Array[AAnnotation]
43 var nas = n_annotations
44 if nas == null then return res
45 for na in nas.n_items do
46 if na.name != name then continue
47 res.add(na)
48 end
49 return res
50 end
51 end
52
53 redef class AAnnotation
54 # The name of the annotation
55 fun name: String
56 do
57 return n_atid.n_id.text
58 end
59
60 # Get the single argument of `self` as a `String`.
61 # Raise error and return null on any inconsistency.
62 fun arg_as_string(modelbuilder: ModelBuilder): nullable String
63 do
64 var args = n_args
65 if args.length == 1 then
66 var arg = args.first.as_string
67 if arg != null then return arg
68 end
69
70 modelbuilder.error(self, "Annotation error: \"{name}\" expects a single String as argument.")
71 return null
72 end
73
74 # Get the single argument of `self` as an `Int`.
75 # Raise error and return null on any inconsistency.
76 fun arg_as_int(modelbuilder: ModelBuilder): nullable Int
77 do
78 var args = n_args
79 if args.length == 1 then
80 var arg = args.first.as_int
81 if arg != null then return arg
82 end
83
84 modelbuilder.error(self, "Annotation error: \"{name}\" expects a single Int as argument.")
85 return null
86 end
87 end
88
89 redef class AAtArg
90 # Get `self` as a `String`.
91 # Return null if not a string.
92 fun as_string: nullable String
93 do
94 if not self isa AExprAtArg then return null
95 var nexpr = n_expr
96 if not nexpr isa AStringFormExpr then return null
97 return nexpr.value.as(not null)
98 end
99
100 # Get `self` as an `Int`.
101 # Return null if not an integer.
102 fun as_int: nullable Int
103 do
104 if not self isa AExprAtArg then return null
105 var nexpr = n_expr
106 if not nexpr isa AIntExpr then return null
107 return nexpr.value.as(not null)
108 end
109
110 # Get `self` as a single identifier.
111 # Return null if not a single identifier.
112 fun as_id: nullable String
113 do
114 if not self isa AExprAtArg then return null
115 var nexpr = n_expr
116 if not nexpr isa ACallExpr then return null
117 if not nexpr.n_expr isa AImplicitSelfExpr then return null
118 if not nexpr.n_args.n_exprs.is_empty then return null
119 return nexpr.n_id.text
120 end
121 end