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 The 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;
39struct url;
40
41struct KICOMMON_API NODE : parse_tree::basic_node<NODE>
42{
43 std::string asString() const;
44
45 std::string typeString() const;
46
47 wxString asWxString() const;
48
49 bool isOverbar() const { return is_type<MARKUP::overbar>(); }
50 bool isSubscript() const { return is_type<MARKUP::subscript>(); }
51 bool isSuperscript() const { return is_type<MARKUP::superscript>(); }
52 bool isURL() const { return is_type<MARKUP::url>(); }
53};
54
55struct markup : sor< subscript,
56 superscript,
57 overbar > {};
58
64struct anyString : plus< seq< not_at< markup >,
65 not_at< url >,
66 utf8::any > > {};
67
68struct escapeSequence : seq< string<'{'>, identifier, string<'}'> > {};
69
70struct anyStringWithinBraces : plus< sor< seq< not_at< markup >,
71 escapeSequence >,
72 seq< not_at< markup >,
73 utf8::not_one<'}'> > > > {};
74
75struct url : seq< sor< string<'h', 't', 't', 'p', ':', '/', '/'>,
76 string<'h', 't', 't', 'p', 's', ':', '/', '/'> >,
77 plus< utf8::not_one<' '> > > {};
78
79template< typename ControlChar >
80struct braces : seq< seq< ControlChar,
81 string<'{'> >,
82 until< string<'}'>,
83 sor< anyStringWithinBraces,
84 subscript,
85 superscript,
86 overbar > > > {};
87
88struct superscript : braces< string<'^'> > {};
89struct subscript : braces< string<'_'> > {};
90struct overbar : braces< string<'~'> > {};
91
95struct anything : sor< anyString,
96 url,
97 subscript,
98 superscript,
99 overbar > {};
100
101struct grammar : until< tao::pegtl::eof, anything > {};
102
103template <typename Rule>
104using selector = parse_tree::selector< Rule,
105 parse_tree::store_content::on< anyStringWithinBraces,
106 url,
107 anyString >,
108 parse_tree::discard_empty::on< superscript,
109 subscript,
110 overbar > >;
111
113{
114public:
115 MARKUP_PARSER( const std::string& source ) :
116 in( std::make_unique<string_input<>>( source, "from_input" ) ),
117 mem_in()
118 {}
119
120 MARKUP_PARSER( const std::string* source ) :
121 in(),
122 mem_in( std::make_unique<memory_input<>>( *source, "from_input" ) )
123 {}
124
125 std::unique_ptr<NODE> Parse();
126
127private:
128 std::unique_ptr<string_input<>> in;
129 std::unique_ptr<memory_input<>> mem_in;
130};
131
132} // namespace MARKUP
133
134
135#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, url, anyString >, parse_tree::discard_empty::on< superscript, subscript, overbar > > selector
STL namespace.
bool isOverbar() const
std::string asString() const
bool isURL() const
bool isSuperscript() const
wxString asWxString() const
bool isSubscript() const
std::string typeString() const
anyString = a run of characters that do not start a command sequence, or if they do,...
Finally, the full grammar.