calyx 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d427f2159dadb5cc46d2a7d7c368c4b111560626
4
+ data.tar.gz: 5bd51e0cf275ec2a2f402f917c3f3d1bd374cba4
5
+ SHA512:
6
+ metadata.gz: a79eb466056cb9137a5d0cf5854966c8f8a2674e6e8ccefc546c3231e908a2a0909b220d122104604c5fb4cf9d8972bc54397accd65452c35df251cdf2e876b6
7
+ data.tar.gz: cdd81f0d9c90f1f06c0c8318c9ada79727ee25f65387cc1427a64ac3d780cf4e499def87a0246f1996f24854a9930090c4a86397c9a475d7f7fe34eb65390380
data/.gitignore ADDED
@@ -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,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mark Rickerby
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Calyx
2
+
3
+ Calyx provides a simple API for generating text with declarative recursive grammars.
data/calyx.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'calyx/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'calyx'
8
+ spec.version = Calyx::VERSION
9
+ spec.authors = ['Mark Rickerby']
10
+ spec.email = ['me@maetl.net']
11
+
12
+ spec.summary = %q{Generate text with declarative recursive grammars}
13
+ spec.description = %q{Calyx provides a simple API for generating text with declarative recursive grammars.}
14
+ spec.homepage = 'https://github.com/maetl/calyx'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ end
data/lib/calyx.rb ADDED
@@ -0,0 +1,99 @@
1
+ module Calyx
2
+ class Grammar
3
+ class << self
4
+ def start(*productions, &production)
5
+ registry[:start] = construct_rule(productions)
6
+ end
7
+
8
+ def rule(name, *productions, &production)
9
+ registry[name.to_sym] = construct_rule(productions)
10
+ end
11
+
12
+ def registry
13
+ @registry ||= {}
14
+ end
15
+
16
+ def construct_rule(productions)
17
+ Production::Choices.parse(productions)
18
+ end
19
+ end
20
+
21
+ module Production
22
+ class NonTerminal
23
+ def initialize(expansion)
24
+ @expansion = expansion.to_sym
25
+ end
26
+
27
+ def evaluate(registry)
28
+ registry[@expansion].evaluate(registry)
29
+ end
30
+ end
31
+
32
+ class Terminal
33
+ def initialize(atom)
34
+ @atom = atom
35
+ end
36
+
37
+ def evaluate(registry)
38
+ @atom
39
+ end
40
+ end
41
+
42
+ class Concat
43
+ DELIMITER = /(\{[A-Za-z0-9_]+\})/.freeze
44
+
45
+ def self.parse(production)
46
+ expansion = production.split(DELIMITER).reject do |chunks|
47
+ chunks.strip.empty?
48
+ end.map do |atom|
49
+ if atom[0] == '{' && atom[atom.length-1] == '}'
50
+ NonTerminal.new(atom.slice(1, atom.length-2))
51
+ else
52
+ Terminal.new(atom)
53
+ end
54
+ end
55
+
56
+ self.new(expansion)
57
+ end
58
+
59
+ def initialize(expansion)
60
+ @expansion = expansion
61
+ end
62
+
63
+ def evaluate(registry)
64
+ @expansion.reduce('') do |exp, atom|
65
+ exp << atom.evaluate(registry)
66
+ end
67
+ end
68
+ end
69
+
70
+ class Choices
71
+ def self.parse(production)
72
+ self.new(production.map { |a| Concat.parse(a) })
73
+ end
74
+
75
+ def initialize(collection)
76
+ @collection = collection
77
+ end
78
+
79
+ def evaluate(registry)
80
+ @collection.sample.evaluate(registry)
81
+ end
82
+ end
83
+ end
84
+
85
+ def initialize(seed=nil)
86
+ @seed = seed
87
+ @seed = Time.new.to_i unless @seed
88
+ srand(@seed)
89
+ end
90
+
91
+ def registry
92
+ self.class.registry
93
+ end
94
+
95
+ def generate
96
+ registry[:start].evaluate(registry)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module Calyx
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calyx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Rickerby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Calyx provides a simple API for generating text with declarative recursive
56
+ grammars.
57
+ email:
58
+ - me@maetl.net
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - calyx.gemspec
70
+ - lib/calyx.rb
71
+ - lib/calyx/version.rb
72
+ homepage: https://github.com/maetl/calyx
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Generate text with declarative recursive grammars
96
+ test_files: []