preface.qbk 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. [section Preface]
  2. [section Description]
  3. Metaparse is a compile-time parser generator library. Metaparse provides tools
  4. to write parsers parsing the content of string literals at compile-time, which
  5. makes it possible to embed domain specific languages (DSLs) into C++ without
  6. altering their original syntax (Note that the DSL code snippets will be written
  7. in string literals, therefore they may need to be escaped).
  8. Assuming that the following template class is available for representing
  9. rational numbers in template metaprogramming:
  10. template <class Num, class Denom>
  11. struct rational;
  12. Metaparse can be used to construct such values (instantiate the `rational`
  13. template class) from string literals. Instead of `rational<1, 3>` one can write
  14. `RATIONAL("1/3")` which can be processed by any standard-compliant C++11
  15. compiler (and mean the same). This can be implemented using Metaparse the
  16. following way:
  17. using namespace boost::metaparse;
  18. typedef
  19. sequence_apply2<
  20. rational,
  21. token<int_>,
  22. last_of<lit_c<'/'>, token<int_>>
  23. >
  24. rational_grammar;
  25. typedef build_parser<entire_input<rational_grammar>> rational_parser;
  26. #define RATIONAL(s) \
  27. (::rational_parser::apply<BOOST_METAPARSE_STRING(s)>::type::run())
  28. Note that this is the entire implementation. Also note that this implementation
  29. can be extended to improve the error reports in certain situations.
  30. [endsect]
  31. [section Scope]
  32. Metaparse is intended to be used by library authors to make their APIs follow
  33. the usual notation of the library's problem domain.
  34. [section Comparsion to Boost.Proto]
  35. Boost.Proto is a tool for building expression templates. Expression templates
  36. can be used for DSL embedding by reinterpreting valid C++ expressions as
  37. expressions written in the DSL to embed.
  38. This technique has the advantages over parsing the content of string literals
  39. (which is Metaparse's approach) that:
  40. * is faster in most cases
  41. * APIs using this technique can "emerge" as a process of advancing the API of a
  42. library step-by-step. Moving to a completely new DSL (with its own syntax) is
  43. a relatively big step.
  44. Using expression templates for DSL embedding has the following disadvantages:
  45. * the syntax of the embedded DSL is limited. It has to be a valid C++
  46. expression. For most libraries, people familiar with the original DSL usually
  47. need to learn the library's syntax to understand the embedded code snippets.
  48. Proto helps embedding DSLs based on expression templates, while Metaparse helps
  49. embedding DSLs based on parsing the content of string literals.
  50. [endsect]
  51. [section Comparison to Boost.Spirit]
  52. Spirit is a tool that can be used to build parsers parsing (among others) the
  53. content of string literals at runtime, while Metaparse is a tool that can be
  54. used to parse the content of string literals at compile-time.
  55. [endsect]
  56. [endsect]
  57. [section Advantages of using this library]
  58. This library is useful to provide an API for C++ libraries dealing with a
  59. problem domain with its own notation. Interfaces built with Metaparse make it
  60. possible for the users of the interface to use the domain's own notation, which
  61. makes it easier to write and maintain the code. Users of the interface don't
  62. need to learn a new notation (trying to follow the problem domain's original
  63. one) library authors constrained by the C++ syntax can provide. Example problem
  64. domains are regular expressions and SQL queries.
  65. Metaparse can also be useful to build libraries validating the content of string
  66. literals at compile time instead of doing it at runtime or not doing it at all.
  67. This can help finding (and fixing) bugs in the code early (during compilation).
  68. An example problem domain is `printf`.
  69. [endsect]
  70. [section Cost of using Metaparse]
  71. The parsers built with Metaparse process the content of the string literals
  72. using template metaprograms. This impacts the library using Metaparse the
  73. following way:
  74. * The maintainer of the API built with Metaparse will need to understand
  75. template metaprogramming.
  76. * The content of the string literals will be (re)parsed during every
  77. compilation. This will impact the compiler's memory consumption and the
  78. compilation speed.
  79. * The users of the library will receive the error reports coming from the
  80. parsers as template error messages of their compiler. (Note that Metaparse
  81. actively tries improving their quality and provides
  82. [link dealing_with_invalid_input tools] for parser authors).
  83. [endsect]
  84. [section Supported platforms]
  85. Metaparse is based on C++98. The only exception is the
  86. [link BOOST_METAPARSE_STRING] macro, which needs C++11 `constexpr`.
  87. Compilers Metaparse is actively (in a CI environment) tested on:
  88. * GCC 4.6, 4.7, 4.8, 4.9
  89. * Clang 3.4, 3.5, 3.6
  90. * Visual C++ 2015
  91. Metaparse is expected to work on Visual C++ 2012 and 2010.
  92. [endsect]
  93. [endsect]