// -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4 -*- // $Header: /srv/cvs/sources/mpak/mpak/tests/Attic/test-mpak-parser.cc,v 1.1 2003/08/20 14:08:02 pbgavin Exp $ // mpak - the advanced package manager // ecstacy - extended c++ portable operating system interface library // Copyright (C) 2003 Peter Gavin // // 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 2 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, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include #include #include #include #include std::string file = "# a test file\n" "blah = \"hello\" # a test comment\n" "%package \"base\" [[ blah ]] { %blah; }\n" "%command_1 ; %command_2\n"; using ecstacy::mem::shared_ptr; using mpak::spec::parser; using std::cout; using std::endl; inline void print_indent (unsigned indent) { for (unsigned i = 0; i < indent; i++) cout.put ('\t'); } void walk_start (shared_ptr node, unsigned indent); void walk_statement_list (shared_ptr node, unsigned indent); void walk_statement (shared_ptr node, unsigned indent); void walk_statement_end (shared_ptr node, unsigned indent); void walk_command (shared_ptr node, unsigned indent); void walk_argument_list (shared_ptr node, unsigned indent); void walk_argument (shared_ptr node, unsigned indent); void walk_block (shared_ptr node, unsigned indent); void walk_assignment (shared_ptr node, unsigned indent); void walk_token (shared_ptr node, unsigned indent); void walk_start (shared_ptr node, unsigned indent) { print_indent (indent); cout << "start:" << endl; if (node->statement_list.get ()) walk_statement_list (node->statement_list, indent + 1); } void walk_statement_list (shared_ptr node, unsigned indent) { print_indent (indent); cout << "statement_list:" << endl; if (node->statement.get ()) walk_statement (node->statement, indent + 1); if (node->statement_list.get ()) walk_statement_list (node->statement_list, indent + 1); } void walk_statement (shared_ptr node, unsigned indent) { print_indent (indent); cout << "statement:" << endl; if (node->command.get ()) walk_command (node->command, indent + 1); if (node->assignment.get ()) walk_assignment (node->assignment, indent + 1); if (node->statement_end.get ()) walk_statement_end (node->statement_end, indent + 1); } void walk_statement_end (shared_ptr node, unsigned indent) { print_indent (indent); cout << "statement_end:" << endl; if (node->semicolon.get ()) walk_token (node->semicolon, indent + 1); if (node->endl.get ()) walk_token (node->endl, indent + 1); } void walk_command (shared_ptr node, unsigned indent) { print_indent (indent); cout << "command:" << endl; if (node->command_begin.get ()) walk_token (node->command_begin, indent + 1); if (node->identifier.get ()) walk_token (node->identifier, indent + 1); if (node->argument_list.get ()) walk_argument_list (node->argument_list, indent + 1); } void walk_argument_list (shared_ptr node, unsigned indent) { print_indent (indent); cout << "argument_list:" << endl; if (node->argument.get ()) walk_argument (node->argument, indent + 1); if (node->argument_list.get ()) walk_argument_list (node->argument_list, indent + 1); } void walk_argument (shared_ptr node, unsigned indent) { print_indent (indent); cout << "argument:" << endl; if (node->string.get ()) walk_token (node->string, indent + 1); if (node->block.get ()) walk_block (node->block, indent + 1); } void walk_block (shared_ptr node, unsigned indent) { print_indent (indent); cout << "block:" << endl; if (node->block_begin.get ()) walk_token (node->block_begin, indent + 1); if (node->statement_list.get ()) walk_statement_list (node->statement_list, indent + 1); if (node->block_end.get ()) walk_token (node->block_end, indent + 1); } void walk_assignment (shared_ptr node, unsigned indent) { print_indent (indent); cout << "assignment:" << endl; if (node->identifier.get ()) walk_token (node->identifier, indent + 1); if (node->equals.get ()) walk_token (node->equals, indent + 1); if (node->string.get ()) walk_token (node->string, indent + 1); } void walk_token (shared_ptr node, unsigned indent) { using mpak::spec::lexer; print_indent (indent); cout << "token:" << endl; ++indent; print_indent (indent); cout << "type: "; switch (node->type) { case lexer::token::type_none: cout << "none" << endl; print_indent (indent); cout << "value: \"" << node->value << '\"' << endl; break; case lexer::token::type_identifier: cout << "identifier" << endl; print_indent (indent); cout << "value: \"" << node->value << '\"' << endl; break; case lexer::token::type_string: cout << "string" << endl; print_indent (indent); cout << "value: \"" << node->value << '\"' << endl; break; case lexer::token::type_command_begin: cout << "command_begin" << endl; break; case lexer::token::type_block_begin: cout << "block_begin" << endl; break; case lexer::token::type_block_end: cout << "block_end" << endl; break; case lexer::token::type_equals: cout << "equals" << endl; break; case lexer::token::type_endl: cout << "endl" << endl; break; case lexer::token::type_semicolon: cout << "semicolon" << endl; break; case lexer::token::type_eof: cout << "eof" << endl; break; default: cout << "unrecognized token type" << endl; break; } } int main (void) { try { std::istringstream ss (file); parser p; shared_ptr t = p.parse ("stringstream", ss.rdbuf ()); walk_start (t->start, 0); } catch (std::runtime_error re) { cout << "error: " << re.what () << endl; throw; } catch (std::logic_error re) { cout << "error: " << re.what () << endl; throw; } }