-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompiler.h
235 lines (209 loc) · 5.19 KB
/
Compiler.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef __MDXtoHTML_COMPILER__
#define __MDXtoHTML_COMPILER__
#include <sstream>
#include "MDXparser.h"
#include "Utils.h"
#include "nodes.h"
class Compiler
{
TokensArray _tokens;
unsigned ptr;
unsigned current_string;
string cur_str()
{
stringstream ret;
ret<<current_string;
return ret.str();
}
unsigned current_token;
string cur_token()
{
stringstream ret;
ret<<current_token;
return ret.str();
}
/**
* Ñäâèãàåò óêàçàòåëü òåêóùåãî òîêåíà âïðàâî íà åäèíèöó è ñ÷èòàåò ïîçèöèþ òåêóùåãî òîêåíà â ôàéëå.
*/
void nextToken()
{
if ( _tokens[ptr].type() == ttNewline )
{
current_string++;
current_token = 1;
}
else
{
current_token++;
}
++ptr;
}
node_document* root;
/**
* Íàáîð ôóíêöèé îïèñûâàþùèõ ãðàììàòèêó è ñòîÿùèõ äåðåâî ðàçáîðà
* document -> contains
* contains -> part '---' contains | part
* part -> block part | block
* block -> text
* | header
* | define
* | theorem
* | example
* | list
*/
node_document* Cdocument()
{
return new node_document( Ccontains() );
}
node_contains* Ccontains()
{
node_part* npart = Cpart();
Token line;
node_contains* nextcontains = 0;
// Åñëè ýòî íå ïîñëåäíèé ðàçäåë, òî çàïîëíÿåì ñëåäóþùèå íîäû
if ( _tokens[ptr].type() == ttLine )
{
line = _tokens[ptr];
nextToken(); nextToken();
nextcontains = Ccontains();
}
return new node_contains(npart, line, nextcontains);
}
node_part* Cpart()
{
node_block* block = Cblock();
node_part* nextpart = 0;
// Åñëè íå áûëî îøèáîê ñáîðêè áëîêà, òî ïðîáóåì ñ÷èòàòü åùå îäíó ÷àñòü
if ( block )
{
nextpart = Cpart();
return new node_part(block, nextpart);
}
else
return 0;
}
node_block* Cblock()
{
TokensArray ta;
string stype;
if ( _tokens[ptr].type() == ttString && _tokens[ptr+1].type() == ttNewline )
{
ta.push_back( _tokens[ptr] );
// Ïðîøëè ñàìó ñòðî÷êó è ïåðåâîä ñòðîêè ïîñëå íå¸.
nextToken(); nextToken();
stype = "string";
}
else if ( _tokens[ptr].type() == ttHeader )
{
// Ïðîâåðÿåì, ÷òî ïîñëå îáúÿâëåíèÿ çàãîëîâêà åñòü åùå ïðîáåë è ñòðîêà
if ( _tokens.size() < ptr + 2 )
MACRO_ERROR_RET("Compiler::After header announcement not found string. String: " + cur_str() + ", token: " + cur_token(), 0);
// Êëàäåì äèåçû
ta.push_back(_tokens[ptr]);
// Çàáèðàåì äèåçû è ïðîáåë
nextToken(); nextToken();
// Êëàäåì ñòðîêó ñ ïåðåâîäîì ñòðîêè.
ta.push_back(_tokens[ptr]);
nextToken(); nextToken();
if ( ta.front().text().length() == 1 )
{
stype = "header1";
}
else if ( ta.front().text().length() == 2 )
{
stype = "header2";
}
else if ( ta.front().text().length() == 3 )
{
stype = "header3";
}
else if ( ta.front().text().length() == 4 )
{
stype = "header4";
}
else
MACRO_ERROR_RET("Compiler::Bad count of '#' in header block. String: " + cur_str() + ", token: " + cur_token(), 0);
}
else if ( _tokens[ptr].type() == ttDefine )
{
stype = "Define";
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
unsigned sep = _tokens[ptr].text().find('-');
if ( sep < 0 )
MACRO_ERROR_RET("Compiler:: void of '-' in Def: clause. String: " + cur_str() + ", token: " + cur_token(), 0);
string def = _tokens[ptr].text().substr(0, sep );
string ine = _tokens[ptr].text().substr(sep + 1, _tokens[ptr].text().length() - sep );
ta.push_back( Token(ttString, def) );
ta.push_back( Token(ttString, ine) );
nextToken(); nextToken();
}
else if ( _tokens[ptr].type() == ttTheorem )
{
stype = "Theorem";
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
// Êëàäåì îïðåäåëåíèå òåîðåìû
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
// Åñëè åñòü äîêàçàòåëüñòâî (åùå 4 òîêåíà: Proof:, ñàìî äîêàçàòåëüñòâî è äâà ðàçäåëèòåëÿ), òî è åãî çàñîâûâàåì
// ToDo: ñäåëàòü äîêàçàòåëüñòâî áëîêîì ñ îòñòóïîì.
if ( _tokens[ptr].type() == ttProof && _tokens.size() > ptr + 2 )
{
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
}
}
else if ( _tokens[ptr].type() == ttNote )
{
stype = "Note";
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
}
else if ( _tokens[ptr].type() == ttComment )
{
stype = "Comment";
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
}
else if ( _tokens[ptr].type() == ttExample )
{
stype = "Example";
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
ta.push_back( _tokens[ptr] );
nextToken(); nextToken();
}
else
MACRO_ERROR_RET("Compiler::Unknown type of node_block '" + _tokens[ptr].text() + "'. String: " + cur_str() + ", token: " + cur_token(), 0);
return new node_block( stype, ta );
}
public:
void load_tokens( TokensArray tokens )
{
_tokens = tokens;
ptr = 0;
current_string = 1;
current_token = 1;
}
void compile()
{
root = Cdocument();
}
void print()
{
cout<<*root;
}
friend inline ostream& operator<<( ostream &out, const Compiler& compiler );
};
inline ostream& operator<<( ostream &out, const Compiler& compiler )
{
return out<<*compiler.root;
}
#endif