1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#ifndef OPENMW_COMPONENTS_FX_LEXER_H
#define OPENMW_COMPONENTS_FX_LEXER_H
#include <cstddef>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include "lexer_types.hpp"
namespace fx
{
namespace Lexer
{
struct LexerException : std::runtime_error
{
LexerException(const std::string& message)
: std::runtime_error(message)
{
}
LexerException(const char* message)
: std::runtime_error(message)
{
}
};
class Lexer
{
public:
struct Block
{
std::size_t line;
std::string_view content;
};
Lexer(std::string_view buffer);
Lexer() = delete;
Token next();
Token peek();
// Jump ahead to next uncommented closing bracket at level zero. Assumes the head is at an opening bracket.
// Returns the contents of the block excluding the brackets and places cursor at closing bracket.
std::optional<std::string_view> jump();
Block getLastJumpBlock() const;
[[noreturn]] void error(const std::string& msg);
private:
void drop();
void advance();
char head();
bool peekChar(char c);
Token scanToken();
Token scanLiteral();
Token scanStringLiteral();
Token scanNumber();
const char* mHead;
const char* mTail;
std::size_t mAbsolutePos;
std::size_t mColumn;
std::size_t mLine;
std::string_view mBuffer;
Token mLastToken;
std::optional<Token> mLookahead;
Block mLastJumpBlock;
};
}
}
#endif
|