resyma 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +31 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +69 -0
- data/LICENSE +674 -0
- data/README.md +167 -0
- data/Rakefile +8 -0
- data/lib/resyma/core/algorithm/engine.rb +189 -0
- data/lib/resyma/core/algorithm/matcher.rb +48 -0
- data/lib/resyma/core/algorithm/tuple.rb +25 -0
- data/lib/resyma/core/algorithm.rb +5 -0
- data/lib/resyma/core/automaton/builder.rb +78 -0
- data/lib/resyma/core/automaton/definition.rb +32 -0
- data/lib/resyma/core/automaton/epsilon_NFA.rb +115 -0
- data/lib/resyma/core/automaton/matchable.rb +16 -0
- data/lib/resyma/core/automaton/regexp.rb +175 -0
- data/lib/resyma/core/automaton/state.rb +22 -0
- data/lib/resyma/core/automaton/transition.rb +58 -0
- data/lib/resyma/core/automaton/visualize.rb +23 -0
- data/lib/resyma/core/automaton.rb +9 -0
- data/lib/resyma/core/parsetree/builder.rb +89 -0
- data/lib/resyma/core/parsetree/converter.rb +61 -0
- data/lib/resyma/core/parsetree/default_converter.rb +331 -0
- data/lib/resyma/core/parsetree/definition.rb +77 -0
- data/lib/resyma/core/parsetree/source.rb +73 -0
- data/lib/resyma/core/parsetree/traversal.rb +26 -0
- data/lib/resyma/core/parsetree.rb +8 -0
- data/lib/resyma/core/utilities.rb +30 -0
- data/lib/resyma/language.rb +290 -0
- data/lib/resyma/nise/date.rb +53 -0
- data/lib/resyma/nise/rubymoji.rb +13 -0
- data/lib/resyma/nise/toml.rb +63 -0
- data/lib/resyma/parsetree.rb +163 -0
- data/lib/resyma/program/automaton.rb +84 -0
- data/lib/resyma/program/parsetree.rb +79 -0
- data/lib/resyma/program/traverse.rb +77 -0
- data/lib/resyma/version.rb +5 -0
- data/lib/resyma.rb +12 -0
- data/resyma.gemspec +47 -0
- data/sig/resyma.rbs +4 -0
- metadata +184 -0
@@ -0,0 +1,79 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "resyma"
|
3
|
+
require "ruby-graphviz"
|
4
|
+
require "parser/current"
|
5
|
+
|
6
|
+
class Visualizer
|
7
|
+
def initialize
|
8
|
+
@viz = GraphViz.new(:G, type: :graph)
|
9
|
+
@id_pool = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def shorten(str, limit = 10)
|
13
|
+
str = str.to_s
|
14
|
+
if str.length > limit
|
15
|
+
str[0...limit] + "..."
|
16
|
+
else
|
17
|
+
str
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def label_of(parsetree)
|
22
|
+
label = parsetree.symbol
|
23
|
+
if parsetree.leaf?
|
24
|
+
"#{label}(#{shorten(parsetree.children.first)})"
|
25
|
+
else
|
26
|
+
label
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def def_node(parsetree)
|
31
|
+
@id_pool += 1
|
32
|
+
label = label_of(parsetree)
|
33
|
+
@viz.add_node(@id_pool.to_s, label: label)
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# @param [Resyma::Core::ParseTree] parsetree
|
38
|
+
#
|
39
|
+
def viz!(parsetree, node = nil)
|
40
|
+
node = def_node(parsetree) if node.nil?
|
41
|
+
return if parsetree.leaf?
|
42
|
+
|
43
|
+
parsetree.children.each do |sub|
|
44
|
+
sub_node = def_node(sub)
|
45
|
+
@viz.add_edge(node, sub_node)
|
46
|
+
viz!(sub, sub_node)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def output(filename)
|
51
|
+
@viz.output(png: filename)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def launch
|
56
|
+
output_filename = "./resyma-parsetree.png"
|
57
|
+
|
58
|
+
OptionParser.new do |opts|
|
59
|
+
opts.on("-o", "--output FILE",
|
60
|
+
"PNG containing the resulting tree") do |file|
|
61
|
+
output_filename = file
|
62
|
+
end
|
63
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
64
|
+
puts opts
|
65
|
+
exit
|
66
|
+
end
|
67
|
+
end.parse!
|
68
|
+
|
69
|
+
source = $stdin.read
|
70
|
+
ast = Parser::CurrentRuby.parse(source)
|
71
|
+
begin
|
72
|
+
pt = Resyma::Core::DEFAULT_CONVERTER.convert(ast)
|
73
|
+
viz = Visualizer.new
|
74
|
+
viz.viz!(pt)
|
75
|
+
viz.output(output_filename)
|
76
|
+
rescue Resyma::Core::ConversionError
|
77
|
+
puts "Cannot convert the AST: #{$!}"
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "resyma"
|
2
|
+
require_relative "parsetree"
|
3
|
+
require "resyma/nise/date"
|
4
|
+
require "resyma/nise/toml"
|
5
|
+
require "resyma/nise/rubymoji"
|
6
|
+
|
7
|
+
class FieldVisualizer < Visualizer
|
8
|
+
def label_of(parsetree)
|
9
|
+
label = super(parsetree)
|
10
|
+
id = parsetree.field.id
|
11
|
+
start = parsetree.field.start.values.map(&:to_a).flatten
|
12
|
+
trans = parsetree.field.trans.values.map(&:to_a).flatten
|
13
|
+
label = "#{label}\##{id}\\n"
|
14
|
+
|
15
|
+
unless start.empty?
|
16
|
+
label += "START\n"
|
17
|
+
start.each do |tuple2|
|
18
|
+
label += "#{tuple2.p},#{tuple2.q.id}/#{tuple2.belongs_to}\\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
unless trans.empty?
|
23
|
+
label += "TRANS\\n"
|
24
|
+
trans.each do |tuple4|
|
25
|
+
label +=
|
26
|
+
"#{tuple4.p},#{tuple4.q.id},#{tuple4.p_},#{tuple4.q_.id}" +
|
27
|
+
"/#{tuple4.belongs_to}\\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
label
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def launch
|
35
|
+
|
36
|
+
option = {
|
37
|
+
output_filename: "./resyma-traverse.png",
|
38
|
+
language: LangDate
|
39
|
+
}
|
40
|
+
|
41
|
+
available_languages = {
|
42
|
+
"date" => LangDate,
|
43
|
+
"toml" => LangTOML,
|
44
|
+
"rubymoji" => LangRubymoji
|
45
|
+
}
|
46
|
+
|
47
|
+
OptionParser.new do |opts|
|
48
|
+
opts.on("-o", "--output FILE",
|
49
|
+
"PNG containing the resulting tree") do |file|
|
50
|
+
option[:output_filename] = file
|
51
|
+
end
|
52
|
+
opts.on("-l", "--language LANG",
|
53
|
+
"Language used to traverse the tree") do |lang|
|
54
|
+
option[:language] = available_languages[lang]
|
55
|
+
if option[:language].nil?
|
56
|
+
raise "Unknown language #{lang}. " +
|
57
|
+
"Available languages are #{available_languages.keys}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
61
|
+
puts opts
|
62
|
+
exit
|
63
|
+
end
|
64
|
+
end.parse!
|
65
|
+
|
66
|
+
puts "Input source of #{option[:language]}"
|
67
|
+
source = $stdin.read
|
68
|
+
ast = Parser::CurrentRuby.parse(source)
|
69
|
+
pt = Resyma::Core::DEFAULT_CONVERTER.convert(ast)
|
70
|
+
lang = option[:language].new
|
71
|
+
lang.build_language(lang.method(:syntax))
|
72
|
+
engine = lang.engine
|
73
|
+
engine.traverse!(pt)
|
74
|
+
viz = FieldVisualizer.new
|
75
|
+
viz.viz!(pt)
|
76
|
+
viz.output(option[:output_filename])
|
77
|
+
end
|
data/lib/resyma.rb
ADDED
data/resyma.gemspec
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/resyma/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "resyma"
|
7
|
+
spec.version = Resyma::VERSION
|
8
|
+
spec.authors = ["Li Dzangfan"]
|
9
|
+
spec.email = ["dzangfan.li@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A regular syntax matcher"
|
12
|
+
spec.description = "A regular syntax matcher facilitate DSL's construction"
|
13
|
+
spec.homepage = "https://github.com/dzangfan/resyma"
|
14
|
+
spec.required_ruby_version = ">= 2.6.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = "bin"
|
28
|
+
spec.executables = spec.files.grep(%r{\A#{spec.bindir}/}) do |f|
|
29
|
+
File.basename(f)
|
30
|
+
end
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
35
|
+
|
36
|
+
spec.add_dependency "parser", "~> 3.0"
|
37
|
+
spec.add_dependency "ruby-graphviz", "~> 1.2"
|
38
|
+
spec.add_dependency "unparser", "~> 0.6"
|
39
|
+
|
40
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
spec.add_development_dependency "rubocop", "~> 1.21"
|
43
|
+
spec.add_development_dependency "yard", "~> 0.9.26"
|
44
|
+
|
45
|
+
# For more information and examples about making a new gem, check out our
|
46
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
47
|
+
end
|
data/sig/resyma.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resyma
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Li Dzangfan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ruby-graphviz
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: unparser
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.6'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.21'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.21'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: yard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.9.26
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.9.26
|
111
|
+
description: A regular syntax matcher facilitate DSL's construction
|
112
|
+
email:
|
113
|
+
- dzangfan.li@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".rspec"
|
119
|
+
- ".rubocop.yml"
|
120
|
+
- CHANGELOG.md
|
121
|
+
- Gemfile
|
122
|
+
- Gemfile.lock
|
123
|
+
- LICENSE
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- lib/resyma.rb
|
127
|
+
- lib/resyma/core/algorithm.rb
|
128
|
+
- lib/resyma/core/algorithm/engine.rb
|
129
|
+
- lib/resyma/core/algorithm/matcher.rb
|
130
|
+
- lib/resyma/core/algorithm/tuple.rb
|
131
|
+
- lib/resyma/core/automaton.rb
|
132
|
+
- lib/resyma/core/automaton/builder.rb
|
133
|
+
- lib/resyma/core/automaton/definition.rb
|
134
|
+
- lib/resyma/core/automaton/epsilon_NFA.rb
|
135
|
+
- lib/resyma/core/automaton/matchable.rb
|
136
|
+
- lib/resyma/core/automaton/regexp.rb
|
137
|
+
- lib/resyma/core/automaton/state.rb
|
138
|
+
- lib/resyma/core/automaton/transition.rb
|
139
|
+
- lib/resyma/core/automaton/visualize.rb
|
140
|
+
- lib/resyma/core/parsetree.rb
|
141
|
+
- lib/resyma/core/parsetree/builder.rb
|
142
|
+
- lib/resyma/core/parsetree/converter.rb
|
143
|
+
- lib/resyma/core/parsetree/default_converter.rb
|
144
|
+
- lib/resyma/core/parsetree/definition.rb
|
145
|
+
- lib/resyma/core/parsetree/source.rb
|
146
|
+
- lib/resyma/core/parsetree/traversal.rb
|
147
|
+
- lib/resyma/core/utilities.rb
|
148
|
+
- lib/resyma/language.rb
|
149
|
+
- lib/resyma/nise/date.rb
|
150
|
+
- lib/resyma/nise/rubymoji.rb
|
151
|
+
- lib/resyma/nise/toml.rb
|
152
|
+
- lib/resyma/parsetree.rb
|
153
|
+
- lib/resyma/program/automaton.rb
|
154
|
+
- lib/resyma/program/parsetree.rb
|
155
|
+
- lib/resyma/program/traverse.rb
|
156
|
+
- lib/resyma/version.rb
|
157
|
+
- resyma.gemspec
|
158
|
+
- sig/resyma.rbs
|
159
|
+
homepage: https://github.com/dzangfan/resyma
|
160
|
+
licenses: []
|
161
|
+
metadata:
|
162
|
+
homepage_uri: https://github.com/dzangfan/resyma
|
163
|
+
source_code_uri: https://github.com/dzangfan/resyma
|
164
|
+
changelog_uri: https://github.com/dzangfan/resyma/CHANGELOG.md
|
165
|
+
post_install_message:
|
166
|
+
rdoc_options: []
|
167
|
+
require_paths:
|
168
|
+
- lib
|
169
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 2.6.0
|
174
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
requirements: []
|
180
|
+
rubygems_version: 3.3.26
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: A regular syntax matcher
|
184
|
+
test_files: []
|