-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLOutputEngine.cpp
65 lines (56 loc) · 2.04 KB
/
XMLOutputEngine.cpp
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
#include "XMLOutputEngine.h"
#include "OutputEngine.h"
#include "enums.h"
#include "error.h"
#include "hack_map.h"
#include <memory>
XMLOutputEngine::XMLOutputEngine (const std::string& out_file_name,
std::shared_ptr<HackMap> hack_map,
int tab_width)
: OutputEngine (out_file_name, hack_map), spaces (0), tab_width (tab_width) {
}
void XMLOutputEngine::write_spaces () {
int temp = spaces;
while ((temp--) != 0) {
out_file << " ";
}
}
void XMLOutputEngine::write_terminal (Token token) {
write_spaces ();
const std::string& tag = hack_map->get_token (token.type);
out_file << "<" << tag << "> " << token.value << " </" << tag << ">\n";
}
void XMLOutputEngine::write_non_terminal (const NonTerminalRules& tag) {
tags.push (tag);
write_spaces ();
out_file << "<" << non_terminals[tag] << ">\n";
spaces += tab_width;
}
void XMLOutputEngine::close_non_terminal () {
if (tags.empty ()) {
throw Error ("Invalid Tags matching");
}
auto tag = tags.top ();
tags.pop ();
spaces -= tab_width;
write_spaces ();
out_file << "</" << non_terminals[tag] << ">\n";
}
std::map<NonTerminalRules, std::string> XMLOutputEngine::non_terminals = {
{ NonTerminalRules::CLASS, "class" },
{ NonTerminalRules::CLASS_VAR_DEC, "ClassVarDec" },
{ NonTerminalRules::SUBROUTINE_DEC, "SubroutineDec" },
{ NonTerminalRules::SUBROUTINE_BODY, "SubroutineBody" },
{ NonTerminalRules::VAR_DEC, "VarDec" },
{ NonTerminalRules::VAR_DEC, "Statements" },
{ NonTerminalRules::IF_STATEMENT, "IfStatement" },
{ NonTerminalRules::LET_STATEMENT, "LetStatement" },
{ NonTerminalRules::STATEMENTS, "Statements" },
{ NonTerminalRules::WHILE_STATEMENT, "WhileStatement" },
{ NonTerminalRules::RETURN_STATEMENT, "ReturnStatement" },
{ NonTerminalRules::DO_STATEMENT, "DoStatement" },
{ NonTerminalRules::PARAMETER_LIST, "ParameterLilst" },
{ NonTerminalRules::EXPRESSION_LIST, "ExpressionList" },
{ NonTerminalRules::EXPRESSION, "Expression" },
{ NonTerminalRules::TERM, "Term" },
};