author | Francesco Abbate <francesco.bbt@gmail.com> | 2010年08月19日 00:19:58 +0200 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2010年08月19日 00:19:58 +0200 |
commit | bef124c9c9e081062a457f6d0b2ab1eda6283a3e (patch) | |
tree | 5a757e1c2a84900feb642a9790dad5d61f870509 /agg-plot/split-parser.h | |
parent | 7cdb95a74e7f352620cd500940b73bf221f9e91a (diff) | |
download | gsl-shell-bef124c9c9e081062a457f6d0b2ab1eda6283a3e.tar.gz |
-rw-r--r-- | agg-plot/split-parser.h | 97 |
diff --git a/agg-plot/split-parser.h b/agg-plot/split-parser.h new file mode 100644 index 00000000..7c13988b --- /dev/null +++ b/agg-plot/split-parser.h @@ -0,0 +1,97 @@ +#ifndef AGGPLOT_SPLIT_PARSER_H +#define AGGPLOT_SPLIT_PARSER_H + +#include "my_tree.h" + +enum direction_e { along_x, along_y }; + +template <class base_type> +class split { + + typedef tree::node<base_type, direction_e> node_type; + +public: + // ----------------------------------------------------string_lexer + class lexer { + const char *m_content; + const char *m_ptr; + + public: + lexer(const char *str) : m_content(str), m_ptr(m_content) {}; + + char next() + { + char c = *m_ptr; + if (c != '0円') + m_ptr++; + return c; + }; + + void push() + { + if (m_ptr > m_content) + m_ptr--; + }; + + bool checknext(char reqchar) + { + char c = *m_ptr; + if (c == reqchar) + { + m_ptr++; + return true; + } + return false; + }; + }; + + // ------------------------------------------------parser: exprlist + static node_type* exprlist (lexer& lex, direction_e dir) + { + typedef tree::tree_node<base_type, direction_e> tree_type; + + tree_type *t = new tree_type(dir); + + for (int c = 0; ; c++) + { + node_type* child = parse(lex); + if (! child) + break; + t->add(child); + } + + return t; + } + +// ------------------------------------------------parser: parse + static node_type* parse (lexer& lex) + { + char t = lex.next(); + + switch (t) + { + case '.': + return new tree::leaf<base_type, direction_e>(); + case 'h': + return exprlist(lex, along_x); + case 'v': + return exprlist(lex, along_y); + case '(': + { + node_type *nd = parse(lex); + if (! lex.checknext(')')) + return 0; + return nd; + } + case ')': + lex.push(); + return 0; + default: + return 0; + } + + return 0; + } +}; + +#endif |