sapphirec
The Sapphire compiler
Lexer.h
1 #pragma once
2 #include "Token.h"
3 #include "sapphirepch.h"
4 #include <array>
5 
6 /* Let's redefine TokenStream, as if it wasn't already defined in Token.h*/
7 typedef std::vector<Token> TokenStream;
8 /* The number of data types currently in Sapphire. */
9 #define TYPE_COUNT 14
10 
11 /* The Lexer for the Sapphire compiler. A Lexer reads source code from a file, and turns it into a stream of tokens the
12  * compiler can understand. */
13 class Lexer
14 {
15  private:
16  Location loc;
17  Location prev_loc;
18 
19  int advance();
20  int rewind();
21  char current_char;
22  int index;
23 
24  Lexer(const std::string& fname);
25 
26  std::string current_line_text;
27  std::string previous_line_text;
28 
29  std::string current_lexed_text;
30 
31  std::string recalculate_current_line(const std::string& text);
32 
33  Token create_string();
34  Token create_number();
35  Token create_identifier();
36 
37  bool is_in_string(const std::string& string, const char& character);
38 
39  public:
40  /* An array containing Sapphire's current data types. */
41  static const std::array<std::string, TYPE_COUNT> types;
42 
43  ~Lexer();
44 
45  /* Lex the given text, turning it into a stream of tokens. */
46  TokenStream lex(const std::string& text);
47 
48  /* Create a new Lexer and return a pointer to it. */
49  static std::unique_ptr<Lexer> make_lexer(const std::string& fname);
50 
51  /* If the Lexer is lexing an impòrted file, give it the location in the parent file at which it was imported. */
52  static void assign_parent_location(std::unique_ptr<Lexer>& lexer, const std::shared_ptr<Location>& loc);
53 };
Definition: Lexer.h:14
Definition: Location.h:6
Definition: Token.h:54