8 #ifndef Sawyer_Document_Markup_H
9 #define Sawyer_Document_Markup_H
11 #include <Sawyer/Exception.h>
12 #include <Sawyer/Lexer.h>
13 #include <Sawyer/LineVector.h>
14 #include <Sawyer/Map.h>
15 #include <Sawyer/Sawyer.h>
17 #include <boost/algorithm/string/join.hpp>
18 #include <boost/regex.hpp>
57 static const char CHAR_LEFT =
'{';
58 static const char CHAR_RIGHT =
'}';
59 static const char CHAR_AT =
'@';
83 : name(name), dflt(dflt) {}
85 bool isRequired()
const {
if (dflt)
return false;
else return true; }
86 bool isOptional()
const {
return !isRequired(); }
91 std::vector<FormalArg> formals_;
95 explicit Function(
const std::string &name,
bool evalArgs =
true)
96 : name_(name), isMacro_(!evalArgs), ellipsis_(0) {}
102 const std::string& name()
const;
107 bool isMacro()
const;
112 Ptr arg(
const std::string &name);
121 Ptr arg(
const std::string &name,
const std::string &dflt);
127 Ptr ellipsis(
size_t n = (
size_t)(-1));
130 size_t nRequiredArgs()
const;
136 size_t nOptionalArgs()
const;
142 size_t nAdditionalArgs()
const;
145 size_t maxArgs()
const;
152 void validateArgs(std::vector<std::string> &actuals , TokenStream&)
const;
155 virtual std::string eval(
const Grammar&,
const std::vector<std::string> &actuals) = 0;
172 Frame(
TokenStream &where,
const std::string &mesg);
192 : eloc_(eloc), tokens_(tokens), mesg_(mesg), passed_(
false) {}
193 ~Trap() {
if (!passed_) eloc_.push(Frame(tokens_, mesg_)); }
194 void passed() { passed_ =
true; }
198 std::vector<Frame> frames_;
200 void push(
const Frame &frame) {
201 frames_.push_back(frame);
206 std::string toString()
const;
233 std::string resultString_;
235 StaticContent(
const std::string &name,
const std::string &resultString)
236 :
Function(name), resultString_(resultString) {}
239 static Ptr instance(
const std::string &name,
const std::string str) {
242 std::string eval(
const Grammar&,
const std::vector<std::string> &args);
254 static Ptr instance(
const std::string &name,
const std::string dfltMesg =
"error") {
255 return Ptr(
new Error(name))->arg(
"message", dfltMesg);
257 std::string eval(
const Grammar&,
const std::vector<std::string> &args);
270 return Ptr(
new Quote(name))->ellipsis();
272 std::string eval(
const Grammar&,
const std::vector<std::string> &args);
286 return Ptr(
new Eval(name))->ellipsis();
288 std::string eval(
const Grammar &grammar,
const std::vector<std::string> &args);
299 static Ptr instance(
const std::string &name) {
300 return Ptr(
new IfEq(name))->arg(
"val1")->arg(
"val2")->arg(
"if_part")->arg(
"else_part",
"");
302 std::string eval(
const Grammar &grammar,
const std::vector<std::string> &args);
312 static Ptr instance(
const std::string &name) {
313 return Ptr(
new Concat(name))->ellipsis();
315 std::string eval(
const Grammar &grammar,
const std::vector<std::string> &args);
329 std::string indentation_;
330 std::ostringstream out_;
333 std::string nonspaces_;
342 : indentLevel_(0), indentation_(
" "), column_(0), pageWidth_(pageWidth), nLineFeeds_(0) {}
385 Reflow& operator()(
const std::string &s);
390 std::string toString();
393 void emitIndentation();
394 void emitAccumulated();
405 static const bool CONSUME =
true;
406 static const bool LEAVE =
false;
417 virtual std::string operator()(
const std::string &s)
const;
420 static std::string unescape(
const std::string &s);
423 static std::string escape(
const std::string &s);