Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

TlpLexer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <iostream>
35 #include <string>
36 #include <vector>
37 
38 
39 namespace ogdf {
40 
41 namespace tlp {
42 
43 
44 struct Token {
45 
46  enum class Type {
47  leftParen,
48  rightParen,
49  identifier,
50  string
51  } type;
52 
53  std::string *value; // Optional token value (avaliable in id and string).
54  size_t line, column; // Where given token occured for printing nice info.
55 
56  Token(const Type &type, size_t line, size_t column);
57  friend std::ostream &operator <<(std::istream &os, const Token &token);
58 
59  bool inline leftParen() const {
60  return type == Type::leftParen;
61  }
62 
63  bool inline rightParen() const {
64  return type == Type::rightParen;
65  }
66 
67  bool inline identifier() const {
68  return type == Type::identifier;
69  }
70 
71  bool inline identifier(const char *str) const {
72  return type == Type::identifier && (*value) == str;
73  }
74 
75  bool inline string() const {
76  return type == Type::string;
77  }
78 
79  bool inline string(const char *str) const {
80  return type == Type::string && (*value) == str;
81  }
82 };
83 
84 std::ostream &operator <<(std::ostream &os, const Token &token);
85 
86 
87 class Lexer {
88 private:
89  std::istream &m_istream;
90  std::string m_buffer;
91  std::string::const_iterator m_begin, m_end;
92  size_t m_line;
93 
94  std::vector<Token> m_tokens;
95 
96  bool fetchBuffer();
97  void cleanValues();
98 
99  bool tokenizeLine();
100  bool tokenizeString();
101  bool tokenizeIdentifier();
102 
103  size_t line() const {
104  return m_line;
105  }
106 
107  size_t column() const {
108  return std::distance(m_buffer.begin(), m_begin) + 1;
109  }
110 
111  static bool isIdentifier(char c);
112 
113 public:
114  explicit Lexer(std::istream &is);
115  ~Lexer();
116 
117  bool tokenize();
118  const std::vector<Token> &tokens() const {
119  return m_tokens;
120  }
121 };
122 
123 
124 } // end namespace tlp
125 
126 } // end namespace ogdf
bool identifier(const char *str) const
Definition: TlpLexer.h:71
const std::vector< Token > & tokens() const
Definition: TlpLexer.h:118
bool rightParen() const
Definition: TlpLexer.h:63
std::string::const_iterator m_end
Definition: TlpLexer.h:91
enum ogdf::tlp::Token::Type type
bool leftParen() const
Definition: TlpLexer.h:59
size_t column() const
Definition: TlpLexer.h:107
size_t m_line
Definition: TlpLexer.h:92
std::istream & m_istream
Definition: TlpLexer.h:89
bool string() const
Definition: TlpLexer.h:75
Token(const Type &type, size_t line, size_t column)
bool string(const char *str) const
Definition: TlpLexer.h:79
size_t column
Definition: TlpLexer.h:54
The namespace for all OGDF objects.
friend std::ostream & operator<<(std::istream &os, const Token &token)
std::vector< Token > m_tokens
Definition: TlpLexer.h:94
bool identifier() const
Definition: TlpLexer.h:67
size_t line() const
Definition: TlpLexer.h:103
std::string m_buffer
Definition: TlpLexer.h:90
std::string * value
Definition: TlpLexer.h:53