Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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