1 /*=============================================================================
2 Copyright (c) 2002 2004 2006 Joel de Guzman
3 http://spirit.sourceforge.net/
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_QUICKBOOK_TEMPLATE_STACK_HPP)
10 #define BOOST_SPIRIT_QUICKBOOK_TEMPLATE_STACK_HPP
16 #include <boost/assert.hpp>
17 #include <boost/filesystem/path.hpp>
18 #include <boost/next_prior.hpp>
19 #include <boost/spirit/include/classic_functor_parser.hpp>
20 #include <boost/spirit/include/classic_symbols.hpp>
21 #include <boost/tuple/tuple.hpp>
23 #include "template_tags.hpp"
28 namespace fs = boost::filesystem;
30 struct template_scope;
32 struct template_symbol
35 std::string const& identifier,
36 std::vector<std::string> const& params,
38 template_scope const* parent = 0);
40 std::string identifier;
41 std::vector<std::string> params;
44 template_scope const* lexical_parent;
47 typedef boost::spirit::classic::symbols<template_symbol> template_symbols;
51 // 1.4-: parent_scope is the previous scope on the dynamic
52 // lookup chain. This moves up the stack skipping
53 // 1.5 templates (but not 1.5 included files).
54 // 1.5+: parent_scope is the template's lexical parent.
56 // This means that a search along the parent_scope chain will follow the
57 // correct lookup chain for that version of quickboook.
59 // symbols contains the templates defined in this scope.
63 template_scope() : parent_scope(), parent_1_4() {}
64 template_scope const* parent_scope;
65 template_scope const* parent_1_4;
66 template_symbols symbols;
71 typedef std::deque<template_scope> deque;
75 typedef boost::spirit::classic::nil_t result_t;
77 parser(template_stack& ts_) : ts(ts_) {}
79 template <typename Scanner>
80 std::ptrdiff_t operator()(Scanner const& scan, result_t) const
82 // search all scopes for the longest matching symbol.
83 typename Scanner::iterator_t f = scan.first;
84 std::ptrdiff_t len = -1;
85 for (template_scope const* i = &*ts.scopes.begin(); i;
86 i = i->parent_scope) {
87 boost::spirit::classic::match<> m = i->symbols.parse(scan);
88 if (m.length() > len) len = m.length();
91 if (len >= 0) scan.first = boost::next(f, len);
98 parser& operator=(parser const&);
102 template_symbol* find(std::string const& symbol) const;
103 template_symbol* find_top_scope(std::string const& symbol) const;
104 template_symbols const& top() const;
105 template_scope const& top_scope() const;
106 // Add the given template symbol to the current scope.
107 // If it doesn't have a scope, sets the symbol's scope to the current
109 bool add(template_symbol const&);
113 void start_template(template_symbol const*);
115 boost::spirit::classic::functor_parser<parser> scope;
118 friend struct parser;
120 template_scope const* parent_1_4;
122 template_stack& operator=(template_stack const&);
126 #endif // BOOST_SPIRIT_QUICKBOOK_TEMPLATE_STACK_HPP