You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.9 KiB

4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 Ola Rinta-Koski
  5. * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef MARKUP_PARSER_H
  21. #define MARKUP_PARSER_H
  22. #include <pegtl.hpp>
  23. #include <pegtl/contrib/parse_tree.hpp>
  24. #include <iostream>
  25. #include <string>
  26. #include <core/utf8.h>
  27. #include <kicommon.h>
  28. namespace MARKUP
  29. {
  30. using namespace tao::pegtl;
  31. struct subscript;
  32. struct superscript;
  33. struct overbar;
  34. struct KICOMMON_API NODE : parse_tree::basic_node<NODE>
  35. {
  36. std::string asString() const;
  37. std::string typeString() const;
  38. wxString asWxString() const;
  39. bool isOverbar() const { return is_type<MARKUP::overbar>(); }
  40. bool isSubscript() const { return is_type<MARKUP::subscript>(); }
  41. bool isSuperscript() const { return is_type<MARKUP::superscript>(); }
  42. };
  43. struct markup : sor< subscript,
  44. superscript,
  45. overbar > {};
  46. /**
  47. * anyString =
  48. * a run of characters that do not start a command sequence, or if they do, they do not start
  49. * a complete command prefix (command char + open brace)
  50. */
  51. struct anyString : plus< seq< not_at< markup >,
  52. utf8::any > > {};
  53. struct escapeSequence : seq< string<'{'>, identifier, string<'}'> > {};
  54. struct anyStringWithinBraces : plus< sor< seq< not_at< markup >,
  55. escapeSequence >,
  56. seq< not_at< markup >,
  57. utf8::not_one<'}'> > > > {};
  58. template< typename ControlChar >
  59. struct braces : seq< seq< ControlChar,
  60. string<'{'> >,
  61. until< string<'}'>,
  62. sor< anyStringWithinBraces,
  63. subscript,
  64. superscript,
  65. overbar > > > {};
  66. struct superscript : braces< string<'^'> > {};
  67. struct subscript : braces< string<'_'> > {};
  68. struct overbar : braces< string<'~'> > {};
  69. /**
  70. * Finally, the full grammar
  71. */
  72. struct anything : sor< anyString,
  73. subscript,
  74. superscript,
  75. overbar > {};
  76. struct grammar : until< tao::pegtl::eof, anything > {};
  77. template <typename Rule>
  78. using selector = parse_tree::selector< Rule,
  79. parse_tree::store_content::on< anyStringWithinBraces,
  80. anyString >,
  81. parse_tree::discard_empty::on< superscript,
  82. subscript,
  83. overbar > >;
  84. class KICOMMON_API MARKUP_PARSER
  85. {
  86. public:
  87. MARKUP_PARSER( const std::string& source ) :
  88. in( std::make_unique<string_input<>>( source, "from_input" ) ),
  89. mem_in()
  90. {}
  91. MARKUP_PARSER( const std::string* source ) :
  92. in(),
  93. mem_in( std::make_unique<memory_input<>>( *source, "from_input" ) )
  94. {}
  95. std::unique_ptr<NODE> Parse();
  96. private:
  97. std::unique_ptr<string_input<>> in;
  98. std::unique_ptr<memory_input<>> mem_in;
  99. };
  100. } // namespace MARKUP
  101. #endif //MARKUP_PARSER_H