KiCad PCB EDA Suite
Loading...
Searching...
No Matches
markup_parser.h
Go to the documentation of this file.
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
21#ifndef MARKUP_PARSER_H
22#define MARKUP_PARSER_H
23
24#include <pegtl.hpp>
25#include <pegtl/contrib/parse_tree.hpp>
26#include <iostream>
27#include <string>
28#include <core/utf8.h>
29#include <kicommon.h>
30
31
32namespace MARKUP
33{
34using namespace tao::pegtl;
35
36struct subscript;
37struct superscript;
38struct overbar;
39
40struct KICOMMON_API NODE : parse_tree::basic_node<NODE>
41{
42 std::string asString() const;
43
44 std::string typeString() const;
45
46 wxString asWxString() const;
47
48 bool isOverbar() const { return is_type<MARKUP::overbar>(); }
49 bool isSubscript() const { return is_type<MARKUP::subscript>(); }
50 bool isSuperscript() const { return is_type<MARKUP::superscript>(); }
51};
52
53struct markup : sor< subscript,
54 superscript,
55 overbar > {};
56
62struct anyString : plus< seq< not_at< markup >,
63 utf8::any > > {};
64
65struct escapeSequence : seq< string<'{'>, identifier, string<'}'> > {};
66
67struct anyStringWithinBraces : plus< sor< seq< not_at< markup >,
68 escapeSequence >,
69 seq< not_at< markup >,
70 utf8::not_one<'}'> > > > {};
71
72template< typename ControlChar >
73struct braces : seq< seq< ControlChar,
74 string<'{'> >,
75 until< string<'}'>,
76 sor< anyStringWithinBraces,
77 subscript,
78 superscript,
79 overbar > > > {};
80
81struct superscript : braces< string<'^'> > {};
82struct subscript : braces< string<'_'> > {};
83struct overbar : braces< string<'~'> > {};
84
88struct anything : sor< anyString,
89 subscript,
90 superscript,
91 overbar > {};
92
93struct grammar : until< tao::pegtl::eof, anything > {};
94
95template <typename Rule>
96using selector = parse_tree::selector< Rule,
97 parse_tree::store_content::on< anyStringWithinBraces,
98 anyString >,
99 parse_tree::discard_empty::on< superscript,
100 subscript,
101 overbar > >;
102
104{
105public:
106 MARKUP_PARSER( const std::string& source ) :
107 in( std::make_unique<string_input<>>( source, "from_input" ) ),
108 mem_in()
109 {}
110
111 MARKUP_PARSER( const std::string* source ) :
112 in(),
113 mem_in( std::make_unique<memory_input<>>( *source, "from_input" ) )
114 {}
115
116 std::unique_ptr<NODE> Parse();
117
118private:
119 std::unique_ptr<string_input<>> in;
120 std::unique_ptr<memory_input<>> mem_in;
121};
122
123} // namespace MARKUP
124
125
126#endif //MARKUP_PARSER_H
MARKUP_PARSER(const std::string *source)
std::unique_ptr< string_input<> > in
MARKUP_PARSER(const std::string &source)
std::unique_ptr< memory_input<> > mem_in
#define KICOMMON_API
Definition: kicommon.h:28
parse_tree::selector< Rule, parse_tree::store_content::on< anyStringWithinBraces, anyString >, parse_tree::discard_empty::on< superscript, subscript, overbar > > selector
STL namespace.
bool isOverbar() const
Definition: markup_parser.h:48
bool isSuperscript() const
Definition: markup_parser.h:50
bool isSubscript() const
Definition: markup_parser.h:49
anyString = a run of characters that do not start a command sequence, or if they do,...
Definition: markup_parser.h:63
Finally, the full grammar.
Definition: markup_parser.h:91