Guest User

Untitled

a guest
Jul 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. (defn tokenize
  2. [^String command]
  3. (let [[escaped?
  4. current-arg
  5. args
  6. state] (loop [cmd command
  7. escaped? false
  8. state :no-token
  9. current-arg ""
  10. args []]
  11. (if (or (nil? cmd)
  12. (zero? (count cmd)))
  13. [escaped? current-arg args state]
  14. (let [char ^Character (first cmd)]
  15. (if escaped?
  16. (recur (rest cmd) false state (str current-arg char) args)
  17. (case state
  18. :single-quote (if (= char \\)
  19. (recur (rest cmd) escaped? :normal current-arg args)
  20. (recur (rest cmd) escaped? state (str current-arg char) args))
  21. :double-quote (case char
  22. \" (recur cmd escaped? :normal current-arg args)
  23. \\ (let [next (second cmd)]
  24. (if (or (= next \")
  25. (= next \\))
  26. (recur (drop 2 cmd) escaped? state (str current-arg next) args)
  27. (recur (drop 2 cmd) escaped? state (str current-arg char next) args)))
  28. (recur (rest cmd) escaped? state (str current-arg char) args))
  29. (:no-token :normal) (case char
  30. \\ (recur (rest cmd) true :normal current-arg args)
  31. \' (recur (rest cmd) escaped? :single-quote current-arg args)
  32. \" (recur (rest cmd) escaped? :double-quote current-arg args)
  33. (if (not (Character/isWhitespace char))
  34. (recur (rest cmd) escaped? :normal (str current-arg char) args)
  35. (if (= state :normal)
  36. (recur (rest cmd) escaped? :no-token "" (conj args current-arg))
  37. (recur (rest cmd) escaped? state current-arg args))))
  38. (throw (IllegalStateException. (format "Tokenizer is in an invalid state: %s" state))))))))]
  39. (if escaped?
  40. (conj args (str current-arg \\))
  41. (if (not= state :no-token)
  42. (conj args current-arg)
  43. args))))
Add Comment
Please, Sign In to add comment