aboutsummaryrefslogtreecommitdiff
path: root/src/yuescript/yue_parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/yuescript/yue_parser.cpp')
-rw-r--r--src/yuescript/yue_parser.cpp27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp
index 986f67a..83aba40 100644
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -309,7 +309,7 @@ YueParser::YueParser() {
309 ); 309 );
310 310
311 MacroNamePair = MacroName >> ':' >> space >> MacroName; 311 MacroNamePair = MacroName >> ':' >> space >> MacroName;
312 ImportAllMacro = '$'; 312 ImportAllMacro = '$' >> not_(UnicodeName);
313 import_tab_item = 313 import_tab_item =
314 VariablePair | 314 VariablePair |
315 NormalPair | 315 NormalPair |
@@ -324,6 +324,7 @@ YueParser::YueParser() {
324 push_indent_match >> (space >> import_tab_list >> pop_indent | pop_indent) 324 push_indent_match >> (space >> import_tab_list >> pop_indent | pop_indent)
325 ) | space; 325 ) | space;
326 import_tab_lines = space_break >> import_tab_line >> *(-(space >> ',') >> space_break >> import_tab_line) >> -(space >> ','); 326 import_tab_lines = space_break >> import_tab_line >> *(-(space >> ',') >> space_break >> import_tab_line) >> -(space >> ',');
327 import_tab_key_value = key_value | ':' >> MacroName | MacroNamePair | ImportAllMacro;
327 ImportTabLit = ( 328 ImportTabLit = (
328 '{' >> Seperator >> 329 '{' >> Seperator >>
329 -(space >> import_tab_list) >> 330 -(space >> import_tab_list) >>
@@ -332,7 +333,7 @@ YueParser::YueParser() {
332 white >> 333 white >>
333 '}' 334 '}'
334 ) | ( 335 ) | (
335 Seperator >> key_value >> *(space >> ',' >> space >> key_value) 336 Seperator >> import_tab_key_value >> *(space >> ',' >> space >> import_tab_key_value)
336 ); 337 );
337 338
338 ImportAs = ImportLiteral >> -(space >> key("as") >> space >> (ImportTabLit | Variable | ImportAllMacro)); 339 ImportAs = ImportLiteral >> -(space >> key("as") >> space >> (ImportTabLit | Variable | ImportAllMacro));
@@ -1083,6 +1084,23 @@ ParseInfo YueParser::parse(std::string_view codes, rule& r) {
1083 return res; 1084 return res;
1084} 1085}
1085 1086
1087ParseInfo YueParser::parse(std::string_view astName, std::string_view codes) {
1088 auto it = _rules.find(astName);
1089 if (it != _rules.end()) {
1090 return parse(codes, *it->second);
1091 }
1092 return {};
1093}
1094
1095bool YueParser::match(std::string_view astName, std::string_view codes) {
1096 auto it = _rules.find(astName);
1097 if (it != _rules.end()) {
1098 auto rEnd = rule(*it->second >> eof());
1099 return parse(codes, rEnd).node;
1100 }
1101 return false;
1102}
1103
1086std::string YueParser::toString(ast_node* node) { 1104std::string YueParser::toString(ast_node* node) {
1087 return _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); 1105 return _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it));
1088} 1106}
@@ -1091,6 +1109,11 @@ std::string YueParser::toString(input::iterator begin, input::iterator end) {
1091 return _converter.to_bytes(std::wstring(begin, end)); 1109 return _converter.to_bytes(std::wstring(begin, end));
1092} 1110}
1093 1111
1112YueParser& YueParser::shared() {
1113 thread_local static YueParser parser;
1114 return parser;
1115}
1116
1094namespace Utils { 1117namespace Utils {
1095void replace(std::string& str, std::string_view from, std::string_view to) { 1118void replace(std::string& str, std::string_view from, std::string_view to) {
1096 size_t start_pos = 0; 1119 size_t start_pos = 0;