diff --git a/mods/CORE/mcl_commands/string_parser.lua b/mods/CORE/mcl_commands/string_parser.lua new file mode 100644 index 000000000..917cac9c4 --- /dev/null +++ b/mods/CORE/mcl_commands/string_parser.lua @@ -0,0 +1,67 @@ +--Similar to the StringParser class of mojang Brigadier library + +local string = string + +local SYNTAX_DOUBLE_QUOTE = "\"" +local SYNTAX_SINGLE_QUOTE = "\'" + +local StringParser = {} + +function StringParser:init(str) + self.cursor = 1 + self.str = str +end + +function StringParser:get_string() + return self.str +end + +function StringParser:set_cursor(cursor) + self.cursor = cursor +end + +function StringParser:get_cursor() + return self.cursor +end + +function StringParser:get_remaining_length() + return string.len(self.str) - self.cursor +end + +function StringParser:get_total_length() + return string.len(self.str) +end + +function StringParser:get_read() + return string.sub(self.str, 1, self.cursor) +end + +function StringParser:get_remaining() + return string.sub(self.str, self.cursor) +end + +function StringParser:can_read(length) + return (self.cursor + (length or 1)) <= string.len(self.str) +end + +function StringParser:peek(offset) + return string.sub(self.cursor + (offset or 0), self.cursor + (offset or 0)) +end + +function StringParser:read() + local r = string.sub(self.cursor, self.cursor) + self.cursor = self.cursor + 1 + return r +end + +function StringParser:skip() + self.cursor = self.cursor + 1 +end + +function StringParser.is_allowed_number() + return false +end + +function StringParser.is_quoted_string_start(char) + return char == SYNTAX_DOUBLE_QUOTE or char == SYNTAX_SINGLE_QUOTE +end \ No newline at end of file