interscript 2.1.0a9 → 2.1.0b1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 863d75eaef5d110e3460efa16357dd784120a11a119d5e33d6551727f4c9f33f
4
- data.tar.gz: 91d2a59dc9b03f9c7e8123f0cc0980e7bcde8dc0277f0e77732d80b0cd0e3508
3
+ metadata.gz: 5fc3d229c727e71662e0fd33a1e40424015f2c4caeea2b592a464554a3610ba1
4
+ data.tar.gz: f254317afa4d3d2372572df7b4751edcfb0fe2f24acc2ffc61978b613d062656
5
5
  SHA512:
6
- metadata.gz: dd8d30f472a7e3a049c8df13537c280d2b577956ce9439aeecc3164d533453c31bf2b66a662c9a8d09f43a610d3029e699113573c64bb005704b620c8bbb218f
7
- data.tar.gz: e002aa79cfd6bf3c07403a8a87342383e575b00ffb8e9cc96271a9e0a1390214d13a01a6cdb5665fbadc881eaefbbf915520ee18a12baac1d70bfae1eb37bbd3
6
+ metadata.gz: 69bf1648abe50c5a27dd3a1c3d96edfd011c790b35fcfdba78ea0dd85c2339497c44add107571b05de4c042889f4b1fe9edb766a31a0613bd1b7d526bc4257de
7
+ data.tar.gz: 155a553e164a7ce32cf13ead6beaffdeb449937087d86fb605ec7813f818a1985fdc36624db8669f86b2e0bea5377ea853d6e960ed821c811cc016416b857537
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
+ require "bundler/setup"
1
2
  require "bundler/gem_tasks"
2
3
  require "rspec/core/rake_task"
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
5
6
 
6
7
  task :compile, [:compiler, :target] do |t, args|
7
- require "bundler/setup"
8
8
  require "interscript"
9
9
 
10
10
  compiler, ext = case args[:compiler]
@@ -50,4 +50,67 @@ task :version, [:ver] do |t, ver|
50
50
  File.write(mapsfile, mapsver)
51
51
  end
52
52
 
53
+ task :generate_visualization_html do
54
+ require "fileutils"
55
+ require "interscript"
56
+ require "interscript/visualize"
57
+
58
+ FileUtils.rm_rf(dir = __dir__+"/visualizations/")
59
+ FileUtils.mkdir_p(dir)
60
+
61
+ Interscript.maps.each do |map|
62
+ html = Interscript::Visualize.(map)
63
+ File.write(dir+map+".html", html)
64
+ end
65
+ end
66
+
67
+ task :generate_metadata_json do
68
+ require "fileutils"
69
+ require "json"
70
+ require "interscript"
71
+
72
+ FileUtils.rm_rf(file = __dir__+"/metadata.json")
73
+
74
+ hash = Interscript.maps.map do |map|
75
+ parsed_map = Interscript.parse(map)
76
+ md = parsed_map.metadata.to_hash
77
+ md["test"] = parsed_map.tests&.data&.first
78
+ [map, md]
79
+ end.to_h
80
+
81
+ File.write(file, JSON.pretty_generate(hash))
82
+ end
83
+
84
+ task :generate_json do
85
+ require "fileutils"
86
+ require "json"
87
+ require "interscript"
88
+
89
+ FileUtils.rm_rf(dir = __dir__+"/json/")
90
+ FileUtils.mkdir_p(dir)
91
+
92
+ (Interscript.maps + Interscript.maps(libraries: true)).each do |map|
93
+ json = JSON.pretty_generate(Interscript.parse(map).to_hash)
94
+ File.write(dir+map+".json", json)
95
+ end
96
+ end
97
+
98
+ task :generate_visualization_json do
99
+ require "fileutils"
100
+ require "interscript"
101
+ require "json"
102
+ require "interscript/visualize"
103
+
104
+ FileUtils.rm_rf(dir = __dir__+"/vis_json/")
105
+ FileUtils.mkdir_p(dir)
106
+
107
+ (Interscript.maps + Interscript.maps(libraries: true)).each do |map_name|
108
+ map = Interscript.parse(map_name)
109
+ map.stages.each do |stage_name, stage|
110
+ json = JSON.pretty_generate(stage.to_visualization_array(map))
111
+ File.write(dir+map_name+"_#{stage_name}.json", json)
112
+ end
113
+ end
114
+ end
115
+
53
116
  task :default => :spec
@@ -7,6 +7,7 @@ module Interscript::DSL
7
7
 
8
8
  return @cache[map_name] if @cache[map_name]
9
9
  path = Interscript.locate(map_name)
10
+ library = path.end_with?(".iml")
10
11
  map_name = File.basename(path, ".imp")
11
12
  map_name = File.basename(map_name, ".iml")
12
13
 
@@ -45,7 +46,7 @@ module Interscript::DSL
45
46
  YAML.load(yaml, exc_fname)
46
47
  end
47
48
 
48
- md = Interscript::DSL::Metadata.new(yaml: true) do
49
+ md = Interscript::DSL::Metadata.new(yaml: true, map_name: map_name, library: library) do
49
50
  yaml.each do |k,v|
50
51
  public_send(k.to_sym, v)
51
52
  end
@@ -1,26 +1,68 @@
1
+ require 'date'
2
+
1
3
  class Interscript::DSL::Metadata
2
4
  attr_accessor :node
3
5
 
4
- def initialize(yaml: false, &block)
6
+ def initialize(yaml: false, map_name: "", library: true, &block)
5
7
  raise ArgumentError, "Can't evaluate metadata from Ruby context" unless yaml
8
+ @map_name = map_name
6
9
  @node = Interscript::Node::MetaData.new
7
10
  self.instance_exec(&block)
11
+ @node[:nonstandard] = {}
12
+
13
+ NECESSARY_KEYS.each do |i|
14
+ unless @node.data.key? i
15
+ warn "[#{@map_name}] Necessary key #{i} wasn't defined. Defaulting to an empty string"
16
+ @node[i] = ""
17
+ end
18
+ end unless library
19
+ end
20
+
21
+ STANDARD_STRING_KEYS = %i{authority_id id
22
+ language source_script destination_script
23
+ name url creation_date adoption_date description
24
+ character source confirmation_date}
25
+
26
+ STANDARD_ARRAY_KEYS = %i{notes}
27
+
28
+ NONSTANDARD_KEYS = %i{special_rules original_description original_notes
29
+ implementation_notes}
30
+
31
+ NECESSARY_KEYS = %i{name language source_script destination_script}
32
+
33
+ STANDARD_STRING_KEYS.each do |sym|
34
+ define_method sym do |stuff|
35
+ case stuff
36
+ when String, Integer, Date
37
+ @node[sym] = stuff.to_s
38
+ when NilClass
39
+ else
40
+ warn "[#{@map_name}] Metadata key #{sym} expects a String, but #{stuff.class} was given"
41
+ @node[sym] = stuff.inspect
42
+ end
43
+ end
8
44
  end
9
45
 
10
- %i{authority_id id language source_script
11
- destination_script name url creation_date
12
- adoption_date description character notes
13
- source confirmation_date}.each do |sym|
46
+ STANDARD_ARRAY_KEYS.each do |sym|
14
47
  define_method sym do |stuff|
15
- @node[sym] = stuff
48
+ stuff = Array(stuff)
49
+
50
+ stuff.map do |i|
51
+ case i
52
+ when String
53
+ i
54
+ else
55
+ warn "[#{@map_name}] Metadata key #{sym} expects all Array elements to be String"
56
+ i.inspect
57
+ end
58
+ end
16
59
  end
17
60
  end
18
61
 
19
- %i{special_rules original_description original_notes
20
- implementation_notes}.each do |sym|
62
+ NONSTANDARD_KEYS.each do |sym|
21
63
  define_method sym do |stuff|
22
- warn "Metadata key #{sym} is non-standard"
23
- @node[sym] = stuff
64
+ warn "[#{@map_name}] Metadata key #{sym} is non-standard"
65
+ (@node[:nonstandard] ||= {})[sym] = stuff
24
66
  end
25
67
  end
26
68
  end
@@ -51,8 +51,23 @@ class Interscript::Node::Item::Any < Interscript::Node::Item
51
51
  end
52
52
 
53
53
  def to_hash
54
- { :class => self.class.to_s,
55
- :data => self.data.map { |i| i.to_hash } }
54
+ hash = { :class => self.class.to_s }
55
+
56
+ case @value
57
+ when Array
58
+ hash[:type] = "Array"
59
+ hash[:data] = data.map { |i| i.to_hash }
60
+ when ::String
61
+ hash[:type] = "String"
62
+ hash[:data] = @value
63
+ when Range
64
+ hash[:type] = "Range"
65
+ hash[:data] = [@value.begin, @value.end]
66
+ when NilClass
67
+ hash[:type] = "nil (bug)"
68
+ end
69
+
70
+ hash
56
71
  end
57
72
 
58
73
  def inspect
@@ -35,15 +35,18 @@ class Interscript::Node::Rule::Sub < Interscript::Node::Rule
35
35
  def to_hash
36
36
  puts self.from.inspect if $DEBUG
37
37
  puts params.inspect if $DEBUG
38
- { :class => self.class.to_s,
38
+ hash = { :class => self.class.to_s,
39
39
  :from => self.from.to_hash,
40
- :to => self.to == :upcase ? :upcase : self.to.to_hash,
41
- :before => self.before&.to_hash,
42
- :not_before => self.not_before&.to_hash,
43
- :after => self.after&.to_hash,
44
- :not_after => self.not_after&.to_hash,
45
- :priority => self.priority
40
+ :to => Symbol === self.to ? self.to : self.to.to_hash
46
41
  }
42
+
43
+ hash[:before] = self.before&.to_hash if self.before
44
+ hash[:not_before] = self.not_before&.to_hash if self.not_before
45
+ hash[:after] = self.after&.to_hash if self.after
46
+ hash[:not_after] = self.not_after&.to_hash if self.not_after
47
+ hash[:priority] = self.priority if self.priority
48
+
49
+ hash
47
50
  end
48
51
 
49
52
  def inspect
@@ -1,3 +1,3 @@
1
1
  module Interscript
2
- VERSION = "2.1.0a9"
2
+ VERSION = "2.1.0b1"
3
3
  end
@@ -0,0 +1,61 @@
1
+ require 'erb'
2
+ require 'interscript/visualize/nodes'
3
+ require 'interscript/visualize/json'
4
+
5
+ def h(str)
6
+ str.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub('"', "&quot;")
7
+ end
8
+
9
+ class Interscript::Visualize
10
+ def self.def_template(template)
11
+ @template = ERB.new(File.read(__dir__+"/visualize/#{template}.html.erb"))
12
+ end
13
+ def get_binding; binding; end
14
+
15
+ def self.call(*args)
16
+ return Map.(*args) if self == Interscript::Visualize
17
+
18
+ tplctx = self.new(*args)
19
+ @template.result(tplctx.get_binding)
20
+ end
21
+
22
+ class Map < self
23
+ def_template :map
24
+
25
+ def initialize(map_name)
26
+ @map = Interscript.parse(map_name)
27
+ end
28
+
29
+ attr_reader :map
30
+
31
+ def render_stage(map_name, stage)
32
+ Stage.(map_name, stage)
33
+ end
34
+ end
35
+
36
+ class Group < self
37
+ def_template :group
38
+
39
+ def initialize(map, group, style=nil)
40
+ @map = map
41
+ @group = group
42
+ @style = style
43
+ end
44
+
45
+ attr_reader :map, :group
46
+
47
+ def render_group(map, group, style=nil)
48
+ Group.(map, group, style)
49
+ end
50
+ end
51
+
52
+ class Stage < Group
53
+ def_template :group
54
+
55
+ def initialize(map_name, stage_name, style=nil)
56
+ @map = Interscript.parse(map_name)
57
+ @group = map.stages[stage_name]
58
+ @style = style
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,59 @@
1
+ <table style='<%= @style %>'>
2
+ <% group.children.each do |rule| %>
3
+ <tr>
4
+ <% case rule
5
+ when Interscript::Node::Rule::Sub %>
6
+ <td>
7
+ Replace
8
+ <td>
9
+ <%= rule.from.to_html(map) %>
10
+ <td>
11
+ <%= Symbol === rule.to ? rule.to : rule.to.to_html(map) %>
12
+ <td>
13
+ <%=
14
+ out = []
15
+ out << "before: #{rule.before.to_html(map)}" if rule.before
16
+ out << "after: #{rule.after.to_html(map)}" if rule.after
17
+ out << "<nobr>not before:</nobr> #{rule.not_before.to_html(map)}" if rule.not_before
18
+ out << "<nobr>not after:</nobr> #{rule.not_after.to_html(map)}" if rule.not_after
19
+ out.join(", ")
20
+ %>
21
+ <% when Interscript::Node::Group::Parallel %>
22
+ <td>
23
+ Parallel
24
+ <td colspan='3'>
25
+ <%= render_group map, rule %>
26
+ </td>
27
+ <% when Interscript::Node::Rule::Funcall %>
28
+ <td>
29
+ <%= h rule.name.to_s.gsub("_", " ").gsub(/^(.)/, &:upcase) %>
30
+ <td>
31
+ <%=
32
+ rule.kwargs.map do |k,v|
33
+ "#{k.to_s.gsub("_", " ")}: #{v}"
34
+ end.join(", ")
35
+ %>
36
+ <% when Interscript::Node::Rule::Run %>
37
+ <td>
38
+ <nobr>Run</nobr>
39
+ <td>
40
+ <a href='#' onclick='this.parentNode.lastElementChild.style.display=this.parentNode.lastElementChild.style.display=="none"?"table":"none";return!1'>
41
+ <%= rule.stage.to_html(map) %>
42
+ </a>
43
+
44
+ <%=
45
+ if rule.stage.map
46
+ doc = map.dep_aliases[rule.stage.map].document
47
+ stage = doc.imported_stages[rule.stage.name]
48
+ else
49
+ doc = map
50
+ stage = map.imported_stages[rule.stage.name]
51
+ end
52
+
53
+ render_group doc, stage, "display: none"
54
+ %>
55
+ <% else %>
56
+ <td colspan='4'><pre><%= h rule.inspect %></pre>
57
+ <% end %>
58
+ <% end %>
59
+ </table>
@@ -0,0 +1,57 @@
1
+ class Interscript::Node::Group
2
+ def to_visualization_array(map=self)
3
+ out = []
4
+
5
+ self.children.each do |rule|
6
+ case rule
7
+ when Interscript::Node::Rule::Sub
8
+ more = []
9
+ more << "before: #{rule.before.to_html(map)}" if rule.before
10
+ more << "after: #{rule.after.to_html(map)}" if rule.after
11
+ more << "<nobr>not before:</nobr> #{rule.not_before.to_html(map)}" if rule.not_before
12
+ more << "<nobr>not after:</nobr> #{rule.not_after.to_html(map)}" if rule.not_after
13
+ more = more.join(", ")
14
+
15
+ out << {
16
+ type: "Replace",
17
+ from: rule.from.to_html(map),
18
+ to: Symbol === rule.to ? rule.to : rule.to.to_html(map),
19
+ more: more
20
+ }
21
+ when Interscript::Node::Group::Parallel
22
+ out << {
23
+ type: "Parallel",
24
+ children: rule.to_visualization_array(map)
25
+ }
26
+ when Interscript::Node::Rule::Funcall
27
+ out << {
28
+ type: rule.name.to_s.gsub("_", " ").gsub(/^(.)/, &:upcase),
29
+ more: rule.kwargs.map do |k,v|
30
+ "#{k.to_s.gsub("_", " ")}: #{v}"
31
+ end.join(", ")
32
+ }
33
+ when Interscript::Node::Rule::Run
34
+ if rule.stage.map
35
+ doc = map.dep_aliases[rule.stage.map].document
36
+ stage = rule.stage.name
37
+ else
38
+ doc = map
39
+ stage = rule.stage.name
40
+ end
41
+
42
+ out << {
43
+ type: "Run",
44
+ doc: doc.name,
45
+ stage: stage
46
+ }
47
+ else
48
+ out << {
49
+ type: "Unknown",
50
+ more: "<pre>#{h rule.inspect}</pre>"
51
+ }
52
+ end
53
+ end
54
+
55
+ out
56
+ end
57
+ end
@@ -0,0 +1,46 @@
1
+ <h1><%= h map.name %></h1>
2
+
3
+ <dl>
4
+ <% map.metadata.data.each do |k,v| %>
5
+ <% case k
6
+ when :authority_id %>
7
+ <dt>Authority ID
8
+ <% when :id %>
9
+ <dt>Standard ID
10
+ <% when :language %>
11
+ <dt>Language
12
+ <% when :source_script %>
13
+ <dt>Source script
14
+ <% when :destination_script %>
15
+ <dt>Destination script
16
+ <% when :name %>
17
+ <dt>Name
18
+ <% when :url %>
19
+ <dt>URL
20
+ <% when :creation_date %>
21
+ <dt>Creation date
22
+ <% when :confirmation_date %>
23
+ <dt>Confirmation date
24
+ <% when :adoption_date %>
25
+ <dt>Adoption date
26
+ <% when :description %>
27
+ <dt>Description
28
+ <% when :source %>
29
+ <dt>Source
30
+ <% when :notes, :implementation_notes, :special_rules, :original_notes, :original_description # We ignore notes for now %>
31
+ <% else %>
32
+ <% p [k, v, map.name] %>
33
+ <dt><%= h k %>
34
+ <% end %>
35
+
36
+ <% case k
37
+ when :url %>
38
+ <dd><a href="<%= h v %>"><%= h v %></a>
39
+ <% when :notes, :implementation_notes, :special_rules, :original_notes, :original_description # We ignore notes for now %>
40
+ <% else %>
41
+ <dd><%= h v %>
42
+ <% end %>
43
+ <% end %>
44
+ </dl>
45
+
46
+ <%= render_stage(self.map.name, :main) %>
@@ -0,0 +1,89 @@
1
+ class Interscript::Node::Item
2
+ class Alias < self
3
+ def to_html(doc)
4
+ if map
5
+ n = doc.dep_aliases[map].full_name
6
+ "#{name.to_s.gsub("_", " ")} from map #{n}"
7
+ else
8
+ "#{name.to_s.gsub("_", " ")}"
9
+ end
10
+ end
11
+ end
12
+
13
+ class Stage < self
14
+ def to_html(doc)
15
+ if map
16
+ n = doc.dep_aliases[map].full_name
17
+ "stage #{name.to_s.gsub("_", " ")} from map #{n}"
18
+ else
19
+ "#{name.to_s.gsub("_", " ")}"
20
+ end
21
+ end
22
+ end
23
+
24
+ class Any < self
25
+ def to_html(doc)
26
+ "<nobr>any (</nobr>" +
27
+ case @value
28
+ when Array
29
+ value.map(&Interscript::Node::Item.method(:try_convert)).map{|i|i.to_html(doc)}.join(", ")
30
+ when ::String
31
+ value.split("").map(&Interscript::Node::Item.method(:try_convert)).map{|i|i.to_html(doc)}.join(", ")
32
+ when Range
33
+ [value.begin, value.end].map(&Interscript::Node::Item.method(:try_convert)).map{|i|i.to_html(doc)}.join(" to ")
34
+ else
35
+ h(value.inspect)
36
+ end +
37
+ ")"
38
+ end
39
+ end
40
+
41
+ class CaptureGroup < self
42
+ def to_html(doc)
43
+ "<nobr>capture group (</nobr>" +
44
+ data.to_html(doc) +
45
+ ")"
46
+ end
47
+ end
48
+
49
+ class CaptureRef < self
50
+ def to_html(_)
51
+ "<nobr>capture reference (</nobr>" +
52
+ id.to_s +
53
+ ")"
54
+ end
55
+ end
56
+
57
+ class Group < self
58
+ def to_html(doc)
59
+ @children.map{|i|i.to_html(doc)}.join(" + ")
60
+ end
61
+ end
62
+
63
+ class Repeat < self
64
+ def to_html(doc)
65
+ str = case self
66
+ when Interscript::Node::Item::Maybe
67
+ "zero or one "
68
+ when Interscript::Node::Item::MaybeSome
69
+ "zero or more of "
70
+ when Interscript::Node::Item::Some
71
+ "one or more of "
72
+ end
73
+ "<nobr>#{str}(</nobr>#{@data.to_html(doc)})"
74
+ end
75
+ end
76
+
77
+ class String < self
78
+ def to_html(_)
79
+ out = ""
80
+ self.data.each_char do |i|
81
+ out << "<ruby>"
82
+ out << "<kbd>#{h i}</kbd>"
83
+ out << "<rt>#{"%04x" % i.ord}</rt>"
84
+ out << "</ruby>"
85
+ end
86
+ out
87
+ end
88
+ end
89
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interscript
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0a9
4
+ version: 2.1.0b1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -106,6 +106,11 @@ files:
106
106
  - lib/interscript/stdlib.rb
107
107
  - lib/interscript/utils/regexp_converter.rb
108
108
  - lib/interscript/version.rb
109
+ - lib/interscript/visualize.rb
110
+ - lib/interscript/visualize/group.html.erb
111
+ - lib/interscript/visualize/json.rb
112
+ - lib/interscript/visualize/map.html.erb
113
+ - lib/interscript/visualize/nodes.rb
109
114
  - requirements.txt
110
115
  homepage: https://www.interscript.com
111
116
  licenses: