Class: C::Expression

Inherits:
Node
  • Object
show all
Defined in:
lib/cast/parse.rb,
lib/cast/c_nodes.rb

Constant Summary

Constants inherited from Node

Node::INSPECT_TAB

Instance Attribute Summary

Attributes inherited from Node

#parent, #pos, #subclasses

Class Method Summary collapse

Methods inherited from Node

#==, #=~, abstract, add_field, #assert_invariants, #attached?, child, #clone, #depth_first, #detach, #detached?, #dup, #each, #eql?, field, #fields, fields, #hash, inherited, initializer, #insert_next, #insert_prev, #inspect, inspect1, #list_next, #list_prev, #match?, #method_missing, new_at, #next, #node_after, #node_before, #postorder, #preorder, #prev, #remove_node, #replace_node, #replace_with, #reverse_depth_first, #reverse_each, #reverse_postorder, #reverse_preorder, subclasses_recursive, #swap_with

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class C::Node

Class Method Details

.parse(s, parser = nil) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cast/parse.rb', line 207

def self.parse(s, parser=nil)
  parser ||= C.default_parser
  if s =~ /\A(\s*(\/\*.*?\*\/)?)*\{/
    # starts with a brace -- must be a CompoundLiteral.  in this
    # case, use a Declarator since only those can handle typeless
    # CompoundLiterals.
    ents = parser.parse("int i = #{s};").entities
    if ents.length == 1                 &&  # 1; int i = 1
        ents[0].declarators.length == 1 &&  # 1, j = 2
        ents[0].declarators[0].init.is_a?(self)
      return ents[0].declarators[0].init.detach
    else
      raise ParseError, "invalid #{self}"
    end
  else
    ents = parser.parse("void f() {#{s};}").entities
    if ents.length == 1                                 &&  # } void f() {
        ents[0].def.stmts.length == 1                   &&  # ;
        ents[0].def.stmts[0].is_a?(ExpressionStatement) &&  # int i
        ents[0].def.stmts[0].expr.is_a?(self)
      return ents[0].def.stmts[0].expr.detach
    else
      raise ParseError, "invalid #{self}"
    end
  end
end