teditor  1.8.0@@fee5e94
Terminal based editor written in C++
lexer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <core/pos2d.h>
5 #include <vector>
6 #include <unordered_map>
7 #include <unordered_set>
8 #include <ostream>
9 #include "core/utils.h"
10 #include "parse_tree.h"
11 
12 namespace teditor {
13 namespace parser {
14 
15 
17 struct TokenDef {
18  uint32_t type;
19  std::string regex;
20  std::string name;
21 };
22 typedef std::vector<TokenDef> TokenDefs;
23 
24 
25 class Scanner;
26 struct NFA;
28 struct Lexer {
29  Lexer(const TokenDefs& t);
30  virtual ~Lexer();
31  virtual Token next(Scanner* sc);
32  Token next(Scanner* sc, const std::unordered_set<uint32_t>& ignoreTypes);
33  Token next(Scanner* sc, uint32_t ignoreType);
34  const char* token2str(uint32_t tok) const;
35 
36  private:
37  std::vector<NFA*> nfas;
38  TokenDefs tokenDefs;
39  std::unordered_map<uint32_t, std::string> names;
40 
41  void reset();
42  bool step(char c, const Point& pt, int& nActives, int& nSoleMatches,
43  int& nMatches);
44  void getLongestMatchingToken(Token& ret, bool lastRemainingState);
45 }; // struct Lexer
46 
47 
48 } // namespace parser
49 } // namespace teditor
parse_tree.h
teditor::parser::Lexer::token2str
const char * token2str(uint32_t tok) const
Definition: lexer.cpp:71
teditor::parser::TokenDef
Definition: lexer.h:17
teditor::parser::Scanner::isEof
virtual bool isEof() const =0
nfa.h
teditor::parser::Token::type
uint32_t type
Definition: parse_tree.h:19
teditor::parser::Scanner::next
virtual char next(Point &pt)=0
teditor::parser::NFA
Ken-Thompson NFA as described here: https://swtch.com/~rsc/regexp/regexp1.html but adjusted to work w...
Definition: nfa.h:23
teditor::parser::Token::end
Point end
Definition: parse_tree.h:23
utils.h
teditor::parser::Lexer::Lexer
Lexer(const TokenDefs &t)
Definition: lexer.cpp:8
teditor::parser::Lexer
Definition: lexer.h:28
teditor::parser::TokenDef::regex
std::string regex
Definition: lexer.h:19
teditor::parser::Token
Definition: parse_tree.h:13
teditor::parser::Token::Unknown
static const uint32_t Unknown
Definition: parse_tree.h:30
teditor::parser::Token::End
static const uint32_t End
Definition: parse_tree.h:28
teditor::parser::Token::Root
static const uint32_t Root
Definition: parse_tree.h:32
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::parser::TokenDef::name
std::string name
Definition: lexer.h:20
teditor::Pos2d< int >
lexer.h
teditor::parser::Scanner
Definition: scanner.h:11
teditor::parser::TokenDef::type
uint32_t type
Definition: lexer.h:18
teditor::parser::Scanner::rewind
virtual void rewind()=0
pos2d.h
teditor::parser::TokenDefs
std::vector< TokenDef > TokenDefs
Definition: lexer.h:22
scanner.h
teditor::parser::Lexer::~Lexer
virtual ~Lexer()
Definition: lexer.cpp:19
teditor::parser::Token::start
Point start
Definition: parse_tree.h:21
teditor::parser::Lexer::next
virtual Token next(Scanner *sc)
Definition: lexer.cpp:32
teditor
Definition: any.hpp:10