sapphire/src/Lexer.h
apio e4fd6c863a Updated Lexer::make_lexer to return a unique_ptr
This is part of my quest to replace all unneeded std::shared_ptrs for
std::unique_ptrs to improve performance, since there is no need for
reference counting for pointers that are uniquely used. I also had to
change the first parameter of Lexer::assign_parent_location to a
std::unique_ptr<Lexer>& so that it can accept the new result of
make_lexer.
2022-06-22 18:12:50 +02:00

54 lines
1.5 KiB
C++

#pragma once
#include "Token.h"
#include "sapphirepch.h"
#include <array>
/* Let's redefine TokenStream, as if it wasn't already defined in Token.h*/
typedef std::vector<Token> TokenStream;
/* The number of data types currently in Sapphire. */
#define TYPE_COUNT 14
/* The Lexer for the Sapphire compiler. A Lexer reads source code from a file, and turns it into a stream of tokens the
* compiler can understand. */
class Lexer
{
private:
Location loc;
Location prev_loc;
int advance();
int rewind();
char current_char;
int index;
Lexer(const std::string& fname);
std::string current_line_text;
std::string previous_line_text;
std::string current_lexed_text;
std::string recalculate_current_line(const std::string& text);
Token create_string();
Token create_number();
Token create_identifier();
bool is_in_string(const std::string& string, const char& character);
public:
/* An array containing Sapphire's current data types. */
static const std::array<std::string, TYPE_COUNT> types;
~Lexer();
/* Lex the given text, turning it into a stream of tokens. */
TokenStream lex(const std::string& text);
/* Create a new Lexer and return a pointer to it. */
static std::unique_ptr<Lexer> make_lexer(const std::string& fname);
/* If the Lexer is lexing an impòrted file, give it the location in the parent file at which it was imported. */
static void assign_parent_location(std::unique_ptr<Lexer>& lexer, const std::shared_ptr<Location>& loc);
};