diff options
Diffstat (limited to '')
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 693a3bc..ea45e85 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
| @@ -59,7 +59,8 @@ YueParser::YueParser() { | |||
| 59 | white = space >> *(line_break >> space); | 59 | white = space >> *(line_break >> space); |
| 60 | alpha_num = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; | 60 | alpha_num = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; |
| 61 | not_alpha_num = not_(alpha_num); | 61 | not_alpha_num = not_(alpha_num); |
| 62 | Name = (range('a', 'z') | range('A', 'Z') | '_') >> *alpha_num; | 62 | Name = (range('a', 'z') | range('A', 'Z') | '_') >> *alpha_num >> not_(larger(255)); |
| 63 | UnicodeName = (range('a', 'z') | range('A', 'Z') | '_' | larger(255)) >> *(larger(255) | alpha_num); | ||
| 63 | num_expo = set("eE") >> -set("+-") >> num_char; | 64 | num_expo = set("eE") >> -set("+-") >> num_char; |
| 64 | num_expo_hex = set("pP") >> -set("+-") >> num_char; | 65 | num_expo_hex = set("pP") >> -set("+-") >> num_char; |
| 65 | lj_num = -set("uU") >> set("lL") >> set("lL"); | 66 | lj_num = -set("uU") >> set("lL") >> set("lL"); |
| @@ -160,23 +161,34 @@ YueParser::YueParser() { | |||
| 160 | 161 | ||
| 161 | #define body (in_block | Statement | empty_block_error) | 162 | #define body (in_block | Statement | empty_block_error) |
| 162 | 163 | ||
| 163 | Variable = pl::user(Name, [](const item_t& item) { | 164 | Variable = pl::user(Name | UnicodeName, [](const item_t& item) { |
| 164 | State* st = reinterpret_cast<State*>(item.user_data); | 165 | State* st = reinterpret_cast<State*>(item.user_data); |
| 165 | for (auto it = item.begin->m_it; it != item.end->m_it; ++it) st->buffer += static_cast<char>(*it); | 166 | for (auto it = item.begin->m_it; it != item.end->m_it; ++it) { |
| 167 | if (*it > 255) { | ||
| 168 | st->buffer.clear(); | ||
| 169 | return true; | ||
| 170 | } | ||
| 171 | st->buffer += static_cast<char>(*it); | ||
| 172 | } | ||
| 166 | auto isValid = Keywords.find(st->buffer) == Keywords.end(); | 173 | auto isValid = Keywords.find(st->buffer) == Keywords.end(); |
| 167 | if (isValid) { | 174 | if (isValid) { |
| 168 | if (st->buffer == st->moduleName) { | 175 | if (st->buffer[0] == '_') { |
| 169 | st->moduleFix++; | 176 | st->usedNames.insert(st->buffer); |
| 170 | st->moduleName = "_module_"s + std::to_string(st->moduleFix); | ||
| 171 | } | 177 | } |
| 172 | } | 178 | } |
| 173 | st->buffer.clear(); | 179 | st->buffer.clear(); |
| 174 | return isValid; | 180 | return isValid; |
| 175 | }); | 181 | }); |
| 176 | 182 | ||
| 177 | LabelName = pl::user(Name, [](const item_t& item) { | 183 | LabelName = pl::user(UnicodeName, [](const item_t& item) { |
| 178 | State* st = reinterpret_cast<State*>(item.user_data); | 184 | State* st = reinterpret_cast<State*>(item.user_data); |
| 179 | for (auto it = item.begin->m_it; it != item.end->m_it; ++it) st->buffer += static_cast<char>(*it); | 185 | for (auto it = item.begin->m_it; it != item.end->m_it; ++it) { |
| 186 | if (*it > 255) { | ||
| 187 | st->buffer.clear(); | ||
| 188 | return true; | ||
| 189 | } | ||
| 190 | st->buffer += static_cast<char>(*it); | ||
| 191 | } | ||
| 180 | auto isValid = LuaKeywords.find(st->buffer) == LuaKeywords.end(); | 192 | auto isValid = LuaKeywords.find(st->buffer) == LuaKeywords.end(); |
| 181 | st->buffer.clear(); | 193 | st->buffer.clear(); |
| 182 | return isValid; | 194 | return isValid; |
| @@ -191,12 +203,12 @@ YueParser::YueParser() { | |||
| 191 | }); | 203 | }); |
| 192 | 204 | ||
| 193 | Self = '@'; | 205 | Self = '@'; |
| 194 | SelfName = '@' >> Name; | 206 | SelfName = '@' >> (Name | UnicodeName); |
| 195 | SelfClass = "@@"; | 207 | SelfClass = "@@"; |
| 196 | SelfClassName = "@@" >> Name; | 208 | SelfClassName = "@@" >> (Name | UnicodeName); |
| 197 | 209 | ||
| 198 | SelfItem = SelfClassName | SelfClass | SelfName | Self; | 210 | SelfItem = SelfClassName | SelfClass | SelfName | Self; |
| 199 | KeyName = SelfItem | Name; | 211 | KeyName = SelfItem | Name | UnicodeName; |
| 200 | VarArg = "..."; | 212 | VarArg = "..."; |
| 201 | 213 | ||
| 202 | check_indent = pl::user(plain_space, [](const item_t& item) { | 214 | check_indent = pl::user(plain_space, [](const item_t& item) { |
| @@ -275,7 +287,7 @@ YueParser::YueParser() { | |||
| 275 | import_name_list = Seperator >> *space_break >> space >> import_name >> *((+space_break | space >> ',' >> *space_break) >> space >> import_name); | 287 | import_name_list = Seperator >> *space_break >> space >> import_name >> *((+space_break | space >> ',' >> *space_break) >> space >> import_name); |
| 276 | ImportFrom = import_name_list >> *space_break >> space >> key("from") >> space >> Exp; | 288 | ImportFrom = import_name_list >> *space_break >> space >> key("from") >> space >> Exp; |
| 277 | 289 | ||
| 278 | ImportLiteralInner = (range('a', 'z') | range('A', 'Z') | set("_-")) >> *(alpha_num | '-'); | 290 | ImportLiteralInner = (range('a', 'z') | range('A', 'Z') | set("_-") | larger(255)) >> *(alpha_num | '-' | larger(255)); |
| 279 | import_literal_chain = Seperator >> ImportLiteralInner >> *('.' >> ImportLiteralInner); | 291 | import_literal_chain = Seperator >> ImportLiteralInner >> *('.' >> ImportLiteralInner); |
| 280 | ImportLiteral = ( | 292 | ImportLiteral = ( |
| 281 | '\'' >> import_literal_chain >> '\'' | 293 | '\'' >> import_literal_chain >> '\'' |
| @@ -608,8 +620,8 @@ YueParser::YueParser() { | |||
| 608 | DotChainItem >> -ExistentialOp | | 620 | DotChainItem >> -ExistentialOp | |
| 609 | Slice | | 621 | Slice | |
| 610 | index >> -ExistentialOp; | 622 | index >> -ExistentialOp; |
| 611 | DotChainItem = '.' >> (Name | Metatable | Metamethod); | 623 | DotChainItem = '.' >> (Name | Metatable | Metamethod | UnicodeName); |
| 612 | ColonChainItem = (expr('\\') | "::") >> (LuaKeyword | Name | Metamethod); | 624 | ColonChainItem = (expr('\\') | "::") >> (LuaKeyword | Name | Metamethod | UnicodeName); |
| 613 | invoke_chain = Invoke >> -ExistentialOp >> -chain_items; | 625 | invoke_chain = Invoke >> -ExistentialOp >> -chain_items; |
| 614 | colon_chain = ColonChainItem >> -ExistentialOp >> -invoke_chain; | 626 | colon_chain = ColonChainItem >> -ExistentialOp >> -invoke_chain; |
| 615 | 627 | ||
| @@ -779,10 +791,10 @@ YueParser::YueParser() { | |||
| 779 | FnArrow = expr("->") | "=>"; | 791 | FnArrow = expr("->") | "=>"; |
| 780 | FunLit = -FnArgsDef >> space >> FnArrow >> -(space >> Body); | 792 | FunLit = -FnArgsDef >> space >> FnArrow >> -(space >> Body); |
| 781 | 793 | ||
| 782 | MacroName = '$' >> Name; | 794 | MacroName = '$' >> UnicodeName; |
| 783 | macro_args_def = '(' >> white >> -FnArgDefList >> white >> ')'; | 795 | macro_args_def = '(' >> white >> -FnArgDefList >> white >> ')'; |
| 784 | MacroLit = -(macro_args_def >> space) >> "->" >> space >> Body; | 796 | MacroLit = -(macro_args_def >> space) >> "->" >> space >> Body; |
| 785 | Macro = key("macro") >> space >> Name >> space >> '=' >> space >> MacroLit; | 797 | Macro = key("macro") >> space >> UnicodeName >> space >> '=' >> space >> MacroLit; |
| 786 | MacroInPlace = '$' >> space >> "->" >> space >> Body; | 798 | MacroInPlace = '$' >> space >> "->" >> space >> Body; |
| 787 | 799 | ||
| 788 | NameList = Seperator >> Variable >> *(space >> ',' >> space >> Variable); | 800 | NameList = Seperator >> Variable >> *(space >> ',' >> space >> Variable); |
| @@ -955,7 +967,15 @@ ParseInfo YueParser::parse(std::string_view codes, rule& r) { | |||
| 955 | State state; | 967 | State state; |
| 956 | res.node.set(::yue::parse(*(res.codes), r, errors, &state)); | 968 | res.node.set(::yue::parse(*(res.codes), r, errors, &state)); |
| 957 | if (state.exportCount > 0) { | 969 | if (state.exportCount > 0) { |
| 958 | res.moduleName = std::move(state.moduleName); | 970 | int index = 0; |
| 971 | std::string moduleName; | ||
| 972 | auto moduleStr = "_module_"s; | ||
| 973 | do { | ||
| 974 | moduleName = moduleStr + std::to_string(index); | ||
| 975 | index++; | ||
| 976 | } while (state.usedNames.find(moduleName) != state.usedNames.end()); | ||
| 977 | res.moduleName = moduleName; | ||
| 978 | res.usedNames = std::move(state.usedNames); | ||
| 959 | res.exportDefault = state.exportDefault; | 979 | res.exportDefault = state.exportDefault; |
| 960 | res.exportMacro = state.exportMacro; | 980 | res.exportMacro = state.exportMacro; |
| 961 | res.exportMetatable = !state.exportMetatable && state.exportMetamethod; | 981 | res.exportMetatable = !state.exportMetatable && state.exportMetamethod; |
