aboutsummaryrefslogtreecommitdiff
path: root/syntax_tree.lua
blob: 64b4efc16646ca36688cb56d880868ccd33bc9a5 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
local repr
repr = require('utils').repr
local insert, remove, concat
do
  local _obj_0 = table
  insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
end
local Source
Source = require("code_obj").Source
local unpack = unpack or table.unpack
local AST = { }
AST.is_syntax_tree = function(n, t)
  if t == nil then
    t = nil
  end
  return type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n) and (t == nil or n.type == t)
end
local types = {
  "Number",
  "Var",
  "Block",
  "EscapedNomsu",
  "Text",
  "List",
  "Dict",
  "DictEntry",
  "IndexChain",
  "Action",
  "FileChunks"
}
for _index_0 = 1, #types do
  local name = types[_index_0]
  local cls = { }
  do
    cls.__class = cls
    cls.__index = cls
    cls.__name = name
    cls.type = name
    cls.is_instance = function(self, x)
      return getmetatable(x) == self
    end
    cls.__tostring = function(self)
      return tostring(self.type) .. "(" .. tostring(repr(tostring(self.source))) .. ", " .. tostring(concat((function()
        local _accum_0 = { }
        local _len_0 = 1
        for _index_1 = 1, #self do
          local v = self[_index_1]
          _accum_0[_len_0] = repr(v)
          _len_0 = _len_0 + 1
        end
        return _accum_0
      end)(), ', ')) .. ")"
    end
    cls.map = function(self, fn)
      local replacement = fn(self)
      if replacement == false then
        return nil
      end
      if replacement then
        replacement = (replacement.__class)(self.source, unpack(replacement))
      else
        local replacements = { }
        local changes = false
        for i, v in ipairs(self) do
          local _continue_0 = false
          repeat
            replacements[#replacements + 1] = v
            if AST.is_syntax_tree(v) then
              local r = v:map(fn)
              if r == v or r == nil then
                _continue_0 = true
                break
              end
              changes = true
              replacements[#replacements] = r
            end
            _continue_0 = true
          until true
          if not _continue_0 then
            break
          end
        end
        if not (changes) then
          return self
        end
        replacement = (self.__class)(self.source, unpack(replacements))
      end
      if self.comments then
        do
          local _accum_0 = { }
          local _len_0 = 1
          local _list_0 = self.comments
          for _index_1 = 1, #_list_0 do
            local c = _list_0[_index_1]
            _accum_0[_len_0] = c
            _len_0 = _len_0 + 1
          end
          replacement.comments = _accum_0
        end
      end
      return replacement
    end
    cls.__eq = function(self, other)
      if type(self) ~= type(other) or #self ~= #other or getmetatable(self) ~= getmetatable(other) then
        return false
      end
      for i = 1, #self do
        if self[i] ~= other[i] then
          return false
        end
      end
      return true
    end
  end
  AST[name] = setmetatable(cls, {
    __tostring = function(self)
      return self.name
    end,
    __call = function(self, source, ...)
      if type(source) == 'string' then
        source = Source:from_string(source)
      end
      for i = 1, select('#', ...) do
        assert(select(i, ...))
      end
      assert(Source:is_instance(source))
      local inst = {
        source = source,
        ...
      }
      setmetatable(inst, self)
      if inst.__init then
        inst:__init()
      end
      return inst
    end
  })
end
AST.Action.__init = function(self)
  local stub_bits
  do
    local _accum_0 = { }
    local _len_0 = 1
    for _index_0 = 1, #self do
      local a = self[_index_0]
      _accum_0[_len_0] = type(a) == 'string' and a or '%'
      _len_0 = _len_0 + 1
    end
    stub_bits = _accum_0
  end
  self.stub = concat(stub_bits, " ")
end
AST.Action.get_args = function(self)
  local _accum_0 = { }
  local _len_0 = 1
  for _index_0 = 1, #self do
    local tok = self[_index_0]
    if type(tok) ~= 'string' then
      _accum_0[_len_0] = tok
      _len_0 = _len_0 + 1
    end
  end
  return _accum_0
end
return AST