string literal

This commit is contained in:
MihailRis
2024-05-10 17:05:18 +03:00
parent 45b0f4be2d
commit 5a2bbb54c2
+14 -12
View File
@@ -7,8 +7,9 @@
using namespace cmd; using namespace cmd;
inline bool is_cmd_identifier_part(char c) { inline bool is_cmd_identifier_part(char c, bool allowColon) {
return is_identifier_part(c) || c == '.' || c == '$' || c == '@'; return is_identifier_part(c) || c == '.' || c == '$' || c == '@' ||
(allowColon && c == ':');
} }
inline bool is_cmd_identifier_start(char c) { inline bool is_cmd_identifier_start(char c) {
@@ -16,7 +17,7 @@ inline bool is_cmd_identifier_start(char c) {
} }
class CommandParser : BasicParser { class CommandParser : BasicParser {
std::string parseIdentifier() { std::string parseIdentifier(bool allowColon) {
char c = peek(); char c = peek();
if (!is_identifier_start(c) && c != '$') { if (!is_identifier_start(c) && c != '$') {
if (c == '"') { if (c == '"') {
@@ -26,7 +27,7 @@ class CommandParser : BasicParser {
throw error("identifier expected"); throw error("identifier expected");
} }
int start = pos; int start = pos;
while (hasNext() && is_cmd_identifier_part(source[pos])) { while (hasNext() && is_cmd_identifier_part(source[pos], allowColon)) {
pos++; pos++;
} }
return std::string(source.substr(start, pos-start)); return std::string(source.substr(start, pos-start));
@@ -49,7 +50,7 @@ public:
std::cout << "type.name: enum" << std::endl; std::cout << "type.name: enum" << std::endl;
return ArgType::enumvalue; return ArgType::enumvalue;
} }
std::string name = parseIdentifier(); std::string name = parseIdentifier(false);
std::cout << "type.name: " << name << std::endl; std::cout << "type.name: " << name << std::endl;
auto found = types.find(name); auto found = types.find(name);
if (found != types.end()) { if (found != types.end()) {
@@ -62,7 +63,7 @@ public:
dynamic::Value parseValue() { dynamic::Value parseValue() {
char c = peek(); char c = peek();
if (is_cmd_identifier_start(c)) { if (is_cmd_identifier_start(c)) {
auto str = parseIdentifier(); auto str = parseIdentifier(true);
if (str == "true") { if (str == "true") {
return true; return true;
} else if (str == "false") { } else if (str == "false") {
@@ -72,8 +73,9 @@ public:
} }
return str; return str;
} }
if (c == '"') { if (c == '"' || c == '\'') {
return parseIdentifier(); nextChar();
return parseString(c);
} }
if (c == '+' || c == '-' || is_digit(c)) { if (c == '+' || c == '-' || is_digit(c)) {
return parseNumber(c == '-' ? -1 : 1); return parseNumber(c == '-' ? -1 : 1);
@@ -98,12 +100,12 @@ public:
} else { } else {
expect('$'); expect('$');
goBack(); goBack();
return parseIdentifier(); return parseIdentifier(false);
} }
} }
Argument parseArgument() { Argument parseArgument() {
std::string name = parseIdentifier(); std::string name = parseIdentifier(false);
expect(':'); expect(':');
std::cout << "arg.name: " << name << std::endl; std::cout << "arg.name: " << name << std::endl;
ArgType type = parseType(); ArgType type = parseType();
@@ -137,7 +139,7 @@ public:
} }
Command parseScheme(executor_func executor) { Command parseScheme(executor_func executor) {
std::string name = parseIdentifier(); std::string name = parseIdentifier(true);
std::vector<Argument> args; std::vector<Argument> args;
std::unordered_map<std::string, Argument> kwargs; std::unordered_map<std::string, Argument> kwargs;
@@ -283,7 +285,7 @@ public:
Prompt parsePrompt(CommandsInterpreter* interpreter) { Prompt parsePrompt(CommandsInterpreter* interpreter) {
auto repo = interpreter->getRepository(); auto repo = interpreter->getRepository();
std::string name = parseIdentifier(); std::string name = parseIdentifier(true);
auto command = repo->get(name); auto command = repo->get(name);
if (command == nullptr) { if (command == nullptr) {
throw error("unknown command "+util::quote(name)); throw error("unknown command "+util::quote(name));