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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +8 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.rubocop.yml +51 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/Rakefile +9 -0
- data/bin/console +7 -0
- data/bin/rake +10 -0
- data/bin/rspec +10 -0
- data/bin/rubocop +10 -0
- data/bin/setup +7 -0
- data/exe/mahoujin +22 -0
- data/lib/mahoujin.rb +15 -0
- data/lib/mahoujin/atoms.rb +32 -0
- data/lib/mahoujin/atoms/choice.rb +12 -0
- data/lib/mahoujin/atoms/concatenation.rb +12 -0
- data/lib/mahoujin/atoms/exception.rb +11 -0
- data/lib/mahoujin/atoms/grouping.rb +11 -0
- data/lib/mahoujin/atoms/non_terminal.rb +11 -0
- data/lib/mahoujin/atoms/optional.rb +11 -0
- data/lib/mahoujin/atoms/prose.rb +11 -0
- data/lib/mahoujin/atoms/rule.rb +11 -0
- data/lib/mahoujin/atoms/rule_name.rb +11 -0
- data/lib/mahoujin/atoms/specific_repetition.rb +11 -0
- data/lib/mahoujin/atoms/syntax.rb +11 -0
- data/lib/mahoujin/atoms/terminal.rb +11 -0
- data/lib/mahoujin/atoms/zero_or_more_repetition.rb +11 -0
- data/lib/mahoujin/graphics/layout.rb +203 -0
- data/lib/mahoujin/graphics/renderer.rb +337 -0
- data/lib/mahoujin/graphics/stringify.rb +67 -0
- data/lib/mahoujin/graphics/styles/basic.rb +48 -0
- data/lib/mahoujin/graphics/styles/json.rb +48 -0
- data/lib/mahoujin/graphics/utilities/rectangle.rb +58 -0
- data/lib/mahoujin/parser.rb +62 -0
- data/lib/mahoujin/transform.rb +39 -0
- data/lib/mahoujin/version.rb +3 -0
- data/mahoujin.gemspec +29 -0
- data/samples/ebnf/syntax.ebnfspec +20 -0
- data/samples/ebnf/syntax.svg +1558 -0
- data/samples/ipv4/syntax.ebnfspec +6 -0
- data/samples/ipv4/syntax.svg +344 -0
- data/samples/json/syntax.ebnfspec +8 -0
- data/samples/json/syntax.svg +1169 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -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
|
data/.codeclimate.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --require spec_helper
|
data/.rubocop.yml
ADDED
@@ -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
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
[](https://travis-ci.org/0x01f7/mahoujin)
|
2
|
+
[](https://gemnasium.com/0x01f7/mahoujin)
|
3
|
+
[](https://codeclimate.com/github/0x01f7/mahoujin/coverage)
|
4
|
+
[](https://codeclimate.com/github/0x01f7/mahoujin)
|
5
|
+
[](https://rubygems.org/gems/mahoujin)
|
6
|
+
[](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
|
+

|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/rake
ADDED
data/bin/rspec
ADDED
data/bin/rubocop
ADDED
data/bin/setup
ADDED
data/exe/mahoujin
ADDED
@@ -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)
|
data/lib/mahoujin.rb
ADDED
@@ -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
|