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