Skip to content

Commit 787693a

Browse files
committed
Start refactoring the YAML printer
I was never really happy with the sqltoaster::printer_t struct and the use of iomanips to control indentation levels and list construction. So, I'm going to rewrite the YAML printing to instead use a stack push/pop system of generic YAML node elements. The first step to this is to get rid of storing the output format in an iomanip xalloc bucket, which was just sill to begin with. Issue #104
1 parent f53913d commit 787693a

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

sqltoaster/main.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int main (int argc, char *argv[])
6363
auto dur = measure<std::chrono::nanoseconds>::execution(p);
6464
sqltoaster::printer ptr(p.res, std::cout);
6565
if (use_yaml)
66-
ptr.set_yaml(std::cout);
66+
ptr.output_format = sqltoaster::OUTPUT_FORMAT_YAML;
6767
if (p.res.code == sqltoast::PARSE_OK)
6868
std::cout << ptr << std::endl;
6969
else if (p.res.code == sqltoast::PARSE_INPUT_ERROR)

sqltoaster/printer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace sqltoaster {
1111

1212
std::ostream& operator<< (std::ostream& out, printer_t& ptr) {
13-
if (! ptr.use_yaml(out)) {
13+
if (ptr.output_format == OUTPUT_FORMAT_DEFAULT) {
1414
unsigned int x = 0;
1515
for (auto stmt_ptr_it = ptr.res.statements.cbegin();
1616
stmt_ptr_it != ptr.res.statements.cend();

sqltoaster/printer.h

+14-16
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,35 @@
1414

1515
namespace sqltoaster {
1616

17+
typedef enum output_format {
18+
OUTPUT_FORMAT_DEFAULT,
19+
OUTPUT_FORMAT_YAML
20+
} output_format_t;
21+
1722
const long LIST_ITEM_OFF = 0;
1823
const long LIST_ITEM_ON = 1;
1924

20-
const long OUTPUT_FORMAT_DEFAULT = 1;
21-
const long OUTPUT_FORMAT_YAML = 2;
22-
23-
const int OUTPUT_FORMAT_XALLOC_INDEX = 0;
24-
const int INDENT_LEVEL_XALLOC_INDEX = 1;
25+
const int INDENT_LEVEL_XALLOC_INDEX = 0;
2526
// Informs the printer whether the current item to be printed is a list item
26-
const int LIST_ITEM_XALLOC_INDEX = 2;
27+
const int LIST_ITEM_XALLOC_INDEX = 1;
2728

2829
typedef struct printer {
29-
int iomanip_indexes[3];
30+
int iomanip_indexes[2];
3031
sqltoast::parse_result_t& res;
31-
printer(sqltoast::parse_result_t& res, std::ostream& out) : res(res)
32+
output_format_t output_format;
33+
printer(
34+
sqltoast::parse_result_t& res,
35+
std::ostream& out) :
36+
res(res),
37+
output_format(OUTPUT_FORMAT_DEFAULT)
3238
{
33-
iomanip_indexes[OUTPUT_FORMAT_XALLOC_INDEX] = std::ios_base::xalloc();
3439
iomanip_indexes[INDENT_LEVEL_XALLOC_INDEX] = std::ios_base::xalloc();
3540
iomanip_indexes[LIST_ITEM_XALLOC_INDEX] = std::ios_base::xalloc();
3641
int idx_indent = iomanip_indexes[INDENT_LEVEL_XALLOC_INDEX];
3742
out.iword(idx_indent) = 0;
3843
int idx_list_item = iomanip_indexes[LIST_ITEM_XALLOC_INDEX];
3944
out.iword(idx_list_item) = LIST_ITEM_OFF;
4045
}
41-
inline void set_yaml(std::ostream& out) {
42-
int idx_format = iomanip_indexes[OUTPUT_FORMAT_XALLOC_INDEX];
43-
out.iword(idx_format) = OUTPUT_FORMAT_YAML;
44-
}
45-
inline bool use_yaml(std::ostream& out) const {
46-
return (out.iword(iomanip_indexes[OUTPUT_FORMAT_XALLOC_INDEX]) == OUTPUT_FORMAT_YAML);
47-
}
4846
inline void indent_push(std::ostream& out) {
4947
int idx_indent = iomanip_indexes[INDENT_LEVEL_XALLOC_INDEX];
5048
long cur_indent = out.iword(idx_indent);

0 commit comments

Comments
 (0)