48 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #ifndef YERBACON_LEX_H
 | |
| #define YERBACON_LEX_H
 | |
| 
 | |
| #include <vector>
 | |
| #include "Yerbacon.hpp"
 | |
| #include <limits>
 | |
| #include <ostream>
 | |
| #include <concepts>
 | |
| 
 | |
| struct tok {
 | |
|     typedef Yerbacon::Exception LexerException;
 | |
|     enum type: const unsigned short {
 | |
|         UNEXPECTED = std::numeric_limits<unsigned char>::max() + 1, IDENTIFIER, NUMBER, ALPHACHAR,
 | |
|         DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.', COMMA = ',', SEMICOLON = ';',
 | |
|         LPAR = '(', LBRACE = '{', LBRACKET = '[', RPAR = ')',
 | |
|         RBRACE = '}', RBRACKET = ']',
 | |
|         PLUS = '+', HYPHEN = '-', DIVIDE = '/',
 | |
|         LCOMP = '>', RCOMP = '<',
 | |
|         SQUOTE = '\'', ASTERISK = '*', STRING = '"',
 | |
|     };
 | |
|     static auto inverseLCharacter(const unsigned char& character) {
 | |
|         return static_cast<unsigned char>(((character + 2) - (character == tok::LPAR)));
 | |
|     };
 | |
|     type toktype;
 | |
|     std::string toktext;
 | |
|     unsigned long line;
 | |
|     tok() = delete;
 | |
|     tok(const type& Type, std::string_view Text, const decltype(line)& line = 1): toktype(Type), toktext(Text), line(line) {}
 | |
|     explicit tok(const type& Type, const decltype(line)& line = 1): tok(Type, std::string(1, Type), line) {};
 | |
|     operator char() const { return static_cast<char>(toktype); }
 | |
|     friend std::ostream& operator<<(std::ostream& output, const tok& it) { return output << it.toktext; }
 | |
| };
 | |
| 
 | |
| auto find_corresponding(std::input_iterator auto begin, std::input_iterator auto end, const unsigned char open, const unsigned char close) {
 | |
|     unsigned short occurrences = 1;
 | |
|     return std::find_if(begin, end, [&open, &close, &occurrences](const char& it){
 | |
|         if (it == open) {
 | |
|             ++occurrences;
 | |
|         } else if (it == close) {
 | |
|             return --occurrences == 0;
 | |
|         }
 | |
|         return false;
 | |
|     });
 | |
| }
 | |
| 
 | |
| std::vector<tok> lex(const std::string_view& in);
 | |
| 
 | |
| #endif //YERBACON_TEST_H
 |