mirror of
https://github.com/cwbaker/lalr.git
synced 2026-09-26 16:33:18 +03:00
Update XML example to parser from UTF-8 to UTF-8 in memory
This commit is contained in:
parent
0dc846f6eb
commit
071273032e
1 changed files with 20 additions and 17 deletions
|
|
@ -4,6 +4,7 @@
|
|||
#include <list>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace lalr;
|
||||
|
|
@ -11,21 +12,23 @@ using namespace lalr;
|
|||
namespace
|
||||
{
|
||||
|
||||
typedef std::basic_string<uint8_t> String;
|
||||
|
||||
struct Attribute
|
||||
{
|
||||
std::string name_;
|
||||
std::string value_;
|
||||
String name_;
|
||||
String value_;
|
||||
|
||||
Attribute( const std::string& name, const std::string& value )
|
||||
: name_( name ),
|
||||
value_( value )
|
||||
Attribute( const String& name, const String& value )
|
||||
: name_( name )
|
||||
, value_( value )
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct Element
|
||||
{
|
||||
std::string name_;
|
||||
String name_;
|
||||
std::list<shared_ptr<Attribute> > attributes_;
|
||||
std::list<shared_ptr<Element> > elements_;
|
||||
|
||||
|
|
@ -61,7 +64,7 @@ struct XmlUserData
|
|||
}
|
||||
};
|
||||
|
||||
static XmlUserData document( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData document( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
const XmlUserData* end = start + length;
|
||||
while ( start != end && !start[0].element_ )
|
||||
|
|
@ -71,28 +74,28 @@ static XmlUserData document( const XmlUserData* start, const ParserNode<char>* n
|
|||
return start != end ? start[0] : XmlUserData();
|
||||
}
|
||||
|
||||
static XmlUserData add_element( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData add_element( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
shared_ptr<Element> element = start[0].element_;
|
||||
element->elements_.push_back( start[1].element_ );
|
||||
return XmlUserData( element );
|
||||
}
|
||||
|
||||
static XmlUserData create_element( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData create_element( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
shared_ptr<Element> element( new Element() );
|
||||
element->elements_.push_back( start[0].element_ );
|
||||
return XmlUserData( element );
|
||||
}
|
||||
|
||||
static XmlUserData short_element( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData short_element( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
shared_ptr<Element> element = start[2].element_;
|
||||
element->name_ = nodes[1].lexeme();
|
||||
return XmlUserData( element );
|
||||
}
|
||||
|
||||
static XmlUserData long_element( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData long_element( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
shared_ptr<Element> element = start[2].element_;
|
||||
if ( !element )
|
||||
|
|
@ -108,7 +111,7 @@ static XmlUserData long_element( const XmlUserData* start, const ParserNode<char
|
|||
return XmlUserData( element );
|
||||
}
|
||||
|
||||
static XmlUserData add_attribute( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData add_attribute( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
LALR_ASSERT( start[0].element_ );
|
||||
shared_ptr<Element> element = start[0].element_;
|
||||
|
|
@ -117,7 +120,7 @@ static XmlUserData add_attribute( const XmlUserData* start, const ParserNode<cha
|
|||
return XmlUserData( element );
|
||||
}
|
||||
|
||||
static XmlUserData create_attribute( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData create_attribute( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
LALR_ASSERT( start[0].attribute_ );
|
||||
shared_ptr<Element> element( new Element() );
|
||||
|
|
@ -125,7 +128,7 @@ static XmlUserData create_attribute( const XmlUserData* start, const ParserNode<
|
|||
return XmlUserData( element );
|
||||
}
|
||||
|
||||
static XmlUserData attribute( const XmlUserData* start, const ParserNode<char>* nodes, size_t length )
|
||||
static XmlUserData attribute( const XmlUserData* start, const ParserNode<uint8_t>* nodes, size_t length )
|
||||
{
|
||||
shared_ptr<Attribute> attribute( new Attribute(nodes[0].lexeme(), nodes[2].lexeme()) );
|
||||
return XmlUserData( attribute );
|
||||
|
|
@ -166,9 +169,9 @@ static void print( const Element* element, int level )
|
|||
void lalr_xml_example()
|
||||
{
|
||||
extern const lalr::ParserStateMachine* xml_parser_state_machine;
|
||||
Parser<const char*, XmlUserData> parser( xml_parser_state_machine );
|
||||
Parser<const uint8_t*, XmlUserData> parser( xml_parser_state_machine );
|
||||
parser.lexer_action_handlers()
|
||||
( "string", &string_literal<const char*> )
|
||||
( "string", &string_literal<const uint8_t*> )
|
||||
;
|
||||
parser.parser_action_handlers()
|
||||
( "document", &document )
|
||||
|
|
@ -190,7 +193,7 @@ void lalr_xml_example()
|
|||
" </document>"
|
||||
;
|
||||
|
||||
parser.parse( input, input + strlen(input) );
|
||||
parser.parse( (const uint8_t*) input, (const uint8_t*) input + strlen(input) );
|
||||
LALR_ASSERT( parser.accepted() );
|
||||
LALR_ASSERT( parser.full() );
|
||||
print( parser.user_data().element_.get(), 0 );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue