Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 namespace ogdf {
39 
40 namespace tlp {
41 
42 
43 struct Token {
44  enum class Type { leftParen, rightParen, identifier, string } type;
45 
46  std::string* value; // Optional token value (avaliable in id and string).
47  size_t line, column; // Where given token occured for printing nice info.
48 
49  Token(const Type& type, size_t line, size_t column);
50  friend std::ostream& operator<<(std::istream& os, const Token& token);
51 
52  bool inline leftParen() const { return type == Type::leftParen; }
53 
54  bool inline rightParen() const { return type == Type::rightParen; }
55 
56  bool inline identifier() const { return type == Type::identifier; }
57 
58  bool inline identifier(const char* str) const {
59  return type == Type::identifier && *value == str;
60  }
61 
62  bool inline string() const { return type == Type::string; }
63 
64  bool inline string(const char* str) const { return type == Type::string && *value == str; }
65 };
66 
67 std::ostream& operator<<(std::ostream& os, const Token& token);
68 
69 class Lexer {
70 private:
71  std::istream& m_istream;
72  std::string m_buffer;
73  std::string::const_iterator m_begin, m_end;
74  size_t m_line;
75 
76  std::vector<Token> m_tokens;
77 
78  bool fetchBuffer();
79  void cleanValues();
80 
81  bool tokenizeLine();
82  bool tokenizeString();
83  bool tokenizeIdentifier();
84 
85  size_t line() const { return m_line; }
86 
87  size_t column() const { return std::distance(m_buffer.begin(), m_begin) + 1; }
88 
89  static bool isIdentifier(char c);
90 
91 public:
92  explicit Lexer(std::istream& is);
93  ~Lexer();
94 
95  bool tokenize();
96 
97  const std::vector<Token>& tokens() const { return m_tokens; }
98 };
99 
100 }
101 }
ogdf::tlp::Token::line
size_t line
Definition: TlpLexer.h:47
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::tlp::Lexer::m_tokens
std::vector< Token > m_tokens
Definition: TlpLexer.h:76
ogdf::tlp::Token::Type::string
@ string
ogdf::tlp::Token::Type::leftParen
@ leftParen
ogdf::tlp::Token::type
enum ogdf::tlp::Token::Type type
ogdf::tlp::Lexer::m_begin
std::string::const_iterator m_begin
Definition: TlpLexer.h:73
ogdf::tlp::Lexer::cleanValues
void cleanValues()
ogdf::tlp::Token
Definition: TlpLexer.h:43
ogdf::tlp::Token::Type::rightParen
@ rightParen
ogdf::tlp::operator<<
std::ostream & operator<<(std::ostream &os, const Token &token)
ogdf::tlp::Lexer::~Lexer
~Lexer()
ogdf::tlp::Token::operator<<
friend std::ostream & operator<<(std::istream &os, const Token &token)
ogdf::tlp::Lexer::m_istream
std::istream & m_istream
Definition: TlpLexer.h:71
ogdf::tlp::Token::Type
Type
Definition: TlpLexer.h:44
ogdf::tlp::Lexer::tokens
const std::vector< Token > & tokens() const
Definition: TlpLexer.h:97
ogdf::tlp::Token::string
bool string() const
Definition: TlpLexer.h:62
ogdf::tlp::Lexer::fetchBuffer
bool fetchBuffer()
ogdf::tlp::Lexer::column
size_t column() const
Definition: TlpLexer.h:87
ogdf::tlp::Lexer::Lexer
Lexer(std::istream &is)
ogdf::tlp::Token::leftParen
bool leftParen() const
Definition: TlpLexer.h:52
ogdf::tlp::Lexer
Definition: TlpLexer.h:69
ogdf::tlp::Token::rightParen
bool rightParen() const
Definition: TlpLexer.h:54
ogdf::tlp::Token::Type::identifier
@ identifier
ogdf::tlp::Lexer::m_buffer
std::string m_buffer
Definition: TlpLexer.h:72
ogdf::tlp::Token::identifier
bool identifier(const char *str) const
Definition: TlpLexer.h:58
ogdf::tlp::Token::string
bool string(const char *str) const
Definition: TlpLexer.h:64
ogdf::tlp::Lexer::tokenizeIdentifier
bool tokenizeIdentifier()
ogdf::tlp::Lexer::isIdentifier
static bool isIdentifier(char c)
ogdf::tlp::Token::column
size_t column
Definition: TlpLexer.h:47
ogdf::tlp::Lexer::tokenizeString
bool tokenizeString()
ogdf::tlp::Lexer::line
size_t line() const
Definition: TlpLexer.h:85
ogdf::tlp::Token::identifier
bool identifier() const
Definition: TlpLexer.h:56
ogdf::tlp::Token::value
std::string * value
Definition: TlpLexer.h:46
ogdf::tlp::Lexer::tokenize
bool tokenize()
ogdf::tlp::Lexer::tokenizeLine
bool tokenizeLine()
ogdf::tlp::Token::Token
Token(const Type &type, size_t line, size_t column)
ogdf::tlp::Lexer::m_end
std::string::const_iterator m_end
Definition: TlpLexer.h:73
ogdf::tlp::Lexer::m_line
size_t m_line
Definition: TlpLexer.h:74