aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/cargo/builder.py
blob: 2442fbfa76a2be9fdfb328ab88e0f071f3416e09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2022-2023 Intel Corporation

"""Provides helpers for building AST

This is meant to make building Meson AST from foreign (largely declarative)
build descriptions easier.
"""

from __future__ import annotations
import dataclasses
import typing as T

from .. import mparser

if T.TYPE_CHECKING:
    import builtins


def _token(tid: str, filename: str, value: mparser.TV_TokenTypes) -> mparser.Token[mparser.TV_TokenTypes]:
    """Create a Token object, but with the line numbers stubbed out.

    :param tid: the token id (such as string, number, etc)
    :param filename: the filename that the token was generated from
    :param value: the value of the token
    :return: A Token object
    """
    return mparser.Token(tid, filename, -1, -1, -1, (-1, -1), value)


def string(value: str, filename: str) -> mparser.StringNode:
    """Build A StringNode

    :param value: the value of the string
    :param filename: the file that the value came from
    :return: A StringNode
    """
    return mparser.StringNode(_token('string', filename, value))


def number(value: int, filename: str) -> mparser.NumberNode:
    """Build A NumberNode

    :param value: the value of the number
    :param filename: the file that the value came from
    :return: A NumberNode
    """
    return mparser.NumberNode(_token('number', filename, str(value)))


def bool(value: builtins.bool, filename: str) -> mparser.BooleanNode:
    """Build A BooleanNode

    :param value: the value of the boolean
    :param filename: the file that the value came from
    :return: A BooleanNode
    """
    return mparser.BooleanNode(_token('bool', filename, value))


def array(value: T.List[mparser.BaseNode], filename: str) -> mparser.ArrayNode:
    """Build an Array Node

    :param value: A list of nodes to insert into the array
    :param filename: The file the array is from
    :return: An ArrayNode built from the arguments
    """
    args = mparser.ArgumentNode(_token('array', filename, 'unused'))
    args.arguments = value
    return mparser.ArrayNode(args, -1, -1, -1, -1)


def identifier(value: str, filename: str) -> mparser.IdNode:
    """Build A IdNode

    :param value: the value of the boolean
    :param filename: the file that the value came from
    :return: A BooleanNode
    """
    return mparser.IdNode(_token('id', filename, value))


def method(name: str, id_: mparser.IdNode,
           pos: T.Optional[T.List[mparser.BaseNode]] = None,
           kw: T.Optional[T.Mapping[str, mparser.BaseNode]] = None,
           ) -> mparser.MethodNode:
    """Create a method call.

    :param name: the name of the method
    :param id_: the object to call the method of
    :param pos: a list of positional arguments, defaults to None
    :param kw: a dictionary of keyword arguments, defaults to None
    :return: a method call object
    """
    args = mparser.ArgumentNode(_token('array', id_.filename, 'unused'))
    if pos is not None:
        args.arguments = pos
    if kw is not None:
        args.kwargs = {identifier(k, id_.filename): v for k, v in kw.items()}
    return mparser.MethodNode(id_.filename, -1, -1, id_, name, args)


def function(name: str, filename: str,
             pos: T.Optional[T.List[mparser.BaseNode]] = None,
             kw: T.Optional[T.Mapping[str, mparser.BaseNode]] = None,
             ) -> mparser.FunctionNode:
    """Create a function call.

    :param name: the name of the function
    :param filename: The name of the current file being evaluated
    :param pos: a list of positional arguments, defaults to None
    :param kw: a dictionary of keyword arguments, defaults to None
    :return: a method call object
    """
    args = mparser.ArgumentNode(_token('array', filename, 'unused'))
    if pos is not None:
        args.arguments = pos
    if kw is not None:
        args.kwargs = {identifier(k, filename): v for k, v in kw.items()}
    return mparser.FunctionNode(filename, -1, -1, -1, -1, name, args)


def equal(lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.ComparisonNode:
    """Create an equality operation

    :param lhs: The left hand side of the equal
    :param rhs: the right hand side of the equal
    :return: A compraison node
    """
    return mparser.ComparisonNode('==', lhs, rhs)


def or_(lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.OrNode:
    """Create and OrNode

    :param lhs: The Left of the Node
    :param rhs: The Right of the Node
    :return: The OrNode
    """
    return mparser.OrNode(lhs, rhs)


def and_(lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.AndNode:
    """Create an AndNode

    :param lhs: The left of the And
    :param rhs: The right of the And
    :return: The AndNode
    """
    return mparser.AndNode(lhs, rhs)


def not_(value: mparser.BaseNode, filename: str) -> mparser.NotNode:
    """Create a not node

    :param value: The value to negate
    :param filename: the string filename
    :return: The NotNode
    """
    return mparser.NotNode(_token('not', filename, ''), value)


def assign(value: mparser.BaseNode, varname: str, filename: str) -> mparser.AssignmentNode:
    """Create an AssignmentNode

    :param value: The rvalue
    :param varname: The lvalue
    :param filename: The filename
    :return: An AssignmentNode
    """
    return mparser.AssignmentNode(filename, -1, -1, varname, value)


def block(filename: str) -> mparser.CodeBlockNode:
    return mparser.CodeBlockNode(_token('node', filename, ''))


@dataclasses.dataclass
class Builder:

    filename: str

    def assign(self, value: mparser.BaseNode, varname: str) -> mparser.AssignmentNode:
        return assign(value, varname, self.filename)

    def string(self, value: str) -> mparser.StringNode:
        """Build A StringNode

        :param value: the value of the string
        :return: A StringNode
        """
        return string(value, self.filename)

    def number(self, value: int) -> mparser.NumberNode:
        """Build A NumberNode

        :param value: the value of the number
        :return: A NumberNode
        """
        return number(value, self.filename)

    def bool(self, value: builtins.bool) -> mparser.BooleanNode:
        """Build A BooleanNode

        :param value: the value of the boolean
        :return: A BooleanNode
        """
        return bool(value, self.filename)

    def array(self, value: T.List[mparser.BaseNode]) -> mparser.ArrayNode:
        """Build an Array Node

        :param value: A list of nodes to insert into the array
        :return: An ArrayNode built from the arguments
        """
        return array(value, self.filename)

    def identifier(self, value: str) -> mparser.IdNode:
        """Build A IdNode

        :param value: the value of the boolean
        :return: A BooleanNode
        """
        return identifier(value, self.filename)

    def method(self, name: str, id_: mparser.IdNode,
               pos: T.Optional[T.List[mparser.BaseNode]] = None,
               kw: T.Optional[T.Mapping[str, mparser.BaseNode]] = None,
               ) -> mparser.MethodNode:
        """Create a method call.

        :param name: the name of the method
        :param id_: the object to call the method of
        :param pos: a list of positional arguments, defaults to None
        :param kw: a dictionary of keyword arguments, defaults to None
        :return: a method call object
        """
        return method(name, id_, pos or [], kw or {})

    def function(self, name: str,
                 pos: T.Optional[T.List[mparser.BaseNode]] = None,
                 kw: T.Optional[T.Mapping[str, mparser.BaseNode]] = None,
                 ) -> mparser.FunctionNode:
        """Create a function call.

        :param name: the name of the function
        :param pos: a list of positional arguments, defaults to None
        :param kw: a dictionary of keyword arguments, defaults to None
        :return: a method call object
        """
        return function(name, self.filename, pos or [], kw or {})

    def equal(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.ComparisonNode:
        """Create an equality operation

        :param lhs: The left hand side of the equal
        :param rhs: the right hand side of the equal
        :return: A compraison node
        """
        return equal(lhs, rhs)

    def or_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.OrNode:
        """Create and OrNode

        :param lhs: The Left of the Node
        :param rhs: The Right of the Node
        :return: The OrNode
        """
        return or_(lhs, rhs)

    def and_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.AndNode:
        """Create an AndNode

        :param lhs: The left of the And
        :param rhs: The right of the And
        :return: The AndNode
        """
        return and_(lhs, rhs)

    def not_(self, value: mparser.BaseNode, filename: str) -> mparser.NotNode:
        """Create a not node

        :param value: The value to negate
        :return: The NotNode
        """
        return not_(value, self.filename)