-
Notifications
You must be signed in to change notification settings - Fork 0
/
jack_tokenizer.h
110 lines (98 loc) · 1.96 KB
/
jack_tokenizer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef _JACK_TOKENIZER_H
#define _JACK_TOKENIZER_H
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include "type_utils.h"
using std::string;
using std::istringstream;
using std::vector;
using std::map;
using std::pair;
class JackTokenizer {
public:
/**
Opens the input file and gets ready
to parse it
*/
JackTokenizer(string jackcode);
/**
Are there more tokens in the input ?
*/
bool hasMoreTokens();
/**
Reads the next token from the input
and makes it the current token.
Should be called only if hasMoreTokens()
is true.
Initially there is no current token
*/
void advance();
/**
Reads the next token from the input
WITHOUT making it the current token.
Should be called only if hasMoreTokens()
is true.
*/
pair<TYPE_TOKEN, string> peek();
/**
Returns the type of the current
token
*/
TYPE_TOKEN tokenType();
/**
Only call this when token type == KEYWORD
*/
TYPE_KEYWORD keyword();
/**
Only call this when token type == SYMBOL
*/
char symbol();
/**
Only call this when token type == IDENTIFIER
*/
string identifier();
/**
Only call this when token type == INT_CONST
*/
int intVal();
/**
Only call this when token type == STRING_CONST
*/
string stringVal();
/* helper functions */
/**
get current line in the source text
*/
int getCurrentLine();
/**
get current column in the source text
*/
int getCurrentColumn();
private:
istringstream m_jackStreamCode;
string m_currentToken;
// keywords (defined in the constructor)
map<TYPE_KEYWORD, string> m_keywords;
vector<char> m_symbols;
// storing current pointer position
int m_curLine, m_curCol;
};
class TokenMismatch : public std::exception {
public:
TokenMismatch( string validToken )
{
std::ostringstream oss;
oss << "The current token is not a valid \"" << validToken << "\"";
this->msg = oss.str();
}
virtual ~TokenMismatch() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
#endif