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;
121 Ptr arg(
const std::string &name,
const std::string &dflt);
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);
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) {}
393 void emitIndentation();
394 void emitAccumulated();
405 static const bool CONSUME =
true;
406 static const bool LEAVE =
false;
423 static std::string
escape(
const std::string &s);
A buffer of characters indexed by line number.
Container associating values with keys.
std::string eval(const Grammar &grammar, const std::vector< std::string > &args)
How to evaluate this function or macro.
Information about the location of an exception.
std::string toString() const
Convert an error location to an error string.
Function that generates an error message.
static Ptr instance(const std::string &name, const std::string dfltMesg="error")
Create a new instance.
std::string eval(const Grammar &, const std::vector< std::string > &args)
How to evaluate this function or macro.
Evaluate arguments a second time.
static Ptr instance(const std::string &name)
Create a new instance.
std::string eval(const Grammar &grammar, const std::vector< std::string > &args)
How to evaluate this function or macro.
Base class for markup functions.
size_t nOptionalArgs() const
Number of optional arguments.
size_t maxArgs() const
Max number of actual arguments possible.
size_t nRequiredArgs() const
Number of required arguments.
size_t nAdditionalArgs() const
Max number of additional arguments.
Ptr arg(const std::string &name, const std::string &dflt)
Declare an optional argument.
Ptr ellipsis(size_t n=(size_t)(-1))
Declare additional arguments.
void validateArgs(std::vector< std::string > &actuals, TokenStream &) const
Check and adjust actual arguments.
SharedPointer< Function > Ptr
Reference-counting pointer to markup function.
virtual std::string eval(const Grammar &, const std::vector< std::string > &actuals)=0
How to evaluate this function or macro.
bool isMacro() const
Whether declaration is for a macro.
Ptr arg(const std::string &name)
Declare a required argument.
const std::string & name() const
Function name.
Grammar & with(const Function::Ptr &)
Insert function.
static std::string escape(const std::string &s)
Escape all special "@", "{", and "}".
static std::string unescape(const std::string &s)
Expand escape sequences "@@", "@{" and "@}".
virtual std::string operator()(const std::string &s) const
Evaluate an entire string.
std::string eval(const Grammar &grammar, const std::vector< std::string > &args)
How to evaluate this function or macro.
Function that quotes its arguments.
static Ptr instance(const std::string &name)
Create a new instance.
std::string eval(const Grammar &, const std::vector< std::string > &args)
How to evaluate this function or macro.
A class that can reflow and indent paragraphs.
size_t pageWidth() const
Property: Page width.
Reflow & lineBreak()
Insert a line break.
Reflow & operator()(const std::string &s)
Insert text.
Reflow & pageWidth(size_t n)
Property: Page width.
Reflow & indentationString(const std::string &s)
Property: Indentation string.
Reflow & operator--()
Increase or decrease indentation.
Reflow(size_t pageWidth=80)
Construct a reflow filter.
std::string toString()
Extract the reflowed string.
Reflow & operator++()
Increase or decrease indentation.
const std::string & indentationString() const
Property: Indentation string.
A function that inserts a string.
std::string eval(const Grammar &, const std::vector< std::string > &args)
How to evaluate this function or macro.
static Ptr instance(const std::string &name, const std::string str)
Create a new instance.
Syntax error when parsing markup.
SyntaxError(const std::string &mesg)
Syntax error.
void errorLocation(const ErrorLocation &eloc)
Set an error location.
Token scanNextToken(const Container::LineVector &content, size_t &at)
Function that obtains the next token.
Error in parsing something.
An ordered list of tokens scanned from input.
Represents one token of input.
Holds a value or nothing.
Creates SharedPointer from this.
Base class for reference counted objects.
Reference-counting intrusive smart pointer.