24#include <ql/errors.hpp>
26#include <boost/algorithm/string/replace.hpp>
27#include <boost/algorithm/string/trim.hpp>
33 if (!
error.stoppedParsingAt.initialised) {
34 out <<
"parsing succeeded";
36 out <<
"parsing stopped at " <<
to_string(
error.stoppedParsingAt) <<
"\n";
37 if (
error.expectedWhere.initialised) {
38 out <<
"expected " <<
error.expectedWhat <<
" in " <<
to_string(
error.expectedWhere) <<
":\n";
39 out <<
error.scriptCurrentLine <<
"\n";
40 out << std::string(std::max<Size>(
error.expectedWhere.columnStart, 1) - 1,
' ') <<
"^--- here\n";
42 out <<
"remaining input is\n<<<<<<<<<<\n" <<
error.remainingInput <<
"\n>>>>>>>>>>\n";
51 success_ = qi::phrase_parse(iter, last, grammar, boost::spirit::qi::space);
55 LocationInfo(boost::spirit::get_line(iter), boost::spirit::get_column(first, iter),
56 boost::spirit::get_line(iter), boost::spirit::get_column(first, iter));
61 boost::spirit::get_line(grammar.
errorPos), boost::spirit::get_column(first, grammar.
errorPos),
62 boost::spirit::get_line(grammar.
errorPos), boost::spirit::get_column(first, grammar.
errorPos));
70 "ScriptParser: unexpected eval stack size (" << grammar.
evalStack.size() <<
"), should be 1");
72 QL_REQUIRE(
ast_,
"ScriptParser: ast is null");
79 return "(script reference is not available)\n";
82 boost::replace_all(
script,
"\r",
"");
85 if (l.lineEnd < l.lineStart || (l.lineEnd == l.lineStart && l.columnEnd <= l.columnStart))
86 return "(script reference invalid: " +
to_string(l) +
")\n";
91 res +=
"<<<<<<<<<<\n";
93 Size pos = 0, currentLine = 1;
94 for (Size line = l.lineStart; line <= l.lineEnd; ++line) {
96 while (pos <
script.size() && currentLine < line) {
104 Size posLineEnd = pos;
105 while (posLineEnd <
script.size() &&
script[posLineEnd] !=
'\n')
108 std::string curr = std::string(std::next(
script.begin(), pos), std::next(
script.begin(), posLineEnd));
110 boost::algorithm::trim(curr);
116 Size columnStart = 1, columnEnd = posLineEnd - pos + 1;
117 if (line == l.lineStart)
118 columnStart = l.columnStart;
119 if (line == l.lineEnd)
120 columnEnd = l.columnEnd;
121 if (columnEnd < columnStart)
122 return "(script reference internal error: columnEnd (" + std::to_string(columnEnd) +
123 ") should be >= columnStart (" + std::to_string(columnStart) +
"))";
125 while (columnStart < columnEnd && pos + columnStart <
script.size() &&
126 script[pos + std::max<Size>(columnStart, 1) - 1] ==
' ')
128 if (compact ==
false)
130 std::string(std::max<Size>(columnStart, 1) - 1,
' ') + std::string(columnEnd - columnStart,
'=') +
'\n';
133 if (compact ==
false)
134 res +=
">>>>>>>>>>\n";
ScriptParser(const std::string &script)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
boost::spirit::line_pos_iterator< std::string::const_iterator > ScriptGrammarIterator
std::string printCodeContext(std::string script, const ASTNode *loc, bool compact)
std::string to_string(const LocationInfo &l)
Serializable Credit Default Swap.
LocationInfo locationInfo
LocationInfo stoppedParsingAt
LocationInfo expectedWhere
std::string remainingInput
std::string scriptCurrentLine
std::string scriptContext
ScriptGrammarIterator errorBegin
std::stack< ASTNodePtr > evalStack
ScriptGrammarIterator errorEnd
ScriptGrammarIterator errorPos
boost::spirit::info errorWhat
string conversion utilities