calyx 0.16.1 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "calyx-docs",
3
+ "version": "1.0.0",
4
+ "main": "index.html",
5
+ "scripts": {
6
+ "build": "node-sass --include-path node_modules/bulma calyx-docs.scss assets/calyx-docs.css",
7
+ "help": "node-sass --help"
8
+ },
9
+ "author": "Mark Rickerby <me@maetl.net>",
10
+ "license": "MIT",
11
+ "devDependencies": {
12
+ "bulma": "^0.5.2",
13
+ "node-sass": "^4.5.3"
14
+ }
15
+ }
@@ -1,6 +1,8 @@
1
+ require 'rubygems/deprecate'
1
2
  require 'calyx/version'
2
3
  require 'calyx/options'
3
4
  require 'calyx/rule'
5
+ require 'calyx/result'
4
6
  require 'calyx/grammar'
5
7
  require 'calyx/errors'
6
8
  require 'calyx/format'
@@ -138,23 +138,35 @@ module Calyx
138
138
  # @param [Hash] rules_map
139
139
  # @return [String]
140
140
  def generate(*args)
141
- start_symbol, rules_map = map_default_args(*args)
142
-
143
- @registry.evaluate(start_symbol, rules_map).flatten.reject do |obj|
144
- obj.is_a?(Symbol)
145
- end.join
141
+ result = generate_result(*args)
142
+ result.text
146
143
  end
147
144
 
148
145
  # Produces a syntax tree of nested list nodes as an output of the grammar.
149
146
  #
147
+ # @deprecated Please use {#generate_result} instead.
148
+ def evaluate(*args)
149
+ warn <<~DEPRECATION
150
+ [DEPRECATION] `evaluate` is deprecated and will be removed in 1.0.
151
+ Please use #generate_result instead.
152
+ See https://github.com/maetl/calyx/issues/23 for more details.
153
+ DEPRECATION
154
+
155
+ result = generate_result(*args)
156
+ result.tree
157
+ end
158
+
159
+ # Produces a generated result from evaluating the grammar.
160
+ #
161
+ # @see Calyx::Result
150
162
  # @overload
151
163
  # @param [Symbol] start_symbol
152
164
  # @param [Hash] rules_map
153
- # @return [Array]
154
- def evaluate(*args)
165
+ # @return [Calyx::Result]
166
+ def generate_result(*args)
155
167
  start_symbol, rules_map = map_default_args(*args)
156
168
 
157
- @registry.evaluate(start_symbol, rules_map)
169
+ Result.new(@registry.evaluate(start_symbol, rules_map))
158
170
  end
159
171
 
160
172
  private
@@ -1,13 +1,25 @@
1
1
  module Calyx
2
+ # Provides access to configuration options while evaluating a grammar.
2
3
  class Options
4
+ # These options are used by default if not explicitly provided during
5
+ # initialization of the grammar.
3
6
  DEFAULTS = {
4
7
  strict: true
5
8
  }
6
9
 
7
- def initialize(opts={})
8
- @options = DEFAULTS.merge(opts)
10
+ # Constructs a new options instance, merging the passed in options with the
11
+ # defaults.
12
+ #
13
+ # @params [Hash, Calyx::Options] options
14
+ def initialize(options={})
15
+ @options = DEFAULTS.merge(options)
9
16
  end
10
17
 
18
+ # Returns the internal random number generator instance. If a seed or random
19
+ # instance is not passed-in directly, a new instance of `Random` is
20
+ # initialized by default.
21
+ #
22
+ # @return [Random]
11
23
  def rng
12
24
  unless @options[:rng]
13
25
  @options[:rng] = if @options[:seed]
@@ -20,20 +32,39 @@ module Calyx
20
32
  @options[:rng]
21
33
  end
22
34
 
35
+ # Returns the next pseudo-random number in the sequence defined by the
36
+ # internal random number generator state.
37
+ #
38
+ # The value returned is a floating point number between 0.0 and 1.0,
39
+ # including 0.0 and excluding 1.0.
40
+ #
41
+ # @return [Float]
23
42
  def rand
24
43
  rng.rand
25
44
  end
26
45
 
46
+ # True if the strict mode option is enabled. This option defines whether or
47
+ # not to raise an error on missing rules. When set to false, missing rules
48
+ # are skipped over when the production is concatenated.
49
+ #
50
+ # @return [TrueClass, FalseClass]
27
51
  def strict?
28
52
  @options[:strict]
29
53
  end
30
54
 
31
- def merge(opts)
32
- Options.new(@options.merge(opts.to_h))
55
+ # Merges two instances together and returns a new instance.
56
+ #
57
+ # @params [Calyx::Options]
58
+ # @return [Calyx::Options]
59
+ def merge(options)
60
+ Options.new(@options.merge(options.to_h))
33
61
  end
34
62
 
63
+ # Serializes instance data to a hash.
64
+ #
65
+ # @return [Hash]
35
66
  def to_h
36
- @options
67
+ @options.dup
37
68
  end
38
69
  end
39
70
  end
@@ -0,0 +1,38 @@
1
+ module Calyx
2
+ # Value object representing a generated grammar result.
3
+ class Result
4
+ def initialize(expression)
5
+ @expression = expression.freeze
6
+ end
7
+
8
+ # The syntax tree of nested nodes representing the production rules which
9
+ # generated this result.
10
+ #
11
+ # @return [Array]
12
+ def tree
13
+ @expression
14
+ end
15
+
16
+ alias_method :to_exp, :tree
17
+
18
+ # The generated text string produced by the grammar.
19
+ #
20
+ # @return [String]
21
+ def text
22
+ @expression.flatten.reject do |obj|
23
+ obj.is_a?(Symbol)
24
+ end.join
25
+ end
26
+
27
+ alias_method :to_s, :text
28
+
29
+ # A symbol produced by converting the generated text string where possible.
30
+ #
31
+ # @return [Symbol]
32
+ def symbol
33
+ text.to_sym
34
+ end
35
+
36
+ alias_method :to_sym, :symbol
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Calyx
2
- VERSION = '0.16.1'.freeze
2
+ VERSION = '0.17.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calyx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rickerby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-02 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,6 +68,17 @@ files:
68
68
  - README.md
69
69
  - SYNTAX.md
70
70
  - calyx.gemspec
71
+ - docs/404.html
72
+ - docs/CNAME
73
+ - docs/_config.yml
74
+ - docs/_layouts/default.html
75
+ - docs/_posts/2017-09-14-welcome-to-jekyll.markdown
76
+ - docs/about.md
77
+ - docs/assets/calyx-docs.css
78
+ - docs/calyx-docs.scss
79
+ - docs/index.html
80
+ - docs/package-lock.json
81
+ - docs/package.json
71
82
  - examples/any_gradient.rb
72
83
  - examples/faker.rb
73
84
  - examples/faker.yml
@@ -87,6 +98,7 @@ files:
87
98
  - lib/calyx/production/unique.rb
88
99
  - lib/calyx/production/weighted_choices.rb
89
100
  - lib/calyx/registry.rb
101
+ - lib/calyx/result.rb
90
102
  - lib/calyx/rule.rb
91
103
  - lib/calyx/version.rb
92
104
  homepage: https://github.com/maetl/calyx