hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
lannotation.H
Go to the documentation of this file.
1 /*
2  * lannotation : hold on to a reference to some section of lexical input
3  */
4 
5 #ifndef HOBBES_UTIL_LANNOTATION_HPP_INCLUDED
6 #define HOBBES_UTIL_LANNOTATION_HPP_INCLUDED
7 
8 #include <hobbes/util/ptr.H>
9 #include <hobbes/util/str.H>
10 #include <string>
11 #include <vector>
12 #include <map>
13 #include <stdexcept>
14 
15 namespace hobbes {
16 
17 typedef std::pair<bool, std::string> BuffOrFilename;
18 typedef std::shared_ptr<BuffOrFilename> BuffOrFilenamePtr;
19 typedef std::pair<size_t, size_t> Pos; // line#, offset
20 typedef std::pair<Pos, Pos> LexRange;
21 
24 
25  BuffOrFilenamePtr bfptr;
26  Pos p0, p1;
27 
28  std::string filename() const;
29  std::string lineDesc() const;
30  str::seq lines(size_t, size_t) const;
31 
32  static LexicalAnnotation null();
34 };
35 
37 public:
40  LexicallyAnnotated(const LexRange&);
41  LexicallyAnnotated(const Pos&, const Pos&);
42 
43  // the lexical annotation data (source file/buffer and line:col range)
44  const LexicalAnnotation& la() const;
45 
46  // push some hidden state to apply in bulk to subsequent annotations
47  static void pushFileContext(const std::string&);
48  static void pushLiteralContext(const std::string&);
49  static void popContext();
50  static LexicalAnnotation make(const Pos&, const Pos&);
51 private:
53 
55 };
56 
57 typedef std::pair<std::string, LexicalAnnotation> annmsg;
58 typedef std::vector<annmsg> annmsgs;
59 
60 class annotated_error : public std::runtime_error {
61 public:
62  annotated_error(const annmsgs&);
63  annotated_error(const LexicalAnnotation&, const std::string&);
64  annotated_error(const LexicallyAnnotated&, const std::string&);
65 
66  const annmsgs& messages() const;
67 
68  static annotated_error fileError(const std::string&, const Pos&, const Pos&, const std::string&);
69  static annotated_error bufferError(const std::string&, const Pos&, const Pos&, const std::string&);
70 private:
71  annmsgs amsgs;
72 
73  static std::string plainDesc(const annmsgs&);
74 };
75 
76 }
77 
78 #endif
static LexicalAnnotation null()
Definition: lannotation.C:64
std::pair< Pos, Pos > LexRange
Definition: lannotation.H:20
static LexicalAnnotation merge(const LexicalAnnotation &, const LexicalAnnotation &)
Definition: lannotation.C:68
T * make(const Args &... args)
Definition: hobbes.H:60
Definition: boot.H:7
std::pair< bool, std::string > BuffOrFilename
Definition: lannotation.H:17
LexicalAnnotation()
Definition: lannotation.C:9
Pos p1
Definition: lannotation.H:26
std::pair< std::string, LexicalAnnotation > annmsg
Definition: lannotation.H:57
str::seq lines(size_t, size_t) const
Definition: lannotation.C:52
annmsgs amsgs
Definition: lannotation.H:71
std::pair< size_t, size_t > Pos
Definition: lannotation.H:19
Definition: lannotation.H:36
Definition: lannotation.H:60
Definition: lannotation.H:22
std::vector< std::string > seq
Definition: str.H:19
BuffOrFilenamePtr bfptr
Definition: lannotation.H:25
Pos p0
Definition: lannotation.H:26
LexicalAnnotation lannotation
Definition: lannotation.H:52
std::string filename() const
Definition: lannotation.C:19
std::string lineDesc() const
Definition: lannotation.C:37
std::vector< annmsg > annmsgs
Definition: lannotation.H:58
std::shared_ptr< BuffOrFilename > BuffOrFilenamePtr
Definition: lannotation.H:18