Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

DotLexer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <iostream>
35 #include <sstream>
36 #include <vector>
37 
38 namespace ogdf {
39 
40 namespace dot {
41 
42 
44 
54 struct Token {
55  enum class Type {
56  // Operators
57  assignment,
58  colon,
59  semicolon,
60  comma,
63  // Brackets
66  leftBrace,
67  rightBrace,
68  // Keywords
69  graph,
70  digraph,
71  subgraph,
72  node,
73  edge,
74  strict,
75  // Values
77  };
78 
82  size_t row;
84  size_t column;
86  std::string* value;
87 
88  Token(size_t tokenRow, size_t tokenColumn, std::string* identifierContent = nullptr);
89 
91  static std::string toString(const Type& type);
92 };
93 
95 
104 class Lexer {
105 private:
106  std::istream& m_input;
107 
108  std::string m_buffer; // Current line of given file.
109  size_t m_row, m_col; // Current position in parsed file.
110 
111  std::vector<Token> m_tokens;
112 
113  bool tokenizeLine();
114 
116 
121  bool match(const Token::Type& type, bool word = false);
122 
124 
129  bool match(const std::string& str, bool word = false);
130 
132 
136  bool identifier(Token& token);
137 
139 
143  bool isDotAlnum(signed char c);
144 
145 public:
147  explicit Lexer(std::istream& input);
148  ~Lexer();
149 
151 
154  bool tokenize();
156  const std::vector<Token>& tokens() const;
157 };
158 
159 }
160 }
ogdf::dot::Token::Type::colon
@ colon
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::dot::Token::Type::edgeOpDirected
@ edgeOpDirected
ogdf::dot::Token
Just a simple token struct representing a DOT file fragment.
Definition: DotLexer.h:54
ogdf::dot::Token::Token
Token(size_t tokenRow, size_t tokenColumn, std::string *identifierContent=nullptr)
ogdf::dot::Token::toString
static std::string toString(const Type &type)
Returns string representation of given token type.
ogdf::dot::Token::Type::identifier
@ identifier
ogdf::dot::Token::Type::comma
@ comma
ogdf::dot::Token::Type
Type
Definition: DotLexer.h:55
ogdf::dot::Token::Type::rightBrace
@ rightBrace
ogdf::dot::Token::row
size_t row
Indicates a token row (line).
Definition: DotLexer.h:82
ogdf::dot::Token::Type::graph
@ graph
ogdf::dot::Lexer::tokenize
bool tokenize()
Scans input and turns it into token list.
ogdf::dot::Token::Type::edge
@ edge
ogdf::dot::Token::Type::edgeOpUndirected
@ edgeOpUndirected
ogdf::dot::Token::Type::semicolon
@ semicolon
ogdf::dot::Token::Type::node
@ node
ogdf::dot::Token::Type::rightBracket
@ rightBracket
ogdf::dot::Token::Type::leftBracket
@ leftBracket
ogdf::dot::Lexer::m_input
std::istream & m_input
Definition: DotLexer.h:106
ogdf::dot::Lexer
Lexical analysis tool.
Definition: DotLexer.h:104
ogdf::dot::Lexer::identifier
bool identifier(Token &token)
Checks whether head is an identifier.
ogdf::dot::Token::Type::digraph
@ digraph
ogdf::dot::Lexer::isDotAlnum
bool isDotAlnum(signed char c)
Checks if character is allowed in an identifier by DOT standard.
ogdf::dot::Token::value
std::string * value
Identifier content (nullptr for non-id tokens).
Definition: DotLexer.h:86
backward::Color::type
type
Definition: backward.hpp:1716
ogdf::dot::Token::Type::leftBrace
@ leftBrace
ogdf::dot::Lexer::tokens
const std::vector< Token > & tokens() const
Returns list of tokens (first use Lexer::tokenize())
ogdf::dot::Lexer::m_tokens
std::vector< Token > m_tokens
Definition: DotLexer.h:111
ogdf::dot::Token::type
Type type
The type of an field.
Definition: DotLexer.h:80
ogdf::dot::Lexer::m_row
size_t m_row
Definition: DotLexer.h:109
ogdf::dot::Lexer::m_col
size_t m_col
Definition: DotLexer.h:109
ogdf::dot::Lexer::Lexer
Lexer(std::istream &input)
Initializes lexer with given input (but does nothing to it).
ogdf::dot::Lexer::match
bool match(const Token::Type &type, bool word=false)
Checks if head matches given token. Advances head on success.
ogdf::dot::Token::column
size_t column
Indicated a token column.
Definition: DotLexer.h:84
ogdf::dot::Token::Type::subgraph
@ subgraph
ogdf::dot::Lexer::~Lexer
~Lexer()
ogdf::dot::Lexer::tokenizeLine
bool tokenizeLine()
ogdf::dot::Token::Type::assignment
@ assignment
ogdf::dot::Lexer::m_buffer
std::string m_buffer
Definition: DotLexer.h:108
ogdf::dot::Token::Type::strict
@ strict