From 9f62980f827505ed1f231b737ec126dda041a475 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 17 Aug 2003 00:58:34 +0000 Subject: [PATCH] First parser implementation that creates the widgets tree. --- parser.cpp | 48 ++++++++++++++++++++++++++++++++++-------------- parser.h | 8 ++++---- parser_test.cpp | 6 ++++-- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/parser.cpp b/parser.cpp index 766c5b1..9f4cfee 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1,5 +1,9 @@ // vim: set expandtab tabstop=4 shiftwidth=4: +// FIXME - ver tema de fallback. +#include "translate.h" + +#include "string.h" #include "parser.h" #include @@ -37,32 +41,48 @@ void Parser::on_start_element(const string& name, const AttributeMap& attrs) { } cerr << "]);" << endl; #endif -/* - stringstream out; - out << "widget = attributes: ["; - if (attrs.size()) { - Hash::const_iterator last = attrs.end(); - last--; - for (Hash::const_iterator i = attrs.begin(); i != last; i++) { //last; i++) { - out << i->first << ": '" << i->second << "', "; - } - out << last->first << ": '" << last->second << "'"; - } - out << "]);" << endl; - return out.str(); -*/ + stack.push(new Translate(name, attrs)); } void Parser::on_end_element(const string& name) { #ifdef DEBUG cerr << "In Parser::on_end_element('" << name << "');" << endl; #endif + Widget* cur = stack.top(); + stack.pop(); + // If is the last widget, it's the root widget. + if (stack.empty()) { + root = cur; + // If is not the last widget, we add it as content of his parent. + } else { + Container* par = dynamic_cast(stack.top()); + // If the parent is a Container, we add curent widget as content. + if (par) { + par->append(cur); + // If not, we raise an exception TODO + } else { + throw "Trying to add content to a non-container widget."; + } + } } void Parser::on_characters(const string& chars) { #ifdef DEBUG cerr << "In Parser::on_characters('" << chars << "');" << endl; #endif + if (!stack.empty()) { + Container* cur = dynamic_cast(stack.top()); + // If we are in a Container, we add curent string widget as content. + if (cur) { + cur->append(new String(chars)); + // If not, we raise an exception TODO + } else { + throw "Trying to add content to a non-container widget."; + } + } else { + // FIXME - investigar si tiene sentido. + throw "Characters with no tags!!!?!?!?!"; + } } void Parser::on_comment(const string& text) { diff --git a/parser.h b/parser.h index 4eb8e79..f792646 100644 --- a/parser.h +++ b/parser.h @@ -25,14 +25,14 @@ namespace bife { typedef std::stack WidgetStack; // Attributes. - public: - /// Widget attributes. - Widget* root; - + protected: /// Stack TODO. WidgetStack stack; // TODO Fallback. + public: + /// Widget attributes. + Widget* root; // Methods. protected: diff --git a/parser_test.cpp b/parser_test.cpp index 7a073ce..705b165 100644 --- a/parser_test.cpp +++ b/parser_test.cpp @@ -1,6 +1,6 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -//#include "chit.h" +#include "chit.h" #include "parser.h" #include #include @@ -16,12 +16,14 @@ int main(int argc, char* argv[]) { } else { file = argv[1]; } - //CHIT tpl; + CHIT tpl; cout << "Parser example:" << endl; cout << "===============" << endl; try { Parser parser; parser.parse_file(file); + // Text is returned in utf-8 encoding. + cout << parser.root->render(tpl) << endl; } catch (exception e) { cerr << "Error: " << e.what() << endl; return 1; -- 2.43.0