MARKUP_PARSER grammar should match everything, even incomplete markup

Fixes https://gitlab.com/kicad/code/kicad/-/issues/13438
This commit is contained in:
Roberto Fernandez Bautista 2023-01-08 23:49:55 +01:00
parent 6ed90134c7
commit e7e151c46c
3 changed files with 124 additions and 9 deletions

View File

@ -49,22 +49,20 @@ struct NODE : parse_tree::basic_node<NODE>
bool isSuperscript() const { return is_type<MARKUP::superscript>(); } bool isSuperscript() const { return is_type<MARKUP::superscript>(); }
}; };
template< typename ControlChar > struct markup : sor< subscript,
struct plain : seq< not_at< seq< ControlChar, string< '{' > > >, ControlChar > {}; superscript,
overbar > {};
struct plainControlChar : sor< plain< string<'_'> >,
plain< string<'^'> >,
plain< string<'~'> > > {};
/** /**
* anyString = * anyString =
* a run of characters that do not start a command sequence, or if they do, they do not start * 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) * a complete command prefix (command char + open brace)
*/ */
struct anyString : plus< sor< utf8::not_one< '~', '_', '^' >, struct anyString : plus< seq< not_at< markup >,
plainControlChar > > {}; utf8::any > > {};
struct anyStringWithinBraces : plus< sor< utf8::not_one< '~', '_', '^', '}' >, struct anyStringWithinBraces : plus< seq< not_at< markup >,
plainControlChar > > {}; utf8::not_one< '}' > > > {};
template< typename ControlChar > template< typename ControlChar >
struct braces : seq< seq< ControlChar, string< '{' > >, struct braces : seq< seq< ControlChar, string< '{' > >,

View File

@ -35,6 +35,7 @@ set( QA_COMMON_SRCS
test_color4d.cpp test_color4d.cpp
test_coroutine.cpp test_coroutine.cpp
test_lib_table.cpp test_lib_table.cpp
test_markup_parser.cpp
test_kicad_string.cpp test_kicad_string.cpp
test_kiid.cpp test_kiid.cpp
test_property.cpp test_property.cpp

View File

@ -0,0 +1,116 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 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/>.
*/
/**
* @file
* Test suite for MARKUP_PARSER
*/
#include <qa_utils/wx_utils/unit_test_utils.h>
// Code under test
#include <markup_parser.h>
/**
* Declare the test suite
*/
BOOST_AUTO_TEST_SUITE( MarkupParser )
void nodeToString( std::unique_ptr<MARKUP::NODE>& aNode, std::string& aStringToPopulate )
{
aStringToPopulate += " {";
if( aNode->isOverbar() )
aStringToPopulate += "OVER";
if( aNode->isSubscript() )
aStringToPopulate += "SUB";
if( aNode->isSuperscript() )
aStringToPopulate += "SUP";
if( aNode->has_content() )
aStringToPopulate += "'" + aNode->string() + "'";
for( auto& c : aNode->children )
nodeToString( c, aStringToPopulate );
aStringToPopulate += "} ";
}
struct PARSE_CASE
{
std::string Input;
std::string ExpectedResult;
};
/**
* Test the #Parse method.
*/
BOOST_AUTO_TEST_CASE( Parse )
{
std::vector<PARSE_CASE> cases =
{
{
"A normal string",
" { {'A normal string'} } "
},
{
"_{A subscript String}",
" { {SUB {'A subscript String'} } } "
},
{
"^{A superscript String}",
" { {SUP {'A superscript String'} } } "
},
{
"~{An overbar String}",
" { {OVER {'An overbar String'} } } "
},
{
"~{An incomplete markup",
" { {'~{An incomplete markup'} } "
},
{
"A string ~{overbar}",
" { {'A string '} {OVER {'overbar'} } } "
},
};
for( auto& c : cases )
{
BOOST_TEST_INFO_SCOPE( c.Input );
MARKUP::MARKUP_PARSER parser( c.Input );
std::unique_ptr<MARKUP::NODE> rootNode = parser.Parse();
BOOST_REQUIRE( rootNode );
std::string result;
nodeToString( rootNode, result );
BOOST_CHECK_EQUAL( result, c.ExpectedResult );
// Uncomment for testing / generating test cases:
// printf( "%s\n", result.c_str() );
}
}
BOOST_AUTO_TEST_SUITE_END()