Class: Liquid::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



3
4
5
6
7
# File 'lib/liquid/parser.rb', line 3

def initialize(input)
  l = Lexer.new(input)
  @tokens = l.tokenize
  @p = 0 # pointer to current location
end

Instance Method Details

#argumentObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/liquid/parser.rb', line 65

def argument
  str = ""
  # might be a keyword argument (identifier: expression)
  if look(:id) && look(:colon, 1)
    str << consume << consume << ' '.freeze
  end

  str << expression
  str
end

#consume(type = nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/liquid/parser.rb', line 13

def consume(type = nil)
  token = @tokens[@p]
  if type && token[0] != type
    raise SyntaxError, "Expected #{type} but found #{@tokens[@p].first}"
  end
  @p += 1
  token[1]
end

#consume?(type) ⇒ Boolean

Only consumes the token if it matches the type Returns the token’s contents if it was consumed or false otherwise.

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/liquid/parser.rb', line 25

def consume?(type)
  token = @tokens[@p]
  return false unless token && token[0] == type
  @p += 1
  token[1]
end

#expressionObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/liquid/parser.rb', line 47

def expression
  token = @tokens[@p]
  if token[0] == :id
    variable_signature
  elsif [:string, :number].include? token[0]
    consume
  elsif token.first == :open_round
    consume
    first = expression
    consume(:dotdot)
    last = expression
    consume(:close_round)
    "(#{first}..#{last})"
  else
    raise SyntaxError, "#{token} is not a valid expression"
  end
end

#id?(str) ⇒ Boolean

Like consume? Except for an :id token of a certain name

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/liquid/parser.rb', line 33

def id?(str)
  token = @tokens[@p]
  return false unless token && token[0] == :id
  return false unless token[1] == str
  @p += 1
  token[1]
end

#jump(point) ⇒ Object



9
10
11
# File 'lib/liquid/parser.rb', line 9

def jump(point)
  @p = point
end

#look(type, ahead = 0) ⇒ Object



41
42
43
44
45
# File 'lib/liquid/parser.rb', line 41

def look(type, ahead = 0)
  tok = @tokens[@p + ahead]
  return false unless tok
  tok[0] == type
end

#variable_signatureObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/liquid/parser.rb', line 76

def variable_signature
  str = consume(:id)
  while look(:open_square)
    str << consume
    str << expression
    str << consume(:close_square)
  end
  if look(:dot)
    str << consume
    str << variable_signature
  end
  str
end