aboutsummaryrefslogtreecommitdiff
path: root/src/yuescript/yue_parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/yuescript/yue_parser.cpp')
-rwxr-xr-xsrc/yuescript/yue_parser.cpp48
1 files changed, 25 insertions, 23 deletions
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<std::string> LuaKeywords = {
17 "end"s, "false"s, "for"s, "function"s, "goto"s, 17 "end"s, "false"s, "for"s, "function"s, "goto"s,
18 "if"s, "in"s, "local"s, "nil"s, "not"s, 18 "if"s, "in"s, "local"s, "nil"s, "not"s,
19 "or"s, "repeat"s, "return"s, "then"s, "true"s, 19 "or"s, "repeat"s, "return"s, "then"s, "true"s,
20 "until"s, "while"s 20 "until"s, "while"s};
21};
22 21
23std::unordered_set<std::string> Keywords = { 22std::unordered_set<std::string> Keywords = {
24 "and"s, "break"s, "do"s, "else"s, "elseif"s, 23 "and"s, "break"s, "do"s, "else"s, "elseif"s,
@@ -31,6 +30,7 @@ std::unordered_set<std::string> Keywords = {
31 "try"s, "unless"s, "using"s, "when"s, "with"s // Yue keywords 30 "try"s, "unless"s, "using"s, "when"s, "with"s // Yue keywords
32}; 31};
33 32
33// clang-format off
34YueParser::YueParser() { 34YueParser::YueParser() {
35 plain_space = *set(" \t"); 35 plain_space = *set(" \t");
36 Break = nl(-expr('\r') >> '\n'); 36 Break = nl(-expr('\r') >> '\n');
@@ -55,14 +55,14 @@ YueParser::YueParser() {
55 +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> 55 +(range('0', '9') | range('a', 'f') | range('A', 'F')) >>
56 -(-set("uU") >> set("lL") >> set("lL")) 56 -(-set("uU") >> set("lL") >> set("lL"))
57 ) | ( 57 ) | (
58 +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") 58 +range('0', '9') >> (
59 ) | ( 59 '.' >> +range('0', '9') >> -(set("eE") >> -expr('-') >> +range('0', '9')) |
60 ( 60 -set("uU") >> set("lL") >> set("lL") |
61 +range('0', '9') >> -('.' >> +range('0', '9')) 61 true_()
62 ) | (
63 '.' >> +range('0', '9')
64 ) 62 )
65 ) >> -(set("eE") >> -expr('-') >> +range('0', '9')); 63 ) | (
64 '.' >> +range('0', '9') >> -(set("eE") >> -expr('-') >> +range('0', '9'))
65 );
66 66
67 Cut = false_(); 67 Cut = false_();
68 Seperator = true_(); 68 Seperator = true_();
@@ -652,6 +652,7 @@ YueParser::YueParser() {
652 BlockEnd = Block >> -(+Break >> Space >> and_(Stop)) >> Stop; 652 BlockEnd = Block >> -(+Break >> Space >> and_(Stop)) >> Stop;
653 File = White >> -Shebang >> -Block >> White >> eof(); 653 File = White >> -Shebang >> -Block >> White >> eof();
654} 654}
655// clang-format on
655 656
656ParseInfo YueParser::parse(std::string_view codes, rule& r) { 657ParseInfo YueParser::parse(std::string_view codes, rule& r) {
657 ParseInfo res; 658 ParseInfo res;
@@ -710,20 +711,20 @@ std::string YueParser::decode(const input& codes) {
710} 711}
711 712
712namespace Utils { 713namespace Utils {
713 void replace(std::string& str, std::string_view from, std::string_view to) { 714void replace(std::string& str, std::string_view from, std::string_view to) {
714 size_t start_pos = 0; 715 size_t start_pos = 0;
715 while((start_pos = str.find(from, start_pos)) != std::string::npos) { 716 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
716 str.replace(start_pos, from.size(), to); 717 str.replace(start_pos, from.size(), to);
717 start_pos += to.size(); 718 start_pos += to.size();
718 }
719 } 719 }
720}
720 721
721 void trim(std::string& str) { 722void trim(std::string& str) {
722 if (str.empty()) return; 723 if (str.empty()) return;
723 str.erase(0, str.find_first_not_of(" \t\r\n")); 724 str.erase(0, str.find_first_not_of(" \t\r\n"));
724 str.erase(str.find_last_not_of(" \t\r\n") + 1); 725 str.erase(str.find_last_not_of(" \t\r\n") + 1);
725 }
726} 726}
727} // namespace Utils
727 728
728std::string ParseInfo::errorMessage(std::string_view msg, const input_range* loc) const { 729std::string ParseInfo::errorMessage(std::string_view msg, const input_range* loc) const {
729 const int ASCII = 255; 730 const int ASCII = 255;
@@ -753,13 +754,14 @@ std::string ParseInfo::errorMessage(std::string_view msg, const input_range* loc
753 } 754 }
754 auto line = Converter{}.to_bytes(std::wstring(begin, end)); 755 auto line = Converter{}.to_bytes(std::wstring(begin, end));
755 while (col < static_cast<int>(line.size()) 756 while (col < static_cast<int>(line.size())
756 && (line[col] == ' ' || line[col] == '\t')) { 757 && (line[col] == ' ' || line[col] == '\t')) {
757 col++; 758 col++;
758 } 759 }
759 Utils::replace(line, "\t"sv, " "sv); 760 Utils::replace(line, "\t"sv, " "sv);
760 std::ostringstream buf; 761 std::ostringstream buf;
761 buf << loc->m_begin.m_line << ": "sv << msg << 762 buf << loc->m_begin.m_line << ": "sv << msg << '\n'
762 '\n' << line << '\n' << std::string(col, ' ') << "^"sv; 763 << line << '\n'
764 << std::string(col, ' ') << "^"sv;
763 return buf.str(); 765 return buf.str();
764} 766}
765 767