74 lines
1.5 KiB
C++
74 lines
1.5 KiB
C++
#ifndef COMDEL_LEXER_H
|
|
#define COMDEL_LEXER_H
|
|
|
|
#include "parse_context.h"
|
|
#include "presult.h"
|
|
#include "source_error.h"
|
|
#include "token.h"
|
|
|
|
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
/** Contains results of tokenizing,
|
|
* if errors isn't empty tokenizing has failed */
|
|
struct LexerResult {
|
|
std::vector<Token> tokens;
|
|
std::vector<SourceError> errors;
|
|
};
|
|
|
|
/** Used to tokenize input string */
|
|
class ComdelLexer {
|
|
|
|
enum Radix {
|
|
BIN_NUMBER = 2,
|
|
DEC_NUMBER = 10,
|
|
HEX_NUMBER = 16
|
|
};
|
|
|
|
/** Source file */
|
|
std::string source;
|
|
std::vector<Token> tokens;
|
|
std::vector<SourceError> errors;
|
|
/** Source file */
|
|
ParseContext *parseContext;
|
|
unsigned fileId;
|
|
|
|
public:
|
|
ComdelLexer(std::string fileName, std::string source, ParseContext *parseContext);
|
|
|
|
LexerResult tokenize();
|
|
|
|
private:
|
|
/** Current parsing info */
|
|
Position tokenBegin;
|
|
Position position;
|
|
char ch;
|
|
|
|
/** Methods used to skip unused content */
|
|
void skipWhitespace();
|
|
void skipComment();
|
|
bool skipMultilineComment();
|
|
void bump(unsigned count = 1);
|
|
|
|
/** Metods used for number parsing */
|
|
unsigned takeNumberInRadix(Radix radix);
|
|
bool digitIsValid(char ch, Radix radix);
|
|
Radix takeRadix();
|
|
|
|
unsigned takeHexColor();
|
|
|
|
PResult<TokenType> nextTokenType();
|
|
|
|
PResult<TokenType> takeString();
|
|
PResult<TokenType> takeRawString();
|
|
|
|
char peek();
|
|
|
|
/** Checks if we reached end of file */
|
|
bool eof();
|
|
};
|
|
|
|
#endif // COMDEL_LEXER_H
|