47 static const TreeList empty_trees = {};
49 struct Tree : std::enable_shared_from_this<Tree> {
58 throw std::runtime_error(
"is an Atom");
61 throw std::runtime_error(
"not a TK_NUMBER");
64 throw std::runtime_error(
"not a TK_STRING");
67 throw std::runtime_error(
"not a TK_BOOL_VALUE");
76 return shared_from_this();
81 void expect(
int k,
int numsubtrees) {
82 if (
kind() != k ||
trees().size() != numsubtrees) {
84 ss <<
"expected kind '" <<
kindToString(k) <<
"' with " << numsubtrees
86 <<
trees().size() <<
" subtrees.\n";
88 throw std::runtime_error(ss.str());
99 template <
typename... Args>
101 return std::make_shared<String>(std::forward<Args>(args)...);
112 template <
typename... Args>
114 return std::make_shared<Number>(std::forward<Args>(args)...);
125 template <
typename... Args>
127 return std::make_shared<Bool>(std::forward<Args>(args)...);
135 for (
auto t : others) {
138 size_t s = std::min(c.
start(), t->range().start());
139 size_t e = std::max(c.
end(), t->range().end());
149 range_(mergeRanges(range_,
trees_)),
163 for (
auto& t :
trees()) {
164 trees_.push_back(fn(t));
188 std::stringstream out;
191 out << t->doubleValue();
194 out << t->stringValue();
198 for (
auto e : t->trees()) {
205 return it_.first->second;
209 if (indent + s.size() <
col || t->isAtom()) {
215 for (
auto e : t->trees()) {
216 out <<
"\n" << std::string(indent + 2,
' ');
217 print(out, e, indent + 2);
223 static inline std::ostream&
operator<<(std::ostream& out, pretty_tree t_) {
224 t_.print(out, t_.tree, 0);
225 return out << std::endl;
229 return out << pretty_tree(t);
void highlight(std::ostream &out) const
Definition: lexer.h:315
virtual const SourceRange & range() const
Definition: tree.h:57
void expect(int k)
Definition: tree.h:78
size_t col
Definition: tree.h:181
String(const std::string &value_)
Definition: tree.h:95
const ErrorReport & operator<<(const ErrorReport &e, const T &t)
Definition: error_report.h:50
static TreeRef create(Args &&...args)
Definition: tree.h:113
Compound(int kind, const SourceRange &range_, TreeList &&trees_)
Definition: tree.h:147
Bool(bool value_)
Definition: tree.h:121
Tree(int kind_)
Definition: tree.h:50
SourceRange range_
Definition: tree.h:173
Compound(int kind, const SourceRange &range_)
Definition: tree.h:146
size_t start() const
Definition: lexer.h:338
bool value_
Definition: tree.h:131
virtual TreeRef map(std::function< TreeRef(TreeRef)> fn) override
Definition: tree.h:161
const SourceRange & range() const override
Definition: tree.h:168
virtual bool boolValue() const
Definition: tree.h:66
virtual bool isAtom() const override
Definition: tree.h:158
tree pretty printer
Definition: tree.h:178
virtual const TreeList & trees() const override
Definition: tree.h:151
virtual TreeRef map(std::function< TreeRef(TreeRef)> fn)
Definition: tree.h:75
static TreeRef create(Args &&...args)
Definition: tree.h:126
double value_
Definition: tree.h:118
static TreeRef create(Args &&...args)
Definition: tree.h:100
TreeList trees_
Definition: tree.h:174
const TreeRef & tree
Definition: tree.h:180
virtual const TreeList & trees() const
Definition: tree.h:69
virtual bool boolValue() const override
Definition: tree.h:122
std::string value_
Definition: tree.h:105
virtual const std::string & stringValue() const
Definition: tree.h:63
const std::string & get_flat(const TreeRef &t)
Definition: tree.h:183
std::vector< TreeRef > TreeList
Definition: tree.h:45
const TreeRef & tree(size_t i) const
Definition: tree.h:72
std::unordered_map< TreeRef, std::string > flat_strings
Definition: tree.h:182
int kind() const
Definition: tree.h:51
int kind_
Definition: tree.h:91
virtual const std::string & stringValue() const override
Definition: tree.h:96
void expect(int k, int numsubtrees)
Definition: tree.h:81
std::string kindToString(int kind)
virtual double doubleValue() const override
Definition: tree.h:109
void print(std::ostream &out, const TreeRef &t, int indent)
Definition: tree.h:207
virtual double doubleValue() const
Definition: tree.h:60
virtual bool isAtom() const
Definition: tree.h:54
pretty_tree(const TreeRef &tree, size_t col=40)
Definition: tree.h:179
static TreeRef create(int kind, const SourceRange &range_, TreeList &&trees_)
Definition: tree.h:155
Number(double value_)
Definition: tree.h:108
size_t end() const
Definition: lexer.h:341
std::shared_ptr< Tree > TreeRef
Definition: tree.h:44