2021-12-30 17:26:51 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Ola Rinta-Koski
|
2023-04-17 14:15:29 +00:00
|
|
|
* Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-12-30 17:26:51 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MARKUP_PARSER_H
|
|
|
|
#define MARKUP_PARSER_H
|
|
|
|
|
|
|
|
#include <pegtl.hpp>
|
2022-01-10 23:22:23 +00:00
|
|
|
#include <pegtl/contrib/parse_tree.hpp>
|
2021-12-30 17:26:51 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <utf8.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace MARKUP
|
|
|
|
{
|
|
|
|
using namespace tao::pegtl;
|
|
|
|
|
|
|
|
struct subscript;
|
|
|
|
struct superscript;
|
|
|
|
struct overbar;
|
|
|
|
|
|
|
|
struct NODE : parse_tree::basic_node<NODE>
|
|
|
|
{
|
|
|
|
std::string asString() const;
|
|
|
|
|
|
|
|
std::string typeString() const;
|
|
|
|
|
2022-02-26 22:56:10 +00:00
|
|
|
wxString asWxString() const;
|
2022-02-26 19:02:34 +00:00
|
|
|
|
2022-01-10 23:22:23 +00:00
|
|
|
bool isOverbar() const { return is_type<MARKUP::overbar>(); }
|
|
|
|
bool isSubscript() const { return is_type<MARKUP::subscript>(); }
|
|
|
|
bool isSuperscript() const { return is_type<MARKUP::superscript>(); }
|
2021-12-30 17:26:51 +00:00
|
|
|
};
|
|
|
|
|
2023-01-08 22:49:55 +00:00
|
|
|
struct markup : sor< subscript,
|
|
|
|
superscript,
|
|
|
|
overbar > {};
|
2021-12-30 17:26:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* anyString =
|
|
|
|
* a run of characters that do not start a command sequence, or if they do, they do not start
|
|
|
|
* a complete command prefix (command char + open brace)
|
|
|
|
*/
|
2023-01-08 22:49:55 +00:00
|
|
|
struct anyString : plus< seq< not_at< markup >,
|
|
|
|
utf8::any > > {};
|
2021-12-30 17:26:51 +00:00
|
|
|
|
2023-04-17 14:15:29 +00:00
|
|
|
struct escapeSequence : seq< string<'{'>, identifier, string<'}'> > {};
|
|
|
|
|
|
|
|
struct anyStringWithinBraces : plus< sor< seq< not_at< markup >,
|
|
|
|
escapeSequence >,
|
|
|
|
seq< not_at< markup >,
|
|
|
|
utf8::not_one<'}'> > > > {};
|
2021-12-30 17:26:51 +00:00
|
|
|
|
2022-02-08 18:50:53 +00:00
|
|
|
template< typename ControlChar >
|
2023-04-17 14:15:29 +00:00
|
|
|
struct braces : seq< seq< ControlChar,
|
|
|
|
string<'{'> >,
|
|
|
|
until< string<'}'>,
|
|
|
|
sor< anyStringWithinBraces,
|
|
|
|
subscript,
|
|
|
|
superscript,
|
|
|
|
overbar > > > {};
|
|
|
|
|
|
|
|
struct superscript : braces< string<'^'> > {};
|
|
|
|
struct subscript : braces< string<'_'> > {};
|
|
|
|
struct overbar : braces< string<'~'> > {};
|
2021-12-30 17:26:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finally, the full grammar
|
|
|
|
*/
|
2022-02-08 18:50:53 +00:00
|
|
|
struct anything : sor< anyString,
|
|
|
|
subscript,
|
|
|
|
superscript,
|
|
|
|
overbar > {};
|
|
|
|
|
|
|
|
struct grammar : until< tao::pegtl::eof, anything > {};
|
2021-12-30 17:26:51 +00:00
|
|
|
|
|
|
|
template <typename Rule>
|
|
|
|
using selector = parse_tree::selector< Rule,
|
2022-02-15 23:05:22 +00:00
|
|
|
parse_tree::store_content::on< anyStringWithinBraces,
|
2022-02-08 18:50:53 +00:00
|
|
|
anyString >,
|
|
|
|
parse_tree::discard_empty::on< superscript,
|
|
|
|
subscript,
|
|
|
|
overbar > >;
|
2021-12-30 17:26:51 +00:00
|
|
|
|
|
|
|
class MARKUP_PARSER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MARKUP_PARSER( const std::string& source ) :
|
2023-03-05 17:25:36 +00:00
|
|
|
in( std::make_unique<string_input<>>( source, "from_input" ) ),
|
|
|
|
mem_in()
|
|
|
|
{}
|
|
|
|
|
|
|
|
MARKUP_PARSER( const std::string* source ) :
|
|
|
|
in(),
|
|
|
|
mem_in( std::make_unique<memory_input<>>( *source, "from_input" ) )
|
2021-12-30 17:26:51 +00:00
|
|
|
{}
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
std::unique_ptr<NODE> Parse();
|
2021-12-30 17:26:51 +00:00
|
|
|
|
|
|
|
private:
|
2023-03-05 17:25:36 +00:00
|
|
|
std::unique_ptr<string_input<>> in;
|
|
|
|
std::unique_ptr<memory_input<>> mem_in;
|
2021-12-30 17:26:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace MARKUP
|
|
|
|
|
|
|
|
|
|
|
|
#endif //MARKUP_PARSER_H
|