Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

DotLexer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <cstddef>
35 #include <iostream>
36 #include <string>
37 #include <vector>
38 
39 namespace ogdf {
40 
41 namespace dot {
42 
43 
45 
55 struct Token {
56  enum class Type {
57  // Operators
58  assignment,
59  colon,
60  semicolon,
61  comma,
64  // Brackets
67  leftBrace,
68  rightBrace,
69  // Keywords
70  graph,
71  digraph,
72  subgraph,
73  node,
74  edge,
75  strict,
76  // Values
78  };
79 
83  size_t row;
85  size_t column;
87  std::string* value;
88 
89  Token(size_t tokenRow, size_t tokenColumn, std::string* identifierContent = nullptr);
90 
92  static std::string toString(const Type& type);
93 };
94 
96 
105 class Lexer {
106 private:
107  std::istream& m_input;
108 
109  std::string m_buffer; // Current line of given file.
110  size_t m_row, m_col; // Current position in parsed file.
111 
112  std::vector<Token> m_tokens;
113 
114  bool tokenizeLine();
115 
117 
122  bool match(const Token::Type& type, bool word = false);
123 
125 
130  bool match(const std::string& str, bool word = false);
131 
133 
137  bool identifier(Token& token);
138 
140 
144  bool isDotAlnum(signed char c);
145 
146 public:
148  explicit Lexer(std::istream& input);
149  ~Lexer();
150 
152 
155  bool tokenize();
157  const std::vector<Token>& tokens() const;
158 };
159 
160 }
161 }
ogdf::dot::Token::Type::colon
@ colon
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::dot::Token::Type::edgeOpDirected
@ edgeOpDirected
ogdf::dot::Token
Just a simple token struct representing a DOT file fragment.
Definition: DotLexer.h:55
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:56
ogdf::dot::Token::Type::rightBrace
@ rightBrace
ogdf::dot::Token::row
size_t row
Indicates a token row (line).
Definition: DotLexer.h:83
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:107
ogdf::dot::Lexer
Lexical analysis tool.
Definition: DotLexer.h:105
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:87
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:112
ogdf::dot::Token::type
Type type
The type of an field.
Definition: DotLexer.h:81
ogdf::dot::Lexer::m_row
size_t m_row
Definition: DotLexer.h:110
ogdf::dot::Lexer::m_col
size_t m_col
Definition: DotLexer.h:110
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:85
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:109
ogdf::dot::Token::Type::strict
@ strict