action.qbk 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Parsers]
  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. to be output by the parser attached to the semantic action. Semantic
  12. actions are associated with a parser using the syntax `p[]`, where `p` is an
  13. arbitrary parser expression.
  14. [heading Header]
  15. // forwards to <boost/spirit/home/qi/action.hpp>
  16. #include <boost/spirit/include/qi_action.hpp>
  17. Also, see __include_structure__.
  18. [heading Model of]
  19. [:__unary_parser_concept__]
  20. [variablelist Notation
  21. [[`a`, `p`][Instances of a parser, `P`]]
  22. [[`A`] [Attribute type exposed by a parser, `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 parser to fail. Both `Context` and the boolean flag are
  26. optional. For more information see below.]]
  27. [[`Attrib`][The attribute obtained from the parse.]]
  28. [[`Context`] [The type of the parser 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_parser_concept__.
  34. [table
  35. [[Expression] [Semantics]]
  36. [[`p[fa]`] [If `p` is successful, call semantic action, `fa`. The function
  37. or function object `fa` is provided the attribute value
  38. parsed by the parser `p`, plus some more context information
  39. and a mutable bool flag which can be used to fail parsing.]]
  40. ]
  41. The possible signatures for functions to be used as semantic actions are:
  42. template <typename Attrib>
  43. void fa(Attrib& attr);
  44. template <typename Attrib, typename Context>
  45. void fa(Attrib& attr, Context& context);
  46. template <typename Attrib, typename Context>
  47. void fa(Attrib& attr, Context& context, bool& pass);
  48. The function or function object is expected to return the value to generate
  49. output from by assigning it to the first parameter, `attr`. Here `Attrib` is
  50. the attribute type of the parser attached to the semantic action.
  51. The type `Context` is the type of the parser execution context. This type is
  52. unspecified and depends on the context the parser is invoked in. The value,
  53. `context` used by semantic actions written using __phoenix__ to access various
  54. context dependent attributes and values. For more information about __phoenix__
  55. placeholder expressions usable in semantic actions see __qi_nonterminal__.
  56. The third parameter, `pass`, can be used by the semantic action to force the
  57. associated parser to fail. If pass is set to `false` the action parser
  58. will immediately return `false` as well, while not invoking `p` and not
  59. generating any output.
  60. [heading Attributes]
  61. [table
  62. [[Expression] [Attribute]]
  63. [[`a[fa]`] [`a: A --> a[fa]: A`]]
  64. ]
  65. [heading Complexity]
  66. The complexity of the action parser is defined by the complexity of the
  67. parser the semantic action is attached to and the complexity of the function
  68. or function object used as the semantic action.
  69. [heading Example]
  70. Examples for semantic actions can be found here:
  71. [link spirit.qi.tutorials.semantic_actions.examples_of_semantic_actions Examples of Semantic Actions].
  72. [endsect] [/ Action]