From 209fca096b0be5c47553f4c4a94d95de33b23d31 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 11 Nov 2022 09:29:01 +0800 Subject: refactor parser to be a little faster. --- src/yue.cpp | 13 +- src/yuescript/ast.hpp | 3 +- src/yuescript/yue_ast.h | 55 +++--- src/yuescript/yue_compiler.cpp | 402 ++++++++++++++++++++++++++--------------- src/yuescript/yue_compiler.h | 3 + src/yuescript/yue_parser.cpp | 27 +-- src/yuescript/yue_parser.h | 7 +- 7 files changed, 318 insertions(+), 192 deletions(-) (limited to 'src') diff --git a/src/yue.cpp b/src/yue.cpp index f25c702..5b4dccc 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -9,7 +9,6 @@ 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 @@ -472,19 +471,13 @@ int main(int narg, const char** args) { } } if (dumpCompileTime) { - auto start = std::chrono::high_resolution_clock::now(); + conf.profiling = true; auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); - auto end = std::chrono::high_resolution_clock::now(); if (!result.codes.empty()) { - std::chrono::duration diff = end - start; - start = std::chrono::high_resolution_clock::now(); - yue::YueParser{}.parse(s); - end = std::chrono::high_resolution_clock::now(); - std::chrono::duration parseDiff = end - start; std::ostringstream buf; buf << file.first << " \n"sv; - buf << "Parse time: "sv << std::setprecision(5) << parseDiff.count() * 1000 << " ms\n"; - buf << "Compile time: "sv << std::setprecision(5) << (diff.count() - parseDiff.count()) * 1000 << " ms\n\n"; + buf << "Parse time: "sv << std::setprecision(5) << result.parseTime * 1000 << " ms\n"; + buf << "Compile time: "sv << std::setprecision(5) << result.compileTime * 1000 << " ms\n\n"; return std::tuple{0, file.first, buf.str()}; } else { std::ostringstream buf; diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index 681c27f..3443c7f 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp @@ -455,8 +455,7 @@ public: void dup(const _ast_list& src) { for (ast_node* obj : src.m_objects) { - m_objects.push_back(obj); - obj->retain(); + push_back(obj); } } diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 21266c9..32dd8ba 100755 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -459,6 +459,12 @@ AST_NODE(variable_pair) AST_MEMBER(variable_pair, &name) AST_END(variable_pair, "variable_pair"sv) +AST_NODE(variable_pair_def) + ast_ptr pair; + ast_ptr defVal; + AST_MEMBER(variable_pair_def, &pair, &defVal) +AST_END(variable_pair_def, "variable_pair_def"sv) + class String_t; AST_NODE(normal_pair) @@ -467,32 +473,41 @@ AST_NODE(normal_pair) AST_MEMBER(normal_pair, &key, &value) AST_END(normal_pair, "normal_pair"sv) -AST_NODE(default_pair) - ast_sel key; +AST_NODE(normal_pair_def) + ast_ptr pair; + ast_ptr defVal; + AST_MEMBER(normal_pair_def, &pair, &defVal) +AST_END(normal_pair_def, "normal_pair_def"sv) + +AST_NODE(normal_def) + ast_ptr item; ast_ptr sep; - ast_ptr value; - ast_ptr defVal; - AST_MEMBER(default_pair, &key, &sep, &value, &defVal) -AST_END(default_pair, "default_pair"sv) + ast_ptr defVal; + AST_MEMBER(normal_def, &item, &sep, &defVal) +AST_END(normal_def, "normal_def") AST_NODE(meta_variable_pair) ast_ptr name; AST_MEMBER(meta_variable_pair, &name) AST_END(meta_variable_pair, "meta_variable_pair"sv) +AST_NODE(meta_variable_pair_def) + ast_ptr pair; + ast_ptr defVal; + AST_MEMBER(meta_variable_pair_def, &pair, &defVal) +AST_END(meta_variable_pair_def, "meta_variable_pair_def"sv) + AST_NODE(meta_normal_pair) ast_sel key; ast_sel value; AST_MEMBER(meta_normal_pair, &key, &value) AST_END(meta_normal_pair, "meta_normal_pair"sv) -AST_NODE(meta_default_pair) - ast_sel key; - ast_ptr sep; - ast_ptr value; - ast_ptr defVal; - AST_MEMBER(meta_default_pair, &key, &sep, &value, &defVal) -AST_END(meta_default_pair, "meta_default_pair"sv) +AST_NODE(meta_normal_pair_def) + ast_ptr pair; + ast_ptr defVal; + AST_MEMBER(meta_normal_pair_def, &pair, &defVal) +AST_END(meta_normal_pair_def, "meta_normal_pair_def"sv) AST_NODE(simple_table) ast_ptr sep; @@ -620,9 +635,6 @@ AST_END(Value, "value"sv) AST_LEAF(default_value) AST_END(default_value, "default_value"sv) -class default_pair_t; -class meta_default_pair_t; - AST_NODE(SpreadExp) ast_ptr exp; AST_MEMBER(SpreadExp, &exp) @@ -631,23 +643,22 @@ AST_END(SpreadExp, "spread_exp"sv) AST_NODE(TableLit) ast_ptr sep; ast_sel_list values; + variable_pair_def_t, normal_pair_def_t, SpreadExp_t, normal_def_t, + meta_variable_pair_def_t, meta_normal_pair_def_t> values; AST_MEMBER(TableLit, &sep, &values) AST_END(TableLit, "table_lit"sv) AST_NODE(TableBlockIndent) ast_ptr sep; ast_sel_list values; + variable_pair_t, normal_pair_t, Exp_t, TableBlockIndent_t, + meta_variable_pair_t, meta_normal_pair_t> values; AST_MEMBER(TableBlockIndent, &sep, &values) AST_END(TableBlockIndent, "table_block_indent"sv) AST_NODE(TableBlock) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock, "table_block"sv) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index f5436d1..c359e02 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -6,6 +6,7 @@ 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 @@ -59,7 +60,7 @@ namespace yue { typedef std::list str_list; -const std::string_view version = "0.15.12"sv; +const std::string_view version = "0.15.13"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -98,7 +99,17 @@ public: #ifndef YUE_NO_MACRO if (L) passOptions(); #endif // YUE_NO_MACRO - _info = _parser.parse(codes); + double parseTime = 0.0; + double compileTime = 0.0; + if (config.profiling) { + auto start = std::chrono::high_resolution_clock::now(); + _info = _parser.parse(codes); + auto stop = std::chrono::high_resolution_clock::now(); + std::chrono::duration diff = stop - start; + parseTime = diff.count(); + } else { + _info = _parser.parse(codes); + } std::unique_ptr globals; std::unique_ptr options; if (!config.options.empty()) { @@ -149,9 +160,19 @@ public: _gotoScopes.push(0); _gotoScope = 1; _varArgs.push({true, false}); - transformBlock(block, out, - config.implicitReturnRoot ? ExpUsage::Return : ExpUsage::Common, - nullptr, true); + if (_config.profiling) { + auto start = std::chrono::high_resolution_clock::now(); + transformBlock(block, out, + config.implicitReturnRoot ? ExpUsage::Return : ExpUsage::Common, + nullptr, true); + auto stop = std::chrono::high_resolution_clock::now(); + std::chrono::duration diff = stop - start; + compileTime = diff.count(); + } else { + transformBlock(block, out, + config.implicitReturnRoot ? ExpUsage::Return : ExpUsage::Common, + nullptr, true); + } popScope(); if (!gotos.empty()) { for (const auto& gotoNode : gotos) { @@ -207,12 +228,12 @@ public: } } #endif // YUE_NO_MACRO - return {std::move(out.back()), Empty, std::move(globals), std::move(options)}; + return {std::move(out.back()), Empty, std::move(globals), std::move(options), parseTime, compileTime}; } catch (const std::logic_error& error) { - return {Empty, error.what(), std::move(globals), std::move(options)}; + return {Empty, error.what(), std::move(globals), std::move(options), parseTime, compileTime}; } } else { - return {Empty, std::move(_info.error), std::move(globals), std::move(options)}; + return {Empty, std::move(_info.error), std::move(globals), std::move(options), parseTime, compileTime}; } } @@ -2055,6 +2076,16 @@ private: tableItems = &table->values.objects(); break; } + case id(): { + auto table = ast_cast(node); + tableItems = &table->values.objects(); + break; + } + case id(): { + auto table = ast_cast(node); + tableItems = &table->pairs.objects(); + break; + } default: YUEE("AST node mismatch", node); break; } if (!tableItems) throw std::logic_error(_info.errorMessage("invalid destructure value"sv, node)); @@ -2063,7 +2094,13 @@ private: auto subMetaDestruct = node->new_ptr(); for (auto pair : *tableItems) { switch (pair->getId()) { - case id(): { + case id(): + case id(): { + Exp_t* defVal = nullptr; + if (auto nd = ast_cast(pair)) { + pair = nd->item.get(); + defVal = nd->defVal.get(); + } ++index; if (!isAssignable(static_cast(pair))) { throw std::logic_error(_info.errorMessage("can't destructure value"sv, pair)); @@ -2087,21 +2124,33 @@ private: pairs.push_back({exp, varName, chain, - nullptr}); + defVal}); } break; } - case id(): { + case id(): + case id(): { + Exp_t* defVal = nullptr; + if (auto vpd = ast_cast(pair)) { + pair = vpd->pair.get(); + defVal = vpd->defVal.get(); + } auto vp = static_cast(pair); auto name = _parser.toString(vp->name); auto chain = toAst('.' + name, vp->name); pairs.push_back({toAst(name, vp).get(), name, chain, - nullptr}); + defVal}); break; } - case id(): { + case id(): + case id(): { + Exp_t* defVal = nullptr; + if (auto npd = ast_cast(pair)) { + pair = npd->pair.get(); + defVal = npd->defVal.get(); + } auto np = static_cast(pair); ast_ptr keyIndex; if (np->key) { @@ -2144,7 +2193,7 @@ private: pairs.push_back({exp, varName, chain, - nullptr}); + defVal}); } break; } @@ -2171,97 +2220,28 @@ private: } break; } - case id(): { - auto dp = static_cast(pair); - if (auto exp = dp->key.as()) { - ++index; - if (!isAssignable(static_cast(exp))) { - throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp)); - } - auto value = singleValueFrom(exp); - auto item = value->item.get(); - 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, - varName, - toAst('[' + std::to_string(index) + ']', value), - dp->defVal}); - } - break; - } - auto chain = dp->new_ptr(); - std::string valueStr; - if (dp->key) { - if (auto key = dp->key->getByPath()) { - auto keyName = _parser.toString(key); - if (!dp->value) valueStr = keyName; - if (LuaKeywords.find(keyName) != LuaKeywords.end()) { - chain->items.push_back(toAst('"' + keyName + '"', key)); - } else { - chain->items.push_back(toAst('.' + keyName, key)); - } - } else if (auto key = dp->key->getByPath()) { - auto callable = dp->new_ptr(); - callable->item.set(key); - auto chainValue = dp->new_ptr(); - chainValue->items.push_back(callable); - chain->items.push_back(newExp(chainValue, dp)); - } else if (auto key = dp->key.as()) { - chain->items.push_back(newExp(key, dp)); - } else if (auto key = dp->key.as()) { - chain->items.push_back(key); - } else - throw std::logic_error(_info.errorMessage("unsupported key for destructuring"sv, dp)); - } - 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, - varName, - chain, - dp->defVal}); - } else { - pairs.push_back({toAst(valueStr, dp).get(), - valueStr, - chain, - dp->defVal}); - } - break; - } - case id(): { - auto mp = static_cast(pair); - auto newPair = pair->new_ptr(); - if (mp->key) { - if (!ast_is(mp->key)) { - throw std::logic_error(_info.errorMessage("destructuring with metatable accessed by expression is not supported."sv, mp->key)); - } - auto key = _parser.toString(mp->key); - _buf << "__"sv << key; - auto newKey = toAst(clearBuf(), mp->key); - newPair->key.set(newKey); - if (newPair->value) { - newPair->value.set(mp->value); - } else { - newPair->value.set(toAst(key, mp->key)); - } - } else { - newPair->value.set(mp->value); + case id(): + case id(): { + Exp_t* defVal = nullptr; + if (auto mvpd = ast_cast(pair)) { + pair = mvpd->pair.get(); + defVal = mvpd->defVal.get(); } - newPair->defVal.set(mp->defVal); - subMetaDestruct->values.push_back(newPair); - break; - } - case id(): { auto mp = static_cast(pair); auto name = _parser.toString(mp->name); _buf << "__"sv << name << ':' << name; - auto newPair = toAst(clearBuf(), pair); - subMetaDestruct->values.push_back(newPair); + auto newPairDef = toAst(clearBuf(), pair); + newPairDef->defVal.set(defVal); + subMetaDestruct->values.push_back(newPairDef); break; } - case id(): { + case id(): + case id(): { + Exp_t* defVal = nullptr; + if (auto mnpd = ast_cast(pair)) { + pair = mnpd->pair.get(); + defVal = mnpd->defVal.get(); + } auto mp = static_cast(pair); auto newPair = pair->new_ptr(); if (mp->key) { @@ -2280,7 +2260,10 @@ private: } } newPair->value.set(mp->value); - subMetaDestruct->values.push_back(newPair); + auto newPairDef = mp->new_ptr(); + newPairDef->pair.set(newPair); + newPairDef->defVal.set(defVal); + subMetaDestruct->values.push_back(newPairDef); break; } default: YUEE("AST node mismatch", pair); break; @@ -2361,32 +2344,52 @@ private: } for (auto item : *dlist) { switch (item->getId()) { - case id(): { - auto mp = static_cast(item); - auto newPair = item->new_ptr(); + case id(): { + auto mvp = static_cast(item); + auto mp = mvp->pair.get(); + auto name = _parser.toString(mp->name); + _buf << "__"sv << name << ':' << name; + auto newPairDef = toAst(clearBuf(), item); + newPairDef->defVal.set(mvp->defVal); + subMetaDestruct->values.push_back(newPairDef); + break; + } + case id(): { + auto mnp = static_cast(item); + auto mp = mnp->pair.get(); + auto newPair = item->new_ptr(); if (mp->key) { - auto key = _parser.toString(mp->key); - _buf << "__"sv << key; - auto newKey = toAst(clearBuf(), mp->key); - newPair->key.set(newKey); - if (newPair->value) { - newPair->value.set(mp->value); - } else { - newPair->value.set(toAst(key, mp->key)); + switch (mp->key->getId()) { + case id(): { + auto key = _parser.toString(mp->key); + _buf << "__"sv << key; + auto newKey = toAst(clearBuf(), mp->key); + newPair->key.set(newKey); + break; + } + case id(): { + newPair->key.set(mp->key); + break; + } + case id(): + newPair->key.set(mp->key); + break; + default: YUEE("AST node mismatch", mp->key); break; } - } else { - newPair->value.set(mp->value); } - newPair->defVal.set(mp->defVal); - subMetaDestruct->values.push_back(newPair); + newPair->value.set(mp->value); + auto newPairDef = item->new_ptr(); + newPairDef->pair.set(newPair); + newPairDef->defVal.set(mnp->defVal); + subMetaDestruct->values.push_back(newPairDef); break; } case id(): { auto mp = static_cast(item); auto name = _parser.toString(mp->name); _buf << "__"sv << name << ':' << name; - auto newPair = toAst(clearBuf(), item); - subMetaDestruct->values.push_back(newPair); + auto newPairDef = toAst(clearBuf(), item); + subMetaDestruct->values.push_back(newPairDef); break; } case id(): { @@ -2414,7 +2417,23 @@ private: } } newPair->value.set(mp->value); - subMetaDestruct->values.push_back(newPair); + auto newPairDef = item->new_ptr(); + newPairDef->pair.set(newPair); + subMetaDestruct->values.push_back(newPairDef); + break; + } + case id(): { + auto pair = static_cast(item); + auto newPairDef = item->new_ptr(); + newPairDef->pair.set(pair); + subDestruct->values.push_back(newPairDef); + break; + } + case id(): { + auto pair = static_cast(item); + auto newPairDef = item->new_ptr(); + newPairDef->pair.set(pair); + subDestruct->values.push_back(newPairDef); break; } default: @@ -5197,14 +5216,28 @@ private: transformForEach(forEach, temp); break; } - case id(): { + case id(): + case id(): { + if (auto pair = ast_cast(item)) { + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + } auto variablePair = static_cast(item); auto nameStr = _parser.toString(variablePair->name); auto assignment = toAst(tableVar + '.' + nameStr + '=' + nameStr, item); transformAssignment(assignment, temp); break; } - case id(): { + case id(): + case id(): { + if (auto pair = ast_cast(item)) { + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + } auto normalPair = static_cast(item); auto assignment = toAst(tableVar + "=nil"s, item); auto chainValue = singleValueFrom(ast_to(assignment->expList->exprs.front()))->item.to(); @@ -5248,10 +5281,18 @@ private: transformAssignment(assignment, temp); break; } - case id(): { + case id(): + case id(): { + auto current = item; + if (auto pair = ast_cast(item)) { + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->item.get(); + } bool lastVarArg = false; BLOCK_START - BREAK_IF(item != values.back()); + BREAK_IF(current != values.back()); auto value = singleValueFrom(item); BREAK_IF(!value); auto chainValue = value->item.as(); @@ -5291,14 +5332,28 @@ private: transformAssignment(assignment, temp); break; } - case id(): { + case id(): + case id(): { + if (auto pair = ast_cast(item)) { + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + } auto metaVarPair = static_cast(item); auto nameStr = _parser.toString(metaVarPair->name); auto assignment = toAst(tableVar + ".<"s + nameStr + ">="s + nameStr, item); transformAssignment(assignment, temp); break; } - case id(): { + case id(): + case id(): { + if (auto pair = ast_cast(item)) { + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + } auto metaNormalPair = static_cast(item); auto assignment = toAst(tableVar + "=nil"s, item); auto chainValue = singleValueFrom(ast_to(assignment->expList->exprs.front()))->item.to(); @@ -5326,14 +5381,6 @@ private: transformAssignment(assignment, temp); break; } - case id(): { - throw std::logic_error(_info.errorMessage("invalid default value"sv, static_cast(item)->defVal)); - break; - } - case id(): { - throw std::logic_error(_info.errorMessage("invalid default value"sv, static_cast(item)->defVal)); - break; - } default: YUEE("AST node mismatch", item); break; } } @@ -5381,7 +5428,50 @@ private: incIndentOffset(); auto metatable = x->new_ptr(); ast_sel metatableItem; - for (auto item : values) { + for (auto value : values) { + auto item = value; + switch (item->getId()) { + case id(): { + auto pair = static_cast(item); + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + break; + } + case id(): { + auto pair = static_cast(item); + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + break; + } + case id(): { + auto pair = static_cast(item); + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + break; + } + case id(): { + auto pair = static_cast(item); + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->pair.get(); + break; + } + case id(): { + auto pair = static_cast(item); + if (pair->defVal) { + throw std::logic_error(_info.errorMessage("invalid default value"sv, pair->defVal)); + } + item = pair->item.get(); + break; + } + } bool isMetamethod = false; switch (item->getId()) { case id(): transformExp(static_cast(item), temp, ExpUsage::Closure); break; @@ -5437,18 +5527,10 @@ private: } break; } - case id(): { - throw std::logic_error(_info.errorMessage("invalid default value"sv, static_cast(item)->defVal)); - break; - } - case id(): { - throw std::logic_error(_info.errorMessage("invalid default value"sv, static_cast(item)->defVal)); - break; - } default: YUEE("AST node mismatch", item); break; } if (!isMetamethod) { - temp.back() = indent() + (item == values.back() ? temp.back() : temp.back() + ',') + nll(item); + temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nll(value); } } if (metatable->pairs.empty() && !metatableItem) { @@ -7518,7 +7600,41 @@ private: } else if (auto tabLit = ast_cast(target)) { auto simpleValue = x->new_ptr(); auto tableLit = x->new_ptr(); - tableLit->values.dup(tabLit->items); + for (auto pair : tabLit->items.objects()) { + switch (pair->getId()) { + case id(): { + auto pairDef = pair->new_ptr(); + pairDef->pair.set(pair); + tableLit->values.push_back(pairDef); + break; + } + case id(): { + auto pairDef = pair->new_ptr(); + pairDef->pair.set(pair); + tableLit->values.push_back(pairDef); + break; + } + case id(): { + auto pairDef = pair->new_ptr(); + pairDef->item.set(pair); + tableLit->values.push_back(pairDef); + break; + } + case id(): { + auto pairDef = pair->new_ptr(); + pairDef->pair.set(pair); + tableLit->values.push_back(pairDef); + break; + } + case id(): { + auto pairDef = pair->new_ptr(); + pairDef->pair.set(pair); + tableLit->values.push_back(pairDef); + break; + } + default: YUEE("AST node mismatch", pair); break; + } + } simpleValue->value.set(tableLit); value->item.set(simpleValue); } else { diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index 9ca641a..a3f4367 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -30,6 +30,7 @@ struct YueConfig { bool reserveLineNumber = true; bool useSpaceOverTab = false; bool exporting = false; + bool profiling = false; int lineOffset = 0; std::string module; Options options; @@ -48,6 +49,8 @@ struct CompileInfo { std::string error; std::unique_ptr globals; std::unique_ptr options; + double parseTime; + double compileTime; }; class YueCompilerImpl; diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 0d9a183..758865e 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -406,10 +406,10 @@ YueParser::YueParser() { simple_table = Seperator >> KeyValue >> *(sym(',') >> KeyValue); Value = SimpleValue | simple_table | ChainValue | Space >> String; - single_string_inner = expr("\\'") | "\\\\" | not_(expr('\'')) >> Any; + single_string_inner = expr('\\') >> set("'\\") | not_(expr('\'')) >> Any; SingleString = symx('\'') >> *single_string_inner >> symx('\''); interp = symx("#{") >> Exp >> sym('}'); - double_string_plain = expr("\\\"") | "\\\\" | not_(expr('"')) >> Any; + double_string_plain = expr('\\') >> set("\"\\") | not_(expr('"')) >> Any; double_string_inner = +(not_(interp) >> double_string_plain); double_string_content = double_string_inner | interp; DoubleString = symx('"') >> Seperator >> *double_string_content >> symx('"'); @@ -484,7 +484,13 @@ YueParser::YueParser() { SpreadExp = sym("...") >> Exp; - TableValue = ((KeyValue | SpreadExp | Exp) >> not_(sym('='))) | meta_default_pair | default_pair; + TableValue = + variable_pair_def | + normal_pair_def | + meta_variable_pair_def | + meta_normal_pair_def | + SpreadExp | + normal_def; table_lit_lines = SpaceBreak >> TableLitLine >> *(-sym(',') >> SpaceBreak >> TableLitLine) >> -sym(','); @@ -564,21 +570,16 @@ YueParser::YueParser() { symx(':') >> not_(':') >> (Exp | TableBlock | +SpaceBreak >> Exp); - default_pair = ( - sym(':') >> Variable >> Seperator | - KeyName >> symx(':') >> not_(':') >> Seperator >> exp_not_tab | - Space >> String >> symx(':') >> not_(':') >> Seperator >> exp_not_tab | - exp_not_tab >> Seperator) >> sym('=') >> Exp; - meta_variable_pair = sym(":<") >> Space >> Variable >> sym('>'); meta_normal_pair = sym('<') >> Space >> -meta_index >> sym(">:") >> (Exp | TableBlock | +(SpaceBreak) >> Exp); - meta_default_pair = ( - sym(":<") >> Space >> Variable >> sym('>') >> Seperator | - sym('<') >> Space >> -meta_index >> sym(">:") >> Seperator >> exp_not_tab - ) >> sym('=') >> Exp; + variable_pair_def = variable_pair >> -(sym('=') >> Exp); + normal_pair_def = normal_pair >> -(sym('=') >> Exp); + meta_variable_pair_def = meta_variable_pair >> -(sym('=') >> Exp); + meta_normal_pair_def = meta_normal_pair >> -(sym('=') >> Exp); + normal_def = Exp >> Seperator >> -(sym('=') >> Exp); KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair; KeyValueList = KeyValue >> *(sym(',') >> KeyValue); diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 88d688c..a4ecafd 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -332,10 +332,13 @@ private: AST_RULE(Export) AST_RULE(variable_pair) AST_RULE(normal_pair) - AST_RULE(default_pair) AST_RULE(meta_variable_pair) AST_RULE(meta_normal_pair) - AST_RULE(meta_default_pair) + AST_RULE(variable_pair_def) + AST_RULE(normal_pair_def) + AST_RULE(normal_def) + AST_RULE(meta_variable_pair_def) + AST_RULE(meta_normal_pair_def) AST_RULE(FnArgDef) AST_RULE(FnArgDefList) AST_RULE(outer_var_shadow) -- cgit v1.2.3-55-g6feb