summaryrefslogtreecommitdiff
path: root/prism/templates/lib/prism/reflection.rb.erb
blob: 3c1d61c6c1ac91ee51805c3880c13c4d4b71e6da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module Prism
  # The Reflection module provides the ability to reflect on the structure of
  # the syntax tree itself, as opposed to looking at a single syntax tree. This
  # is useful in metaprogramming contexts.
  module Reflection
    # A field represents a single piece of data on a node. It is the base class
    # for all other field types.
    class Field
      # The name of the field.
      attr_reader :name

      # Initializes the field with the given name.
      def initialize(name)
        @name = name
      end
    end

    # A node field represents a single child node in the syntax tree. It
    # resolves to a Prism::Node in Ruby.
    class NodeField < Field
    end

    # An optional node field represents a single child node in the syntax tree
    # that may or may not be present. It resolves to either a Prism::Node or nil
    # in Ruby.
    class OptionalNodeField < Field
    end

    # A node list field represents a list of child nodes in the syntax tree. It
    # resolves to an array of Prism::Node instances in Ruby.
    class NodeListField < Field
    end

    # A constant field represents a constant value on a node. Effectively, it
    # represents an identifier found within the source. It resolves to a symbol
    # in Ruby.
    class ConstantField < Field
    end

    # An optional constant field represents a constant value on a node that may
    # or may not be present. It resolves to either a symbol or nil in Ruby.
    class OptionalConstantField < Field
    end

    # A constant list field represents a list of constant values on a node. It
    # resolves to an array of symbols in Ruby.
    class ConstantListField < Field
    end

    # A string field represents a string value on a node. It almost always
    # represents the unescaped value of a string-like literal. It resolves to a
    # string in Ruby.
    class StringField < Field
    end

    # A location field represents the location of some part of the node in the
    # source code. For example, the location of a keyword or an operator. It
    # resolves to a Prism::Location in Ruby.
    class LocationField < Field
    end

    # An optional location field represents the location of some part of the
    # node in the source code that may or may not be present. It resolves to
    # either a Prism::Location or nil in Ruby.
    class OptionalLocationField < Field
    end

    # An integer field represents an integer value. It is used to represent the
    # value of an integer literal, the depth of local variables, and the number
    # of a numbered reference. It resolves to an Integer in Ruby.
    class IntegerField < Field
    end

    # A float field represents a double-precision floating point value. It is
    # used exclusively to represent the value of a floating point literal. It
    # resolves to a Float in Ruby.
    class FloatField < Field
    end

    # A flags field represents a bitset of flags on a node. It resolves to an
    # integer in Ruby. Note that the flags cannot be accessed directly on the
    # node because the integer is kept private. Instead, the various flags in
    # the bitset should be accessed through their query methods.
    class FlagsField < Field
      # The names of the flags in the bitset.
      attr_reader :flags

      # Initializes the flags field with the given name and flags.
      def initialize(name, flags)
        super(name)
        @flags = flags
      end
    end

    # Returns the fields for the given node.
    def self.fields_for(node)
      case node.type
      <%- nodes.each do |node| -%>
      when :<%= node.human %>
        [<%= node.fields.map { |field|
          case field
          when Prism::Template::NodeField
            "NodeField.new(:#{field.name})"
          when Prism::Template::OptionalNodeField
            "OptionalNodeField.new(:#{field.name})"
          when Prism::Template::NodeListField
            "NodeListField.new(:#{field.name})"
          when Prism::Template::ConstantField
            "ConstantField.new(:#{field.name})"
          when Prism::Template::OptionalConstantField
            "OptionalConstantField.new(:#{field.name})"
          when Prism::Template::ConstantListField
            "ConstantListField.new(:#{field.name})"
          when Prism::Template::StringField
            "StringField.new(:#{field.name})"
          when Prism::Template::LocationField
            "LocationField.new(:#{field.name})"
          when Prism::Template::OptionalLocationField
            "OptionalLocationField.new(:#{field.name})"
          when Prism::Template::UInt8Field, Prism::Template::UInt32Field, Prism::Template::IntegerField
            "IntegerField.new(:#{field.name})"
          when Prism::Template::DoubleField
            "FloatField.new(:#{field.name})"
          when Prism::Template::FlagsField
            found = flags.find { |flag| flag.name == field.kind }.tap { |found| raise "Expected to find #{field.kind}" unless found }
            "FlagsField.new(:#{field.name}, [#{found.values.map { |value| ":#{value.name.downcase}?" }.join(", ")}])"
          else
            raise field.class.name
          end
        }.join(", ") %>]
      <%- end -%>
      else
        raise "Unknown node type: #{node.type.inspect}"
      end
    end
  end
end