Cherry-pick MARKUP_PARSER from rockola/kicad-strokefont.
This commit is contained in:
parent
c108d25897
commit
e41a4d406a
|
@ -370,6 +370,7 @@ set( COMMON_SRCS
|
|||
lockfile.cpp
|
||||
lset.cpp
|
||||
marker_base.cpp
|
||||
markup_parser.cpp
|
||||
netclass.cpp
|
||||
observable.cpp
|
||||
origin_transforms.cpp
|
||||
|
@ -499,6 +500,13 @@ target_include_directories( common
|
|||
${CMAKE_BINARY_DIR}
|
||||
)
|
||||
|
||||
# text markup support
|
||||
add_dependencies( common pegtl )
|
||||
target_include_directories( common PUBLIC
|
||||
$<TARGET_PROPERTY:pegtl,INTERFACE_INCLUDE_DIRECTORIES>
|
||||
)
|
||||
|
||||
|
||||
set( PCB_COMMON_SRCS
|
||||
base_screen.cpp
|
||||
eda_text.cpp
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* This program source code file is part of KiCad, a free EDA CAD application.
|
||||
*
|
||||
* Copyright (C) 2021 Ola Rinta-Koski
|
||||
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <markup_parser.h>
|
||||
#include <sstream>
|
||||
|
||||
using namespace MARKUP;
|
||||
|
||||
|
||||
MARKUP::MARKUP_NODE MARKUP_PARSER::Parse()
|
||||
{
|
||||
//string_input<> in( source, "from_input" );
|
||||
auto root = parse_tree::parse<MARKUP::grammar, MARKUP::NODE, MARKUP::selector>( in );
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
std::ostream& operator<<( std::ostream& os, const MARKUP_NODE& node )
|
||||
{
|
||||
os << "<";
|
||||
|
||||
if( !node->is_root() )
|
||||
os << node->asString();
|
||||
|
||||
for( const auto& child : node->children )
|
||||
os << " " << child;
|
||||
|
||||
os << ">";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
std::string NODE::typeString() const
|
||||
{
|
||||
std::stringstream os;
|
||||
|
||||
if( is<MARKUP::subscript>() ) os << "SUBSCRIPT";
|
||||
else if( is<MARKUP::superscript>() ) os << "SUPERSCRIPT";
|
||||
else if( is<MARKUP::anyString>() ) os << "ANYSTRING";
|
||||
else if( is<MARKUP::anyStringWithinBraces>() ) os << "ANYSTRINGWITHINBRACES";
|
||||
else if( is<MARKUP::varNamespaceName>() ) os << "VARNAMESPACENAME";
|
||||
else if( is<MARKUP::varName>() ) os << "VARNAME";
|
||||
else os << "other";
|
||||
|
||||
return os.str();
|
||||
}
|
||||
|
||||
|
||||
std::string NODE::asString() const
|
||||
{
|
||||
std::stringstream os;
|
||||
|
||||
os << name(); // << "{" << typeString() << "}";
|
||||
|
||||
if( has_content() )
|
||||
os << " \"" << string() << "\"";
|
||||
|
||||
return os.str();
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* This program source code file is part of KiCad, a free EDA CAD application.
|
||||
*
|
||||
* Copyright (C) 2021 Ola Rinta-Koski
|
||||
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
||||
*
|
||||
* 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>
|
||||
#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;
|
||||
|
||||
bool isOverbar() const { return is<MARKUP::overbar>(); }
|
||||
bool isSubscript() const { return is<MARKUP::subscript>(); }
|
||||
bool isSuperscript() const { return is<MARKUP::superscript>(); }
|
||||
};
|
||||
|
||||
struct varPrefix : string<'$', '{'> {};
|
||||
|
||||
struct subPrefix : string<'_', '{'> {};
|
||||
|
||||
struct supPrefix : string<'^', '{'> {};
|
||||
|
||||
struct tildePrefix : string<'~', '{'> {};
|
||||
|
||||
struct closeBrace : string<'}'> {};
|
||||
|
||||
struct varName : plus<sor<identifier_other, string<' '>>> {};
|
||||
|
||||
struct varNamespaceName : plus<identifier> {};
|
||||
|
||||
struct varNamespace : seq<varNamespaceName, string<':'>> {};
|
||||
|
||||
struct variable : seq<varPrefix, opt<varNamespace>, varName, closeBrace> {};
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
struct anyString : plus<sor<utf8::not_one<'~', '$', '_', '^'>,
|
||||
seq<not_at<subPrefix>, string<'_'>>,
|
||||
seq<not_at<supPrefix>, string<'^'>>,
|
||||
seq<not_at<tildePrefix>, string<'~'>>>> {};
|
||||
|
||||
struct prefixedSuperscript : seq<supPrefix, superscript> {};
|
||||
|
||||
struct prefixedSubscript : seq<subPrefix, subscript> {};
|
||||
|
||||
struct prefixedOverbar : seq<tildePrefix, overbar> {};
|
||||
|
||||
struct anyStringWithinBraces : plus<sor<utf8::not_one<'~', '$', '_', '^', '}'>>> {};
|
||||
|
||||
struct superscript : until<closeBrace, sor<variable, anyStringWithinBraces>> {};
|
||||
|
||||
struct subscript : until<closeBrace, sor<variable, anyStringWithinBraces>> {};
|
||||
|
||||
struct overbar : until<closeBrace, sor<variable, anyStringWithinBraces>> {};
|
||||
|
||||
/**
|
||||
* Finally, the full grammar
|
||||
*/
|
||||
struct grammar : star<sor<variable,
|
||||
prefixedSubscript,
|
||||
prefixedSuperscript,
|
||||
prefixedOverbar,
|
||||
anyString>> {};
|
||||
|
||||
template <typename Rule>
|
||||
using selector = parse_tree::selector< Rule,
|
||||
parse_tree::store_content::on<varNamespaceName,
|
||||
varName,
|
||||
anyString,
|
||||
anyStringWithinBraces>,
|
||||
parse_tree::discard_empty::on<superscript,
|
||||
subscript,
|
||||
overbar>>;
|
||||
|
||||
typedef std::unique_ptr<MARKUP::NODE, std::default_delete<MARKUP::NODE>> MARKUP_NODE;
|
||||
|
||||
|
||||
class MARKUP_PARSER
|
||||
{
|
||||
public:
|
||||
MARKUP_PARSER( const std::string& source ) :
|
||||
in( source, "from_input" )
|
||||
{}
|
||||
|
||||
MARKUP_NODE Parse();
|
||||
|
||||
private:
|
||||
string_input<> in;
|
||||
};
|
||||
|
||||
} // namespace MARKUP
|
||||
|
||||
std::ostream& operator<<( std::ostream& os, const MARKUP::MARKUP_NODE& node );
|
||||
|
||||
#endif //MARKUP_PARSER_H
|
|
@ -39,3 +39,4 @@ add_subdirectory( potrace )
|
|||
add_subdirectory( nlohmann_json )
|
||||
add_subdirectory( picosha2 )
|
||||
add_subdirectory( json_schema_validator )
|
||||
add_subdirectory( pegtl )
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
add_library( pegtl INTERFACE )
|
||||
|
||||
target_include_directories( pegtl INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} )
|
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2020 Dr. Colin Hirsch and Daniel Frey
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue