summaryrefslogtreecommitdiff
path: root/parser.cpp
blob: 73ae368756571a85779ea34ec2645db3195a3d9a (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdexcept>
#include <cstring>
#include <cctype>
#include <cassert>
#include "parser.h"

using namespace std;


ParseError::ParseError(const string &what_arg)
	:runtime_error(what_arg){}
ParseError::ParseError(const char *what_arg)
	:runtime_error(what_arg){}


static bool isinitwordchar(char c){
	return isalpha(c)||c=='_';
}

static bool iswordchar(char c){
	return isalpha(c)||isdigit(c)||c=='_';
}

static const vector<string> tok_symbols={
	"==", "!=", ">", "<", ">=", "<=",
	":=", "=",
	"+", "-", "*", "/", "%",
	"(", ")", ",",
	"{", "}", "?{", "??{", "}&",
};

static bool isSymbolPrefix(const string &s){
	for(const string &sym : tok_symbols){
		if(s.size()<=sym.size()&&sym.substr(0,s.size())==s)return true;
	}
	return false;
}

template <typename T>
static bool contains(const vector<T> &v,const T &target){
	for(const T &t : v){
		if(t==target)return true;
	}
	return false;
}


class Token{
public:
	enum class Type{
		word,
		number,
		string,
		symbol,
	};

	Type type;
	string str;
	Site site;

	Token(Type type,const string &str,const Site &site)
		:type(type),str(str),site(site){}
};


class Tokeniser{
	const string &source;
	const string &filename;
	i64 idx,nextidx;
	i64 lnum,linex;
	Token::Type ttype;

	bool eof(i64 at){
		return at>=(i64)source.size();
	}

	string get_() const {
		if(eof())throw runtime_error("Tokeniser::get() on eof");
		if(nextidx==-1)throw runtime_error("Tokeniser::get() before advance");
		if(nextidx==-2)throw runtime_error("Tokeniser::get() after eof");
		assert(nextidx>=0);
		return source.substr(idx,nextidx-idx);
	}

public:
	Tokeniser(const string &source,const string &filename)
		:source(source),filename(filename),
		 idx(0),nextidx(-1),
		 lnum(1),linex(1){}

	bool eof() const {
		return idx>=(i64)source.size();
	}

	Token get() const {
		return Token(ttype,get_(),Site(filename,lnum,linex));
	}

	// Returns whether there are more tokens
	bool advance(){
		if(eof())return false;

		while(idx<nextidx){
			if(source[idx]=='\n'){
				lnum++;
				linex=1;
			} else {
				linex++;
			}
			idx++;
		}

		while(true){
			i64 origidx=idx;
			while(!eof()&&isspace(source[idx])){
				if(source[idx]=='\n'){
					lnum++;
					linex=1;
				} else {
					linex++;
				}
				idx++;
			}
			if(eof())return false;

			if(source[idx]=='#'){
				while(!eof()&&source[idx]!='\n')idx++;
				idx++;
				lnum++;
				linex=1;
				if(eof())return false;
			}

			if(idx==origidx)break;
		}

		nextidx=idx;

		// Word
		if(isinitwordchar(source[nextidx])){
			ttype=Token::Type::word;
			do nextidx++;
			while(!eof(nextidx)&&iswordchar(source[nextidx]));
			return true;
		}

		// Number literal
		if(isdigit(source[nextidx])||(!eof(nextidx+1)&&source[nextidx]=='-'&&isdigit(source[nextidx+1]))){
			ttype=Token::Type::number;
			if(source[nextidx]=='-')nextidx++;
			while(!eof(nextidx)&&isdigit(source[nextidx]))nextidx++;
			if(eof(nextidx))return true;
			if(source[nextidx]=='.'){
				nextidx++;
				if(eof(nextidx)||!isdigit(source[nextidx])){
					throw ParseError("Incomplete floating point literal at EOF");
				}
				while(!eof(nextidx)&&isdigit(source[nextidx]))nextidx++;
				if(eof(nextidx))return true;
			}
			if(strchr("eE",source[nextidx])!=NULL){
				nextidx++;
				if(eof(nextidx)||strchr("+-0123456789",source[nextidx])==NULL){
					throw ParseError("Incomplete floating point literal at EOF");
				}
				if(strchr("+-",source[nextidx])!=NULL){
					nextidx++;
					if(eof(nextidx))throw ParseError("Incomplete floating point literal at EOF");
				}
				while(!eof(nextidx)&&isdigit(source[nextidx]))nextidx++;
			}
			return true;
		}

		// String literal
		if(source[nextidx]=='"'){
			ttype=Token::Type::string;
			nextidx++;
			while(!eof(nextidx)&&source[nextidx]!='"'){
				if(source[nextidx]=='\\')nextidx++;
			}
			if(eof(nextidx))throw ParseError("Incomplete string literal at EOF");
			nextidx++;
			return true;
		}

		// Symbol
		if(isSymbolPrefix({source[idx]})){
			ttype=Token::Type::symbol;
			nextidx++;
			while(!eof(nextidx)){
				if(!isSymbolPrefix(get_()))return true;
				nextidx++;
			}
			if(contains(tok_symbols,get_()))return true;
			else throw ParseError("Unknown symbol at EOF");
		}

		throw ParseError("Unknown token starting at '"+source.substr(idx,5)+"'");
	}
};

StatementList parse(const string &source,const string &filename){
	Tokeniser tokeniser(source,filename);
	StatementList stl;
	while(tokeniser.advance()){
		Token tok=tokeniser.get();
		
	}
	return stl;
}