From 2ef8f936224fe8506673561d033142aeddcc44c1 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 19 Aug 2022 18:05:31 +0800 Subject: fix format. --- src/.clang-format | 17 + src/yue.cpp | 62 ++-- src/yue_wasm.cpp | 12 +- src/yuescript/ast.cpp | 24 +- src/yuescript/ast.hpp | 138 ++++---- src/yuescript/parser.cpp | 701 ++++++++++++++++++----------------------- src/yuescript/parser.hpp | 140 ++++---- src/yuescript/yue_ast.h | 28 +- src/yuescript/yue_compiler.cpp | 353 ++++++++++----------- src/yuescript/yue_compiler.h | 9 +- src/yuescript/yue_parser.cpp | 48 +-- src/yuescript/yue_parser.h | 18 +- src/yuescript/yuescript.cpp | 8 +- 13 files changed, 737 insertions(+), 821 deletions(-) create mode 100644 src/.clang-format (limited to 'src') diff --git a/src/.clang-format b/src/.clang-format new file mode 100644 index 0000000..5d305ae --- /dev/null +++ b/src/.clang-format @@ -0,0 +1,17 @@ +# Format Style Options - Created with Clang Power Tools +--- +BasedOnStyle: WebKit +AllowShortCaseLabelsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLoopsOnASingleLine: true +BreakBeforeBraces: Attach +Cpp11BracedListStyle: true +IndentCaseLabels: true +NamespaceIndentation: None +SpaceBeforeCpp11BracedList: false +TabWidth: 4 +AlignEscapedNewlines: DontAlign +AlwaysBreakBeforeMultilineStrings: true +FixNamespaceComments: true +UseTab: Always +... diff --git a/src/yue.cpp b/src/yue.cpp index 6011d94..07d0fef 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -9,28 +9,28 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include "yuescript/yue_compiler.h" #include "yuescript/yue_parser.h" -#include -#include +#include #include -#include #include -#include #include +#include +#include +#include +#include #include -#include #include -#include +#include using namespace std::string_view_literals; using namespace std::string_literals; #include "ghc/fs_std.hpp" #include "linenoise.hpp" -#if not (defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY) -#define _DEFER(code,line) std::shared_ptr _defer_##line(nullptr, [&](auto){code;}) -#define DEFER(code) _DEFER(code,__LINE__) +#if not(defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY) +#define _DEFER(code, line) std::shared_ptr _defer_##line(nullptr, [&](auto) { code; }) +#define DEFER(code) _DEFER(code, __LINE__) extern "C" { -#include "lua.h" #include "lauxlib.h" +#include "lua.h" #include "lualib.h" int luaopen_yue(lua_State* L); } // extern "C" @@ -81,7 +81,7 @@ void pushOptions(lua_State* L, int lineOffset) { #endif // not (defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY) #ifndef YUE_NO_MACRO -#define YUE_ARGS nullptr,openlibs +#define YUE_ARGS nullptr, openlibs #else #define YUE_ARGS #endif // YUE_NO_MACRO @@ -90,7 +90,7 @@ void pushOptions(lua_State* L, int lineOffset) { static const char luaminifyCodes[] = #include "LuaMinify.h" -static void pushLuaminify(lua_State* L) { + static void pushLuaminify(lua_State * L) { if (luaL_loadbuffer(L, luaminifyCodes, sizeof(luaminifyCodes) / sizeof(luaminifyCodes[0]) - 1, "=(luaminify)") != 0) { std::string err = "failed to load luaminify module.\n"s + lua_tostring(L, -1); luaL_error(L, err.c_str()); @@ -103,27 +103,27 @@ static void pushLuaminify(lua_State* L) { int main(int narg, const char** args) { const char* help = -"Usage: yue [options|files|directories] ...\n\n" -" -h Print this message\n" + "Usage: yue [options|files|directories] ...\n\n" + " -h Print this message\n" #ifndef YUE_COMPILER_ONLY -" -e str Execute a file or raw codes\n" -" -m Generate minified codes\n" + " -e str Execute a file or raw codes\n" + " -m Generate minified codes\n" #endif // YUE_COMPILER_ONLY -" -t path Specify where to place compiled files\n" -" -o file Write output to file\n" -" -s Use spaces in generated codes instead of tabs\n" -" -p Write output to standard out\n" -" -b Dump compile time (doesn't write output)\n" -" -g Dump global variables used in NAME LINE COLUMN\n" -" -l Write line numbers from source codes\n" -" -v Print version\n" + " -t path Specify where to place compiled files\n" + " -o file Write output to file\n" + " -s Use spaces in generated codes instead of tabs\n" + " -p Write output to standard out\n" + " -b Dump compile time (doesn't write output)\n" + " -g Dump global variables used in NAME LINE COLUMN\n" + " -l Write line numbers from source codes\n" + " -v Print version\n" #ifndef YUE_COMPILER_ONLY -" -- Read from standard in, print to standard out\n" -" (Must be first and only argument)\n\n" -" Execute without options to enter REPL, type symbol '$'\n" -" in a single line to start/stop multi-line mode\n" + " -- Read from standard in, print to standard out\n" + " (Must be first and only argument)\n\n" + " Execute without options to enter REPL, type symbol '$'\n" + " in a single line to start/stop multi-line mode\n" #endif // YUE_COMPILER_ONLY -; + ; #ifndef YUE_COMPILER_ONLY if (narg == 1) { lua_State* L = luaL_newstate(); @@ -276,7 +276,7 @@ int main(int narg, const char** args) { std::string targetPath; std::string resultFile; std::string workPath; - std::list> files; + std::list> files; for (int i = 1; i < narg; ++i) { std::string arg = args[i]; if (arg == "--"sv) { @@ -449,7 +449,7 @@ int main(int narg, const char** args) { std::cout << "Error: -o can not be used with multiple input files\n"sv; std::cout << help; } - std::list>> results; + std::list>> results; for (const auto& file : files) { auto task = std::async(std::launch::async, [=]() { std::ifstream input(file.first, std::ios::in); diff --git a/src/yue_wasm.cpp b/src/yue_wasm.cpp index 8c93eb1..395388b 100644 --- a/src/yue_wasm.cpp +++ b/src/yue_wasm.cpp @@ -9,8 +9,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include "yuescript/yue_compiler.h" extern "C" { -#include "lua.h" #include "lauxlib.h" +#include "lua.h" #include "lualib.h" int luaopen_yue(lua_State* L); } // extern "C" @@ -23,15 +23,14 @@ static void openlibs(void* state) { lua_pop(L, 1); } -#define YUE_ARGS nullptr,openlibs +#define YUE_ARGS nullptr, openlibs #include using namespace std::string_view_literals; #include using namespace emscripten; -struct YueResult -{ +struct YueResult { std::string codes; std::string err; }; @@ -47,8 +46,8 @@ YueResult tolua(const std::string& codes, bool reserveLineNumber = true, bool im std::string version() { return std::string(yue::version); } -#define _DEFER(code,line) std::shared_ptr _defer_##line(nullptr, [&](auto){code;}) -#define DEFER(code) _DEFER(code,__LINE__) +#define _DEFER(code, line) std::shared_ptr _defer_##line(nullptr, [&](auto) { code; }) +#define DEFER(code) _DEFER(code, __LINE__) void pushYue(lua_State* L, std::string_view name) { lua_getglobal(L, "package"); // package @@ -167,4 +166,3 @@ EMSCRIPTEN_BINDINGS(yue) { function("exec", &exec); function("version", &version); } - diff --git a/src/yuescript/ast.cpp b/src/yuescript/ast.cpp index 3deefb1..239aec6 100644 --- a/src/yuescript/ast.cpp +++ b/src/yuescript/ast.cpp @@ -13,11 +13,9 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "yuescript/ast.hpp" - namespace parserlib { - -traversal ast_node::traverse(const std::function& func) { +traversal ast_node::traverse(const std::function& func) { return func(this); } @@ -45,27 +43,25 @@ ast_node* ast_node::getByTypeIds(int* begin, int* end) { return current; } -bool ast_node::visitChild(const std::function&) { +bool ast_node::visitChild(const std::function&) { return false; } - /** Asks all members to construct themselves from the stack. The members are asked to construct themselves in reverse order. from a node stack. @param st stack. */ -void ast_container::construct(ast_stack &st) { - for(ast_member_vector::reverse_iterator it = m_members.rbegin(); - it != m_members.rend(); - ++it) - { +void ast_container::construct(ast_stack& st) { + for (ast_member_vector::reverse_iterator it = m_members.rbegin(); + it != m_members.rend(); + ++it) { ast_member* member = *it; member->construct(st); } } -traversal ast_container::traverse(const std::function& func) { +traversal ast_container::traverse(const std::function& func) { traversal action = func(this); switch (action) { case traversal::Stop: return traversal::Stop; @@ -96,7 +92,7 @@ traversal ast_container::traverse(const std::function& fu return traversal::Continue; } -bool ast_container::visitChild(const std::function& func) { +bool ast_container::visitChild(const std::function& func) { const auto& members = this->members(); for (auto member : members) { switch (member->get_type()) { @@ -121,7 +117,6 @@ bool ast_container::visitChild(const std::function& func) { return false; } - /** parses the given input. @param i input. @param g root rule of grammar. @@ -143,5 +138,4 @@ ast_node* parse(input& i, rule& g, error_list& el, void* ud) { return st.front(); } - -} //namespace parserlib +} // namespace parserlib diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index 4860fc9..681c27f 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp @@ -11,40 +11,48 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #pragma once - #include #include #include -#include #include +#include #include "yuescript/parser.hpp" - namespace parserlib { - class ast_node; -template class ast_ptr; -template class ast_list; -template class ast; - +template +class ast_ptr; +template +class ast_list; +template +class ast; /** type of AST node stack. -*/ + */ typedef std::vector ast_stack; typedef std::list node_container; - -template struct Counter { enum { value = Counter::value }; }; -template<> struct Counter<0> { enum { value = 0 }; }; +template +struct Counter { + enum { value = Counter::value }; +}; +template <> +struct Counter<0> { + enum { value = 0 }; +}; #define COUNTER_READ Counter<__LINE__>::value -#define COUNTER_INC template<> struct Counter<__LINE__> { enum { value = Counter<__LINE__-1>::value + 1}; } +#define COUNTER_INC \ + template <> \ + struct Counter<__LINE__> { \ + enum { value = Counter<__LINE__ - 1>::value + 1 }; \ + } class ast_node; -template -constexpr typename std::enable_if::value,int>::type +template +constexpr typename std::enable_if::value, int>::type id(); enum class traversal { @@ -54,10 +62,11 @@ enum class traversal { }; /** Base class for AST nodes. -*/ + */ class ast_node : public input_range { public: - ast_node() : _ref(0) {} + ast_node() + : _ref(0) { } void retain() { ++_ref; @@ -74,32 +83,32 @@ public: from a node stack. @param st stack. */ - virtual void construct(ast_stack&) {} + virtual void construct(ast_stack&) { } /** interface for visiting AST tree use. - */ - virtual traversal traverse(const std::function& func); + */ + virtual traversal traverse(const std::function& func); template struct select_last { - using type = typename decltype((std::enable_if{}, ...))::type; + using type = typename decltype((std::enable_if{}, ...))::type; }; template using select_last_t = typename select_last::type; - template + template select_last_t* getByPath() { int types[] = {id()...}; return static_cast*>(getByTypeIds(std::begin(types), std::end(types))); } - virtual bool visitChild(const std::function& func); + virtual bool visitChild(const std::function& func); virtual int getId() const = 0; virtual const std::string_view getName() const = 0; - template + template inline ast_ptr new_ptr() const { auto item = new T; item->m_begin.m_line = m_begin.m_line; @@ -108,27 +117,28 @@ public: item->m_end.m_col = m_end.m_col; return ast_ptr(item); } + private: int _ref; ast_node* getByTypeIds(int* begin, int* end); }; -template -constexpr typename std::enable_if::value,int>::type +template +constexpr typename std::enable_if::value, int>::type id() { return 0; } -template +template T* ast_cast(ast_node* node) { return node && id() == node->getId() ? static_cast(node) : nullptr; } -template +template T* ast_to(ast_node* node) { assert(node->getId() == id()); return static_cast(node); } -template +template bool ast_is(ast_node* node) { if (!node) return false; bool result = false; @@ -141,12 +151,11 @@ bool ast_is(ast_node* node) { class ast_member; /** type of ast member vector. -*/ + */ typedef std::vector ast_member_vector; - /** base class for AST nodes with children. -*/ + */ class ast_container : public ast_node { public: void add_members(std::initializer_list members) { @@ -169,9 +178,10 @@ public: */ virtual void construct(ast_stack& st) override; - virtual traversal traverse(const std::function& func) override; + virtual traversal traverse(const std::function& func) override; + + virtual bool visitChild(const std::function& func) override; - virtual bool visitChild(const std::function& func) override; private: ast_member_vector m_members; @@ -184,10 +194,10 @@ enum class ast_holder_type { }; /** Base class for children of ast_container. -*/ + */ class ast_member { public: - virtual ~ast_member() {} + virtual ~ast_member() { } /** interface for filling the the member from a node stack. @param st stack. @@ -199,10 +209,10 @@ public: virtual ast_holder_type get_type() const = 0; }; - class _ast_ptr : public ast_member { public: - _ast_ptr(ast_node* node) : m_ptr(node) { + _ast_ptr(ast_node* node) + : m_ptr(node) { if (node) node->retain(); } @@ -250,6 +260,7 @@ public: virtual ast_holder_type get_type() const override { return ast_holder_type::Pointer; } + protected: ast_node* m_ptr; }; @@ -260,11 +271,14 @@ protected: @tparam Required if true, the object is required. @tparam T type of object to control. */ -template class ast_ptr : public _ast_ptr { +template +class ast_ptr : public _ast_ptr { public: - ast_ptr(T* node = nullptr) : _ast_ptr(node) {} + ast_ptr(T* node = nullptr) + : _ast_ptr(node) { } - ast_ptr(const ast_ptr& other) : _ast_ptr(other.get()) {} + ast_ptr(const ast_ptr& other) + : _ast_ptr(other.get()) { } ast_ptr& operator=(const ast_ptr& other) { set(other.get()); @@ -315,17 +329,21 @@ public: m_ptr = node; node->retain(); } + private: virtual bool accept(ast_node* node) override { - return node && (std::is_same() || id() == node->getId()); + return node && (std::is_same() || id() == node->getId()); } }; -template class ast_sel : public _ast_ptr { +template +class ast_sel : public _ast_ptr { public: - ast_sel() : _ast_ptr(nullptr) {} + ast_sel() + : _ast_ptr(nullptr) { } - ast_sel(const ast_sel& other) : _ast_ptr(other.get()) {} + ast_sel(const ast_sel& other) + : _ast_ptr(other.get()) { } ast_sel& operator=(const ast_sel& other) { set(other.get()); @@ -355,6 +373,7 @@ public: m_ptr = node; node->retain(); } + private: virtual bool accept(ast_node* node) override { if (!node) return false; @@ -428,14 +447,14 @@ public: } void clear() { - for(ast_node* obj : m_objects) { + for (ast_node* obj : m_objects) { if (obj) obj->release(); } m_objects.clear(); } void dup(const _ast_list& src) { - for(ast_node* obj : src.m_objects) { + for (ast_node* obj : src.m_objects) { m_objects.push_back(obj); obj->retain(); } @@ -444,6 +463,7 @@ public: virtual ast_holder_type get_type() const override { return ast_holder_type::List; } + protected: node_container m_objects; }; @@ -454,7 +474,8 @@ protected: @tparam Required if true, the object is required. @tparam T type of object to control. */ -template class ast_list : public _ast_list { +template +class ast_list : public _ast_list { public: ast_list() { } @@ -471,7 +492,7 @@ public: /** Pops objects of type T from the stack until no more objects can be popped. @param st stack. */ - virtual void construct(ast_stack &st) override { + virtual void construct(ast_stack& st) override { while (!st.empty()) { ast_node* node = st.back(); // if the object was not not of the appropriate type, @@ -491,13 +512,15 @@ public: throw std::logic_error("Invalid AST stack."); } } + private: virtual bool accept(ast_node* node) override { - return node && (std::is_same() || id() == node->getId()); + return node && (std::is_same() || id() == node->getId()); } }; -template class ast_sel_list : public _ast_list { +template +class ast_sel_list : public _ast_list { public: ast_sel_list() { } @@ -511,7 +534,7 @@ public: return *this; } - virtual void construct(ast_stack &st) override { + virtual void construct(ast_stack& st) override { while (!st.empty()) { ast_node* node = st.back(); if (!ast_sel_list::accept(node)) { @@ -528,6 +551,7 @@ public: throw std::logic_error("Invalid AST stack."); } } + private: virtual bool accept(ast_node* node) override { if (!node) return false; @@ -541,7 +565,8 @@ private: /** AST function which creates an object of type T and pushes it to the node stack. */ -template class ast { +template +class ast { public: /** constructor. @param r rule to attach the AST function to. @@ -549,8 +574,9 @@ public: ast(rule& r) { r.set_parse_proc(&_parse_proc); } + private: - //parse proc + // parse proc static void _parse_proc(const pos& b, const pos& e, void* d) { ast_stack* st = reinterpret_cast(d); T* obj = new T; @@ -561,7 +587,6 @@ private: } }; - /** parses the given input. @param i input. @param g root rule of grammar. @@ -572,5 +597,4 @@ private: */ ast_node* parse(input& i, rule& g, error_list& el, void* ud); - -} //namespace parserlib +} // namespace parserlib diff --git a/src/yuescript/parser.cpp b/src/yuescript/parser.cpp index a44ae34..a6eb513 100644 --- a/src/yuescript/parser.cpp +++ b/src/yuescript/parser.cpp @@ -9,222 +9,210 @@ Redistributions in binary form must reproduce the above copyright notice, this l THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ +#include #include #include -#include #include #include #include #include "yuescript/parser.hpp" - namespace parserlib { - -//internal private class that manages access to the public classes' internals. +// internal private class that manages access to the public classes' internals. class _private { public: - //get the internal expression object from the expression. + // get the internal expression object from the expression. static _expr* get_expr(const expr& e) { return e.m_expr; } - //create new expression from given expression + // create new expression from given expression static expr construct_expr(_expr* e) { return e; } - //get the internal expression object from the rule. + // get the internal expression object from the rule. static _expr* get_expr(rule& r) { return r.m_expr; } - //get the internal parse proc from the rule. + // get the internal parse proc from the rule. static parse_proc get_parse_proc(rule& r) { return r.m_parse_proc; } }; - class _context; - -//parser state +// parser state class _state { public: - //position + // position pos m_pos; - //size of match vector + // size of match vector size_t m_matches; - //constructor + // constructor _state(_context& con); }; - -//match +// match class _match { public: - //rule matched + // rule matched rule* m_rule; - //begin position + // begin position pos m_begin; - //end position + // end position pos m_end; - //null constructor - _match() {} + // null constructor + _match() { } - //constructor from parameters - _match(rule* r, const pos& b, const pos& e) : - m_rule(r), - m_begin(b), - m_end(e) - { + // constructor from parameters + _match(rule* r, const pos& b, const pos& e) + : m_rule(r) + , m_begin(b) + , m_end(e) { } }; - -//match vector +// match vector typedef std::vector<_match> _match_vector; - -//parsing context +// parsing context class _context { public: - //user data + // user data void* m_user_data; - //current position + // current position pos m_pos; - //error position + // error position pos m_error_pos; - //input begin + // input begin input::iterator m_begin; - //input end + // input end input::iterator m_end; - //matches + // matches _match_vector m_matches; - //constructor - _context(input& i, void* ud) : - m_user_data(ud), - m_pos(i), - m_error_pos(i), - m_begin(i.begin()), - m_end(i.end()) - { + // constructor + _context(input& i, void* ud) + : m_user_data(ud) + , m_pos(i) + , m_error_pos(i) + , m_begin(i.begin()) + , m_end(i.end()) { } - //check if the end is reached + // check if the end is reached bool end() const { return m_pos.m_it == m_end; } - //get the current symbol + // get the current symbol input::value_type symbol() const { assert(!end()); return *m_pos.m_it; } - //set the longest possible error + // set the longest possible error void set_error_pos() { if (m_pos.m_it > m_error_pos.m_it) { m_error_pos = m_pos; } } - //next column + // next column void next_col() { ++m_pos.m_it; ++m_pos.m_col; } - //next line + // next line void next_line() { ++m_pos.m_line; m_pos.m_col = 1; } - //restore the state + // restore the state void restore(const _state& st) { m_pos = st.m_pos; m_matches.resize(st.m_matches); } - //parse non-term rule. + // parse non-term rule. bool parse_non_term(rule& r); - //parse term rule. + // parse term rule. bool parse_term(rule& r); - //execute all the parse procs + // execute all the parse procs void do_parse_procs(void* d) const { - for(_match_vector::const_iterator it = m_matches.begin(); - it != m_matches.end(); - ++it) { - const _match &m = *it; + for (_match_vector::const_iterator it = m_matches.begin(); + it != m_matches.end(); + ++it) { + const _match& m = *it; parse_proc p = _private::get_parse_proc(*m.m_rule); p(m.m_begin, m.m_end, d); } } private: - //parse non-term rule. + // parse non-term rule. bool _parse_non_term(rule& r); - //parse term rule. + // parse term rule. bool _parse_term(rule& r); }; - -//base class for expressions +// base class for expressions class _expr { public: - //destructor. + // destructor. virtual ~_expr() { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const = 0; - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const = 0; }; - -//single character expression. +// single character expression. class _char : public _expr { public: - //constructor. - _char(char c) : - m_char(c) - { + // constructor. + _char(char c) + : m_char(c) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return _parse(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { return _parse(con); } private: - //character + // character input::value_type m_char; - //internal parse + // internal parse bool _parse(_context& con) const { if (!con.end()) { input::value_type ch = con.symbol(); @@ -238,34 +226,33 @@ private: } }; - -//string expression. +// string expression. class _string : public _expr { public: - //constructor from ansi string. - _string(const char* s) : - m_string(Converter{}.from_bytes(s)) - { + // constructor from ansi string. + _string(const char* s) + : m_string(Converter{}.from_bytes(s)) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return _parse(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { return _parse(con); } private: - //string + // string input m_string; - //parse the string + // parse the string bool _parse(_context& con) const { - for(input::const_iterator it = m_string.begin(), - end = m_string.end();;) { + for (input::const_iterator it = m_string.begin(), + end = m_string.end(); + ;) { if (it == end) return true; if (con.end()) break; if (con.symbol() != *it) break; @@ -277,44 +264,43 @@ private: } }; - -//set expression. +// set expression. class _set : public _expr { public: - //constructor from ansi string. + // constructor from ansi string. _set(const char* s) { auto str = Converter{}.from_bytes(s); for (auto ch : str) { - _add(ch); + _add(ch); } } - //constructor from range. + // constructor from range. _set(int min, int max) { assert(min >= 0); assert(min <= max); m_quick_set.resize((size_t)max + 1U); - for(; min <= max; ++min) { + for (; min <= max; ++min) { m_quick_set[(size_t)min] = true; } } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return _parse(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { return _parse(con); } private: - //set is kept as an array of flags, for quick access + // set is kept as an array of flags, for quick access std::vector m_quick_set; std::unordered_set m_large_set; - //add character + // add character void _add(size_t i) { if (i <= m_quick_set.size() || i <= 255) { if (i >= m_quick_set.size()) { @@ -326,7 +312,7 @@ private: } } - //internal parse + // internal parse bool _parse(_context& con) const { if (!con.end()) { size_t ch = con.symbol(); @@ -345,59 +331,53 @@ private: } }; - -//base class for unary expressions +// base class for unary expressions class _unary : public _expr { public: - //constructor. - _unary(_expr* e) : - m_expr(e) - { + // constructor. + _unary(_expr* e) + : m_expr(e) { } - //destructor. + // destructor. virtual ~_unary() { delete m_expr; } protected: - //expression - _expr *m_expr; + // expression + _expr* m_expr; }; - -//terminal +// terminal class _term : public _unary { public: - //constructor. - _term(_expr* e) : - _unary(e) - { + // constructor. + _term(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return m_expr->parse_term(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { return m_expr->parse_term(con); } }; - -//user +// user class _user : public _unary { public: - //constructor. - _user(_expr* e, const user_handler& callback) : - _unary(e), - m_handler(callback) - { + // constructor. + _user(_expr* e, const user_handler& callback) + : _unary(e) + , m_handler(callback) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { pos pos = con.m_pos; if (m_expr->parse_non_term(con)) { @@ -407,7 +387,7 @@ public: return false; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { pos pos = con.m_pos; if (m_expr->parse_term(con)) { @@ -416,31 +396,30 @@ public: } return false; } + private: user_handler m_handler; }; - -//loop 0 +// loop 0 class _loop0 : public _unary { public: - //constructor. - _loop0(_expr* e) : - _unary(e) - { + // constructor. + _loop0(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { - //if parsing of the first fails, restore the context and stop + // if parsing of the first fails, restore the context and stop _state st(con); if (!m_expr->parse_non_term(con)) { con.restore(st); return true; } - //parse the rest - for(;;) { + // parse the rest + for (;;) { _state st(con); if (!m_expr->parse_non_term(con)) { con.restore(st); @@ -451,17 +430,17 @@ public: return true; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { - //if parsing of the first fails, restore the context and stop + // if parsing of the first fails, restore the context and stop _state st(con); if (!m_expr->parse_term(con)) { con.restore(st); return true; } - //parse the rest until no more parsing is possible - for(;;) { + // parse the rest until no more parsing is possible + for (;;) { _state st(con); if (!m_expr->parse_term(con)) { con.restore(st); @@ -473,23 +452,21 @@ public: } }; - -//loop 1 +// loop 1 class _loop1 : public _unary { public: - //constructor. - _loop1(_expr* e) : - _unary(e) - { + // constructor. + _loop1(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { - //parse the first; if the first fails, stop + // parse the first; if the first fails, stop if (!m_expr->parse_non_term(con)) return false; - //parse the rest until no more parsing is possible - for(;;) { + // parse the rest until no more parsing is possible + for (;;) { _state st(con); if (!m_expr->parse_non_term(con)) { con.restore(st); @@ -500,13 +477,13 @@ public: return true; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { - //parse the first; if the first fails, stop + // parse the first; if the first fails, stop if (!m_expr->parse_term(con)) return false; - //parse the rest until no more parsing is possible - for(;;) { + // parse the rest until no more parsing is possible + for (;;) { _state st(con); if (!m_expr->parse_term(con)) { con.restore(st); @@ -518,24 +495,22 @@ public: } }; - -//optional +// optional class _optional : public _unary { public: - //constructor. - _optional(_expr* e) : - _unary(e) - { + // constructor. + _optional(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { _state st(con); if (!m_expr->parse_non_term(con)) con.restore(st); return true; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { _state st(con); if (!m_expr->parse_term(con)) con.restore(st); @@ -543,17 +518,15 @@ public: } }; - -//and +// and class _and : public _unary { public: - //constructor. - _and(_expr* e) : - _unary(e) - { + // constructor. + _and(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { _state st(con); bool ok = m_expr->parse_non_term(con); @@ -561,7 +534,7 @@ public: return ok; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { _state st(con); bool ok = m_expr->parse_term(con); @@ -570,17 +543,15 @@ public: } }; - -//not +// not class _not : public _unary { public: - //constructor. - _not(_expr* e) : - _unary(e) - { + // constructor. + _not(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { _state st(con); bool ok = !m_expr->parse_non_term(con); @@ -588,7 +559,7 @@ public: return ok; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { _state st(con); bool ok = !m_expr->parse_term(con); @@ -597,24 +568,22 @@ public: } }; - -//newline +// newline class _nl : public _unary { public: - //constructor. - _nl(_expr* e) : - _unary(e) - { + // constructor. + _nl(_expr* e) + : _unary(e) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { if (!m_expr->parse_non_term(con)) return false; con.next_line(); return true; } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { if (!m_expr->parse_term(con)) return false; con.next_line(); @@ -622,61 +591,56 @@ public: } }; - -//base class for binary expressions +// base class for binary expressions class _binary : public _expr { public: - //constructor. - _binary(_expr* left, _expr* right) : - m_left(left), m_right(right) - { + // constructor. + _binary(_expr* left, _expr* right) + : m_left(left) + , m_right(right) { } - //destructor. + // destructor. virtual ~_binary() { delete m_left; delete m_right; } protected: - //left and right expressions - _expr* m_left, *m_right; + // left and right expressions + _expr *m_left, *m_right; }; - -//sequence +// sequence class _seq : public _binary { public: - //constructor. - _seq(_expr* left, _expr* right) : - _binary(left, right) - { + // constructor. + _seq(_expr* left, _expr* right) + : _binary(left, right) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { if (!m_left->parse_non_term(con)) return false; return m_right->parse_non_term(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { if (!m_left->parse_term(con)) return false; return m_right->parse_term(con); } }; - -//choice +// choice class _choice : public _binary { public: - //constructor. - _choice(_expr* left, _expr* right) : - _binary(left, right) - { + // constructor. + _choice(_expr* left, _expr* right) + : _binary(left, right) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { _state st(con); if (m_left->parse_non_term(con)) return true; @@ -684,7 +648,7 @@ public: return m_right->parse_non_term(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { _state st(con); if (m_left->parse_term(con)) return true; @@ -693,56 +657,52 @@ public: } }; - -//reference to rule +// reference to rule class _ref : public _expr { public: - //constructor. - _ref(rule& r) : - m_rule(r) - { + // constructor. + _ref(rule& r) + : m_rule(r) { } - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return con.parse_non_term(m_rule); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { return con.parse_term(m_rule); } private: - //reference - rule &m_rule; + // reference + rule& m_rule; }; - -//eof +// eof class _eof : public _expr { public: - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return parse_term(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { return con.end(); } }; - -//any +// any class _any : public _expr { public: - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context& con) const { return parse_term(con); } - //parse terminal + // parse terminal virtual bool parse_term(_context& con) const { if (!con.end()) { con.next_col(); @@ -753,115 +713,108 @@ public: } }; - -//true +// true class _true : public _expr { public: - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context&) const { return true; } - //parse terminal + // parse terminal virtual bool parse_term(_context&) const { return true; } }; - -//false -class _false: public _expr { +// false +class _false : public _expr { public: - //parse with whitespace + // parse with whitespace virtual bool parse_non_term(_context&) const { return false; } - //parse terminal + // parse terminal virtual bool parse_term(_context&) const { return false; } }; -//exception thrown when left recursion terminates successfully +// exception thrown when left recursion terminates successfully struct _lr_ok { rule* m_rule; - _lr_ok(rule* r) : m_rule(r) {} + _lr_ok(rule* r) + : m_rule(r) { } }; - -//constructor -_state::_state(_context& con) : - m_pos(con.m_pos), - m_matches(con.m_matches.size()) -{ +// constructor +_state::_state(_context& con) + : m_pos(con.m_pos) + , m_matches(con.m_matches.size()) { } - -//parse non-term rule. +// parse non-term rule. bool _context::parse_non_term(rule& r) { - //save the state of the rule + // save the state of the rule rule::_state old_state = r.m_state; - //success/failure result + // success/failure result bool ok = false; - //compute the new position + // compute the new position size_t new_pos = m_pos.m_it - m_begin; - //check if we have left recursion + // check if we have left recursion bool lr = new_pos == r.m_state.m_pos; - //update the rule's state + // update the rule's state r.m_state.m_pos = new_pos; - //handle the mode of the rule + // handle the mode of the rule switch (r.m_state.m_mode) { - //normal parse + // normal parse case rule::_PARSE: if (lr) { - //first try to parse the rule by rejecting it, so alternative branches are examined + // first try to parse the rule by rejecting it, so alternative branches are examined r.m_state.m_mode = rule::_REJECT; ok = _parse_non_term(r); - //if the first try is successful, try accepting the rule, - //so other elements of the sequence are parsed + // if the first try is successful, try accepting the rule, + // so other elements of the sequence are parsed if (ok) { r.m_state.m_mode = rule::_ACCEPT; - //loop until no more parsing can be done - for(;;) { - //store the correct state, in order to backtrack if the call fails + // loop until no more parsing can be done + for (;;) { + // store the correct state, in order to backtrack if the call fails _state st(*this); - //update the rule position to the current position, - //because at this state the rule is resolving the left recursion + // update the rule position to the current position, + // because at this state the rule is resolving the left recursion r.m_state.m_pos = m_pos.m_it - m_begin; - //if parsing fails, restore the last good state and stop + // if parsing fails, restore the last good state and stop if (!_parse_non_term(r)) { restore(st); break; } } - //since the left recursion was resolved successfully, - //return via a non-local exit + // since the left recursion was resolved successfully, + // return via a non-local exit r.m_state = old_state; throw _lr_ok(r.this_ptr()); } - } - else { + } else { try { ok = _parse_non_term(r); - } - catch (const _lr_ok &ex) { - //since left recursions may be mutual, we must test which rule's left recursion - //was ended successfully + } catch (const _lr_ok& ex) { + // since left recursions may be mutual, we must test which rule's left recursion + // was ended successfully if (ex.m_rule == r.this_ptr()) { ok = true; - } - else { + } else { r.m_state = old_state; throw; } @@ -869,24 +822,22 @@ bool _context::parse_non_term(rule& r) { } break; - //reject the left recursive rule + // reject the left recursive rule case rule::_REJECT: if (lr) { ok = false; - } - else { + } else { r.m_state.m_mode = rule::_PARSE; ok = _parse_non_term(r); r.m_state.m_mode = rule::_REJECT; } break; - //accept the left recursive rule + // accept the left recursive rule case rule::_ACCEPT: if (lr) { ok = true; - } - else { + } else { r.m_state.m_mode = rule::_PARSE; ok = _parse_non_term(r); r.m_state.m_mode = rule::_ACCEPT; @@ -894,76 +845,73 @@ bool _context::parse_non_term(rule& r) { break; } - //restore the rule's state + // restore the rule's state r.m_state = old_state; return ok; } - -//parse term rule. +// parse term rule. bool _context::parse_term(rule& r) { - //save the state of the rule + // save the state of the rule rule::_state old_state = r.m_state; - //success/failure result + // success/failure result bool ok = false; - //compute the new position + // compute the new position size_t new_pos = m_pos.m_it - m_begin; - //check if we have left recursion + // check if we have left recursion bool lr = new_pos == r.m_state.m_pos; - //update the rule's state + // update the rule's state r.m_state.m_pos = new_pos; - //handle the mode of the rule + // handle the mode of the rule switch (r.m_state.m_mode) { - //normal parse + // normal parse case rule::_PARSE: if (lr) { - //first try to parse the rule by rejecting it, so alternative branches are examined + // first try to parse the rule by rejecting it, so alternative branches are examined r.m_state.m_mode = rule::_REJECT; ok = _parse_term(r); - //if the first try is successful, try accepting the rule, - //so other elements of the sequence are parsed + // if the first try is successful, try accepting the rule, + // so other elements of the sequence are parsed if (ok) { r.m_state.m_mode = rule::_ACCEPT; - //loop until no more parsing can be done - for(;;) { - //store the correct state, in order to backtrack if the call fails + // loop until no more parsing can be done + for (;;) { + // store the correct state, in order to backtrack if the call fails _state st(*this); - //update the rule position to the current position, - //because at this state the rule is resolving the left recursion + // update the rule position to the current position, + // because at this state the rule is resolving the left recursion r.m_state.m_pos = m_pos.m_it - m_begin; - //if parsing fails, restore the last good state and stop + // if parsing fails, restore the last good state and stop if (!_parse_term(r)) { restore(st); break; } } - //since the left recursion was resolved successfully, - //return via a non-local exit + // since the left recursion was resolved successfully, + // return via a non-local exit r.m_state = old_state; throw _lr_ok(r.this_ptr()); } } else { try { ok = _parse_term(r); - } - catch (const _lr_ok& ex) { - //since left recursions may be mutual, we must test which rule's left recursion - //was ended successfully + } catch (const _lr_ok& ex) { + // since left recursions may be mutual, we must test which rule's left recursion + // was ended successfully if (ex.m_rule == r.this_ptr()) { ok = true; - } - else { + } else { r.m_state = old_state; throw; } @@ -971,7 +919,7 @@ bool _context::parse_term(rule& r) { } break; - //reject the left recursive rule + // reject the left recursive rule case rule::_REJECT: if (lr) { ok = false; @@ -982,7 +930,7 @@ bool _context::parse_term(rule& r) { } break; - //accept the left recursive rule + // accept the left recursive rule case rule::_ACCEPT: if (lr) { ok = true; @@ -994,14 +942,13 @@ bool _context::parse_term(rule& r) { break; } - //restore the rule's state + // restore the rule's state r.m_state = old_state; return ok; } - -//parse non-term rule internal. +// parse non-term rule internal. bool _context::_parse_non_term(rule& r) { bool ok = false; if (_private::get_parse_proc(r)) { @@ -1016,8 +963,7 @@ bool _context::_parse_non_term(rule& r) { return ok; } - -//parse term rule internal. +// parse term rule internal. bool _context::_parse_term(rule& r) { bool ok = false; if (_private::get_parse_proc(r)) { @@ -1032,57 +978,46 @@ bool _context::_parse_term(rule& r) { return ok; } - -//get syntax error +// get syntax error static error _syntax_error(_context& con) { return error(con.m_error_pos, con.m_error_pos, ERROR_SYNTAX_ERROR); } - -//get eof error +// get eof error static error _eof_error(_context& con) { return error(con.m_error_pos, con.m_error_pos, ERROR_INVALID_EOF); } - /** constructor from input. @param i input. */ -pos::pos(input& i) : - m_it(i.begin()), - m_line(1), - m_col(1) -{ +pos::pos(input& i) + : m_it(i.begin()) + , m_line(1) + , m_col(1) { } - /** character terminal constructor. @param c character. */ -expr::expr(char c) : - m_expr(new _char(c)) -{ +expr::expr(char c) + : m_expr(new _char(c)) { } - /** null-terminated string terminal constructor. @param s null-terminated string. */ -expr::expr(const char* s) : - m_expr(new _string(s)) -{ +expr::expr(const char* s) + : m_expr(new _string(s)) { } - /** rule reference constructor. @param r rule. */ -expr::expr(rule& r) : - m_expr(new _ref(r)) -{ +expr::expr(rule& r) + : m_expr(new _ref(r)) { } - /** creates a zero-or-more loop out of this expression. @return a zero-or-more loop expression. */ @@ -1090,7 +1025,6 @@ expr expr::operator*() const { return _private::construct_expr(new _loop0(m_expr)); } - /** creates a one-or-more loop out of this expression. @return a one-or-more loop expression. */ @@ -1098,7 +1032,6 @@ expr expr::operator+() const { return _private::construct_expr(new _loop1(m_expr)); } - /** creates an optional out of this expression. @return an optional expression. */ @@ -1106,7 +1039,6 @@ expr expr::operator-() const { return _private::construct_expr(new _optional(m_expr)); } - /** creates an AND-expression. @return an AND-expression. */ @@ -1114,7 +1046,6 @@ expr expr::operator&() const { return _private::construct_expr((new _and(m_expr))); } - /** creates a NOT-expression. @return a NOT-expression. */ @@ -1122,25 +1053,22 @@ expr expr::operator!() const { return _private::construct_expr(new _not(m_expr)); } - /** constructor. @param b begin position. @param e end position. */ -input_range::input_range(const pos& b, const pos& e) : -m_begin(b), -m_end(e) {} - +input_range::input_range(const pos& b, const pos& e) + : m_begin(b) + , m_end(e) { } /** constructor. @param b begin position. @param e end position. @param t error type. */ -error::error(const pos& b, const pos& e, int t) : -input_range(b, e), -m_type(t) {} - +error::error(const pos& b, const pos& e, int t) + : input_range(b, e) + , m_type(t) { } /** compare on begin position. @param e the other error to compare this with. @@ -1150,45 +1078,44 @@ bool error::operator<(const error& e) const { return m_begin.m_it < e.m_begin.m_it; } -rule::rule() : -m_expr(nullptr), -m_parse_proc(nullptr) {} +rule::rule() + : m_expr(nullptr) + , m_parse_proc(nullptr) { } /** character terminal constructor. @param c character. */ -rule::rule(char c) : -m_expr(new _char(c)), -m_parse_proc(nullptr) {} +rule::rule(char c) + : m_expr(new _char(c)) + , m_parse_proc(nullptr) { } /** null-terminated string terminal constructor. @param s null-terminated string. */ -rule::rule(const char* s) : -m_expr(new _string(s)), -m_parse_proc(nullptr) {} +rule::rule(const char* s) + : m_expr(new _string(s)) + , m_parse_proc(nullptr) { } /** constructor from expression. @param e expression. */ -rule::rule(const expr& e) : -m_expr(_private::get_expr(e)), -m_parse_proc(nullptr) {} - +rule::rule(const expr& e) + : m_expr(_private::get_expr(e)) + , m_parse_proc(nullptr) { } /** constructor from rule. @param r rule. */ -rule::rule(rule& r) : -m_expr(new _ref(r)), -m_parse_proc(nullptr) {} +rule::rule(rule& r) + : m_expr(new _ref(r)) + , m_parse_proc(nullptr) { } rule& rule::operator=(rule& r) { m_expr = new _ref(r); return *this; } -rule &rule::operator=(const expr& e) { +rule& rule::operator=(const expr& e) { m_expr = _private::get_expr(e); return *this; } @@ -1200,14 +1127,12 @@ rule::rule(const rule&) { throw std::logic_error("invalid operation"); } - /** deletes the internal object that represents the expression. -*/ + */ rule::~rule() { delete m_expr; } - /** creates a zero-or-more loop out of this rule. @return a zero-or-more loop rule. */ @@ -1215,7 +1140,6 @@ expr rule::operator*() { return _private::construct_expr(new _loop0(new _ref(*this))); } - /** creates a one-or-more loop out of this rule. @return a one-or-more loop rule. */ @@ -1223,7 +1147,6 @@ expr rule::operator+() { return _private::construct_expr(new _loop1(new _ref(*this))); } - /** creates an optional out of this rule. @return an optional rule. */ @@ -1231,7 +1154,6 @@ expr rule::operator-() { return _private::construct_expr(new _optional(new _ref(*this))); } - /** creates an AND-expression out of this rule. @return an AND-expression out of this rule. */ @@ -1239,7 +1161,6 @@ expr rule::operator&() { return _private::construct_expr(new _and(new _ref(*this))); } - /** creates a NOT-expression out of this rule. @return a NOT-expression out of this rule. */ @@ -1247,7 +1168,6 @@ expr rule::operator!() { return _private::construct_expr(new _not(new _ref(*this))); } - /** sets the parse procedure. @param p procedure. */ @@ -1256,29 +1176,26 @@ void rule::set_parse_proc(parse_proc p) { m_parse_proc = p; } - /** creates a sequence of expressions. @param left left operand. @param right right operand. @return an expression which parses a sequence. */ -expr operator >> (const expr& left, const expr& right) { +expr operator>>(const expr& left, const expr& right) { return _private::construct_expr( new _seq(_private::get_expr(left), _private::get_expr(right))); } - /** creates a choice of expressions. @param left left operand. @param right right operand. @return an expression which parses a choice. */ -expr operator | (const expr& left, const expr& right) { +expr operator|(const expr& left, const expr& right) { return _private::construct_expr( new _choice(_private::get_expr(left), _private::get_expr(right))); } - /** converts a parser expression into a terminal. @param e expression. @return an expression which parses a terminal. @@ -1288,7 +1205,6 @@ expr term(const expr& e) { new _term(_private::get_expr(e))); } - /** creates a set expression from a null-terminated string. @param s null-terminated string with characters of the set. @return an expression which parses a single character out of a set. @@ -1297,7 +1213,6 @@ expr set(const char* s) { return _private::construct_expr(new _set(s)); } - /** creates a range expression. @param min min character. @param max max character. @@ -1307,7 +1222,6 @@ expr range(int min, int max) { return _private::construct_expr(new _set(min, max)); } - /** creates an expression which increments the line counter and resets the column counter when the given expression is parsed successfully; used for newline characters. @@ -1318,7 +1232,6 @@ expr nl(const expr& e) { return _private::construct_expr(new _nl(_private::get_expr(e))); } - /** creates an expression which tests for the end of input. @return an expression that handles the end of input. */ @@ -1326,7 +1239,6 @@ expr eof() { return _private::construct_expr(new _eof()); } - /** creates a not expression. @param e expression. @return the appropriate expression. @@ -1335,7 +1247,6 @@ expr not_(const expr& e) { return !e; } - /** creates an and expression. @param e expression. @return the appropriate expression. @@ -1344,7 +1255,6 @@ expr and_(const expr& e) { return &e; } - /** creates an expression that parses any character. @return the appropriate expression. */ @@ -1352,28 +1262,24 @@ expr any() { return _private::construct_expr(new _any()); } - /** parsing succeeds without consuming any input. -*/ + */ expr true_() { return _private::construct_expr(new _true()); } - /** parsing fails without consuming any input. -*/ + */ expr false_() { return _private::construct_expr(new _false()); } - /** parse with target expression and let user handle result. -*/ + */ expr user(const expr& e, const user_handler& handler) { return _private::construct_expr(new _user(_private::get_expr(e), handler)); } - /** parses the given input. The parse procedures of each rule parsed are executed before this function returns, if parsing succeeds. @@ -1384,16 +1290,16 @@ expr user(const expr& e, const user_handler& handler) { @return true on parsing success, false on failure. */ bool parse(input& i, rule& g, error_list& el, void* d, void* ud) { - //prepare context + // prepare context _context con(i, ud); - //parse grammar + // parse grammar if (!con.parse_non_term(g)) { el.push_back(_syntax_error(con)); return false; } - //if end is not reached, there was an error + // if end is not reached, there was an error if (!con.end()) { if (con.m_error_pos.m_it < con.m_end) { el.push_back(_syntax_error(con)); @@ -1403,10 +1309,9 @@ bool parse(input& i, rule& g, error_list& el, void* d, void* ud) { return false; } - //success; execute the parse procedures + // success; execute the parse procedures con.do_parse_procs(d); return true; } - -} //namespace parserlib +} // namespace parserlib diff --git a/src/yuescript/parser.hpp b/src/yuescript/parser.hpp index f70475f..eed0bff 100644 --- a/src/yuescript/parser.hpp +++ b/src/yuescript/parser.hpp @@ -11,24 +11,22 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #pragma once - -//gcc chokes without rule::rule(const rule &), -//msvc complains when rule::rule(const rule &) is defined. +// gcc chokes without rule::rule(const rule &), +// msvc complains when rule::rule(const rule &) is defined. #ifdef _MSC_VER -#pragma warning (disable: 4521) +#pragma warning(disable : 4521) #endif - -#include -#include -#include -#include #include +#include +#include #include +#include +#include namespace parserlib { -///type of the parser's input. +/// type of the parser's input. typedef std::basic_string input; typedef input::iterator input_it; typedef std::wstring_convert> Converter; @@ -38,7 +36,6 @@ class _expr; class _context; class rule; - struct item_t { input_it begin; input_it end; @@ -46,31 +43,31 @@ struct item_t { }; typedef std::function user_handler; - -///position into the input. +/// position into the input. class pos { public: - ///interator into the input. + /// interator into the input. input::iterator m_it; - ///line. + /// line. int m_line; - ///column. + /// column. int m_col; - ///null constructor. - pos():m_line(0),m_col(0) {} + /// null constructor. + pos() + : m_line(0) + , m_col(0) { } /** constructor from input. @param i input. */ - pos(input &i); + pos(input& i); }; - /** a grammar expression. -*/ + */ class expr { public: /** character terminal constructor. @@ -114,19 +111,19 @@ public: expr operator!() const; private: - //internal expression + // internal expression _expr* m_expr; - //internal constructor from internal expression - expr(_expr* e) : m_expr(e) {} + // internal constructor from internal expression + expr(_expr* e) + : m_expr(e) { } - //assignment not allowed + // assignment not allowed expr& operator=(expr&); friend class _private; }; - /** type of procedure to invoke when a rule is successfully parsed. @param b begin position of input. @param e end position of input. @@ -134,20 +131,19 @@ private: */ typedef void (*parse_proc)(const pos& b, const pos& e, void* d); - -///input range. +/// input range. class input_range { public: - virtual ~input_range() {} + virtual ~input_range() { } - ///begin position. + /// begin position. pos m_begin; - ///end position. + /// end position. pos m_end; - ///empty constructor. - input_range() {} + /// empty constructor. + input_range() { } /** constructor. @param b begin position. @@ -156,24 +152,22 @@ public: input_range(const pos& b, const pos& e); }; - -///enum with error types. +/// enum with error types. enum ERROR_TYPE { - ///syntax error + /// syntax error ERROR_SYNTAX_ERROR = 1, - ///invalid end of file + /// invalid end of file ERROR_INVALID_EOF, - ///first user error + /// first user error ERROR_USER = 100 }; - -///error. +/// error. class error : public input_range { public: - ///type + /// type int m_type; /** constructor. @@ -190,13 +184,11 @@ public: bool operator<(const error& e) const; }; - -///type of error list. +/// type of error list. typedef std::list error_list; - /** represents a rule. -*/ + */ class rule { public: /** character terminal constructor. @@ -227,7 +219,7 @@ public: rule(const rule& r); /** deletes the internal object that represents the expression. - */ + */ ~rule(); /** creates a zero-or-more loop out of this rule. @@ -270,40 +262,40 @@ public: rule& operator=(const expr&); private: - //mode + // mode enum _MODE { _PARSE, _REJECT, _ACCEPT }; - //state + // state struct _state { - //position in source code, relative to start + // position in source code, relative to start size_t m_pos; - //mode + // mode _MODE m_mode; - //constructor - _state(size_t pos = -1, _MODE mode = _PARSE) : - m_pos(pos), m_mode(mode) {} + // constructor + _state(size_t pos = -1, _MODE mode = _PARSE) + : m_pos(pos) + , m_mode(mode) { } }; - //internal expression + // internal expression _expr* m_expr; - //associated parse procedure. + // associated parse procedure. parse_proc m_parse_proc; - //state + // state _state m_state; friend class _private; friend class _context; }; - /** creates a sequence of expressions. @param left left operand. @param right right operand. @@ -311,7 +303,6 @@ private: */ expr operator>>(const expr& left, const expr& right); - /** creates a choice of expressions. @param left left operand. @param right right operand. @@ -319,21 +310,18 @@ expr operator>>(const expr& left, const expr& right); */ expr operator|(const expr& left, const expr& right); - /** converts a parser expression into a terminal. @param e expression. @return an expression which parses a terminal. */ expr term(const expr& e); - /** creates a set expression from a null-terminated string. @param s null-terminated string with characters of the set. @return an expression which parses a single character out of a set. */ expr set(const char* s); - /** creates a range expression. @param min min character. @param max max character. @@ -341,7 +329,6 @@ expr set(const char* s); */ expr range(int min, int max); - /** creates an expression which increments the line counter and resets the column counter when the given expression is parsed successfully; used for newline characters. @@ -350,48 +337,40 @@ expr range(int min, int max); */ expr nl(const expr& e); - /** creates an expression which tests for the end of input. @return an expression that handles the end of input. */ expr eof(); - /** creates a not expression. @param e expression. @return the appropriate expression. */ expr not_(const expr& e); - /** creates an and expression. @param e expression. @return the appropriate expression. */ expr and_(const expr& e); - /** creates an expression that parses any character. @return the appropriate expression. */ expr any(); - /** parsing succeeds without consuming any input. -*/ + */ expr true_(); - /** parsing fails without consuming any input. -*/ + */ expr false_(); - /** parse with target expression and let user handle result. -*/ + */ expr user(const expr& e, const user_handler& handler); - /** parses the given input. The parse procedures of each rule parsed are executed before this function returns, if parsing succeeds. @@ -403,20 +382,19 @@ expr user(const expr& e, const user_handler& handler); */ bool parse(input& i, rule& g, error_list& el, void* d, void* ud); - /** output the specific input range to the specific stream. @param stream stream. @param ir input range. @return the stream. */ -template T& operator<<(T& stream, const input_range& ir) { - for(input::const_iterator it = ir.m_begin.m_it; - it != ir.m_end.m_it; - ++it) { - stream << (typename T::char_type)*it; +template +T& operator<<(T& stream, const input_range& ir) { + for (input::const_iterator it = ir.m_begin.m_it; + it != ir.m_end.m_it; + ++it) { + stream << (typename T::char_type) * it; } return stream; } - -} //namespace parserlib +} // namespace parserlib diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 620dfcc..386fa28 100755 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -14,18 +14,16 @@ namespace parserlib { using namespace std::string_view_literals; #define AST_LEAF(type) \ -COUNTER_INC; \ -class type##_t : public ast_node \ -{ \ -public: \ - virtual int getId() const override { return COUNTER_READ; } + COUNTER_INC; \ + class type##_t : public ast_node { \ + public: \ + virtual int getId() const override { return COUNTER_READ; } #define AST_NODE(type) \ -COUNTER_INC; \ -class type##_t : public ast_container \ -{ \ -public: \ - virtual int getId() const override { return COUNTER_READ; } + COUNTER_INC; \ + class type##_t : public ast_container { \ + public: \ + virtual int getId() const override { return COUNTER_READ; } #define AST_MEMBER(type, ...) \ type##_t() { \ @@ -34,8 +32,12 @@ public: \ #define AST_END(type, name) \ virtual const std::string_view getName() const override { return name; } \ -}; \ -template<> constexpr int id() { return COUNTER_READ; } + } \ + ; \ + template <> \ + constexpr int id() { return COUNTER_READ; } + +// clang-format off AST_LEAF(Num) AST_END(Num, "num"sv) @@ -849,4 +851,6 @@ AST_NODE(File) AST_MEMBER(File, &block) AST_END(File, "file"sv) +// clang-format on + } // namespace parserlib diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index b730a9f..17979ab 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -6,23 +6,23 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ +#include +#include +#include +#include #include -#include #include -#include +#include #include -#include -#include -#include -#include "yuescript/yue_parser.h" #include "yuescript/yue_compiler.h" +#include "yuescript/yue_parser.h" #ifndef YUE_NO_MACRO extern "C" { -#include "lua.h" #include "lauxlib.h" +#include "lua.h" #include "lualib.h" } // extern "C" @@ -30,9 +30,9 @@ extern "C" { #define YUE_MODULE "__yue_modules__" #if LUA_VERSION_NUM > 501 - #ifndef LUA_COMPAT_5_1 - #define lua_objlen lua_rawlen - #endif // LUA_COMPAT_5_1 +#ifndef LUA_COMPAT_5_1 +#define lua_objlen lua_rawlen +#endif // LUA_COMPAT_5_1 #endif // LUA_VERSION_NUM #endif // YUE_NO_MACRO @@ -40,17 +40,22 @@ extern "C" { namespace yue { #define BLOCK_START do { -#define BLOCK_END } while (false); -#define BREAK_IF(cond) if (cond) break - -#define _DEFER(code,line) std::shared_ptr _defer_##line(nullptr, [&](auto){code;}) -#define DEFER(code) _DEFER(code,__LINE__) -#define YUEE(msg,node) throw std::logic_error( \ +#define BLOCK_END \ + } \ + while (false) \ + ; +#define BREAK_IF(cond) \ + if (cond) break + +#define _DEFER(code, line) std::shared_ptr _defer_##line(nullptr, [&](auto) { code; }) +#define DEFER(code) _DEFER(code, __LINE__) +#define YUEE(msg, node) throw std::logic_error( \ _info.errorMessage( \ "[File] "s + __FILE__ \ - + ",\n[Func] "s + __FUNCTION__ \ - + ",\n[Line] "s + std::to_string(__LINE__) \ - + ",\n[Error] "s + msg, node)) + + ",\n[Func] "s + __FUNCTION__ \ + + ",\n[Line] "s + std::to_string(__LINE__) \ + + ",\n[Error] "s + msg, \ + node)) typedef std::list str_list; @@ -62,9 +67,9 @@ public: #ifndef YUE_NO_MACRO YueCompilerImpl(lua_State* sharedState, const std::function& luaOpen, - bool sameModule): - L(sharedState), - _luaOpen(luaOpen) { + bool sameModule) + : L(sharedState) + , _luaOpen(luaOpen) { BLOCK_START BREAK_IF(!sameModule); BREAK_IF(!L); @@ -153,8 +158,7 @@ public: std::tie(line, col) = var.second; globals->push_back({var.first, line, col}); } - std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB) - { + std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB) { if (varA.line < varB.line) { return true; } else if (varA.line == varB.line) { @@ -222,6 +226,7 @@ public: } #endif // YUE_NO_MACRO } + private: #ifndef YUE_NO_MACRO bool _stateOwner = false; @@ -234,22 +239,20 @@ private: YueParser _parser; ParseInfo _info; int _indentOffset = 0; - struct VarArgState - { + struct VarArgState { bool hasVar; bool usedVar; }; std::stack _varArgs; std::stack _enableReturn; std::stack _withVars; - struct ContinueVar - { + struct ContinueVar { std::string var; ast_ptr condAssign; }; std::stack _continueVars; std::list> _codeCache; - std::unordered_map> _globals; + std::unordered_map> _globals; std::ostringstream _buf; std::ostringstream _joinBuf; const std::string _newLine = "\n"; @@ -271,7 +274,7 @@ private: }; struct Scope { GlobalMode mode = GlobalMode::None; - std::unique_ptr> vars; + std::unique_ptr> vars; std::unique_ptr> allows; std::unique_ptr> globals; }; @@ -312,7 +315,7 @@ private: void pushScope() { _scopes.emplace_back(); - _scopes.back().vars = std::make_unique>(); + _scopes.back().vars = std::make_unique>(); } void popScope() { @@ -377,7 +380,7 @@ private: bool isConst(const std::string& name) const { bool isConst = false; decltype(_scopes.back().allows.get()) allows = nullptr; - for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { + for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { if (it->allows) allows = it->allows.get(); } bool checkShadowScopeOnly = false; @@ -524,8 +527,10 @@ private: } std::string join(const str_list& items) { - if (items.empty()) return Empty; - else if (items.size() == 1) return items.front(); + if (items.empty()) + return Empty; + else if (items.size() == 1) + return items.front(); for (const auto& item : items) { _joinBuf << item; } @@ -536,8 +541,10 @@ private: } std::string join(const str_list& items, std::string_view sep) { - if (items.empty()) return Empty; - else if (items.size() == 1) return items.front(); + if (items.empty()) + return Empty; + else if (items.size() == 1) + return items.front(); std::string sepStr = std::string(sep); auto begin = ++items.begin(); _joinBuf << items.front(); @@ -659,7 +666,8 @@ private: Statement_t* lastStatementFrom(const node_container& stmts) const { if (!stmts.empty()) { - auto it = stmts.end(); --it; + auto it = stmts.end(); + --it; while (!static_cast(*it)->content && it != stmts.begin()) { --it; } @@ -864,15 +872,17 @@ private: case id(): return true; } - } else switch (firstItem->getId()) { - case id(): - case id(): - return true; - } + } else + switch (firstItem->getId()) { + case id(): + case id(): + return true; + } } else { if (std::find_if(chainItems.begin(), chainItems.end(), [](ast_node* node) { - return ast_is(node); - }) != chainItems.end()) { + return ast_is(node); + }) + != chainItems.end()) { return false; } auto lastItem = chainItems.back(); @@ -1155,7 +1165,7 @@ private: break; } } - } else if (expList->exprs.size() == 1){ + } else if (expList->exprs.size() == 1) { auto exp = static_cast(expList->exprs.back()); if (isPureBackcall(exp)) { transformExp(exp, out, ExpUsage::Common); @@ -1277,7 +1287,7 @@ private: } } } - if (!defs.empty()) _buf << indent() << "local "sv << join(defs,", "sv); + if (!defs.empty()) _buf << indent() << "local "sv << join(defs, ", "sv); } } return clearBuf(); @@ -1392,15 +1402,15 @@ private: auto j = values.begin(); auto je = --values.end(); while (j != je) { - ++i; ++j; + ++i; + ++j; } bool holdItem = false; for (auto it = i; it != exprs.end(); ++it) { BLOCK_START auto value = singleValueFrom(*it); BREAK_IF(!value); - if (value->item.is() || - value->getByPath()) { + if (value->item.is() || value->getByPath()) { holdItem = true; break; } @@ -1524,7 +1534,8 @@ private: popScope(); temp.push_back(indent() + "end"s + nlr(x)); } - } else break; + } else + break; auto newExpList = x->new_ptr(); auto newAssign = x->new_ptr(); auto newAssignment = x->new_ptr(); @@ -1946,13 +1957,12 @@ private: switch (pair->getId()) { case id(): { ++index; - if (!isAssignable(static_cast(pair))) { + if (!isAssignable(static_cast(pair))) { throw std::logic_error(_info.errorMessage("can't destructure value"sv, pair)); } auto value = singleValueFrom(pair); auto item = value->item.get(); - if (ast_is(item) || - item->getByPath()) { + if (ast_is(item) || item->getByPath()) { auto subPairs = destructFromExp(pair, optional); for (auto& p : subPairs) { pairs.push_back({p.target, p.targetVar, @@ -1963,12 +1973,10 @@ private: auto exp = static_cast(pair); auto varName = singleVariableFrom(exp, false); if (varName == "_"sv) break; - pairs.push_back({ - exp, + pairs.push_back({exp, varName, '[' + std::to_string(index) + ']', - nullptr - }); + nullptr}); } break; } @@ -1994,37 +2002,30 @@ private: if (auto exp = np->value.as()) { if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp)); auto item = singleValueFrom(exp)->item.get(); - if (ast_is(item) || - item->getByPath()) { + if (ast_is(item) || item->getByPath()) { auto subPairs = destructFromExp(exp, optional); for (auto& p : subPairs) { - pairs.push_back({ - p.target, + pairs.push_back({p.target, p.targetVar, keyName + sep + p.structure, - p.defVal - }); + p.defVal}); } } else { auto varName = singleVariableFrom(exp, false); - pairs.push_back({ - exp, + pairs.push_back({exp, varName, keyName, - nullptr - }); + nullptr}); } break; } if (np->value.is()) { auto subPairs = destructFromExp(np->value, optional); for (auto& p : subPairs) { - pairs.push_back({ - p.target, + pairs.push_back({p.target, p.targetVar, keyName + sep + p.structure, - p.defVal - }); + p.defVal}); } } break; @@ -2034,16 +2035,14 @@ private: ++index; auto subPairs = destructFromExp(tb, optional); for (auto& p : subPairs) { - pairs.push_back({ - p.target, + pairs.push_back({p.target, p.targetVar, '[' + std::to_string(index) + ']' + sep + p.structure, - p.defVal - }); + p.defVal}); } break; } - case id() : { + case id(): { auto dp = static_cast(pair); if (auto exp = dp->key.as()) { ++index; @@ -2052,17 +2051,14 @@ private: } auto value = singleValueFrom(exp); auto item = value->item.get(); - if (ast_is(item) || - item->getByPath()) { + if (ast_is(item) || item->getByPath()) { throw std::logic_error(_info.errorMessage("invalid use of default value"sv, dp->defVal)); } else { auto varName = singleVariableFrom(exp, false); - pairs.push_back({ - exp, + pairs.push_back({exp, varName, '[' + std::to_string(index) + ']', - dp->defVal - }); + dp->defVal}); } break; } @@ -2081,19 +2077,15 @@ private: if (auto exp = dp->value.get()) { if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp)); auto varName = singleVariableFrom(exp, false); - pairs.push_back({ - exp, + pairs.push_back({exp, varName, keyName, - dp->defVal - }); + dp->defVal}); } else { - pairs.push_back({ - toAst(valueStr, dp).get(), + pairs.push_back({toAst(valueStr, dp).get(), valueStr, keyName, - dp->defVal - }); + dp->defVal}); } break; } @@ -2157,12 +2149,10 @@ private: value->item.set(simpleValue); auto subPairs = destructFromExp(newExp(value, subMetaDestruct), optional); for (const auto& p : subPairs) { - pairs.push_back({ - p.target, + pairs.push_back({p.target, p.targetVar, ".#"s + (p.structure.empty() ? Empty : sep + p.structure), - p.defVal - }); + p.defVal}); } } return pairs; @@ -2373,8 +2363,7 @@ private: if (!leftValue) throw std::logic_error(_info.errorMessage("left hand expression is not assignable"sv, leftExp)); auto chain = leftValue->item.as(); if (!chain) throw std::logic_error(_info.errorMessage("left hand expression is not assignable"sv, leftValue)); - BLOCK_START - { + BLOCK_START { auto dot = ast_cast(chain->items.back()); if (dot && dot->name.is()) { throw std::logic_error(_info.errorMessage("can not apply update to a metatable"sv, leftExp)); @@ -2457,10 +2446,11 @@ private: right = '(' + right + ')'; } _buf << join(temp); - if (!defs.empty()) _buf << defs; - else _buf << indent() << left; - _buf << " = "sv << left << - ' ' << op << ' ' << right << nll(assignment); + if (!defs.empty()) + _buf << defs; + else + _buf << indent() << left; + _buf << " = "sv << left << ' ' << op << ' ' << right << nll(assignment); out.push_back(clearBuf()); break; } @@ -2709,7 +2699,8 @@ private: } else { auto x = values.front(); auto arg = newExp(static_cast(x), x); - auto begin = values.begin(); begin++; + auto begin = values.begin(); + begin++; for (auto it = begin; it != values.end(); ++it) { auto unary = static_cast(*it); auto value = static_cast(singleUnaryExpFrom(unary) ? unary->expos.back() : nullptr); @@ -2943,7 +2934,7 @@ private: } } - void transformCallable(Callable_t* callable, str_list& out, const ast_sel& invoke = {}) { + void transformCallable(Callable_t* callable, str_list& out, const ast_sel& invoke = {}) { auto item = callable->item.get(); switch (item->getId()) { case id(): { @@ -3042,9 +3033,7 @@ private: temp.push_back(Empty); } auto& bodyCodes = temp.back(); - _buf << "function("sv << - (isFatArrow ? "self"s : Empty) << - ')'; + _buf << "function("sv << (isFatArrow ? "self"s : Empty) << ')'; if (!bodyCodes.empty()) { _buf << nll(funLit) << bodyCodes; popScope(); @@ -3077,7 +3066,7 @@ private: } const auto& nodes = block->statements.objects(); LocalMode mode = LocalMode::None; - Local_t* any = nullptr, *capital = nullptr; + Local_t *any = nullptr, *capital = nullptr; for (auto it = nodes.begin(); it != nodes.end(); ++it) { auto node = *it; auto stmt = static_cast(node); @@ -3086,7 +3075,8 @@ private: bool cond = false; BLOCK_START BREAK_IF(it == nodes.begin()); - auto last = it; --last; + auto last = it; + --last; auto lst = static_cast(*last); if (lst->appendix) { throw std::logic_error(_info.errorMessage("statement decorator must be placed at the end of pipe chain"sv, lst->appendix.get())); @@ -3104,7 +3094,8 @@ private: BLOCK_END if (!cond) throw std::logic_error(_info.errorMessage("pipe chain must be following a value"sv, x)); stmt->content.set(nullptr); - auto next = it; ++next; + auto next = it; + ++next; BLOCK_START BREAK_IF(next == nodes.end()); BREAK_IF(!static_cast(*next)->content.as()); @@ -3122,7 +3113,8 @@ private: ast_ptr arg; { auto block = x->new_ptr(); - auto next = it; ++next; + auto next = it; + ++next; if (next != nodes.end()) { for (auto i = next; i != nodes.end(); ++i) { block->statements.push_back(*i); @@ -3240,7 +3232,8 @@ private: for (const auto& destruct : info.destructures) for (const auto& item : destruct.items) if (!item.targetVar.empty()) { - if (std::isupper(item.targetVar[0]) && capital) { capital->decls.push_back(item.targetVar); + if (std::isupper(item.targetVar[0]) && capital) { + capital->decls.push_back(item.targetVar); } else if (any) { any->decls.push_back(item.targetVar); } @@ -3249,7 +3242,8 @@ private: if (info.assignment) { auto defs = transformAssignDefs(info.assignment->expList, DefOp::Get); for (const auto& def : defs) { - if (std::isupper(def[0]) && capital) { capital->decls.push_back(def); + if (std::isupper(def[0]) && capital) { + capital->decls.push_back(def); } else if (any) { any->decls.push_back(def); } @@ -3295,9 +3289,7 @@ private: BREAK_IF(!last); auto x = last; auto expList = expListFrom(last); - BREAK_IF(!expList || - (last->appendix && - last->appendix->item.is())); + BREAK_IF(!expList || (last->appendix && last->appendix->item.is())); auto expListLow = x->new_ptr(); expListLow->exprs.dup(expList->exprs); auto returnNode = x->new_ptr(); @@ -3764,14 +3756,18 @@ private: _buf << indent() << "end"sv << nll(def); temp.back() = clearBuf(); } - if (varNames.empty()) varNames = arg.name; - else varNames.append(", "s + arg.name); + if (varNames.empty()) + varNames = arg.name; + else + varNames.append(", "s + arg.name); } if (argDefList->varArg) { auto& arg = argItems.emplace_back(); arg.name = "..."sv; - if (varNames.empty()) varNames = arg.name; - else varNames.append(", "s + arg.name); + if (varNames.empty()) + varNames = arg.name; + else + varNames.append(", "s + arg.name); _varArgs.top().hasVar = true; } if (assignSelf) { @@ -3790,7 +3786,7 @@ private: out.push_back(join(temp)); } - void transformSelfName(SelfName_t* selfName, str_list& out, const ast_sel& invoke = {}) { + void transformSelfName(SelfName_t* selfName, str_list& out, const ast_sel& invoke = {}) { auto x = selfName; auto name = selfName->name.get(); switch (name->getId()) { @@ -3899,7 +3895,7 @@ private: pushScope(); } auto partOne = x->new_ptr(); - for (auto it = chainList.begin();it != opIt;++it) { + for (auto it = chainList.begin(); it != opIt; ++it) { partOne->items.push_back(*it); } BLOCK_START @@ -3972,7 +3968,8 @@ private: partOne->items.clear(); partOne->items.push_back(toAst(objVar, x)); partOne->items.push_back(dotItem); - auto it = opIt; ++it; + auto it = opIt; + ++it; if (it != chainList.end() && ast_is(*it)) { if (auto invoke = ast_cast(*it)) { @@ -3999,7 +3996,7 @@ private: pushScope(); auto partTwo = x->new_ptr(); partTwo->items.push_back(toAst(objVar, x)); - for (auto it = ++opIt;it != chainList.end();++it) { + for (auto it = ++opIt; it != chainList.end(); ++it) { partTwo->items.push_back(*it); } switch (usage) { @@ -4248,7 +4245,8 @@ private: case id(): { auto colonItem = static_cast(item); auto current = it; - auto next = current; ++next; + auto next = current; + ++next; auto followItem = next != chainList.end() ? *next : nullptr; if (current != chainList.begin()) { --current; @@ -4272,7 +4270,7 @@ private: switch (chainList.front()->getId()) { case id(): case id(): - chainValue->items.push_back(toAst(_withVars.top(), x)); + chainValue->items.push_back(toAst(_withVars.top(), x)); break; } for (auto i = chainList.begin(); i != current; ++i) { @@ -4366,7 +4364,8 @@ private: transformSlice(static_cast(item), temp); break; case id(): { - auto next = it; ++next; + auto next = it; + ++next; auto followItem = next != chainList.end() ? *next : nullptr; ast_sel invoke; if (ast_is(followItem)) { @@ -4464,7 +4463,7 @@ private: return Empty; } - std::tuple expandMacroStr(ChainValue_t* chainValue) { + std::tuple expandMacroStr(ChainValue_t* chainValue) { const auto& chainList = chainValue->items.objects(); auto x = ast_to(chainList.front())->item.to(); auto macroName = _parser.toString(x->name); @@ -4490,7 +4489,7 @@ private: auto item = *(++chainList.begin()); if (auto invoke = ast_cast(item)) { args = &invoke->args.objects(); - } else if (auto invoke = ast_cast(item)){ + } else if (auto invoke = ast_cast(item)) { args = &invoke->args.objects(); } } @@ -4599,7 +4598,7 @@ private: return {type, codes, std::move(localVars)}; } - std::tuple, std::unique_ptr, std::string, str_list> expandMacro(ChainValue_t* chainValue, ExpUsage usage, bool allowBlockMacroReturn) { + std::tuple, std::unique_ptr, std::string, str_list> expandMacro(ChainValue_t* chainValue, ExpUsage usage, bool allowBlockMacroReturn) { auto x = ast_to(chainValue->items.front())->item.to(); const auto& chainList = chainValue->items.objects(); std::string type, codes; @@ -4928,10 +4927,10 @@ private: forceAddToScope(indexVar); temp.push_back(indent() + "local "s + indexVar + " = 1"s + nll(item)); _buf << "for "sv << keyVar << ',' << valueVar << " in pairs "sv << objVar - << "\n\tif "sv << indexVar << "=="sv << keyVar - << "\n\t\t"sv << tableVar << "[]="sv << valueVar - << "\n\t\t"sv << indexVar << "+=1"sv - << "\n\telse "sv << tableVar << '[' << keyVar << "]="sv << valueVar; + << "\n\tif "sv << indexVar << "=="sv << keyVar + << "\n\t\t"sv << tableVar << "[]="sv << valueVar + << "\n\t\t"sv << indexVar << "+=1"sv + << "\n\telse "sv << tableVar << '[' << keyVar << "]="sv << valueVar; auto forEach = toAst(clearBuf(), item); transformForEach(forEach, temp); break; @@ -4998,7 +4997,7 @@ private: auto chainValue = value->item.as(); BREAK_IF(!chainValue); BREAK_IF(chainValue->items.size() != 1); - BREAK_IF((!chainValue->getByPath())); + BREAK_IF((!chainValue->getByPath())); auto indexVar = getUnusedName("_index_"); _buf << "for "sv << indexVar << "=1,select '#',...\n\t"sv << tableVar << "[]= select "sv << indexVar << ",..."sv; transformFor(toAst(clearBuf(), item), temp); @@ -5098,8 +5097,7 @@ private: transformAssignment(assignment, temp); popScope(); out.push_back(join(temp)); - out.back() = indent() + "do"s + nll(x) + - out.back() + indent() + "end"s + nlr(x); + out.back() = indent() + "do"s + nll(x) + out.back() + indent() + "end"s + nlr(x); break; } case ExpUsage::Return: @@ -5205,14 +5203,15 @@ private: str_list tmp; if (!metatable->pairs.empty()) { transform_simple_table(metatable, tmp); - } else switch (metatableItem->getId()) { - case id(): - transformExp(static_cast(metatableItem.get()), tmp, ExpUsage::Closure); - break; - case id(): - transformTableBlock(static_cast(metatableItem.get()), tmp); - break; - } + } else + switch (metatableItem->getId()) { + case id(): + transformExp(static_cast(metatableItem.get()), tmp, ExpUsage::Closure); + break; + case id(): + transformTableBlock(static_cast(metatableItem.get()), tmp); + break; + } tabStr += tmp.back(); tabStr += ')'; out.push_back(tabStr); @@ -5349,9 +5348,7 @@ private: assignment->action.set(assign); transformAssignment(assignment, temp); popScope(); - out.back() = indent() + "do"s + nll(comp) + - out.back() + temp.back() + - indent() + "end"s + nlr(comp); + out.back() = indent() + "do"s + nll(comp) + out.back() + temp.back() + indent() + "end"s + nlr(comp); break; } case ExpUsage::Return: @@ -5529,7 +5526,7 @@ private: } void transformInvokeArgs(InvokeArgs_t* invokeArgs, str_list& out) { - if (invokeArgs->args.size() > 1) { + if (invokeArgs->args.size() > 1) { /* merge all the key-value pairs into one table from arguments in the end */ auto lastArg = invokeArgs->args.back(); @@ -5614,18 +5611,18 @@ private: return traversal::Stop == body->traverse([&](ast_node* node) { if (auto stmt = ast_cast(node)) { if (stmt->content.is()) { - return _parser.toString(stmt->content) == "continue"sv ? - traversal::Stop : traversal::Return; + return _parser.toString(stmt->content) == "continue"sv ? traversal::Stop : traversal::Return; } else if (expListFrom(stmt)) { return traversal::Continue; } return traversal::Return; - } else switch (node->getId()) { - case id(): - case id(): - case id(): - return traversal::Return; - } + } else + switch (node->getId()) { + case id(): + case id(): + case id(): + return traversal::Return; + } return traversal::Continue; }); } @@ -5781,7 +5778,7 @@ private: out.push_back(join(temp)); } - void transformForInPlace(For_t* forNode, str_list& out, ExpList_t* assignExpList = nullptr) { + void transformForInPlace(For_t* forNode, str_list& out, ExpList_t* assignExpList = nullptr) { auto x = forNode; str_list temp; if (assignExpList) { @@ -5807,11 +5804,7 @@ private: } void checkOperatorAvailable(const std::string& op, ast_node* node) { - if (op == "&"sv || - op == "~"sv || - op == "|"sv || - op == ">>"sv || - op == "<<"sv) { + if (op == "&"sv || op == "~"sv || op == "|"sv || op == ">>"sv || op == "<<"sv) { if (getLuaTarget(node) < 503) { throw std::logic_error(_info.errorMessage("bitwise operator is not available when not targeting Lua version 5.3 or higher"sv, node)); } @@ -5874,7 +5867,7 @@ private: out.push_back(join(temp)); } - void transformForEachInPlace(ForEach_t* forEach, str_list& out, ExpList_t* assignExpList = nullptr) { + void transformForEachInPlace(ForEach_t* forEach, str_list& out, ExpList_t* assignExpList = nullptr) { auto x = forEach; str_list temp; if (assignExpList) { @@ -5929,10 +5922,12 @@ private: transformDoubleString(static_cast(key), temp); temp.back() = '[' + temp.back() + ']'; break; - case id(): transformSingleString(static_cast(key), temp); + case id(): + transformSingleString(static_cast(key), temp); temp.back() = '[' + temp.back() + ']'; break; - case id(): transformLuaString(static_cast(key), temp); + case id(): + transformLuaString(static_cast(key), temp); temp.back() = "[ "s + temp.back() + ']'; break; default: YUEE("AST node mismatch", key); break; @@ -6012,7 +6007,7 @@ private: } } - std::pair defineClassVariable(Assignable_t* assignable) { + std::pair defineClassVariable(Assignable_t* assignable) { if (auto variable = assignable->item.as()) { auto name = _parser.toString(variable); if (addToScope(name)) { @@ -6150,7 +6145,8 @@ private: case id(): { size_t inc = transform_class_member_list(static_cast(content), members, classVar); auto it = members.end(); - for (size_t i = 0; i < inc; ++i, --it); + for (size_t i = 0; i < inc; ++i, --it) + ; for (; it != members.end(); ++it) { auto& member = *it; if (member.type == MemType::Property) { @@ -6164,7 +6160,7 @@ private: case id(): transformStatement(static_cast(content), statements); break; - default:YUEE("AST node mismatch", content); break; + default: YUEE("AST node mismatch", content); break; } } for (auto& member : members) { @@ -6667,9 +6663,7 @@ private: } for (auto _exp : expList->exprs.objects()) { auto exp = static_cast(_exp); - if (!variableFrom(exp) && - !exp->getByPath() && - !exp->getByPath()) { + if (!variableFrom(exp) && !exp->getByPath() && !exp->getByPath()) { throw std::logic_error(_info.errorMessage("left hand expressions must be variables in export statement"sv, x)); } } @@ -7068,7 +7062,7 @@ private: if (ast_is(import->target)) { x = import->target.get(); bool importAllMacro = import->target.is(); - std::list> macroPairs; + std::list> macroPairs; auto newTab = x->new_ptr(); if (auto tabLit = import->target.as()) { for (auto item : tabLit->items.objects()) { @@ -7369,8 +7363,7 @@ private: for (auto branch_ : branches) { auto branch = static_cast(branch_); if (auto value = singleValueFrom(branch->valueList); - value && (value->item.is() || - value->getByPath())) { + value && (value->item.is() || value->getByPath())) { if (!firstBranch) { temp.push_back(indent() + "else"s + nll(branch)); pushScope(); @@ -7440,8 +7433,7 @@ private: if (!singleValueFrom(exp)) { tmp.back() = '(' + tmp.back() + ')'; } - temp.back().append(' ' + tmp.back() + " == "s + - (exp == exprs.back() ? objVar : objVar + " or"s)); + temp.back().append(' ' + tmp.back() + " == "s + (exp == exprs.back() ? objVar : objVar + " or"s)); } temp.back().append(" then"s + nll(branch)); pushScope(); @@ -7674,22 +7666,23 @@ const std::string YueCompilerImpl::Empty; YueCompiler::YueCompiler(void* sharedState, const std::function& luaOpen, - bool sameModule): + bool sameModule) + : #ifndef YUE_NO_MACRO -_compiler(std::make_unique(static_cast(sharedState), luaOpen, sameModule)) {} + _compiler(std::make_unique(static_cast(sharedState), luaOpen, sameModule)) { +} #else -_compiler(std::make_unique()) { + _compiler(std::make_unique()) { (void)sharedState; (void)luaOpen; (void)sameModule; } #endif // YUE_NO_MACRO -YueCompiler::~YueCompiler() {} +YueCompiler::~YueCompiler() { } CompileInfo YueCompiler::compile(std::string_view codes, const YueConfig& config) { return _compiler->compile(codes, config); } } // namespace yue - diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index 3df5451..9ca641a 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -8,14 +8,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #pragma once +#include +#include +#include #include #include #include -#include -#include -#include #include -#include +#include namespace yue { @@ -59,6 +59,7 @@ public: bool sameModule = false); virtual ~YueCompiler(); CompileInfo compile(std::string_view codes, const YueConfig& config = {}); + private: std::unique_ptr _compiler; }; diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 31a2560..a22bd3a 100755 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -17,8 +17,7 @@ std::unordered_set LuaKeywords = { "end"s, "false"s, "for"s, "function"s, "goto"s, "if"s, "in"s, "local"s, "nil"s, "not"s, "or"s, "repeat"s, "return"s, "then"s, "true"s, - "until"s, "while"s -}; + "until"s, "while"s}; std::unordered_set Keywords = { "and"s, "break"s, "do"s, "else"s, "elseif"s, @@ -31,6 +30,7 @@ std::unordered_set Keywords = { "try"s, "unless"s, "using"s, "when"s, "with"s // Yue keywords }; +// clang-format off YueParser::YueParser() { plain_space = *set(" \t"); Break = nl(-expr('\r') >> '\n'); @@ -55,14 +55,14 @@ YueParser::YueParser() { +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> -(-set("uU") >> set("lL") >> set("lL")) ) | ( - +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") - ) | ( - ( - +range('0', '9') >> -('.' >> +range('0', '9')) - ) | ( - '.' >> +range('0', '9') + +range('0', '9') >> ( + '.' >> +range('0', '9') >> -(set("eE") >> -expr('-') >> +range('0', '9')) | + -set("uU") >> set("lL") >> set("lL") | + true_() ) - ) >> -(set("eE") >> -expr('-') >> +range('0', '9')); + ) | ( + '.' >> +range('0', '9') >> -(set("eE") >> -expr('-') >> +range('0', '9')) + ); Cut = false_(); Seperator = true_(); @@ -652,6 +652,7 @@ YueParser::YueParser() { BlockEnd = Block >> -(+Break >> Space >> and_(Stop)) >> Stop; File = White >> -Shebang >> -Block >> White >> eof(); } +// clang-format on ParseInfo YueParser::parse(std::string_view codes, rule& r) { ParseInfo res; @@ -710,20 +711,20 @@ std::string YueParser::decode(const input& codes) { } namespace Utils { - void replace(std::string& str, std::string_view from, std::string_view to) { - size_t start_pos = 0; - while((start_pos = str.find(from, start_pos)) != std::string::npos) { - str.replace(start_pos, from.size(), to); - start_pos += to.size(); - } +void replace(std::string& str, std::string_view from, std::string_view to) { + size_t start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.size(), to); + start_pos += to.size(); } +} - void trim(std::string& str) { - if (str.empty()) return; - str.erase(0, str.find_first_not_of(" \t\r\n")); - str.erase(str.find_last_not_of(" \t\r\n") + 1); - } +void trim(std::string& str) { + if (str.empty()) return; + str.erase(0, str.find_first_not_of(" \t\r\n")); + str.erase(str.find_last_not_of(" \t\r\n") + 1); } +} // namespace Utils std::string ParseInfo::errorMessage(std::string_view msg, const input_range* loc) const { const int ASCII = 255; @@ -753,13 +754,14 @@ std::string ParseInfo::errorMessage(std::string_view msg, const input_range* loc } auto line = Converter{}.to_bytes(std::wstring(begin, end)); while (col < static_cast(line.size()) - && (line[col] == ' ' || line[col] == '\t')) { + && (line[col] == ' ' || line[col] == '\t')) { col++; } Utils::replace(line, "\t"sv, " "sv); std::ostringstream buf; - buf << loc->m_begin.m_line << ": "sv << msg << - '\n' << line << '\n' << std::string(col, ' ') << "^"sv; + buf << loc->m_begin.m_line << ": "sv << msg << '\n' + << line << '\n' + << std::string(col, ' ') << "^"sv; return buf.str(); } diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index b363ad7..3ee219e 100755 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -8,14 +8,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #pragma once -#include -#include -#include #include #include +#include #include +#include +#include #include -#include +#include #include "yuescript/ast.hpp" #include "yuescript/yue_ast.h" @@ -35,7 +35,7 @@ struct ParseInfo { std::string errorMessage(std::string_view msg, const input_range* loc) const; }; -template +template struct identity { typedef T type; }; #define AST_RULE(type) \ @@ -50,7 +50,7 @@ class YueParser { public: YueParser(); - template + template ParseInfo parse(std::string_view codes) { return parse(codes, getRule()); } @@ -338,8 +338,8 @@ private: }; namespace Utils { - void replace(std::string& str, std::string_view from, std::string_view to); - void trim(std::string& str); -}; +void replace(std::string& str, std::string_view from, std::string_view to); +void trim(std::string& str); +} // namespace Utils } // namespace yue diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 98e214f..08978e5 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -19,11 +19,12 @@ using namespace std::string_literals; extern "C" { -#include "lua.h" #include "lauxlib.h" +#include "lua.h" static const char yuescriptCodes[] = #include "yuescript/yuescript.h" + ; static void init_yuescript(lua_State* L) { if (luaL_loadbuffer(L, yuescriptCodes, sizeof(yuescriptCodes) / sizeof(yuescriptCodes[0]) - 1, "=(yuescript)") != 0) { @@ -40,6 +41,7 @@ static void init_yuescript(lua_State* L) { static const char stpCodes[] = #include "yuescript/stacktraceplus.h" + ; static int init_stacktraceplus(lua_State* L) { if (luaL_loadbuffer(L, stpCodes, sizeof(stpCodes) / sizeof(stpCodes[0]) - 1, "=(stacktraceplus)") != 0) { @@ -218,8 +220,7 @@ static const luaL_Reg yuelib[] = { {"version", nullptr}, {"options", nullptr}, {"load_stacktraceplus", nullptr}, - {nullptr, nullptr} -}; + {nullptr, nullptr}}; YUE_API int luaopen_yue(lua_State* L) { #if LUA_VERSION_NUM > 501 @@ -243,4 +244,3 @@ YUE_API int luaopen_yue(lua_State* L) { } } // extern "C" - -- cgit v1.2.3-55-g6feb