scanner.rb 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. require 'strscan'
  2. module Journey
  3. class Scanner
  4. def initialize
  5. @ss = nil
  6. end
  7. def scan_setup str
  8. @ss = StringScanner.new str
  9. end
  10. def eos?
  11. @ss.eos?
  12. end
  13. def pos
  14. @ss.pos
  15. end
  16. def pre_match
  17. @ss.pre_match
  18. end
  19. def next_token
  20. return if @ss.eos?
  21. until token = scan || @ss.eos?; end
  22. token
  23. end
  24. private
  25. def scan
  26. case
  27. # /
  28. when text = @ss.scan(/\//)
  29. [:SLASH, text]
  30. when text = @ss.scan(/\*/)
  31. [:STAR, text]
  32. when text = @ss.scan(/\(/)
  33. [:LPAREN, text]
  34. when text = @ss.scan(/\)/)
  35. [:RPAREN, text]
  36. when text = @ss.scan(/\|/)
  37. [:OR, text]
  38. when text = @ss.scan(/\./)
  39. [:DOT, text]
  40. when text = @ss.scan(/:\w+/)
  41. [:SYMBOL, text]
  42. when text = @ss.scan(/[\w%\-~]+/)
  43. [:LITERAL, text]
  44. # any char
  45. when text = @ss.scan(/./)
  46. [:LITERAL, text]
  47. end
  48. end
  49. end
  50. end