2022-03-11 16:00:09 +00:00
|
|
|
#pragma once
|
|
|
|
#include "Token.h"
|
2022-06-14 14:29:51 +00:00
|
|
|
#include <array>
|
2022-03-11 16:00:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2022-06-14 14:29:51 +00:00
|
|
|
#include <vector>
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-08 18:05:28 +00:00
|
|
|
/* Let's redefine TokenStream, as if it wasn't already defined in Token.h*/
|
2022-03-11 16:00:09 +00:00
|
|
|
typedef std::vector<Token> TokenStream;
|
2022-06-08 18:05:28 +00:00
|
|
|
/* The number of data types currently in Sapphire. */
|
2022-06-02 16:25:01 +00:00
|
|
|
#define TYPE_COUNT 14
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
/* 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. */
|
2022-03-11 16:00:09 +00:00
|
|
|
class Lexer
|
|
|
|
{
|
2022-06-14 14:29:51 +00:00
|
|
|
private:
|
|
|
|
Location loc;
|
|
|
|
Location prev_loc;
|
|
|
|
|
|
|
|
int advance();
|
|
|
|
int rewind();
|
|
|
|
char current_char;
|
|
|
|
int index;
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
Lexer(const std::string& fname);
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
std::string current_line_text;
|
|
|
|
std::string previous_line_text;
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
std::string current_lexed_text;
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
std::string recalculate_current_line(const std::string& text);
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
Token create_string();
|
|
|
|
Token create_number();
|
|
|
|
Token create_identifier();
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
bool is_in_string(const std::string& string, const char& character);
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
public:
|
|
|
|
/* An array containing Sapphire's current data types. */
|
|
|
|
static const std::array<std::string, TYPE_COUNT> types;
|
2022-06-08 18:06:56 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
~Lexer();
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
/* Lex the given text, turning it into a stream of tokens. */
|
|
|
|
TokenStream lex(const std::string& text);
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
/* Create a new Lexer and return a pointer to it. */
|
|
|
|
static std::shared_ptr<Lexer> make_lexer(const std::string& fname);
|
2022-03-11 16:00:09 +00:00
|
|
|
|
2022-06-14 14:29:51 +00:00
|
|
|
/* 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::shared_ptr<Lexer>& lexer, const std::shared_ptr<Location>& loc);
|
2022-03-11 16:00:09 +00:00
|
|
|
};
|