File: expression.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (534 lines) | stat: -rw-r--r-- 14,632 bytes parent folder | download | duplicates (11)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
[/==============================================================================
    Copyright (C) 2001-2010 Joel de Guzman
    Copyright (C) 2001-2005 Dan Marsden
    Copyright (C) 2001-2010 Thomas Heller

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
===============================================================================/]

[section:expression Phoenix Expressions]

A Phoenix Expression is a model of the __proto_expr__ Concept. These expressions
are wrapped inside an [link phoenix.inside.actor Actor] template. The `actor` provides
the function call operator which evaluates the expressions.
The `actor` is the domain specific wrapper around Phoenix expressions.

By design, Phoenix Expressions do not carry any information on how they will be
evaluated later on. They are the data structure on which the `Actions` will work.

The library provides a convenience template to define expressions:
    
    template <template <typename> Actor, typename Tag, typename A0, ..., typename A1>
    struct expr_ext
        : proto::transform<expr_ext<Actor, Tag, A0, ..., A1> >
    {
        typedef __unspecified__ base_expr;
        typedef Actor<base_expr> type;

        typedef __unspecified__ proto_grammar;

        static type make(A0 a0, ..., A1 a1);
    };

    template <typename Tag, typename A0, ..., typename A1>
    struct expr : expr_ext<actor, Tag, A0, ..., A1> {};

[*Notation]
[variablelist
    [[`A0...AN`] [Child node types]]
    [[`a0...aN`] [Child node objects]]
    [[`G0...GN`] [__proto__ grammar types]]
]

[*Expression Semantics]
[table
    [[Expression] [Semantics]]
    [[`expr<Tag, A0...AN>::type`] [The type of Expression having tag `Tag` and `A0...AN` children]]
    [[`expr<Tag, G0...GN>`] [A __proto__ grammar and __proto_pass_through_transform__]]
    [[`expr<Tag, A0...AN>::make(a0...aN)`] [Returns a Phoenix Expression]]
]

[note
	You might have noticed the template template argument `Actor` used in
	`expr_ext`. This can be a user supplied custom Actor adding other member
	functions or objects than the default `actor` template. See
	[link phoenix.examples.extending_actors Extending Actors] for more details.
]

[heading meta_grammar]

Defining expressions is only part of the game to make it a valid Phoenix Expression.
In order to use the expressions in the Phoenix domain, we need to "register" them
to our grammar.

The `meta_grammar` is a struct for exactly that purpose. It is an openly extendable __proto__ Grammar:
    
    struct meta_grammar
        : proto::switch_<meta_grammar>
    {
        template <typename Tag, typename Dummy>
        struct case_
            : proto::not_<proto::_>
        {};
    };

As you can see, by default the `meta_grammar` matches nothing. With every [link phoenix.modules Module]
you include this grammar gets extended by various expressions.

[heading Example]

Define an expression:

    template <typename Lhs, typename Rhs>
    struct plus
        : expr<proto::tag::plus, Lhs, Rhs>
    {};

And add it to the grammar:

    template <>
    struct meta_grammar::case_<proto::tag::plus>
        : enable_rule<
            plus<
                meta_grammar
              , meta_grammar
            >
        >
    {};

Define a generator function to make the life of our potential users easier:

    template <typename Lhs, typename Rhs>
    typename plus<Lhs, Rhs>::type
    plus(Lhs const & lhs, Rhs const & rhs)
    {
        return expression::plus<Lhs, Rhs>::make(lhs, rhs);
    }

Look if it really works:

    plus(6, 5)();

returns 11!

    proto::display_expr(plus(5, 6));

prints:

    plus(
        terminal(6)
      , terminal(5)
    )

See [@../../example/define_expression.cpp define_expression.cpp] for the full example.

[note
	The example shown here only works because `default_actions` knows how to handle
	an expression having the `proto::tag::plus` and two children. This is because
	`default_actions` uses the `proto::_default<meta_grammar>` transform to
    evaluate operators and functions. Learn more about actions
    [link phoenix.inside.actions here].
]

[section Boilerplate Macros]

When having more and more expressions, you start to realize that this is a very
repetetive task. Phoenix provides boilerplate macros that make defining Phoenix
Expressions as you have seen in the
[link phoenix.inside.expression previous section] look like a piece of cake.


[/
These expressions generate the following:
* A tag (in the underlying namespace tag)
* An expression 
* an expression type in namespace expression
]

[section BOOST_PHOENIX_DEFINE_EXPRESSION]

[heading Description]

`BOOST_PHOENIX_DEFINE_EXPRESSION` is a macro that can be used to generate all the
necessary boilerplate to create Phoenix Expressions

[heading Synopsis]
    
    BOOST_PHOENIX_DEFINE_EXPRESSION(
        (namespace_seq)(name)
      , (child_grammar0)
        (child_grammar1)
        ...
    )

[heading Semantics]

The above macro generates the necessary code for an expression `name` in
namespace `namespace_seq`. The sequence of `(child_grammarN)` declares how many
children the expression will have and what `proto::grammar` they match.

The macro should be used at global scope. `namespace_seq` shall be the sequence
of namespaces under which the following symbols will be defined:

    namespace tag
    {
        struct name;
    }

    namespace expression
    {
        template <typename A0, typename A1 ... typename AN>
        struct name
            : boost::phoenix::expr<
                tag::name
              , A0
              , A1
                ...
              , AN
            >
    }

    namespace rule
    {
        struct name
            : boost::phoenix::expr<
                child_grammar0
              , child_grammar1
                ...
              , child_grammarN
            >
        {};
    }

    namespace functional
    {
        struct make_name; // A polymorphic function object that can be called to create the expression node
    }

    namespace result_of
    {
        template <typename A0, typename A1 ... typename AN>
        struct make_name; // The result type of the expression node
    }

    // convenience polymorphic function to create an expression node
    template <typename A0, typename A1 ... typename AN>
    result_of::make_name<A0, A1 ... AN>
    make_name(A0 const & a0, A1 const & a1 ... AN const & an);

This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
enable the rule for further use in actions.

[heading Header]

    #include <boost/phoenix/core/expression.hpp>

[heading Example]

The example from the previous section can be rewritten as:

    BOOST_PHOENIX_DEFINE_EXPRESSION(
        (plus)
      , (meta_grammar)               // Lhs
        (meta_grammar)               // Rhs
    )

    template <typename Lhs, typename Rhs>
    typename plus<Lhs, Rhs>::type
    plus(Lhs const & lhs, Rhs const & rhs)
    {
        return expression::plus<Lhs, Rhs>::make(lhs, rhs);
    }

[endsect]

[section BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG]

[heading Description]

`BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG` is a macro that can be used to generate all the
necessary boilerplate to create Phoenix Expressions

[heading Synopsis]
    
    BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG(
        (namespace_seq)(name)
      , (child_grammar0)
        (child_grammar1)
        ...
        (child_grammarN)
      , N
    )

[heading Semantics]

The above macro generates the necessary code for an expression `name` in
namespace `namespace_seq`. `N` is the maximum number of variable children.
All but the last elements in the grammar sequence are required children of
the expression, and the last denotes a variable number of children. The number
of children an expression of this kind can hold is therefor `N-1` plus the size of
the sequence

The macro should be used at global scope. `namespace_seq` shall be the sequence
of namespaces under which the following symbols will be defined:

    namespace tag
    {
        struct name;
    }

    namespace expression
    {
        template <typename A0, typename A1 ... typename AN>
        struct name
            : boost::phoenix::expr<
                tag::name
              , A0
              , A1
                ...
              , AN
            >
        {};
    }

    namespace rule
    {
        struct name
            : expression::name<
                child_grammar0
              , child_grammar1
                ...
              , proto::vararg<child_grammarN>
            >
        {};
    }

    namespace functional
    {
        struct make_name; // A polymorphic function object that can be called to create the expression node
    }

    namespace result_of
    {
        template <typename A0, typename A1 ... typename AN>
        struct make_name; // The result type of the expression node
    }

    // convenience polymorphic function to create an expression node
    template <typename A0, typename A1 ... typename AN>
    result_of::make_name<A0, A1 ... AN>
    make_name(A0 const & a0, A1 const & a1 ... AN const & an);

This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
enable the rule for further use in actions.

[heading Header]

    #include <boost/phoenix/core/expression.hpp>

[heading Example]

    BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG(
        (boost)(phoenix)(mem_fun_ptr)
      , (meta_grammar)                // Pointer to Object
        (meta_grammar)                // Member pointer
        (meta_grammar)                // Variable number of arguments
      , BOOST_PHOENIX_LIMIT
    )

This defines the member function pointer operator expression as described in
[link phoenix.modules.operator operators].

[endsect]

[section BOOST_PHOENIX_DEFINE_EXPRESSION_EXT]

[heading Description]

`BOOST_PHOENIX_DEFINE_EXPRESSION_EXT` is a macro that can be used to generate all the
necessary boilerplate to create Phoenix Expressions

[heading Synopsis]
    
    BOOST_PHOENIX_DEFINE_EXPRESSION_EXT(
        actor
      , (namespace_seq)(name)
      , (child_grammar0)
        (child_grammar1)
        ...
        (child_grammarN)
      , N
    )

[heading Semantics]

The above macro generates the necessary code for an expression `name` in
namespace `namespace_seq`. The sequence of `(child_grammarN)` declares how many
children the expression will have and what `proto::grammar` they match.

The macro should be used at global scope. `namespace_seq` shall be the sequence
of namespaces under which the following symbols will be defined:

    namespace tag
    {
        struct name;
    }

    namespace expression
    {
        template <typename A0, typename A1 ... typename AN>
        struct name
            : boost::phoenix::expr_ext<
                actor
              , tag::name
              , A0
              , A1
                ...
              , AN
            >
    }

    namespace rule
    {
        struct name
            : boost::phoenix::expr<
                child_grammar0
              , child_grammar1
                ...
              , child_grammarN
            >
        {};
    }

    namespace functional
    {
        struct make_name; // A polymorphic function object that can be called to create the expression node
    }

    namespace result_of
    {
        template <typename A0, typename A1 ... typename AN>
        struct make_name; // The result type of the expression node
    }

    // convenience polymorphic function to create an expression node
    template <typename A0, typename A1 ... typename AN>
    result_of::make_name<A0, A1 ... AN>
    make_name(A0 const & a0, A1 const & a1 ... AN const & an);

This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
enable the rule for further use in actions.

[heading Header]

    #include <boost/phoenix/core/expression.hpp>

[heading Example]

    BOOST_PHOENIX_DEFINE_EXPRESSION_EXT(
        if_actor
      , (boost)(phoenix)(if_)
      , (meta_grammar) // Cond
        (meta_grammar) // Then
    )

This defines the if_ expression. The custom actor defines `else_` as a member.

[endsect]

[section BOOST_PHOENIX_DEFINE_EXPRESSION_EXT_VARARG]

[heading Description]

`BOOST_PHOENIX_DEFINE_EXPRESSION_EXT_VARARG` is a macro that can be used to generate all the
necessary boilerplate to create Phoenix Expressions

[heading Synopsis]
    
    BOOST_PHOENIX_DEFINE_EXPRESSION_EXT_VARARG(
        actor
      , (namespace_seq)(name)
      , (child_grammar0)
        (child_grammar1)
        ...
        (child_grammarN)
      , N
    )

[heading Semantics]

The above macro generates the necessary code for an expression `name` in
namespace `namespace_seq`. `N` is the maximum number of variable children.
All but the last elements in the grammar sequence are required children of
the expression, and the last denotes a variable number of children. The number
of children an expression of this kind can hold is therefor `N-1` plus the size of
the sequence

The macro should be used at global scope. `namespace_seq` shall be the sequence
of namespaces under which the following symbols will be defined:

    namespace tag
    {
        struct name;
    }

    namespace expression
    {
        template <typename A0, typename A1 ... typename AN>
        struct name
            : boost::phoenix::expr_ext<
                actor
              , tag::name
              , A0
              , A1
                ...
              , AN
            >
        {};
    }

    namespace rule
    {
        struct name
            : expression::name<
                child_grammar0
              , child_grammar1
                ...
              , proto::vararg<child_grammarN>
            >
        {};
    }

    namespace functional
    {
        struct make_name; // A polymorphic function object that can be called to create the expression node
    }

    namespace result_of
    {
        template <typename A0, typename A1 ... typename AN>
        struct make_name; // The result type of the expression node
    }

    // convenience polymorphic function to create an expression node
    template <typename A0, typename A1 ... typename AN>
    result_of::make_name<A0, A1 ... AN>
    make_name(A0 const & a0, A1 const & a1 ... AN const & an);

This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
enable the rule for further use in actions.

[heading Header]

    #include <boost/phoenix/core/expression.hpp>

[heading Example]

TBD

[endsect]

[endsect]

[endsect]