command argument enumeration type

This commit is contained in:
MihailRis
2024-05-09 18:48:14 +03:00
parent 44d0a462f6
commit de318bb8ef
2 changed files with 33 additions and 3 deletions
+32 -3
View File
@@ -8,7 +8,7 @@
using namespace cmd; using namespace cmd;
inline bool is_cmd_identifier_part(char c) { inline bool is_cmd_identifier_part(char c) {
return is_identifier_part(c) || c == '.' || c == '$'; return is_identifier_part(c) || c == '.' || c == '$' || c == '@';
} }
class SchemeParser : BasicParser { class SchemeParser : BasicParser {
@@ -32,7 +32,8 @@ class SchemeParser : BasicParser {
{"num", ArgType::number}, {"num", ArgType::number},
{"int", ArgType::integer}, {"int", ArgType::integer},
{"str", ArgType::string}, {"str", ArgType::string},
{"selector", ArgType::selector}, {"@", ArgType::selector},
{"enum", ArgType::enumvalue},
}; };
public: public:
SchemeParser(std::string_view filename, std::string_view source) SchemeParser(std::string_view filename, std::string_view source)
@@ -40,6 +41,10 @@ public:
} }
ArgType parseType() { ArgType parseType() {
if (peek() == '[') {
std::cout << "type.name: enum" << std::endl;
return ArgType::enumvalue;
}
std::string name = parseIdentifier(); std::string name = parseIdentifier();
std::cout << "type.name: " << name << std::endl; std::cout << "type.name: " << name << std::endl;
auto found = types.find(name); auto found = types.find(name);
@@ -72,11 +77,35 @@ public:
throw error("invalid character '"+std::string({c})+"'"); throw error("invalid character '"+std::string({c})+"'");
} }
std::string parseEnum() {
if (peek() == '[') {
nextChar();
if (peek() == ']') {
throw error("empty enumeration is not allowed");
}
auto enumvalue = std::string(readUntil(']'));
if (enumvalue.find(' ') != std::string::npos) {
throw error("use '|' as separator, not a space");
}
nextChar();
return enumvalue;
} else {
expect('$');
goBack();
return parseIdentifier();
}
}
Argument parseArgument() { Argument parseArgument() {
std::string name = parseIdentifier(); std::string name = parseIdentifier();
expect(':'); expect(':');
std::cout << "arg.name: " << name << std::endl; std::cout << "arg.name: " << name << std::endl;
ArgType type = parseType(); ArgType type = parseType();
std::string enumname = "";
if (type == ArgType::enumvalue) {
enumname = parseEnum();
std::cout << "enum: " << enumname << std::endl;
}
bool optional = false; bool optional = false;
dynamic::Value def {}; dynamic::Value def {};
dynamic::Value origin {}; dynamic::Value origin {};
@@ -98,7 +127,7 @@ public:
break; break;
} }
} }
return Argument {name, type, optional, def, origin}; return Argument {name, type, optional, def, origin, enumname};
} }
Command parse(executor_func executor) { Command parse(executor_func executor) {
+1
View File
@@ -19,6 +19,7 @@ namespace cmd {
bool optional; bool optional;
dynamic::Value def; dynamic::Value def;
dynamic::Value origin; dynamic::Value origin;
std::string enumname;
}; };
struct CommandInput { struct CommandInput {