LL1Checker 5.0
“Tool for verifying LL(1) grammars and validating input strings.”
Loading...
Searching...
No Matches
lexer.hpp
1#pragma once
2#include "symbol_table.hpp"
3#include <regex>
4#include <string>
5#include <boost/spirit/include/lex_lexertl.hpp>
6#include <boost/bind.hpp>
7#include <boost/ref.hpp>
8#include <vector>
9
10using BaseLexer = boost::spirit::lex::lexertl::lexer<>;
11using Iterator = typename BaseLexer::iterator_type;
12
14class Lex {
15 std::string filename_;
16 std::string input_;
17 Iterator iter_, end_;
18 size_t current_{0};
19public:
21 struct Token {
22 symbol_table::TokenID type;
23 size_t pos;
24 };
25
26
30 Lex(std::string input, bool from_string);
31
32 void Tokenize();
33
41 Token Next();
42
48 std::string& input();
49
72 static std::string format_error_window(const std::string& input, size_t pos,
73 size_t& out_err_line,
74 size_t& out_err_col,
75 size_t context_lines = 2,
76 size_t max_line_width = 40,
77 size_t window_width = 20);
78
79private:
80
82 struct Pattern {
83 symbol_table::TokenID type;
84 std::string regex;
85
86 };
87
88 std::vector<Token> tokens_;
89};
static std::string format_error_window(const std::string &input, size_t pos, size_t &out_err_line, size_t &out_err_col, size_t context_lines=2, size_t max_line_width=40, size_t window_width=20)
Generate a formatted snippet around an error location.
Definition lexer.cpp:65
std::string input_
Complete contents of the input file.
Definition lexer.hpp:16
std::string filename_
Path of the file being lexed.
Definition lexer.hpp:15
Token Next()
Retrieves the next token from input string in a lazy way.
Definition lexer.cpp:167
std::string & input()
Access the raw input buffer.
Definition lexer.cpp:163
Lex(std::string input, bool from_string)
Definition lexer.cpp:12
Internal helper: a regex pattern and its associated token type.
Definition lexer.hpp:82
std::string regex
The regex used to match and extract the token.
Definition lexer.hpp:84
symbol_table::TokenID type
Token ID for this pattern.
Definition lexer.hpp:83
Represents a single token produced by the lexer.
Definition lexer.hpp:21
size_t pos
Byte-offset in the input where this token starts.
Definition lexer.hpp:23
symbol_table::TokenID type
Token ID.
Definition lexer.hpp:22