Latest stuff, rewriting lexer
This commit is contained in:
+50
-101
@@ -1,111 +1,60 @@
|
||||
/// AUTOGENERATED COPYRIGHT HEADER START
|
||||
// Copyright (C) 2017-2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
||||
// AUTOGENERATED COPYRIGHT HEADER END
|
||||
#pragma once
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <inttypes.h>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace blitz {
|
||||
class Lexer {
|
||||
public:
|
||||
enum class Token : uint64_t {
|
||||
TokenUnknown,
|
||||
TokenEOF,
|
||||
TokenNewLine,
|
||||
// ToDo:
|
||||
// - Figure out a way to let the lexer output line and character information?
|
||||
|
||||
// Symbols
|
||||
TokenPlus,
|
||||
TokenMinus,
|
||||
TokenSlashForward,
|
||||
TokenSlashBackward,
|
||||
TokenMultiply,
|
||||
TokenEqual,
|
||||
TokenOctothorp,
|
||||
TokenPercent,
|
||||
TokenDollar,
|
||||
TokenRoundBracketOpen,
|
||||
TokenRoundBracketClose,
|
||||
TokenSquareBracketOpen,
|
||||
TokenSquareBracketClose,
|
||||
TokenAngleBracketOpen,
|
||||
TokenAngleBracketClose,
|
||||
TokenDot,
|
||||
TokenColon,
|
||||
TokenComma,
|
||||
TokenSemicolon,
|
||||
TokenCaret,
|
||||
TokenBitNot /*~*/,
|
||||
namespace blitz {
|
||||
struct token {
|
||||
uint64_t line;
|
||||
uint64_t character;
|
||||
|
||||
// String Delimiter
|
||||
TokenDoubleQuote,
|
||||
|
||||
// Types
|
||||
TokenText,
|
||||
TokenNumber,
|
||||
TokenDecimal,
|
||||
TokenQuotedText, // Text encapsulated by TokenDoubleQuote
|
||||
TokenComment,
|
||||
|
||||
// Binary
|
||||
TokenNot,
|
||||
TokenAnd, TokenOr, TokenXor,
|
||||
TokenShl, TokenShr,
|
||||
TokenSal, TokenSar,
|
||||
TokenFalse, TokenTrue,
|
||||
|
||||
// Conversion
|
||||
TokenFloat,
|
||||
TokenString, TokenHex,
|
||||
TokenInt,
|
||||
|
||||
// Control
|
||||
TokenIf, TokenThen, TokenElseIf, TokenElse, TokenEndIf,
|
||||
TokenSelect, TokenCase, TokenDefault, // End Select = TokenEnd, TokenSelect.
|
||||
TokenGoto, TokenGosub,
|
||||
TokenReturn,
|
||||
TokenFunction, // End Function = TokenEnd, TokenFunction.
|
||||
TokenEnd,
|
||||
TokenStop /* DEBUGGER! Ignore in Release mode. */,
|
||||
|
||||
// Loop
|
||||
TokenFor, TokenTo, TokenNext,
|
||||
TokenWhile, TokenWend,
|
||||
TokenRepeat, TokenUntil, TokenForever,
|
||||
TokenExit,
|
||||
|
||||
// Math
|
||||
TokenAbs, TokenSign /*Sgn*/,
|
||||
TokenCos, TokenSin, TokenTan,
|
||||
TokenACos, TokenASin, TokenATan, TokenATan2,
|
||||
TokenLog, TokenLog10,
|
||||
TokenCeil, TokenFloor,
|
||||
TokenMod,
|
||||
TokenPi,
|
||||
TokenExp, TokenSqr,
|
||||
|
||||
// Variables
|
||||
TokenConst,
|
||||
TokenGlobal,
|
||||
TokenLocal,
|
||||
|
||||
// Including files.
|
||||
TokenInclude,
|
||||
};
|
||||
|
||||
public:
|
||||
Lexer();
|
||||
~Lexer();
|
||||
|
||||
std::pair<Token, std::string> GetCurrentToken();
|
||||
std::pair<Token, std::string> GetNextToken(std::shared_ptr<std::istream> fs);
|
||||
|
||||
private:
|
||||
blitz::Lexer::Token ConvertTextToToken(Token in, std::string text);
|
||||
|
||||
private:
|
||||
Token m_currentToken = Token::TokenUnknown;
|
||||
std::string m_currentText = "";
|
||||
Token m_overrideToken = Token::TokenUnknown;
|
||||
std::string m_overrideText = "";
|
||||
std::string text;
|
||||
enum class variant : uint64_t {
|
||||
UNKNOWN, // We have absolutely no fucking clue.
|
||||
CONTROL, // All kinds of control signals, like NewLine, EndOfFile, ...
|
||||
SYMBOL, // All kinds of symbols.
|
||||
TEXT, // HelloWorld
|
||||
NUMBER, // 1, 1%
|
||||
DECIMAL, // 1.0, 1#
|
||||
STRING, // "HelloWorld"
|
||||
COMMENT, // ; Whatever
|
||||
} type;
|
||||
};
|
||||
}
|
||||
|
||||
class lexer {
|
||||
std::filesystem::path _file;
|
||||
std::ifstream _stream;
|
||||
|
||||
// Current location in the file.
|
||||
uint64_t _line;
|
||||
uint64_t _character;
|
||||
|
||||
blitz::token _current;
|
||||
blitz::token _override;
|
||||
|
||||
public:
|
||||
~lexer();
|
||||
lexer(std::filesystem::path file);
|
||||
|
||||
/** Retrieve the current token information.
|
||||
*/
|
||||
blitz::token current();
|
||||
|
||||
/** Retrieve the next token in the given stream.
|
||||
*
|
||||
* This will replace the current token.
|
||||
*/
|
||||
blitz::token next();
|
||||
};
|
||||
} // namespace blitz
|
||||
|
||||
Reference in New Issue
Block a user