Class: Liquid::Lexer
- Inherits:
-
Object
- Object
- Liquid::Lexer
- Defined in:
- lib/liquid/lexer.rb
Constant Summary collapse
- SPECIALS =
{ '|'.freeze => :pipe, '.'.freeze => :dot, ':'.freeze => :colon, ','.freeze => :comma, '['.freeze => :open_square, ']'.freeze => :close_square, '('.freeze => :open_round, ')'.freeze => :close_round }
- IDENTIFIER =
/[\w\-?!]+/
- SINGLE_STRING_LITERAL =
/'[^\']*'/
- DOUBLE_STRING_LITERAL =
/"[^\"]*"/
- NUMBER_LITERAL =
/-?\d+(\.\d+)?/
- DOTDOT =
/\.\./
- COMPARISON_OPERATOR =
/==|!=|<>|<=?|>=?|contains/
Instance Method Summary collapse
-
#initialize(input) ⇒ Lexer
constructor
A new instance of Lexer.
- #tokenize ⇒ Object
Constructor Details
#initialize(input) ⇒ Lexer
Returns a new instance of Lexer.
21 22 23 |
# File 'lib/liquid/lexer.rb', line 21 def initialize(input) @ss = StringScanner.new(input.rstrip) end |
Instance Method Details
#tokenize ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/liquid/lexer.rb', line 25 def tokenize @output = [] while !@ss.eos? @ss.skip(/\s*/) tok = case when t = @ss.scan(COMPARISON_OPERATOR) then [:comparison, t] when t = @ss.scan(SINGLE_STRING_LITERAL) then [:string, t] when t = @ss.scan(DOUBLE_STRING_LITERAL) then [:string, t] when t = @ss.scan(NUMBER_LITERAL) then [:number, t] when t = @ss.scan(IDENTIFIER) then [:id, t] when t = @ss.scan(DOTDOT) then [:dotdot, t] else c = @ss.getch if s = SPECIALS[c] [s,c] else raise SyntaxError, "Unexpected character #{c}" end end @output << tok end @output << [:end_of_string] end |