9
3

keywords.qbk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  4. Copyright (C) 2011 Thomas Bernard
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ===============================================================================/]
  8. [section:keyword_list Keyword List Operator]
  9. [heading Description]
  10. The keyword list operator, `kwd("k1")[a] / kwd("k2")[b]`, works tightly with the kwd, ikwd, dkwd and idkwd directives
  11. to effeciently match keyword lists. As long as one of the keywords specified through the kwd, ikwd, dkwd or idkwd directive
  12. matches, the keyword will be immediatly followed by the keyword's associated subject parser.
  13. The parser will continue parsing input as long as the one of the keywords and it's associated parser succeed.
  14. Writing :
  15. (kwd("k1")[a] / kwd("k2")[b] / ... )
  16. is equivalent to:
  17. *( "k1" > a | "k2" > b ... ).
  18. [heading Header]
  19. // forwards to <boost/spirit/repository/home/qi/operator/keywords.hpp>
  20. #include <boost/spirit/repository/include/qi_keywords.hpp>
  21. [heading Expression Semantics]
  22. [table
  23. [[Expression] [Semantics]]
  24. [[`kwd(k1)[a] / kwd(k2)[b]`] [Match `lit(k1) > a` or `lit(k2) > b`, equivalent to `lit(k1) > a | lit(k2) > b`]]
  25. ]
  26. [heading Attributes]
  27. [table
  28. [[Expression] [Attribute]]
  29. [[`kwd("k1")[a] / kwd("k2")[b]`]
  30. [``a: A, b: B --> (kwd(k1)[a] / kwd(k2)[b]): tuple<A, B>
  31. a: A, b: Unused --> (kwd(k1)[a] / kwd(k2)[b]): optional<A>
  32. a: Unused, b: B --> (kwd("k1")[a] / kwd(k2)[b]): optional<B>
  33. a: Unused, b: Unused --> (kwd(k1)[a] / kwd(k2)[b]): Unused
  34. a: A, b: A -->(kwd(k1)[a] / kwd(k2)[b]): tuple<A, A>``]]
  35. ]
  36. [note The keyword list parser works tightly with the kwd, ikwd, dkwd and idkwd directives
  37. and can't be used without it. A compile time error will warn you
  38. of any mistakes. This parser collects all the kwd directives and
  39. extracts the keyword literals or parsers from the directives to internaly
  40. build a Ternary Search Tree (TST) and permutation loop (for complex parsers)
  41. to effectively parse the keywords.
  42. Because you can't mix character types inside a TST you must take
  43. care not to mix wide strings with normal strings in the keywords you supply
  44. to a keyword list. Should it happen the compiler will trap the mistake for you.]
  45. [note The kwd directive also works a bit like the repeat directive
  46. and can be used to formulate additional contraints on the number of
  47. times a keyword can or must occur while parsing a keyword list.]
  48. [note The kwd, dkwd and ikwd, idkwd directives can be mixed inside a keyword list. This has
  49. however a small overhead and should be avoided when possible.]
  50. [heading Complexity]
  51. [:The overall complexity of the keyword list parser is defined by the
  52. sum of the complexities of its elements.]
  53. [heading Example]
  54. [import ../../example/qi/keywords.cpp]
  55. [note The test harness for the example(s) below is presented in the
  56. __qi_basics_examples__ section.]
  57. Declare a small data structure representing a person:
  58. [reference_keyword_list_test_data_structure]
  59. Some using declarations:
  60. [reference_using_declarations_keyword_list]
  61. Now let's declare a keyword parser:
  62. [reference_keyword_list_no_constraint_rule]
  63. A couple of input string variations run on the same parser:
  64. [reference_keyword_list]
  65. Now let's delcare a parser with some occurrence constraints:
  66. [reference_keyword_list_constraint_rule]
  67. And see how it works in these two cases:
  68. [reference_keyword_list_constraints]
  69. [endsect] [/ Keyword list]