From 6fe1d4ea7c32a7c20a40d9bc7887b5a7d665ed8c Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sun, 6 Oct 2019 19:19:23 -0500 Subject: hscript: Factor parser errors to a macro; check key validity --- hscript/script.cc | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/hscript/script.cc b/hscript/script.cc index e7746bc..0e72c81 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -52,6 +52,21 @@ const Script *Script::load(std::string path, ScriptOptions opts) { return Script::load(file, opts); } +/*! Determines if the specified +key+ has been defined in this version of + * HorizonScript. + */ +bool is_key(std::string key) { + return true; +} + +#define PARSER_ERROR(err_str) \ + any_error = true;\ + output_error("installfile:" + std::to_string(lineno),\ + err_str, "", (opts.test(Pretty)));\ + if(!opts.test(ScriptOptionFlags::KeepGoing)) {\ + break;\ + } + const Script *Script::load(std::istream &sstream, ScriptOptions opts) { Script *the_script = new Script; @@ -68,6 +83,7 @@ const Script *Script::load(std::istream &sstream, ScriptOptions opts) { } const std::string line(nextline); + std::string key; std::string::size_type start, key_end, value_begin; start = line.find_first_not_of(delim); if(start == std::string::npos) { @@ -77,15 +93,20 @@ const Script *Script::load(std::istream &sstream, ScriptOptions opts) { key_end = line.find_first_of(delim, start); value_begin = line.find_first_not_of(delim, key_end); + key = line.substr(start, key_end); if(key_end == std::string::npos || value_begin == std::string::npos) { /* Key without value */ - any_error = true; - output_error("installfile:" + std::to_string(lineno), - "key '" + line.substr(start, key_end) + - "' has no value", - "", (opts.test(Pretty))); - if(!opts.test(ScriptOptionFlags::KeepGoing)) { - break; + PARSER_ERROR("key '" + key + "' has no value") + } + + if(!is_key(line.substr(start, key_end))) { + /* Invalid key */ + if(opts.test(StrictMode)) { + PARSER_ERROR("key '" + key + "' is not defined") + } else { + output_warning("installfile:" + std::to_string(lineno), + "key '" + key + "' is not defined", "", + opts.test(Pretty)); } } } -- cgit v1.2.3-70-g09d2