Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local parseCommand = function(str)
- --[[
- * Lua uses the \ for escaping characters - not the pattern part though, that
- uses the % - so every character that is after a % is an escape sequence.
- * Here is a common one: %s - this is a character set - contains all whitespace
- characters, including \t, space itself, etc.
- * + is suffix. It means that we have more than 0 of the character
- (or character set, like %s) which it follows
- * ( and ) enclose a capture - .gsub returns the content of every capture, or if
- there is no capture at all, the whole pattern
- * . means ANY character
- * as you can see, we captured a sequence of characters with "(.+)" - the pattern
- looks for SOME whitespaces at the beginning, then for some characters of ANY kind,
- then whitespaces again - we caprute the ANY character part, and that's what .gsub
- returns - yay, we have the full command without spaces on its ends
- ]]
- str = str:gsub("%s+(.+)%s+")
- local words = {}
- --[[
- * .gmatch is an iterator - it creates a table which contains all occurences of the
- pattern in the string - then it returns a function which - if used properly in a
- for loop, iterates through that table
- * ^ means NOT, so [^%s] is a pattern which contains no whitespaces, but does contain
- something (hence the +) - result: we have a words table filled with the words
- ]]
- for word in str:gmatch("[^%s]+") do table.insert(words, word) end
- -- now words[1] contains the command itself
- if words[1] then
- -- you can go on from now I think
- if words[1] == "open" and words[3] == "door" then
- elseif words[1] == "close" and words[3] == "door" then
- elseif words[1] == "turn" and words[3] == "lights" then
- elseif words[1] == "add" then
- elseif words[1] == "remove" then
- else
- -- prints an error message about words[1] not being a real command
- end
- else
- -- str was empty
- -- you can remove this branch of course
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement