lexer_api.qbk 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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:lexer_api Lexer API]
  8. [heading Description]
  9. The library provides a couple of free functions to make using the lexer a snap.
  10. These functions have three forms. The first form, `tokenize`, simplifies the
  11. usage of a stand alone lexer (without parsing). The second form,
  12. `tokenize_and_parse`, combines a lexer step with parsing on
  13. the token level (without a skipper). The third, `tokenize_and_phrase_parse`,
  14. works on the token level as well, but additionally employs a skip parser. The
  15. latter two versions can take in attributes by reference that will hold the
  16. parsed values on a successful parse.
  17. [heading Header]
  18. // forwards to <boost/spirit/home/lex/tokenize_and_parse.hpp>
  19. #include <boost/spirit/include/lex_tokenize_and_parse.hpp>
  20. For variadic attributes:
  21. // forwards to <boost/spirit/home/lex/tokenize_and_parse_attr.hpp>
  22. #include <boost/spirit/include/lex_tokenize_and_parse_attr.hpp>
  23. The variadic attributes version of the API allows one or more
  24. attributes to be passed into the API functions. The functions taking two
  25. or more attributes are usable when the parser expression is a
  26. __qi_sequence__ only. In this case each of the
  27. attributes passed have to match the corresponding part of the sequence.
  28. Also, see __include_structure__.
  29. [heading Namespace]
  30. [table
  31. [[Name]]
  32. [[`boost::spirit::lex::tokenize` ]]
  33. [[`boost::spirit::lex::tokenize_and_parse` ]]
  34. [[`boost::spirit::lex::tokenize_and_phrase_parse` ]]
  35. [[`boost::spirit::qi::skip_flag::postskip` ]]
  36. [[`boost::spirit::qi::skip_flag::dont_postskip` ]]
  37. ]
  38. [heading Synopsis]
  39. The `tokenize` function is one of the main lexer API functions. It
  40. simplifies using a lexer to tokenize a given input sequence. It's main
  41. purpose is to use the lexer to tokenize all the input.
  42. Both functions take a pair of iterators spanning the underlying input
  43. stream to scan, the lexer object (built from the token definitions),
  44. and an (optional) functor being called for each of the generated tokens. If no
  45. function object `f` is given, the generated tokens will be discarded.
  46. The functions return `true` if the scanning of the input succeeded (the
  47. given input sequence has been successfully matched by the given
  48. token definitions).
  49. The argument `f` is expected to be a function (callable) object taking a single
  50. argument of the token type and returning a bool, indicating whether
  51. the tokenization should be canceled. If it returns `false` the function
  52. `tokenize` will return `false` as well.
  53. The `initial_state` argument forces lexing to start with the given lexer state.
  54. If this is omitted lexing starts in the `"INITIAL"` state.
  55. template <typename Iterator, typename Lexer>
  56. inline bool
  57. tokenize(
  58. Iterator& first
  59. , Iterator last
  60. , Lexer const& lex
  61. , typename Lexer::char_type const* initial_state = 0);
  62. template <typename Iterator, typename Lexer, typename F>
  63. inline bool
  64. tokenize(
  65. Iterator& first
  66. , Iterator last
  67. , Lexer const& lex
  68. , F f
  69. , typename Lexer::char_type const* initial_state = 0);
  70. The `tokenize_and_parse` function is one of the main lexer API
  71. functions. It simplifies using a lexer as the underlying token source
  72. while parsing a given input sequence.
  73. The functions take a pair of iterators spanning the underlying input
  74. stream to parse, the lexer object (built from the token definitions)
  75. and a parser object (built from the parser grammar definition). Additionally
  76. they may take the attributes for the parser step.
  77. The function returns `true` if the parsing succeeded (the given input
  78. sequence has been successfully matched by the given grammar).
  79. template <typename Iterator, typename Lexer, typename ParserExpr>
  80. inline bool
  81. tokenize_and_parse(
  82. Iterator& first
  83. , Iterator last
  84. , Lexer const& lex
  85. , ParserExpr const& expr)
  86. template <typename Iterator, typename Lexer, typename ParserExpr
  87. , typename Attr1, typename Attr2, ..., typename AttrN>
  88. inline bool
  89. tokenize_and_parse(
  90. Iterator& first
  91. , Iterator last
  92. , Lexer const& lex
  93. , ParserExpr const& expr
  94. , Attr1 const& attr1, Attr2 const& attr2, ..., AttrN const& attrN);
  95. The functions `tokenize_and_phrase_parse` take a pair of iterators spanning
  96. the underlying input stream to parse, the lexer object (built from the token
  97. definitions) and a parser object (built from the parser grammar definition).
  98. The additional skipper parameter will be used as the skip parser during
  99. the parsing process. Additionally they may take the attributes for the parser
  100. step.
  101. The function returns `true` if the parsing succeeded (the given input
  102. sequence has been successfully matched by the given grammar).
  103. template <typename Iterator, typename Lexer, typename ParserExpr
  104. , typename Skipper>
  105. inline bool
  106. tokenize_and_phrase_parse(
  107. Iterator& first
  108. , Iterator last
  109. , Lexer const& lex
  110. , ParserExpr const& expr
  111. , Skipper const& skipper
  112. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip);
  113. template <typename Iterator, typename Lexer, typename ParserExpr
  114. , typename Skipper, typename Attribute>
  115. inline bool
  116. tokenize_and_phrase_parse(
  117. Iterator& first
  118. , Iterator last
  119. , Lexer const& lex
  120. , ParserExpr const& expr
  121. , Skipper const& skipper
  122. , Attribute& attr);
  123. template <typename Iterator, typename Lexer, typename ParserExpr
  124. , typename Skipper, typename Attribute>
  125. inline bool
  126. tokenize_and_phrase_parse(
  127. Iterator& first
  128. , Iterator last
  129. , Lexer const& lex
  130. , ParserExpr const& expr
  131. , Skipper const& skipper
  132. , BOOST_SCOPED_ENUM(skip_flag) post_skip, Attribute& attr);
  133. The maximum number of supported arguments is limited by the preprocessor
  134. constant `SPIRIT_ARGUMENTS_LIMIT`. This constant defaults to the value defined
  135. by the preprocessor constant `PHOENIX_LIMIT` (which in turn defaults to `10`).
  136. [note The variadic function with two or more attributes internally combine
  137. references to all passed attributes into a `fusion::vector` and forward
  138. this as a combined attribute to the corresponding one attribute function.]
  139. The `tokenize_and_phrase_parse` functions not taking an explicit `skip_flag`
  140. as one of their arguments invoke the passed skipper after a successful match
  141. of the parser expression. This can be inhibited by using the other versions of
  142. that function while passing `skip_flag::dont_postskip` to the corresponding
  143. argument.
  144. [heading Template parameters]
  145. [table
  146. [[Parameter] [Description]]
  147. [[`Iterator`] [__fwditer__ pointing to the underlying input sequence to parse.]]
  148. [[`Lexer`] [A lexer (token definition) object.]]
  149. [[`F`] [A function object called for each generated token.]]
  150. [[`ParserExpr`] [An expression that can be converted to a Qi parser.]]
  151. [[`Skipper`] [Parser used to skip white spaces.]]
  152. [[`Attr1`, `Attr2`, ..., `AttrN`][One or more attributes.]]
  153. ]
  154. [endsect]