Module: Interscript::DSL

Defined in:
lib/interscript/dsl.rb,
lib/interscript/utils/helpers.rb

Defined Under Namespace

Modules: Items, SymbolMM Classes: Aliases, Document, Group, Metadata, Stage, Tests

Class Method Summary collapse

Class Method Details

.original_parseObject



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
# File 'lib/interscript/utils/helpers.rb', line 30

def self.parse(map_name, reverse: true)
  # map name aliases? here may be a place to wrap it

  return @cache[map_name] if @cache[map_name]

  # This is a composition, so let's make a new virtual map
  # that calls all maps in a sequence.
  if map_name.include? "|"
    map_parts = map_name.split("|").map(&:strip)

    doc = Interscript::DSL::Document.new(map_name) do
      map_parts.each_with_index do |i, idx|
        dependency i, as: :"part#{idx}"
      end

      stage {
        map_parts.each_with_index do |i, idx|
          run map[:"part#{idx}"].stage.main
        end
      }
    end.node

    return @cache[map_name] = doc
  end

  path = begin
    Interscript.locate(map_name)
  rescue Interscript::MapNotFoundError => e
    # But maybe we called the map in a reversed fashion?
    begin
      raise e if reverse == false # Protect from an infinite loop
      reverse_name = Interscript::Node::Document.reverse_name(map_name)
      return @cache[map_name] = parse(reverse_name, reverse: false).reverse
    rescue Interscript::MapNotFoundError
      raise e
    end
  end
  library = path.end_with?(".iml")

  map_name = File.basename(path, ".imp")
  map_name = File.basename(map_name, ".iml")

  ruby = []
  yaml = []

  file = File.read(path).split("\n")
  exc_fname = File.expand_path(path, Dir.pwd)

  md_reading = false
  md_indent = nil
  md_inner_indent = nil
  file.each do |l|
    if md_reading && l =~ /\A#{md_indent}\}\s*\z/
      md_reading = false
    elsif md_reading
      ruby << ""
      yaml << l
    elsif l =~ /\A(\s*)metadata\s*\{\z/
      md_indent = $1
      md_reading = true
    else
      yaml << ""
      ruby << l
    end
  end
  raise Interscript::MapLogicError, "metadata stage isn't terminated" if md_reading
  ruby, yaml = ruby.join("\n"), yaml.join("\n")

  obj = Interscript::DSL::Document.new(map_name)
  obj.instance_eval ruby, exc_fname, 1

  yaml = if yaml =~ /\A\s*\z/
    {}
  else
    unsafe_load = if YAML.respond_to? :unsafe_load
      :unsafe_load
    else
      :load
    end
    YAML.public_send(unsafe_load, yaml, filename: exc_fname)
  end

  md = Interscript::DSL::Metadata.new(yaml: true, map_name: map_name, library: library) do
    yaml.each do |k,v|
      public_send(k.to_sym, v)
    end
  end
  obj.node. = md.node

  @cache[map_name] = obj.node
end

.parse(map_name, **kwargs) ⇒ Object



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
# File 'lib/interscript/dsl.rb', line 5

def self.parse(map_name, reverse: true)
  # map name aliases? here may be a place to wrap it

  return @cache[map_name] if @cache[map_name]

  # This is a composition, so let's make a new virtual map
  # that calls all maps in a sequence.
  if map_name.include? "|"
    map_parts = map_name.split("|").map(&:strip)

    doc = Interscript::DSL::Document.new(map_name) do
      map_parts.each_with_index do |i, idx|
        dependency i, as: :"part#{idx}"
      end

      stage {
        map_parts.each_with_index do |i, idx|
          run map[:"part#{idx}"].stage.main
        end
      }
    end.node

    return @cache[map_name] = doc
  end

  path = begin
    Interscript.locate(map_name)
  rescue Interscript::MapNotFoundError => e
    # But maybe we called the map in a reversed fashion?
    begin
      raise e if reverse == false # Protect from an infinite loop
      reverse_name = Interscript::Node::Document.reverse_name(map_name)
      return @cache[map_name] = parse(reverse_name, reverse: false).reverse
    rescue Interscript::MapNotFoundError
      raise e
    end
  end
  library = path.end_with?(".iml")

  map_name = File.basename(path, ".imp")
  map_name = File.basename(map_name, ".iml")

  ruby = []
  yaml = []

  file = File.read(path).split("\n")
  exc_fname = File.expand_path(path, Dir.pwd)

  md_reading = false
  md_indent = nil
  md_inner_indent = nil
  file.each do |l|
    if md_reading && l =~ /\A#{md_indent}\}\s*\z/
      md_reading = false
    elsif md_reading
      ruby << ""
      yaml << l
    elsif l =~ /\A(\s*)metadata\s*\{\z/
      md_indent = $1
      md_reading = true
    else
      yaml << ""
      ruby << l
    end
  end
  raise Interscript::MapLogicError, "metadata stage isn't terminated" if md_reading
  ruby, yaml = ruby.join("\n"), yaml.join("\n")

  obj = Interscript::DSL::Document.new(map_name)
  obj.instance_eval ruby, exc_fname, 1

  yaml = if yaml =~ /\A\s*\z/
    {}
  else
    unsafe_load = if YAML.respond_to? :unsafe_load
      :unsafe_load
    else
      :load
    end
    YAML.public_send(unsafe_load, yaml, filename: exc_fname)
  end

  md = Interscript::DSL::Metadata.new(yaml: true, map_name: map_name, library: library) do
    yaml.each do |k,v|
      public_send(k.to_sym, v)
    end
  end
  obj.node. = md.node

  @cache[map_name] = obj.node
end