|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Interscript |
| 4 | + module Isc |
| 5 | + # Serializes a Node::Document back to ISC source — the inverse of |
| 6 | + # Parser + DocumentBuilder + NodeAdapter. The Python compiler |
| 7 | + # bridge generates ISC from in-memory documents because the |
| 8 | + # rewritten python runtime executes ISC sources, not compiled |
| 9 | + # modules. Every construct the generator emits must round-trip: |
| 10 | + # spec/interscript/isc/generator_spec.rb pins behavior equality. |
| 11 | + class Generator |
| 12 | + def self.generate(doc) |
| 13 | + new(doc).generate |
| 14 | + end |
| 15 | + |
| 16 | + def initialize(doc) |
| 17 | + @doc = doc |
| 18 | + end |
| 19 | + |
| 20 | + def generate |
| 21 | + out = "system #{quote(@doc.name.to_s)} {\n" |
| 22 | + out << generate_aliases |
| 23 | + out << generate_dependencies |
| 24 | + @doc.stages.each do |name, group| |
| 25 | + out << " stage #{name} {\n" << generate_group(group, 2) << " }\n" |
| 26 | + end |
| 27 | + out << "}\n" |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def generate_aliases |
| 33 | + return "" if @doc.aliases.empty? |
| 34 | + |
| 35 | + out = +" aliases {\n" |
| 36 | + @doc.aliases.each_value do |alias_def| |
| 37 | + out << " #{alias_def.name} = #{item(alias_def.data)}\n" |
| 38 | + end |
| 39 | + out << " }\n" |
| 40 | + end |
| 41 | + |
| 42 | + def generate_dependencies |
| 43 | + return "" if @doc.dependencies.empty? |
| 44 | + |
| 45 | + out = +"" |
| 46 | + @doc.dependencies.each do |dep| |
| 47 | + out << " dependency #{quote(dep.full_name.to_s)}" |
| 48 | + out << " as #{dep.name}" if dep.name |
| 49 | + out << "\n" |
| 50 | + end |
| 51 | + out |
| 52 | + end |
| 53 | + |
| 54 | + def generate_group(group, indent) |
| 55 | + Array(group.children).map { |child| emit(child, indent) }.join |
| 56 | + end |
| 57 | + |
| 58 | + def emit(child, indent) |
| 59 | + pad = " " * indent |
| 60 | + case child |
| 61 | + when Interscript::Node::Group::Parallel |
| 62 | + "#{pad}parallel {\n#{generate_group(child, indent + 2)}#{pad}}\n" |
| 63 | + when Interscript::Node::Group |
| 64 | + generate_group(child, indent) |
| 65 | + when Interscript::Node::Rule::Sub |
| 66 | + rule(child, pad) |
| 67 | + when Interscript::Node::Rule::Run |
| 68 | + "#{pad}run #{run_target(child)}\n" |
| 69 | + when Interscript::Node::Rule::Funcall |
| 70 | + funcall(child, pad) |
| 71 | + else |
| 72 | + raise UnsupportedConstruct, "cannot generate #{child.class} as a stage item" |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + def rule(sub, pad) |
| 77 | + out = "#{pad}sub {\n" |
| 78 | + out << "#{pad} from #{item(sub.from)}\n" |
| 79 | + out << "#{pad} to #{target(sub.to)}\n" |
| 80 | + {before: sub.before, after: sub.after, |
| 81 | + not_before: sub.not_before, not_after: sub.not_after}.each do |kw, value| |
| 82 | + out << "#{pad} #{kw} #{item(value)}\n" if value |
| 83 | + end |
| 84 | + out << "#{pad}}\n" |
| 85 | + end |
| 86 | + |
| 87 | + def target(to) |
| 88 | + return to.to_s if %i[upcase downcase].include?(to) |
| 89 | + |
| 90 | + item(to) |
| 91 | + end |
| 92 | + |
| 93 | + def run_target(run) |
| 94 | + stage = run.stage |
| 95 | + stage.map ? "map.#{stage.map}.stage.#{stage.name}" : "stage.#{stage.name}" |
| 96 | + end |
| 97 | + |
| 98 | + def funcall(rule, pad) |
| 99 | + unless rule.kwargs.empty? |
| 100 | + raise UnsupportedConstruct, |
| 101 | + "cannot generate #{rule.name}(...) with kwargs as ISC" |
| 102 | + end |
| 103 | + |
| 104 | + "#{pad}#{rule.name}\n" |
| 105 | + end |
| 106 | + |
| 107 | + # -- items -- |
| 108 | + |
| 109 | + def item(i) |
| 110 | + case i |
| 111 | + when Interscript::Node::Item::String |
| 112 | + quote(i.data) |
| 113 | + when Interscript::Node::Item::Any |
| 114 | + any(i) |
| 115 | + when Interscript::Node::Item::Alias |
| 116 | + identifier(i.name) |
| 117 | + when Interscript::Node::Item::CaptureGroup |
| 118 | + "capture(#{item(i.data)})" |
| 119 | + when Interscript::Node::Item::CaptureRef |
| 120 | + "ref(#{i.id})" |
| 121 | + when Interscript::Node::Item::Group |
| 122 | + i.children.map { |c| item(c) }.join(" + ") |
| 123 | + when Interscript::Node::Item::Maybe |
| 124 | + "maybe(#{item(i.data)})" |
| 125 | + when Interscript::Node::Item::Some |
| 126 | + "some(#{item(i.data)})" |
| 127 | + when ::String |
| 128 | + quote(i) |
| 129 | + when ::Symbol |
| 130 | + identifier(i) |
| 131 | + else |
| 132 | + raise UnsupportedConstruct, "cannot generate #{i.class} as an item" |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def any(node) |
| 137 | + case node.value |
| 138 | + when ::String |
| 139 | + "any(#{quote(node.value)})" |
| 140 | + when ::Range |
| 141 | + "any(#{quote(node.value.first)}..#{quote(node.value.last)})" |
| 142 | + when ::Array |
| 143 | + inner = node.value.map { |v| item(v) } |
| 144 | + return "any(#{inner.first})" if inner.size == 1 |
| 145 | + |
| 146 | + "any([#{inner.join(", ")}])" |
| 147 | + else |
| 148 | + raise UnsupportedConstruct, "cannot generate Any of #{node.value.class}" |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + def identifier(sym) |
| 153 | + name = sym.to_s |
| 154 | + raise UnsupportedConstruct, "#{name.inspect} is not a bare identifier" unless /\A[A-Za-z_][A-Za-z0-9_]*\z/.match?(name) |
| 155 | + |
| 156 | + name |
| 157 | + end |
| 158 | + |
| 159 | + ESCAPE_MAP = { |
| 160 | + "\\" => "\\\\", |
| 161 | + "\"" => "\\\"" |
| 162 | + }.freeze |
| 163 | + |
| 164 | + def quote(str) |
| 165 | + escaped = str.to_s.gsub(/[\\"]/) { |c| ESCAPE_MAP.fetch(c) } |
| 166 | + # Only control and DEL characters need escaping; both parsers |
| 167 | + # read raw UTF-8, including astral code points (surrogate-pair |
| 168 | + # \u escapes are NOT re-joined). |
| 169 | + escaped = escaped.each_char.map { |c| |
| 170 | + (c.ord < 0x20 || c.ord == 0x7f) ? format("\\u%04x", c.ord) : c |
| 171 | + }.join |
| 172 | + "\"" + escaped + "\"" |
| 173 | + end |
| 174 | + |
| 175 | + class UnsupportedConstruct < StandardError; end |
| 176 | + end |
| 177 | + end |
| 178 | +end |
0 commit comments