A simple scripting language in C++
Ferenc Szontágh
2025-04-12 7d7a1e80c8a8c1e52446453d1b86d3c3b945ec29
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
#ifndef LEXER_HPP
#define LEXER_HPP
 
#include <istream>
#include <sstream>
#include <vector>
 
#include "VariableTypes.hpp"
#include "options.h"
#include "Token.hpp"
 
class Lexer {
  public:
    Lexer(const std::string & source, const std::string & filename);
    std::vector<Token> tokenize();
 
  private:
    const std::string & src;
    const std::string & filename;
    size_t              pos;
    int                 lineNumber = 1;
    size_t              colNumber  = 1;
    size_t              charNumber = 1;
 
    char peek() const;
    char advance();
    bool isAtEnd() const;
 
    Token string();
    Token number();
    Token identifier();
    Token variable();
    Token comment();
    Token keywordOrIdentifier();
    Token singleCharToken(TokenType type, const std::string & lexeme);
    bool matchSequence(const std::string & sequence) const;
    Token variableDeclaration(Variables::Type type);
    void matchAndConsume(const std::string & sequence);
 
 
    // validate number types from string
    template <typename Numeric> static bool is_number(const std::string & s) {
        Numeric n;
        return ((std::istringstream(s) >> n >> std::ws).eof());
    }
 
    bool matchSequence(const std::string & sequence) { return src.substr(pos, sequence.length()) == sequence; }
};
 
#endif  // LEXER_HPP