From 813e4744038ec3a1ca8095111074c1cb2a824d83 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 19 Nov 2015 14:53:52 +0200 Subject: Implement entire page usage generation --- cli/context.cxx | 85 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 11 deletions(-) (limited to 'cli/context.cxx') diff --git a/cli/context.cxx b/cli/context.cxx index a014824..d13f1e5 100644 --- a/cli/context.cxx +++ b/cli/context.cxx @@ -1403,6 +1403,69 @@ format (output_type ot, string const& s, bool para) } string context:: +substitute (const string& s, semantics::cli_unit& u) +{ + string r; + + // Scan the string looking for variables ($var$). + // + size_t b (0), e (b); + for (size_t n (s.size ()); e != n; ++e) + { + if (s[e] == '$' && e + 1 != n) + { + if (s[e + 1] == '$') // Escape. + { + r.append (s, b, ++e - b); // Keep one, skip the other. + b = e + 1; + continue; + } + + // Scan for as long as it is a C identifier. + // + size_t p (e + 1); // Position of the second '$'. + for (; p != n; ++p) + { + char c (s[p]); + + if (!(c == '_' || + ('a' <= c && c <= 'z') || + ('A' <= c && c <= 'Z') || + (p != e + 1 && '0' <= c && c <= '9'))) + break; + } + + // Note: check that the second '$' is not escaped. + // + if (p != n && s[p] == '$' && (p + 1 == n || s[p + 1] != '$')) + { + r.append (s, b, e - b); + + // Lookup and substiute the variable. + // + ++e; + string v (s, e, p - e); + + if (semantics::doc* d = u.lookup ("", "var: " + v)) + r += d->front (); + else + { + cerr << "error: undefined variable '" << v << "' in '" << s << "'" + << endl; + throw generation_failed (); + } + + e = p; + b = e + 1; + } + } + } + + r.append (s, b, e - b); // Last chunk. + return r; +} + +string context:: fq_name (semantics::nameable& n, bool cxx_name) { using namespace semantics; @@ -1423,23 +1486,23 @@ fq_name (semantics::nameable& n, bool cxx_name) return r; } -void context:: -cli_open () +string context:: +ns_open (const string& qn, bool last) { string::size_type b (0), e; do { - e = cli.find ("::", b); - string n (cli, b, e == string::npos ? e : e - b); + e = qn.find ("::", b); + string n (qn, b, e == string::npos ? e : e - b); - if (!n.empty ()) + if (!n.empty () && (last || e != string::npos)) os << "namespace " << n << "{"; b = e; if (b == string::npos) - break; + return n; b += 2; @@ -1447,22 +1510,22 @@ cli_open () } void context:: -cli_close () +ns_close (const string& qn, bool last) { string::size_type b (0), e; do { - e = cli.find ("::", b); - string n (cli, b, e == string::npos ? e : e - b); + e = qn.find ("::", b); + string n (qn, b, e == string::npos ? e : e - b); - if (!n.empty ()) + if (!n.empty () && (last || e != string::npos)) os << "}"; b = e; if (b == string::npos) - break; + return; b += 2; -- cgit v1.1