31 #ifndef CWCHESS_PGN_ANALYSIS 32 #include <boost/spirit/include/classic_spirit.hpp> 33 #include <boost/spirit/include/classic_numerics.hpp> 44 #define TRACE_PGN_GRAMMAR 1 45 #define TRACE_PGN_WHITESPACE 0 46 #define TRACE_PGN_PRINTABLE 0 47 #define TRACE_PGN_ANNOTATION 0 48 #define TRACE_PGN_INTEGER 0 49 #define TRACE_PGN_COMMENT 0 50 #define TRACE_PGN_SAN_MOVE 0 51 #define TRACE_PGN_SAN_INTERNAL 0 52 #define TRACE_PGN_PUNCTUATION_JUNK 0 54 class PgnGrammar :
public boost::spirit::classic::grammar<PgnGrammar> {
61 BOOST_SPIRIT_DEBUG_TRACE_GRAMMAR_NAME(*
this,
"PgnGrammar", 1);
65 template <
typename ScannerT>
68 typedef typename ScannerT::iteration_policy_t iteration_policy_t;
69 typedef boost::spirit::classic::match_policy match_policy_t;
70 typedef typename ScannerT::action_policy_t action_policy_t;
71 typedef boost::spirit::classic::scanner_policies<iteration_policy_t, match_policy_t, action_policy_t> policies_t;
72 typedef boost::spirit::classic::scanner<typename ScannerT::iterator_t, policies_t> non_tree_scanner_t;
73 typedef boost::spirit::classic::rule<non_tree_scanner_t> no_tree_rule_t;
75 typedef boost::spirit::classic::rule<ScannerT> rule_t;
77 boost::spirit::classic::uint_parser<unsigned, 10, 1, 3> uint3_p;
85 rule_t punctuation_junk;
87 rule_t tagname_continuation;
89 rule_t printable_string;
90 rule_t printable_comment;
93 no_tree_rule_t white_space;
94 no_tree_rule_t escaped_data;
98 rule_t disambiguation;
102 rule_t game_termination;
104 rule_t move_number_indication;
107 rule_t numeric_annotation_glyph;
108 rule_t text_annotation1_junk;
109 rule_t text_annotation2_junk;
111 rule_t parenthesis_junk;
116 rule_t last_white_move;
119 rule_t movetext_section_white;
120 rule_t movetext_section_black;
121 rule_t white_black_move;
122 rule_t recursive_variation_white;
123 rule_t recursive_variation_black;
124 rule_t wrong_color_white_junk;
125 rule_t wrong_color_black_junk;
126 rule_t movetext_section;
132 rule_t
const& start(
void)
const {
return PGN_database; }
136 using namespace boost::spirit::classic;
140 * ((*~ch_p(
'\n') >>* ch_p(
'\n')) - tag_pair)
199 | ( ch_p(
'\\') >> (ch_p(
'\\') | ch_p(
'"')))
225 blank >> !white_space
226 | eol >> !white_space
231 ch_p(
'%') >>* (anychar_p - eol_p) >> eol_p
236 eol_p >>* escaped_data
258 ch_p(
'"') >>* printable_string >> ch_p(
'"')
263 tagname_begin >>* tagname_continuation
281 str_p(
"..") >> !ch_p(
'.')
284 move_number_indication
286 integer >> (ch_p(
'.') | ch_p(
' '))
291 ( piece >> !ch_p(
'x') >> square
292 | piece >> disambiguation >> !ch_p(
'x') >> square
293 | file >> ch_p(
'x') >> square >> !promotion
294 | square >> !promotion
305 !(text_annotation1_junk >> !white_space) >>* (!comment >> (numeric_annotation_glyph | text_annotation2_junk) >> !white_space)
308 numeric_annotation_glyph
313 text_annotation1_junk
323 text_annotation2_junk
325 ch_p(
'=') >> !( ch_p(
'-')
328 | ch_p(
'-') >> ( ch_p(
'=')
329 | (!ch_p(
'/') >> ch_p(
'+'))
331 | ch_p(
'+') >> ( (!ch_p(
'/') >> ch_p(
'-'))
339 ch_p(
'{') >>* printable_comment >> ch_p(
'}') >> !white_space >> !comment
340 | ch_p(
';') >>* printable >> eol >> !white_space >> !comment
341 | parenthesis_junk >> !white_space >> !comment
347 ch_p(
'(') >> !white_space >> ch_p(
'{') >>* printable_comment >> ch_p(
'}') >> !white_space >> ch_p(
')')
352 ch_p(
'[') >> !white_space >> tag_name >> !white_space >> tag_value >> !white_space >> ch_p(
']') >> !white_space
357 tag_pair >> !tag_section
379 SAN_move >> !punctuation_junk >> !white_space >> !(annotation >> !white_space)
384 move_number_indication >> !white_space >> move >> !comment >>* recursive_variation_white
395 move_number_indication >> !white_space >> elipsis >> !white_space >> comment
400 move_number_indication >> !white_space >> elipsis >> !white_space >> move >> !comment >>* recursive_variation_black
403 movetext_section_white
405 white_black_move >>* white_black_move >> !last_white_move
409 movetext_section_black
411 black_move >> !movetext_section_white
416 move_number_indication >> !white_space >> move >> !comment >> move >> !comment >>* recursive_variation_black
417 | white_move >> black_move
420 recursive_variation_white
422 ch_p(
'(') >> !white_space >> !comment >> (movetext_section_white | wrong_color_black_junk) >> ch_p(
')') >> !white_space >> !comment
425 recursive_variation_black
427 ch_p(
'(') >> !white_space >> !comment >> (movetext_section_black | wrong_color_white_junk) >> ch_p(
')') >> !white_space >> !comment
430 wrong_color_white_junk
432 movetext_section_white
435 wrong_color_black_junk
437 movetext_section_black
442 !comment >> (movetext_section_white | movetext_section_black)
447 !tag_section >> !movetext_section >> game_termination >> !white_space >> !comment
452 !white_space >> leading_junk >>* PGN_game;
456 BOOST_SPIRIT_DEBUG_TRACE_RULE(leading_junk, TRACE_PGN_GRAMMAR);
457 BOOST_SPIRIT_DEBUG_TRACE_RULE(file, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
458 BOOST_SPIRIT_DEBUG_TRACE_RULE(rank, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
459 BOOST_SPIRIT_DEBUG_TRACE_RULE(piece, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
460 BOOST_SPIRIT_DEBUG_TRACE_RULE(check, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
461 BOOST_SPIRIT_DEBUG_TRACE_RULE(punctuation_junk, TRACE_PGN_GRAMMAR & TRACE_PGN_PUNCTUATION_JUNK);
462 BOOST_SPIRIT_DEBUG_TRACE_RULE(tagname_begin, TRACE_PGN_GRAMMAR);
463 BOOST_SPIRIT_DEBUG_TRACE_RULE(tagname_continuation, TRACE_PGN_GRAMMAR);
464 BOOST_SPIRIT_DEBUG_TRACE_RULE(printable, TRACE_PGN_GRAMMAR & TRACE_PGN_PRINTABLE);
465 BOOST_SPIRIT_DEBUG_TRACE_RULE(printable_string, TRACE_PGN_GRAMMAR & TRACE_PGN_PRINTABLE);
466 BOOST_SPIRIT_DEBUG_TRACE_RULE(printable_comment, TRACE_PGN_GRAMMAR & TRACE_PGN_PRINTABLE);
467 BOOST_SPIRIT_DEBUG_TRACE_RULE(promotion, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
468 BOOST_SPIRIT_DEBUG_TRACE_RULE(blank, TRACE_PGN_GRAMMAR & TRACE_PGN_WHITESPACE);
469 BOOST_SPIRIT_DEBUG_TRACE_RULE(white_space, TRACE_PGN_GRAMMAR & TRACE_PGN_WHITESPACE);
470 BOOST_SPIRIT_DEBUG_TRACE_RULE(escaped_data, TRACE_PGN_GRAMMAR & TRACE_PGN_WHITESPACE);
471 BOOST_SPIRIT_DEBUG_TRACE_RULE(eol, TRACE_PGN_GRAMMAR & TRACE_PGN_WHITESPACE);
472 BOOST_SPIRIT_DEBUG_TRACE_RULE(integer, TRACE_PGN_GRAMMAR & TRACE_PGN_INTEGER);
473 BOOST_SPIRIT_DEBUG_TRACE_RULE(square, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
474 BOOST_SPIRIT_DEBUG_TRACE_RULE(disambiguation, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_INTERNAL);
475 BOOST_SPIRIT_DEBUG_TRACE_RULE(
string, TRACE_PGN_GRAMMAR);
476 BOOST_SPIRIT_DEBUG_TRACE_RULE(tag_name, TRACE_PGN_GRAMMAR);
477 BOOST_SPIRIT_DEBUG_TRACE_RULE(tag_value, TRACE_PGN_GRAMMAR);
478 BOOST_SPIRIT_DEBUG_TRACE_RULE(game_termination, TRACE_PGN_GRAMMAR);
479 BOOST_SPIRIT_DEBUG_TRACE_RULE(elipsis, TRACE_PGN_GRAMMAR);
480 BOOST_SPIRIT_DEBUG_TRACE_RULE(move_number_indication, TRACE_PGN_GRAMMAR);
481 BOOST_SPIRIT_DEBUG_TRACE_RULE(SAN_move, TRACE_PGN_GRAMMAR & TRACE_PGN_SAN_MOVE);
482 BOOST_SPIRIT_DEBUG_TRACE_RULE(annotation, TRACE_PGN_GRAMMAR);
483 BOOST_SPIRIT_DEBUG_TRACE_RULE(numeric_annotation_glyph, TRACE_PGN_GRAMMAR & TRACE_PGN_ANNOTATION);
484 BOOST_SPIRIT_DEBUG_TRACE_RULE(text_annotation1_junk, TRACE_PGN_GRAMMAR & TRACE_PGN_ANNOTATION);
485 BOOST_SPIRIT_DEBUG_TRACE_RULE(text_annotation2_junk, TRACE_PGN_GRAMMAR & TRACE_PGN_ANNOTATION);
486 BOOST_SPIRIT_DEBUG_TRACE_RULE(comment, TRACE_PGN_GRAMMAR & TRACE_PGN_COMMENT);
487 BOOST_SPIRIT_DEBUG_TRACE_RULE(parenthesis_junk, TRACE_PGN_GRAMMAR & TRACE_PGN_COMMENT);
488 BOOST_SPIRIT_DEBUG_TRACE_RULE(tag_pair, TRACE_PGN_GRAMMAR);
489 BOOST_SPIRIT_DEBUG_TRACE_RULE(tag_section, TRACE_PGN_GRAMMAR);
490 BOOST_SPIRIT_DEBUG_TRACE_RULE(move, TRACE_PGN_GRAMMAR);
491 BOOST_SPIRIT_DEBUG_TRACE_RULE(white_move, TRACE_PGN_GRAMMAR);
492 BOOST_SPIRIT_DEBUG_TRACE_RULE(last_white_move, TRACE_PGN_GRAMMAR);
493 BOOST_SPIRIT_DEBUG_TRACE_RULE(arena_junk, TRACE_PGN_GRAMMAR);
494 BOOST_SPIRIT_DEBUG_TRACE_RULE(black_move, TRACE_PGN_GRAMMAR);
495 BOOST_SPIRIT_DEBUG_TRACE_RULE(movetext_section_white, TRACE_PGN_GRAMMAR);
496 BOOST_SPIRIT_DEBUG_TRACE_RULE(movetext_section_black, TRACE_PGN_GRAMMAR);
497 BOOST_SPIRIT_DEBUG_TRACE_RULE(white_black_move, TRACE_PGN_GRAMMAR);
498 BOOST_SPIRIT_DEBUG_TRACE_RULE(recursive_variation_white, TRACE_PGN_GRAMMAR);
499 BOOST_SPIRIT_DEBUG_TRACE_RULE(recursive_variation_black, TRACE_PGN_GRAMMAR);
500 BOOST_SPIRIT_DEBUG_TRACE_RULE(wrong_color_white_junk, TRACE_PGN_GRAMMAR);
501 BOOST_SPIRIT_DEBUG_TRACE_RULE(wrong_color_black_junk, TRACE_PGN_GRAMMAR);
502 BOOST_SPIRIT_DEBUG_TRACE_RULE(movetext_section, TRACE_PGN_GRAMMAR);
503 BOOST_SPIRIT_DEBUG_TRACE_RULE(PGN_game, TRACE_PGN_GRAMMAR);
504 BOOST_SPIRIT_DEBUG_TRACE_RULE(PGN_database, TRACE_PGN_GRAMMAR);
514 #endif // PGNGRAMMAR_H A namespace for all chess related objects that are not related to the GUI.