-
Notifications
You must be signed in to change notification settings - Fork 5
/
lexer.h
68 lines (57 loc) · 1.97 KB
/
lexer.h
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
#pragma once
#include <iostream>
#include <string>
#include <memory>
struct Location {
unsigned int line, column;
};
std::ostream &operator<<(std::ostream& out, Location loc);
class Token {
public:
enum Kind {
BOOLLITERAL, INTLITERAL, FLOATLITERAL, STRLITERAL, CHARLITERAL, NIL,
IDENTIFIER, RELATION, OPERATOR,
MODULE, IMPORT, BEGIN, END, EXTERN, PROCEDURE, EXIT, RETURN, VAR, CONST, TYPE,
ARRAY, RECORD, POINTER, OF, TO,
IF, THEN, ELSIF, ELSE, CASE, WITH,
REPEAT, UNTIL, WHILE, DO, FOR, BY, LOOP,
DOT, RANGE, COMMA, COLON, SEMICOLON, ASSIGNMENT, PIPE, CARET, TILDE,
LPAREN, RPAREN, LCURLY, RCURLY, LSQUARE, RSQUARE,
END_OF_FILE, OTHER
};
Token() : kind(END_OF_FILE), loc{0, 0}, text("") {}
Token(Kind kind, Location loc) : kind(kind), loc(loc), text("") {}
Token(Kind kind, Location loc, char charval) : kind(kind), loc(loc), text(""), charval(charval) {}
Token(Kind kind, Location loc, int intval) : kind(kind), loc(loc), text(""), intval(intval) {}
Token(Kind kind, Location loc, double floatval) : kind(kind), loc(loc), text(""), floatval(floatval) {}
Token(Kind kind, Location loc, std::string text);
Kind getKind() const { return kind; }
Location getLocation() const { return loc; }
std::string getText() const { return text; }
char getCharVal() const { return charval; }
int getIntVal() const { return intval; }
double getFloatVal() const { return floatval; }
bool getBoolVal() const { return boolval; }
private:
Kind kind;
Location loc;
std::string text;
char charval;
int intval;
double floatval;
bool boolval;
};
std::string kind_to_string(Token::Kind kind);
class Lexer {
public:
Lexer(std::istream *in)
: in(in), loc{0, 0} {
nextToken();
}
std::shared_ptr<Token> nextToken(void);
private:
void take(void);
std::istream *in;
Location loc;
char lastChar;
};