Lexer tokens now include source name as part of the token

This commit is contained in:
2024-07-05 18:18:11 +01:00
parent 65ce50f620
commit 036ac03176
3 changed files with 25 additions and 15 deletions

View File

@@ -482,7 +482,8 @@ namespace Lexer
return token; return token;
} }
Err tokenise_buffer(string_view source, std::vector<Token *> &tokens) Err tokenise_buffer(string_view source_name, string_view source,
std::vector<Token *> &tokens)
{ {
size_t column = 0, line = 1; size_t column = 0, line = 1;
while (source.size() > 0) while (source.size() > 0)
@@ -571,8 +572,9 @@ namespace Lexer
if (is_token) if (is_token)
{ {
t.line = line; t.source_name = source_name;
Token *acc = new Token{t}; t.line = line;
Token *acc = new Token{t};
tokens.push_back(acc); tokens.push_back(acc);
} }
} }
@@ -580,17 +582,20 @@ namespace Lexer
} }
Token::Token() Token::Token()
{} {
}
Token::Token(Token::Type type, string_view content, size_t col, size_t line, Token::Token(Token::Type type, string_view content, size_t col, size_t line,
OperandType optype) OperandType optype)
: type{type}, column{col}, line{line}, content{content}, : type{type}, operand_type{optype}, column{col}, line{line},
operand_type{optype} content{content}
{} {
}
Err::Err(Err::Type type, size_t col, size_t line) Err::Err(Err::Type type, size_t col, size_t line)
: col{col}, line{line}, type{type} : col{col}, line{line}, type{type}
{} {
}
std::string to_string(const Token::Type &type) std::string to_string(const Token::Type &type)
{ {

View File

@@ -27,15 +27,20 @@ namespace Lexer
{ {
enum class Type enum class Type
{ {
// Preprocessor and other parse time constants
PP_CONST, // %const(<symbol>)... PP_CONST, // %const(<symbol>)...
PP_USE, // %use <string> PP_USE, // %use <string>
PP_END, // %end PP_END, // %end
PP_REFERENCE, // $<symbol> PP_REFERENCE, // $<symbol>
GLOBAL, GLOBAL,
STAR, STAR,
// Literals
LITERAL_NUMBER, LITERAL_NUMBER,
LITERAL_CHAR, LITERAL_CHAR,
LITERAL_STRING, LITERAL_STRING,
SYMBOL,
// Instruction
NOOP, NOOP,
HALT, HALT,
PUSH, PUSH,
@@ -70,11 +75,7 @@ namespace Lexer
CALL, CALL,
CALL_STACK, CALL_STACK,
RET, RET,
SYMBOL,
} type; } type;
size_t column, line;
std::string content;
enum class OperandType enum class OperandType
{ {
NIL, NIL,
@@ -86,9 +87,12 @@ namespace Lexer
LONG LONG
} operand_type; } operand_type;
size_t column, line;
std::string source_name, content;
Token(); Token();
Token(Token::Type, std::string_view content = "", size_t col = 0, Token(Token::Type, std::string_view content = "", size_t col = 0,
size_t line = 0, OperandType optype = OperandType::NIL); size_t line = 0, OperandType type = OperandType::NIL);
}; };
struct Err struct Err
@@ -110,7 +114,8 @@ namespace Lexer
Err(Type type = Type::OK, size_t col = 0, size_t line = 0); Err(Type type = Type::OK, size_t col = 0, size_t line = 0);
}; };
Err tokenise_buffer(std::string_view, std::vector<Token *> &); Err tokenise_buffer(std::string_view source_name, std::string_view content,
std::vector<Token *> &vec);
std::string to_string(const Token::Type &); std::string to_string(const Token::Type &);
std::string to_string(const Token::OperandType &); std::string to_string(const Token::OperandType &);

View File

@@ -85,7 +85,7 @@ int main(int argc, const char *argv[])
} }
original = string_view{source_str}; original = string_view{source_str};
src = string_view{source_str}; src = string_view{source_str};
lerr = tokenise_buffer(src, tokens); lerr = tokenise_buffer(source_name, src, tokens);
if (lerr.type != Lexer ::Err::Type::OK) if (lerr.type != Lexer ::Err::Type::OK)
{ {