action.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section:action Semantic Actions with Generators]
  8. [heading Description]
  9. Semantic actions may be attached to any point in the grammar specification.
  10. They allow to call a function or function object in order to provide the value
  11. output by the generator attached to the semantic action. Semantic
  12. actions are associated with a generator using the syntax `g[]`, where `g` is an
  13. arbitrary generator expression.
  14. [heading Header]
  15. // forwards to <boost/spirit/home/karma/action.hpp>
  16. #include <boost/spirit/include/karma_action.hpp>
  17. Also, see __include_structure__.
  18. [heading Model of]
  19. [:__unary_generator_concept__]
  20. [variablelist Notation
  21. [[`a`, `g`][Instances of a generator, `G`]]
  22. [[`A`] [Attribute type exposed by a generator, `a`]]
  23. [[`fa`] [A (semantic action) function with signature `void(Attrib&, Context&, bool&)`.
  24. The third parameter is a boolean flag that can be set to false to
  25. force the generator to fail. Both `Context` and the boolean flag are
  26. optional. For more information see below.]]
  27. [[`Attrib`][The attribute to be used to generate output from.]]
  28. [[`Context`] [The type of the generator execution context. For more
  29. information see below.]]
  30. ]
  31. [heading Expression Semantics]
  32. Semantics of an expression is defined only where it differs from, or is not
  33. defined in __unary_generator_concept__.
  34. [table
  35. [[Expression] [Semantics]]
  36. [[`g[fa]`] [Call semantic action, `fa` /before/ invoking `g`. The function
  37. or function object `fa` is expected to provide the value
  38. to generate output from to the generator `g`.]]
  39. ]
  40. The possible signatures for functions to be used as semantic actions are:
  41. template <typename Attrib>
  42. void fa(Attrib& attr);
  43. template <typename Attrib, typename Context>
  44. void fa(Attrib& attr, Context& context);
  45. template <typename Attrib, typename Context>
  46. void fa(Attrib& attr, Context& context, bool& pass);
  47. The function or function object is expected to return the value to generate
  48. output from by assigning it to the first parameter, `attr`. Here `Attrib` is
  49. the attribute type of the generator attached to the semantic action.
  50. The type `Context` is the type of the generator execution context. This type is
  51. unspecified and depends on the context the generator is invoked in. The value
  52. `context` is used by semantic actions written using __phoenix__ to access various
  53. context dependent attributes and values. For more information about __phoenix__
  54. placeholder expressions usable in semantic actions see __karma_nonterminal_concept__.
  55. The third parameter, `pass`, can be used by the semantic action to force the
  56. associated generator to fail. If pass is set to `false` the action generator
  57. will immediately return `false` as well, while not invoking `g` and not
  58. generating any output.
  59. [heading Attributes]
  60. [table
  61. [[Expression] [Attribute]]
  62. [[`a[fa]`] [`a: A --> a[fa]: A`]]
  63. ]
  64. [heading Complexity]
  65. The complexity of the action generator is defined by the complexity of the
  66. generator the semantic action is attached to and the complexity of the function
  67. or function object used as the semantic action.
  68. [important Please note that the use of semantic actions in __karma__ generally
  69. forces the library to create a /copy/ of the attribute, which might
  70. be a costly operation. Always consider using other means of
  71. associating a value with a particular generator first.]
  72. [heading Example]
  73. [note The test harness for the example(s) below is presented in the
  74. __karma_basics_examples__ section.]
  75. Some includes:
  76. [reference_karma_includes]
  77. Some using declarations:
  78. [reference_karma_using_declarations_action]
  79. Some examples:
  80. [reference_karma_action]
  81. More examples for semantic actions can be found here:
  82. [link spirit.karma.tutorials.semantic_actions.examples_of_semantic_actions Examples of Semantic Actions].
  83. [endsect] [/ Action]