summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lex.cpp22
-rw-r--r--src/lex.hpp4
-rw-r--r--src/parser.cpp70
-rw-r--r--src/parser.hpp2
-rw-r--r--src/parser_state.cpp18
-rw-r--r--src/parser_state.hpp26
-rw-r--r--src/symbol.hpp6
7 files changed, 70 insertions, 78 deletions
diff --git a/src/lex.cpp b/src/lex.cpp
index 38d138c..63549c9 100644
--- a/src/lex.cpp
+++ b/src/lex.cpp
@@ -234,10 +234,10 @@ int lex_t::lex_keyword(parser_value_t *value) {
break;
}
- char *token = get_token();
- const keyword_t key = { .keyword = token, .token = 0 };
+ std::string *token = get_token();
+ const keyword_t key = { .keyword = token->c_str(), .token = 0 };
const keyword_t *ret = static_cast<const keyword_t*>(bsearch(&key, keywords, array_size(keywords), sizeof(keyword_t), compare_keywords));
- free(token);
+ delete token;
if (!ret)
return syntax_error(value);
@@ -262,15 +262,13 @@ int lex_t::lex_block(parser_value_t *value) {
bool str = false;
bool pp = false;
- size_t pos = 0;
- size_t len = 1024;
- char *buf = static_cast<char*>(std::malloc(len));
+ std::string *buf = new std::string;
char prev = 0;
while (true) {
if (!next(true)) {
- std::free(buf);
+ delete buf;
return unterminated_block(value);
}
@@ -316,17 +314,11 @@ int lex_t::lex_block(parser_value_t *value) {
}
}
- if (pos >= len) {
- len *= 2;
- buf = static_cast<char*>(std::realloc(buf, len));
- }
-
- buf[pos++] = cur;
+ *buf += cur;
prev = cur;
}
- value->str = strndup(buf, pos);
- std::free(buf);
+ value->str = buf;
next(true);
consume(true);
diff --git a/src/lex.hpp b/src/lex.hpp
index 8f7e038..7405279 100644
--- a/src/lex.hpp
+++ b/src/lex.hpp
@@ -78,8 +78,8 @@ private:
return buffer[start + tok_len];
}
- char * get_token() {
- return strndup(buffer+start, tok_len);
+ std::string * get_token() {
+ return new std::string(buffer+start, tok_len);
}
diff --git a/src/parser.cpp b/src/parser.cpp
index 0333243..ce408eb 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -66,8 +66,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
switch (token) {
case TOK_SYMBOL_LC:
parser->state = STATE_RULE_BAR;
- state->new_rule(value->str);
- free(value->str);
+ state->new_rule(*value->str);
+ delete value->str;
return 1;
case TOK_TYPE:
@@ -111,13 +111,13 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_RULE:
switch (token) {
case TOK_SYMBOL_LC:
- state->add_rule_nonterminal(value->str);
- free(value->str);
+ state->add_rule_nonterminal(*value->str);
+ delete value->str;
return 1;
case TOK_SYMBOL_UC:
- state->add_rule_terminal(value->str);
- free(value->str);
+ state->add_rule_terminal(*value->str);
+ delete value->str;
return 1;
case TOK_CHAR:
@@ -125,8 +125,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
return 1;
case TOK_BLOCK:
- state->add_rule(value->str);
- free(value->str);
+ state->add_rule(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
@@ -147,8 +147,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case TOK_SYMBOL:
case TOK_SYMBOL_UC:
case TOK_SYMBOL_LC:
- state->add_rule_var(value->str);
- free(value->str);
+ state->add_rule_var(*value->str);
+ delete value->str;
parser->state = STATE_RULE_VAR;
return 1;
}
@@ -166,13 +166,13 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_RULE_VAR_POST:
switch (token) {
case TOK_SYMBOL_LC:
- state->add_rule_nonterminal(value->str);
- free(value->str);
+ state->add_rule_nonterminal(*value->str);
+ delete value->str;
return 1;
case TOK_SYMBOL_UC:
- state->add_rule_terminal(value->str);
- free(value->str);
+ state->add_rule_terminal(*value->str);
+ delete value->str;
return 1;
case TOK_CHAR:
@@ -180,8 +180,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
return 1;
case TOK_BLOCK:
- state->add_rule(value->str);
- free(value->str);
+ state->add_rule(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
@@ -196,14 +196,14 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_TYPE:
switch (token) {
case TOK_SYMBOL_LC:
- state->add_type_nonterminal(value->str);
- free(value->str);
+ state->add_type_nonterminal(*value->str);
+ delete value->str;
parser->state = STATE_TYPE_NONTERM;
return 1;
case TOK_SYMBOL_UC:
- state->add_type_terminal(value->str);
- free(value->str);
+ state->add_type_terminal(*value->str);
+ delete value->str;
parser->state = STATE_TYPE_TERM;
return 1;
}
@@ -212,8 +212,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_TYPE_NONTERM:
if (token == TOK_BLOCK) {
- state->set_type_nonterminal(value->str);
- free(value->str);
+ state->set_type_nonterminal(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
}
@@ -222,8 +222,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_TYPE_TERM:
if (token == TOK_BLOCK) {
- state->set_type_terminal(value->str);
- free(value->str);
+ state->set_type_terminal(*value->str);
+ delete value->str;
parser->state = STATE_TYPE_TERM_BLOCK;
return 1;
}
@@ -235,8 +235,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case TOK_SYMBOL:
case TOK_SYMBOL_UC:
case TOK_SYMBOL_LC:
- state->set_type_terminal_name(value->str);
- free(value->str);
+ state->set_type_terminal_name(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
}
@@ -244,8 +244,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_HEADER:
if (token == TOK_BLOCK) {
- state->set_header_block(value->str);
- free(value->str);
+ state->set_header_block(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
}
@@ -254,8 +254,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_SOURCE:
if (token == TOK_BLOCK) {
- state->set_source_block(value->str);
- free(value->str);
+ state->set_source_block(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
}
@@ -264,8 +264,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case STATE_EXTRA_ARG:
if (token == TOK_BLOCK) {
- state->add_extra_arg(value->str);
- free(value->str);
+ state->add_extra_arg(*value->str);
+ delete value->str;
parser->state = STATE_EXTRA_ARG_NAME;
return 1;
}
@@ -276,8 +276,8 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case TOK_SYMBOL:
case TOK_SYMBOL_UC:
case TOK_SYMBOL_LC:
- state->set_extra_arg_name(value->str);
- free(value->str);
+ state->set_extra_arg_name(*value->str);
+ delete value->str;
parser->state = STATE_INIT;
return 1;
}
@@ -289,7 +289,7 @@ int parser_push(parser_t *parser, int token, const parser_value_t *value, parser
case TOK_SYMBOL_LC:
case TOK_CHAR:
case TOK_BLOCK:
- free(value->str);;
+ delete value->str;;
}
return -1;
diff --git a/src/parser.hpp b/src/parser.hpp
index c5dd139..cc139fa 100644
--- a/src/parser.hpp
+++ b/src/parser.hpp
@@ -46,7 +46,7 @@ enum parser_token_t {
};
typedef struct parser_value {
- char *str;
+ std::string *str;
uint64_t number;
const char *error;
} parser_value_t;
diff --git a/src/parser_state.cpp b/src/parser_state.cpp
index f0e9bdd..7ecffd9 100644
--- a/src/parser_state.cpp
+++ b/src/parser_state.cpp
@@ -29,7 +29,7 @@
namespace solar {
-void parser_state_t::new_rule(const char *nonterm) {
+void parser_state_t::new_rule(const std::string &nonterm) {
if (grammar.rules.empty()) {
// start rule
add_rule_nonterminal(nonterm);
@@ -40,12 +40,12 @@ void parser_state_t::new_rule(const char *nonterm) {
current_vars = std::vector<std::string>();
}
-void parser_state_t::add_rule_nonterminal(const char *nonterm) {
+void parser_state_t::add_rule_nonterminal(const std::string &nonterm) {
current.get_rhs().emplace_back(symbol_t::make_nonterm(nonterm));
current_vars.emplace_back();
}
-void parser_state_t::add_rule_terminal(const char *term) {
+void parser_state_t::add_rule_terminal(const std::string &term) {
current.get_rhs().emplace_back(symbol_t::make_term(term));
current_vars.emplace_back();
}
@@ -59,27 +59,27 @@ void parser_state_t::add_rule(const std::string &action) {
grammar.rules.emplace_back(rule_t{std::move(current), std::move(current_vars), action});
}
-void parser_state_t::add_rule_var(const char *var) {
+void parser_state_t::add_rule_var(const std::string &var) {
current_vars.back() = var;
}
-void parser_state_t::add_type_nonterminal(const char *nonterm) {
+void parser_state_t::add_type_nonterminal(const std::string &nonterm) {
current_var = nonterm;
}
-void parser_state_t::add_type_terminal(const char *term) {
+void parser_state_t::add_type_terminal(const std::string &term) {
current_var = term;
}
-void parser_state_t::set_type_nonterminal(const char *type) {
+void parser_state_t::set_type_nonterminal(const std::string &type) {
grammar.nonterm_types.insert(std::make_pair(current_var, type));
}
-void parser_state_t::set_type_terminal(const char *type) {
+void parser_state_t::set_type_terminal(const std::string &type) {
current_type = type;
}
-void parser_state_t::set_type_terminal_name(const char *name) {
+void parser_state_t::set_type_terminal_name(const std::string &name) {
grammar.term_types.insert(std::make_pair(symbol_t::make_term(current_var.c_str()), std::make_pair(current_type, name)));
}
diff --git a/src/parser_state.hpp b/src/parser_state.hpp
index 4fd057d..c1bbdda 100644
--- a/src/parser_state.hpp
+++ b/src/parser_state.hpp
@@ -49,35 +49,35 @@ public:
return grammar;
}
- void set_header_block(const char *value) {
+ void set_header_block(const std::string &value) {
grammar.header_block = value;
}
- void set_source_block(const char *value) {
+ void set_source_block(const std::string &value) {
grammar.source_block = value;
}
- void add_extra_arg(const char *type) {
+ void add_extra_arg(const std::string &type) {
current_extra_arg = type;
}
- void set_extra_arg_name(const char *name) {
+ void set_extra_arg_name(const std::string &name) {
grammar.extra_args.emplace_back(current_extra_arg, name);
}
- void new_rule(const char *nonterm);
- void add_rule_nonterminal(const char *nonterm);
- void add_rule_terminal(const char *term);
+ void new_rule(const std::string &nonterm);
+ void add_rule_nonterminal(const std::string &nonterm);
+ void add_rule_terminal(const std::string &term);
void add_rule_terminal(unsigned char term);
void add_rule(const std::string &action = "");
- void add_rule_var(const char *var);
+ void add_rule_var(const std::string &var);
- void add_type_nonterminal(const char *nonterm);
- void add_type_terminal(const char *term);
- void set_type_nonterminal(const char *type);
- void set_type_terminal(const char *type);
- void set_type_terminal_name(const char *name);
+ void add_type_nonterminal(const std::string &nonterm);
+ void add_type_terminal(const std::string &term);
+ void set_type_nonterminal(const std::string &type);
+ void set_type_terminal(const std::string &type);
+ void set_type_terminal_name(const std::string &name);
};
}
diff --git a/src/symbol.hpp b/src/symbol.hpp
index 68222e2..0e79eb6 100644
--- a/src/symbol.hpp
+++ b/src/symbol.hpp
@@ -39,7 +39,7 @@ enum symbol_type_t {
};
struct symbol_t : public std::tuple<symbol_type_t, std::string> {
- symbol_t(symbol_type_t type, const char *value) : std::tuple<symbol_type_t, std::string>(type, value) {}
+ symbol_t(symbol_type_t type, const std::string &value) : std::tuple<symbol_type_t, std::string>(type, value) {}
symbol_type_t get_type() const {
return std::get<0>(*this);
@@ -49,11 +49,11 @@ struct symbol_t : public std::tuple<symbol_type_t, std::string> {
return std::get<1>(*this);
}
- static symbol_t make_nonterm(const char *value) {
+ static symbol_t make_nonterm(const std::string &value) {
return symbol_t(SYMBOL_TYPE_NONTERM, value);
}
- static symbol_t make_term(const char *value) {
+ static symbol_t make_term(const std::string &value) {
return symbol_t(SYMBOL_TYPE_TERM, value);
}