hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
data.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_PARSE_DATAUTIL_HPP_INCLUDED
3 #define HOBBES_PARSE_DATAUTIL_HPP_INCLUDED
4 
5 #include <hobbes/util/array.H>
6 #include <string>
7 #include <sstream>
8 #include <vector>
9 #include <stack>
10 #include <set>
11 #include <map>
12 #include <stdexcept>
13 #include <typeinfo>
14 #include <cxxabi.h>
15 
16 #include <memory>
17 
18 namespace hobbes {
19 
20 typedef unsigned int nat;
21 typedef std::vector<nat> nats;
22 
23 template <typename T>
24  std::string show(const T* t) {
25  std::ostringstream ss;
26  t->show(ss);
27  return ss.str();
28  }
29 
30 template <typename T>
31  std::string show(const std::vector<T*>& ts) {
32  std::ostringstream ss;
33  ss << "[";
34  if (ts.size() > 0) {
35  ts[0]->show(ss);
36  for (unsigned int i = 1; i < ts.size(); ++i) {
37  ss << ", ";
38  ts[i]->show(ss);
39  }
40  }
41  ss << "]";
42  return ss.str();
43  }
44 
45 // a little bit of state to map a sequence index to line numbers
46 // we just watch characters and record newlines
47 class linedb {
48 public:
49  typedef std::shared_ptr<linedb> ptr;
50  typedef std::pair<nat, nat> LineCol;
51 
52  linedb(nat stype, const std::string& sdesc);
53 
54  nat sourceType() const;
55  const std::string& sourceDesc() const;
56 
57  void step(char x);
58  void reset();
59 
60  LineCol pos(nat i) const;
61 private:
62  nat stype;
63  std::string sdesc;
64  nat c;
65  nats lp; // sequence positions corresponding to line index
66 };
67 
68 // load a set of lines around an implicated set from the specified line database
69 typedef std::vector<std::string> strings;
70 typedef std::pair<nats, strings> ldblines;
71 
72 ldblines load(const linedb::ptr& ldb, const linedb::LineCol& i, const linedb::LineCol& f);
73 
74 template <typename K, typename V>
75  V mapLookup(const std::map<K, V>& m, const K& k) {
76  typedef typename std::map<K, V>::const_iterator CI;
77  CI i = m.find(k);
78  if (i == m.end()) {
79  throw std::runtime_error("Unexpected map index undefined");
80  } else {
81  return i->second;
82  }
83  }
84 
85 }
86 
87 #endif
88 
nat stype
Definition: data.H:62
std::pair< nat, nat > LineCol
Definition: data.H:50
const std::string & sourceDesc() const
Definition: data.C:18
void step(char x)
Definition: data.C:22
unsigned int nat
Definition: data.H:20
void reset()
Definition: data.C:29
Definition: boot.H:7
LineCol pos(nat i) const
Definition: data.C:34
nat sourceType() const
Definition: data.C:14
V mapLookup(const std::map< K, V > &m, const K &k)
Definition: data.H:75
std::shared_ptr< linedb > ptr
Definition: data.H:49
nats lp
Definition: data.H:65
std::string show(const Expr &e)
Definition: expr.C:19
std::pair< nats, strings > ldblines
Definition: data.H:70
ldblines load(const linedb::ptr &ldb, const linedb::LineCol &i, const linedb::LineCol &f)
Definition: data.C:48
std::string sdesc
Definition: data.H:63
nat c
Definition: data.H:64
Definition: data.H:47
std::vector< nat > nats
Definition: data.H:21
std::vector< std::string > strings
Definition: data.H:69
LexicalAnnotation m(const YYLTYPE &p)
Definition: hexpr.parse.C:127
linedb(nat stype, const std::string &sdesc)
Definition: data.C:11