Class: Rbexy::Parser

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

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(tokens)
  @tokens = tokens
  @position = 0
end

Instance Attribute Details

#positionObject

Returns the value of attribute position.



6
7
8
# File 'lib/rbexy/parser.rb', line 6

def position
  @position
end

#tokensObject (readonly)

Returns the value of attribute tokens.



5
6
7
# File 'lib/rbexy/parser.rb', line 5

def tokens
  @tokens
end

Instance Method Details

#parseObject



13
14
15
16
# File 'lib/rbexy/parser.rb', line 13

def parse
  validate_tokens!
  Nodes::Template.new(parse_tokens)
end

#parse_attrObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbexy/parser.rb', line 102

def parse_attr
  name = take!(:ATTR_NAME)[1]
  value = nil

  if take(:OPEN_ATTR_VALUE)
    value = parse_text || parse_expression
    raise ParseError, "Missing attribute value" unless value
    take(:CLOSE_ATTR_VALUE)
  else
    value = default_empty_attr_value
  end

  Nodes::XmlAttr.new(name, value)
end

#parse_attrsObject



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

def parse_attrs
  return [] unless take(:OPEN_ATTRS)

  attrs = []

  eventually!(:CLOSE_ATTRS)
  until take(:CLOSE_ATTRS)
    attrs << (parse_splat_attr || parse_silent_newline || parse_attr)
  end

  attrs
end

#parse_childrenObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rbexy/parser.rb', line 117

def parse_children
  children = []

  eventually!(:OPEN_TAG_END)
  until take(:OPEN_TAG_END)
    children << parse_token
  end

  take(:TAG_NAME)
  take!(:CLOSE_TAG_END)

  children
end

#parse_expressionObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbexy/parser.rb', line 37

def parse_expression
  return unless take(:OPEN_EXPRESSION)

  statements = []

  eventually!(:CLOSE_EXPRESSION)
  until take(:CLOSE_EXPRESSION)
    statements << (parse_expression_body || parse_tag)
  end

  Nodes::ExpressionGroup.new(statements)
end

#parse_expression!Object



50
51
52
53
# File 'lib/rbexy/parser.rb', line 50

def parse_expression!
  peek!(:OPEN_EXPRESSION)
  parse_expression
end

#parse_expression_bodyObject



55
56
57
58
# File 'lib/rbexy/parser.rb', line 55

def parse_expression_body
  return unless token = take(:EXPRESSION_BODY)
  Nodes::Expression.new(token[1])
end

#parse_silent_newlineObject



97
98
99
100
# File 'lib/rbexy/parser.rb', line 97

def parse_silent_newline
  return unless take(:SILENT_NEWLINE)
  Nodes::SilentNewline.new
end

#parse_splat_attrObject



88
89
90
91
92
93
94
95
# File 'lib/rbexy/parser.rb', line 88

def parse_splat_attr
  return unless take(:OPEN_ATTR_SPLAT)

  expression = parse_expression!
  take!(:CLOSE_ATTR_SPLAT)

  expression
end

#parse_tagObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rbexy/parser.rb', line 60

def parse_tag
  return unless take(:OPEN_TAG_DEF)

  name = take!(:TAG_NAME)
  members = []
  members.concat(take_all(:SILENT_NEWLINE).map { Nodes::SilentNewline.new })
  members.concat(parse_attrs)

  take!(:CLOSE_TAG_DEF)

  children = parse_children

  Nodes::XmlNode.new(name[1], members, children)
end

#parse_textObject



32
33
34
35
# File 'lib/rbexy/parser.rb', line 32

def parse_text
  return unless token = take(:TEXT)
  Nodes::Text.new(token[1])
end

#parse_tokenObject



28
29
30
# File 'lib/rbexy/parser.rb', line 28

def parse_token
  parse_text || parse_silent_newline || parse_expression || parse_tag || parse_declaration
end

#parse_tokensObject



18
19
20
21
22
23
24
25
26
# File 'lib/rbexy/parser.rb', line 18

def parse_tokens
  results = []

  while result = parse_token
    results << result
  end

  results
end