error_handling.qbk 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Mini XML - Error Handling]
  8. A parser will not be complete without error handling. Spirit2 provides some
  9. facilities to make it easy to adapt a grammar for error handling. We'll wrap up
  10. the Qi tutorial with another version of the mini xml parser, this time, with
  11. error handling.
  12. The full cpp file for this example can be found here:
  13. [@../../example/qi/mini_xml3.cpp]
  14. [import ../../example/qi/mini_xml3.cpp]
  15. Here's the grammar:
  16. [tutorial_xml3_grammar]
  17. What's new?
  18. [heading Readable Names]
  19. First, when we call the base class, we give the grammar a name:
  20. : mini_xml_grammar::base_type(xml, "xml")
  21. Then, we name all our rules:
  22. xml.name("xml");
  23. node.name("node");
  24. text.name("text");
  25. start_tag.name("start_tag");
  26. end_tag.name("end_tag");
  27. [heading On Success]
  28. `on_success` declares a handler that is applied when a rule is
  29. succesfully matched.
  30. on_success(rule, handler)
  31. This specifies that the handler will be called when a rule is
  32. matched successfully. The handler has the following signature:
  33. void handler(
  34. fusion::vector<
  35. Iterator& first,
  36. Iterator const& last,
  37. Iterator const& i> args,
  38. Context& context)
  39. `first` points to the position in the input sequence before the rule
  40. is matched. `last` points to the last position in the input sequence.
  41. `i` points to the position in the input sequence following the last
  42. character that was consumed by the rule.
  43. A success handler can be used to annotate each matched rule in the
  44. grammar with additional information about the portion of the input
  45. that matched the rule. In a compiler application, this can be a
  46. combination of file, line number and column number from the input
  47. stream for reporting diagnostics or other messages.
  48. [heading On Error]
  49. `on_error` declares our error handler:
  50. on_error<Action>(rule, handler)
  51. This will specify what we will do when we get an error. We will print out an
  52. error message using phoenix:
  53. on_error<fail>
  54. (
  55. xml
  56. , std::cout
  57. << val("Error! Expecting ")
  58. << _4 // what failed?
  59. << val(" here: \"")
  60. << construct<std::string>(_3, _2) // iterators to error-pos, end
  61. << val("\"")
  62. << std::endl
  63. );
  64. we choose to `fail` in our example for the `Action`: Quit and fail. Return a
  65. no_match (false). It can be one of:
  66. [table
  67. [[`Action`] [Description]]
  68. [[fail] [Quit and fail. Return a no_match.]]
  69. [[retry] [Attempt error recovery, possibly moving the iterator position.]]
  70. [[accept] [Force success, moving the iterator position appropriately.]]
  71. [[rethrow] [Rethrows the error.]]
  72. ]
  73. `rule` is the rule to which the handler is attached. In our case, we are attaching to the
  74. `xml` rule.
  75. `handler` is the actual error handling function. It expects 4 arguments:
  76. [table
  77. [[Arg] [Description]]
  78. [[first] [The position of the iterator when the rule with the handler was entered.]]
  79. [[last] [The end of input.]]
  80. [[error-pos] [The actual position of the iterator where the error occurred.]]
  81. [[what] [What failed: a string describing the failure.]]
  82. ]
  83. [heading Expectation Points]
  84. You might not have noticed it, but some of our expressions changed from using
  85. the `>>` to `>`. Look, for example:
  86. end_tag =
  87. "</"
  88. > lit(_r1)
  89. > '>'
  90. ;
  91. What is it? It's the /expectation/ operator. You will have some "deterministic
  92. points" in the grammar. Those are the places where backtracking *cannot* occur.
  93. For our example above, when you get a `"</"`, you definitely must see a valid
  94. end-tag label next. It should be the one you got from the start-tag. After that,
  95. you definitely must have a `'>'` next. Otherwise, there is no point in
  96. proceeding and trying other branches, regardless where they are. The
  97. input is definitely erroneous. When this happens, an expectation_failure
  98. exception is thrown. Somewhere outward, the error handler will catch the
  99. exception.
  100. Try building the parser: [@../../example/qi/mini_xml3.cpp]. You can find some
  101. examples in: [@../../example/qi/mini_xml_samples] for testing purposes.
  102. "4.toyxml" has an error in it:
  103. <foo><bar></foo></bar>
  104. Running the example with this gives you:
  105. Error! Expecting "bar" here: "foo></bar>"
  106. Error! Expecting end_tag here: "<bar></foo></bar>"
  107. -------------------------
  108. Parsing failed
  109. -------------------------
  110. [endsect]