Newer
Older
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
#pragma once
// [config-parser.h]: configuration file parser
//
// -------------------------------------------------------------------------------------------- //
#include "cfg-value.h"
#include "cfg-cmd.h"
#include "cfg-vec.h"
#include "lexeme-list.h"
#include "mem-buffer.h"
#include <stack>
namespace scm
{
// call back default
// -------------------------------------------------------------------------------------------- //
class parserCallBack {
public:
virtual bool call_back(const cfgCommand& cmd) { return true; }
parserCallBack() {}
virtual ~parserCallBack() {}
};
// -------------------------------------------------------------------------------------------- //
class ConfigParser {
public:
// -------------------------------------------------------------------------------------------- //
ConfigParser();
~ConfigParser();
// -------------------------------------------------------------------------------------------- //
// run
// -------------------------------------------------------------------------------------------- //
bool run(const char* filename);
bool run(const char* filename, parserCallBack& pcb);
// -------------------------------------------------------------------------------------------- //
// get & check calls
// -------------------------------------------------------------------------------------------- //
bool is_varname(const char* name) const;
const cfgVector get_variable(const int idx) const;
const cfgVector get_variable(const char* name) const;
bool get_value(const char* name, int* value) const;
bool get_value(const char* name, float* value) const;
bool get_value(const char* name, double* value) const;
bool get_value(const char* name, long double* value) const;
bool get_value(const char* name, char** value) const;
bool get_value(const char* name, std::string& value) const;
bool get_value(const char* name, bool* value) const;
// -------------------------------------------------------------------------------------------- //
// adding variable
// -------------------------------------------------------------------------------------------- //
bool add(const cfgVector& rvalue);
bool is_valid_name(const char* name) const;
// -------------------------------------------------------------------------------------------- //
// print
// -------------------------------------------------------------------------------------------- //
void print() const;
// -------------------------------------------------------------------------------------------- //
private:
// datatypes
// -------------------------------------------------------------------------------------------- //
// --- helper struct to control parsing state
// -------------------------------------------------------------------------------------------- //
struct parserState {
parserState();
parserState(const parserState& state);
~parserState();
void truncate_name_space();
void append_name_space(const char* name);
const char* get_global_name(const char* varname);
int idx; // lexeme index
char *name_space; // current namespace
private: // allocation data
int nalloc;
static const int c_alloc_init = 64;
char *name_buf;
int name_buf_nalloc;
};
// -------------------------------------------------------------------------------------------- //
private:
// processing (private)
// -------------------------------------------------------------------------------------------- //
// --- expression rpn evaluation
bool get_rpn(
mem_buffer<int>& rpn,
mem_buffer<int>& rpn_key,
const LexemeList& lexeme_list,
const int lex_beg, const int lex_end) const;
bool evaluate_rpn(
std::stack<cfgVector>& dyn_expr,
const mem_buffer< int >& rpn,
const mem_buffer<int>& rpn_key,
const int rpn_beg, const int rpn_end,
const LexemeList& lexeme_list,
const char* name_space,
const bool exe_cntrl) const;
bool evaluate_rpn(
cfgVector& res,
const mem_buffer< int >& rpn,
const mem_buffer<int>& rpn_key,
const LexemeList& lexeme_list,
const char* name_space,
const bool exe_cntrl) const;
// -------------------------------------------------------------------------------------------- //
// --- command setup
bool set_command(
cfgCommand& cmd,
const mem_buffer< int >& rpn,
const mem_buffer< int >& fun_narg,
const LexemeList& lexeme_list,
const bool exe_cntrl) const;
// -------------------------------------------------------------------------------------------- //
// --- syntax analysis
bool parse_syntax(const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_block(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_command(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_if_statement(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_while_statement(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_name_space(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_assignment(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
bool parse_address_assignment(
parserState& state, const LexemeList& lexeme_list, const bool exe_cntrl, parserCallBack& pcb);
// -------------------------------------------------------------------------------------------- //
// --- add variable (no additional checks)
bool add_unsafe(const cfgVector& rvalue);
// -------------------------------------------------------------------------------------------- //
private:
// static (private)
// -------------------------------------------------------------------------------------------- //
// +,- [priority=1, assoc.=left]
// *,/,% [priority=2, assoc.=left]
// +,- [priority=3, assoc.=right]
// ^ [priority=4, assoc.=right]
static int operator_priority(const Lexeme::TYPE type);
static bool operator_priority_lt(
const Lexeme::TYPE typeA, const Lexeme::TYPE typeB);
static int function_nargs(const Lexeme::TYPE type);
static bool is_operator_binary(const Lexeme::TYPE type);
static bool is_operator_unary(const Lexeme::TYPE type);
static bool is_operator_assoc_left(const Lexeme::TYPE type);
static bool is_operator_assoc_right(const Lexeme::TYPE type);
private:
// data (private)
// -------------------------------------------------------------------------------------------- //
int nvars;
int nalloc_vars;
static const int c_alloc_vars_init = 64;
cfgVector *var;
};
// -------------------------------------------------------------------------------------------- //
}