mahoujin 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +8 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +51 -0
  6. data/.travis.yml +7 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +78 -0
  10. data/Rakefile +9 -0
  11. data/bin/console +7 -0
  12. data/bin/rake +10 -0
  13. data/bin/rspec +10 -0
  14. data/bin/rubocop +10 -0
  15. data/bin/setup +7 -0
  16. data/exe/mahoujin +22 -0
  17. data/lib/mahoujin.rb +15 -0
  18. data/lib/mahoujin/atoms.rb +32 -0
  19. data/lib/mahoujin/atoms/choice.rb +12 -0
  20. data/lib/mahoujin/atoms/concatenation.rb +12 -0
  21. data/lib/mahoujin/atoms/exception.rb +11 -0
  22. data/lib/mahoujin/atoms/grouping.rb +11 -0
  23. data/lib/mahoujin/atoms/non_terminal.rb +11 -0
  24. data/lib/mahoujin/atoms/optional.rb +11 -0
  25. data/lib/mahoujin/atoms/prose.rb +11 -0
  26. data/lib/mahoujin/atoms/rule.rb +11 -0
  27. data/lib/mahoujin/atoms/rule_name.rb +11 -0
  28. data/lib/mahoujin/atoms/specific_repetition.rb +11 -0
  29. data/lib/mahoujin/atoms/syntax.rb +11 -0
  30. data/lib/mahoujin/atoms/terminal.rb +11 -0
  31. data/lib/mahoujin/atoms/zero_or_more_repetition.rb +11 -0
  32. data/lib/mahoujin/graphics/layout.rb +203 -0
  33. data/lib/mahoujin/graphics/renderer.rb +337 -0
  34. data/lib/mahoujin/graphics/stringify.rb +67 -0
  35. data/lib/mahoujin/graphics/styles/basic.rb +48 -0
  36. data/lib/mahoujin/graphics/styles/json.rb +48 -0
  37. data/lib/mahoujin/graphics/utilities/rectangle.rb +58 -0
  38. data/lib/mahoujin/parser.rb +62 -0
  39. data/lib/mahoujin/transform.rb +39 -0
  40. data/lib/mahoujin/version.rb +3 -0
  41. data/mahoujin.gemspec +29 -0
  42. data/samples/ebnf/syntax.ebnfspec +20 -0
  43. data/samples/ebnf/syntax.svg +1558 -0
  44. data/samples/ipv4/syntax.ebnfspec +6 -0
  45. data/samples/ipv4/syntax.svg +344 -0
  46. data/samples/json/syntax.ebnfspec +8 -0
  47. data/samples/json/syntax.svg +1169 -0
  48. metadata +202 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa8e14e81fdce25c2a02c5e2f07b3cf851425627
4
+ data.tar.gz: c590eb53a3ff7b1c3173909b712ee63b943dba90
5
+ SHA512:
6
+ metadata.gz: 1ac5f46f0a3b10b15034684f0a87be605cd80af3386849f591d1285f36539b6d167401858f817d3e6fb96fc6a72d04099fa13c2878eb844c055574075f715381
7
+ data.tar.gz: 9cf3f40ed6a9f1065de95bdc902dc404634899fdab515b7989623a614f1f0824c7c1e5827b94b78f477ad35f8d4ae0f0181199d7fa8d36afabb28df36a7bf59e
@@ -0,0 +1,8 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
4
+
5
+ ratings:
6
+ paths:
7
+ - 'lib/**/*'
8
+ - 'spec/**/*'
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --require spec_helper
@@ -0,0 +1,51 @@
1
+ require: rubocop-rspec
2
+
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'bin/*'
7
+ - 'exe/*'
8
+
9
+
10
+ Metrics/AbcSize:
11
+ Enabled: false
12
+
13
+ Metrics/ClassLength:
14
+ Enabled: false
15
+
16
+ Metrics/LineLength:
17
+ Enabled: false
18
+
19
+ Metrics/MethodLength:
20
+ Enabled: false
21
+
22
+ Metrics/ParameterLists:
23
+ Enabled: false
24
+
25
+
26
+ Style/BlockDelimiters:
27
+ Enabled: false
28
+
29
+ Style/Documentation:
30
+ Enabled: false
31
+
32
+ Style/ExtraSpacing:
33
+ Enabled: false
34
+
35
+ Style/ParallelAssignment:
36
+ Enabled: false
37
+
38
+ Style/SignalException:
39
+ Enabled: false
40
+
41
+ Style/SpaceAroundOperators:
42
+ Enabled: false
43
+
44
+ Style/SpaceBeforeComma:
45
+ Enabled: false
46
+
47
+ Style/SpaceInsideHashLiteralBraces:
48
+ Enabled: false
49
+
50
+ Style/SpaceInsideParens:
51
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ before_install: gem install bundler -v 1.11.2
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ - 2.3.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sou.K
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,78 @@
1
+ [![Build Status](http://img.shields.io/travis/0x01f7/mahoujin.svg)](https://travis-ci.org/0x01f7/mahoujin)
2
+ [![Dependency Status](http://img.shields.io/gemnasium/0x01f7/mahoujin.svg)](https://gemnasium.com/0x01f7/mahoujin)
3
+ [![Test Coverage](https://img.shields.io/codeclimate/coverage/github/0x01f7/mahoujin.svg)](https://codeclimate.com/github/0x01f7/mahoujin/coverage)
4
+ [![Code Climate](http://img.shields.io/codeclimate/github/0x01f7/mahoujin.svg)](https://codeclimate.com/github/0x01f7/mahoujin)
5
+ [![Gem Version](http://img.shields.io/gem/v/mahoujin.svg)](https://rubygems.org/gems/mahoujin)
6
+ [![License](http://img.shields.io/:license-mit-blue.svg)](http://0x01f7.mit-license.org)
7
+
8
+
9
+ # Mahoujin
10
+
11
+ Generate syntax diagram(aka railroad diagram) from EBNF specification
12
+
13
+
14
+ ## Requirements
15
+
16
+ * [Cairo](http://cairographics.org) >= 1.12.0
17
+ * [Ruby](https://www.ruby-lang.org) >= 2.0.0
18
+
19
+
20
+ ## Installation
21
+
22
+ * Use Ruby's package manager
23
+
24
+ ```Text
25
+ $ gem install mahoujin
26
+ ```
27
+
28
+ * Or from source
29
+
30
+ ```Text
31
+ $ git clone https://github.com/0x01f7/mahoujin.git
32
+ $ cd mahoujin
33
+ $ ./bin/setup
34
+ $ ./bin/rake install
35
+ ```
36
+
37
+
38
+ ## Usage
39
+
40
+ Run command:
41
+
42
+ ```
43
+ $ mahoujin SYNTAX.ebnfspec -o SYNTAX.svg --style Json
44
+ ```
45
+
46
+ Note: the **SYNTAX.ebnfspec** uses **EBNF** which is defined in [ISO/IEC 14977](https://www.cl.cam.ac.uk/~mgk25/iso-14977.pdf) to describe it's rules.
47
+
48
+
49
+ ## Samples - EBNF
50
+
51
+ **SYNTAX.ebnfspec**:
52
+
53
+ ```Text
54
+ syntax = syntax rule, {syntax rule};
55
+ syntax rule = meta identifier, '=', definitions list, ';';
56
+ definitions list = single definition, {'|', single definition};
57
+ single definition = term, {',', term};
58
+ term = factor, ['-', exception];
59
+ exception = factor;
60
+ factor = [integer, '*'], primary;
61
+ primary = optional sequence | repeated sequence
62
+ | grouped sequence | special sequence
63
+ | meta identifier | terminal string;
64
+ optional sequence = '[', definitions list, ']';
65
+ repeated sequence = '{', definitions list, '}';
66
+ grouped sequence = '(', definitions list, ')';
67
+ special sequence = '?', {character - '?'}, '?';
68
+ meta identifier = letter, {letter | decimal digit | ? ASCII space character ?};
69
+ terminal string = "'", character - "'", {character - "'"}, "'"
70
+ | '"', character - '"', {character - '"'}, '"';
71
+ integer = decimal digit, {decimal digit};
72
+ character = ? any character in ASCII alphabet characters ?;
73
+ decimal digit = ? any character in ASCII number characters ?;
74
+ ```
75
+
76
+ **SYNTAX.svg**:
77
+
78
+ ![SYNTAX.svg](https://rawgit.com/0x01f7/mahoujin/master/samples/ebnf/syntax.svg)
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+ RuboCop::RakeTask.new(:rubocop)
8
+
9
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mahoujin'
5
+ require 'irb'
6
+
7
+ IRB.start
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
5
+ Pathname.new(__FILE__).realpath)
6
+
7
+ require 'rubygems'
8
+ require 'bundler/setup'
9
+
10
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
5
+ Pathname.new(__FILE__).realpath)
6
+
7
+ require 'rubygems'
8
+ require 'bundler/setup'
9
+
10
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
5
+ Pathname.new(__FILE__).realpath)
6
+
7
+ require 'rubygems'
8
+ require 'bundler/setup'
9
+
10
+ load Gem.bin_path('rubocop', 'rubocop')
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+ set -vx
6
+
7
+ bundle install
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mahoujin'
4
+ require 'optionparser'
5
+
6
+ options = {}
7
+ options[:infile] = OptionParser.new do |opts|
8
+ opts.banner = "Usage: #{opts.program_name} [options] INFILE"
9
+ opts.separator ''
10
+ opts.on('-o', '--output OUTFILE', 'Write output to a file instead of STDOUT') { |f| options[:outfile] = f }
11
+ opts.on('-s', "--style STYLE", "Choose a graphics style: #{Mahoujin::Graphics::Styles.constants.join(', ')}") { |s| options[:style] = s }
12
+ opts.on('-h', '--help', 'Show this message') { puts opts; exit }
13
+ opts.on('-v', '--version', 'Show version') { puts Mahoujin::VERSION; exit }
14
+ end.parse(ARGV).shift
15
+
16
+ op = lambda do |ostream|
17
+ istream = options[:infile] ? File.read(options[:infile]) : STDIN.read
18
+ style = Mahoujin::Graphics::Styles.const_get(options.fetch(:style, :Json)).new
19
+ ast = Mahoujin::Transform.new.apply(Mahoujin::Parser.new.parse(istream))
20
+ Mahoujin::Graphics::Renderer.new.render(ast, ostream, style)
21
+ end
22
+ options[:outfile] ? File.open(options[:outfile], 'w', &op) : STDOUT.tap(&op)
@@ -0,0 +1,15 @@
1
+ require 'cairo'
2
+ require 'parslet'
3
+
4
+ require 'mahoujin/atoms'
5
+ require 'mahoujin/parser'
6
+ require 'mahoujin/transform'
7
+
8
+ require 'mahoujin/graphics/layout'
9
+ require 'mahoujin/graphics/renderer'
10
+ require 'mahoujin/graphics/stringify'
11
+ require 'mahoujin/graphics/styles/basic'
12
+ require 'mahoujin/graphics/styles/json'
13
+ require 'mahoujin/graphics/utilities/rectangle'
14
+
15
+ require 'mahoujin/version'
@@ -0,0 +1,32 @@
1
+ require 'mahoujin/atoms/choice'
2
+ require 'mahoujin/atoms/concatenation'
3
+ require 'mahoujin/atoms/exception'
4
+ require 'mahoujin/atoms/grouping'
5
+ require 'mahoujin/atoms/non_terminal'
6
+ require 'mahoujin/atoms/optional'
7
+ require 'mahoujin/atoms/prose'
8
+ require 'mahoujin/atoms/rule_name'
9
+ require 'mahoujin/atoms/rule'
10
+ require 'mahoujin/atoms/specific_repetition'
11
+ require 'mahoujin/atoms/syntax'
12
+ require 'mahoujin/atoms/terminal'
13
+ require 'mahoujin/atoms/zero_or_more_repetition'
14
+
15
+ module Mahoujin
16
+ module Atoms
17
+ constants.each do |constant_name|
18
+ constant = const_get(constant_name)
19
+ next unless constant.is_a?(Class)
20
+
21
+ constant.class_exec do
22
+ def self.underscore_name
23
+ name.split('::').last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
24
+ end
25
+
26
+ def bbox
27
+ @bbox ||= Graphics::Utilities::Rectangle.new
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,12 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Choice
4
+ attr_reader :atoms
5
+
6
+ def initialize(atoms)
7
+ raise if atoms.size < 2
8
+ @atoms = atoms.freeze
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Concatenation
4
+ attr_reader :atoms
5
+
6
+ def initialize(atoms)
7
+ raise if atoms.size < 2
8
+ @atoms = atoms.freeze
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Exception
4
+ attr_reader :content
5
+
6
+ def initialize(content)
7
+ @content = content.freeze
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Grouping
4
+ attr_reader :atom
5
+
6
+ def initialize(atom)
7
+ @atom = atom
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class NonTerminal
4
+ attr_reader :content
5
+
6
+ def initialize(content)
7
+ @content = content.freeze
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Optional
4
+ attr_reader :atom
5
+
6
+ def initialize(atom)
7
+ @atom = atom
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Prose
4
+ attr_reader :content
5
+
6
+ def initialize(content)
7
+ @content = content.freeze
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class Rule
4
+ attr_reader :rule_name_atom, :atom
5
+
6
+ def initialize(rule_name_atom, atom)
7
+ @rule_name_atom, @atom = rule_name_atom, atom
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mahoujin
2
+ module Atoms
3
+ class RuleName
4
+ attr_reader :content
5
+
6
+ def initialize(content)
7
+ @content = content.freeze
8
+ end
9
+ end
10
+ end
11
+ end