Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
40namespace ogdf {
41
42namespace tlp {
43
44
45struct 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
69std::ostream& operator<<(std::ostream& os, const Token& token);
70
71class Lexer {
72private:
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
82
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
93public:
94 explicit Lexer(std::istream& is);
96
97 bool tokenize();
98
99 const std::vector<Token>& tokens() const { return m_tokens; }
100};
101
102}
103}
size_t column() const
Definition TlpLexer.h:89
Lexer(std::istream &is)
static bool isIdentifier(char c)
std::istream & m_istream
Definition TlpLexer.h:73
bool tokenizeIdentifier()
std::string::const_iterator m_begin
Definition TlpLexer.h:75
std::string::const_iterator m_end
Definition TlpLexer.h:75
std::vector< Token > m_tokens
Definition TlpLexer.h:78
const std::vector< Token > & tokens() const
Definition TlpLexer.h:99
size_t line() const
Definition TlpLexer.h:87
std::string m_buffer
Definition TlpLexer.h:74
std::ostream & operator<<(std::ostream &os, const Token &token)
The namespace for all OGDF objects.
std::string * value
Definition TlpLexer.h:48
Token(const Type &type, size_t line, size_t column)
bool rightParen() const
Definition TlpLexer.h:56
bool string(const char *str) const
Definition TlpLexer.h:66
bool identifier() const
Definition TlpLexer.h:58
enum ogdf::tlp::Token::Type type
friend std::ostream & operator<<(std::istream &os, const Token &token)
bool identifier(const char *str) const
Definition TlpLexer.h:60
bool string() const
Definition TlpLexer.h:64
bool leftParen() const
Definition TlpLexer.h:54