Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef P_arser_cpp
- #define P_arser_cpp
- #include <iostream>
- #include <string>
- namespace command
- {
- namespace detail
- {
- inline bool is_whitespace(const char ch) {
- return (' ' == ch) || ('\t' == ch) ||
- ('\n' == ch) ;
- }
- inline bool is_letter(const char ch) {
- return (('a' <= ch) && (ch <= 'z')) ||
- (('A' <= ch) && (ch <= 'Z')) || ('_' == ch);
- }
- inline bool is_digit(const char ch) {
- return ('0' <= ch) && (ch <= '9');
- }
- inline bool is_letter_or_digit(const char ch) {
- return is_letter(ch) || is_digit(ch);
- }
- inline bool match(const char ch1, const char ch2) {
- return (ch1 == ch2);
- }
- const std::string_view attributes[] =
- {
- "none_at", "color", "shape", "coords", "radio", "count", "except",
- };
- constexpr std::size_t attributes_size = std::size(attributes);
- const std::string_view commands[] =
- {
- "none_commnd", "set", "spawn", "kill"
- };
- constexpr std::size_t commands_size = std::size(attributes);
- }
- namespace parser
- {
- void init(), parser();
- std::string command;
- unsigned index;
- }
- namespace lexer
- {
- struct token
- {
- //
- static struct attribute
- {
- enum attribute_type {
- none_att, color, shape, coords, radio, count, except
- };
- static attribute_type get_enum_attribute(const std::string_view str) {
- std::size_t index = detail::attributes_size;
- }
- attribute() : type(none_att) {}
- attribute_type type;
- } enum_attribute;
- //
- static struct command
- {
- enum command_type {
- none_commnd, set, spawn, kill
- };
- static command_type get_enum_command(std::string_view str) {
- std::size_t index = detail::commands_size;
- }
- command() : type(none_commnd) {}
- command_type type;
- } enum_command;
- //
- enum token_type {
- none_tok, slash, c_open, brackets, c_close, ll_open, ll_close, equal, comma, colon, identifier, numeric, unknow, end
- };
- enum value_type {
- none_val, integer, string,
- spawn, kill, set,
- color, shape, coords, radio, count, except
- };
- //
- token() : tok_type(none_tok) {}
- void clear()
- {
- value.clear();
- tok_type = none_tok; val_type = none_val;
- }
- token_type tok_type;
- static value_type val_type;
- static std::string value;
- } cur_token;
- //
- std::string token::value = {};
- token::value_type token::val_type = token::none_val;
- token::attribute token::enum_attribute = {};
- token::command token::enum_command = {};
- //
- token::token_type get_token()
- {
- ++parser::index;
- while (detail::is_whitespace(parser::command[parser::index]))
- ++parser::index;
- if (parser::index >= parser::command.length())
- {
- return token::end;
- }
- switch (parser::command[parser::index])
- {
- case '/':
- return token::slash;
- case ']':
- return token::c_close;
- case '{':
- return token::ll_open;
- case '}':
- return token::ll_close;
- case ',':
- return token::comma;
- case ':':
- return token::colon;
- case '=':
- return token::equal;
- case '[':
- if (detail::match(parser::command[++parser::index], ']'))
- goto label_brackets;
- else
- {
- --parser::index;
- goto label_c_open;
- }
- break;
- default:
- if (detail::is_letter(parser::command[parser::index]))
- {
- while (detail::is_letter_or_digit(parser::command[parser::index + 1]))
- {
- token::value += parser::command[parser::index];
- ++parser::index;
- }
- token::val_type = token::string;
- }
- else if (detail::is_digit(parser::command[parser::index]))
- {
- while (detail::is_digit(parser::command[parser::index + 1]))
- {
- token::value += parser::command[parser::index];
- ++parser::index;
- }
- token::val_type = token::integer;
- }
- token::value += parser::command[parser::index];
- switch (token::val_type) {
- case token::string:
- goto label_identifier;
- case token::integer:
- goto label_numeric;
- }
- }
- return token::unknow;
- label_c_open: return token::c_open;
- label_brackets: return token::brackets;
- label_numeric: return token::numeric;
- label_identifier: return token::identifier;
- }
- const std::string_view to_str(const token::value_type tok) {
- switch (tok) {
- case token::none_val:
- return "NONE_VAL";
- case token::integer:
- return "INTEGER";
- case token::string:
- return "STRING";
- case token::spawn:
- return "SPAWN";
- case token::kill:
- return "KILL";
- case token::set:
- return "SET";
- case token::color:
- return "COLOR";
- case token::shape:
- return "SHAPE";
- case token::coords:
- return "COORDS";
- case token::radio:
- return "RADIO";
- case token::count:
- return "COUNT";
- case token::except:
- return "EXCEPT";
- }
- return "";
- }
- const std::string_view to_str(const token::token_type tok)
- {
- switch (tok) {
- case token::identifier:
- return "IDENTIFIER";
- case token::numeric:
- return "NUMERIC";
- case token::unknow:
- return "UNKNOW";
- case token::end:
- return "END";
- case token::none_tok:
- return "NONE_TOK";
- case token::slash:
- return "/";
- case token::c_open:
- return "[";
- case token::brackets:
- return "[]";
- case token::c_close:
- return "]";
- case token::ll_open:
- return "{";
- case token::ll_close:
- return "}";
- case token::equal:
- return "=";
- case token::comma:
- return ",";
- case token::colon:
- return ":";
- }
- return "";
- }
- }
- namespace parser {
- using namespace lexer;
- //
- struct State {
- enum state_type {
- none_stte,
- };
- State() : type(none_stte) {}
- void clear() {
- type = none_stte;
- }
- state_type type;
- } cur_state;
- //
- void init()
- {
- command.clear();
- index = -1;
- lexer::cur_token.clear();
- parser::cur_state.clear();
- }
- void parse(std::string str)
- {
- using namespace lexer;
- parser::init();
- command = std::move(str);
- while (cur_token.tok_type != token::end)
- {
- cur_token.tok_type = get_token();
- }
- }
- }
- }
- #endif //P_arser_cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement