calyx 0.11.1 → 0.11.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/calyx/format.rb +3 -3
- data/lib/calyx/grammar.rb +18 -16
- data/lib/calyx/modifiers.rb +2 -2
- data/lib/calyx/production/choices.rb +3 -3
- data/lib/calyx/production/concat.rb +3 -3
- data/lib/calyx/production/expression.rb +3 -3
- data/lib/calyx/production/memo.rb +2 -2
- data/lib/calyx/production/non_terminal.rb +2 -2
- data/lib/calyx/production/terminal.rb +1 -1
- data/lib/calyx/production/weighted_choices.rb +3 -3
- data/lib/calyx/registry.rb +21 -16
- data/lib/calyx/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc594e0fddc6c14df3cd938cc1ab458f7ffc490f
|
4
|
+
data.tar.gz: 68f4c73ef62ae94daea3194b11d3c7e0e5df7904
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9e136e726881b1847690a290565125546fd7a41f90f7bbd4320542eaee82be2df9796acbf11d53cb71c77d2b82e97e29d228f050ac765d688bc36ac1c672a75
|
7
|
+
data.tar.gz: 9290522a14840247671d1c76228b0e515687b97713890e4b303fe10f128cf1e7c7d5b061775dd060534ad67e7e98143b398ae9ab5cba74289c25bab8c4adebfd
|
data/README.md
CHANGED
@@ -417,7 +417,8 @@ Rough plan for stabilising the API and features for a `1.0` release.
|
|
417
417
|
| `0.9` | ~~return grammar tree from `#evaluate`, with flattened string from `#generate` being separate~~ |
|
418
418
|
| `0.10` | ~~inject custom string functions for parameterised rules, transforms and mappings~~ |
|
419
419
|
| `0.11` | ~~support YAML format (and JSON?)~~ |
|
420
|
-
| `
|
420
|
+
| `0.12` | indirection (allow output of a rule to be used as the name for another rule) |
|
421
|
+
| `1.0` | ~~API documentation~~ |
|
421
422
|
|
422
423
|
## Credits
|
423
424
|
|
data/lib/calyx/format.rb
CHANGED
@@ -7,7 +7,7 @@ module Calyx
|
|
7
7
|
# Accepts a JSON or YAML file path, identified by its extension (`.json`
|
8
8
|
# or `.yml`).
|
9
9
|
#
|
10
|
-
# @param
|
10
|
+
# @param [String] filename
|
11
11
|
# @return [Calyx::Grammar]
|
12
12
|
def self.load(filename)
|
13
13
|
file = File.read(filename)
|
@@ -23,7 +23,7 @@ module Calyx
|
|
23
23
|
|
24
24
|
# Converts the given string of YAML data to a grammar instance.
|
25
25
|
#
|
26
|
-
# @param
|
26
|
+
# @param [String] data
|
27
27
|
# @return [Calyx::Grammar]
|
28
28
|
def self.load_yml(data)
|
29
29
|
require 'yaml'
|
@@ -32,7 +32,7 @@ module Calyx
|
|
32
32
|
|
33
33
|
# Converts the given string of JSON data to a grammar instance.
|
34
34
|
#
|
35
|
-
# @param
|
35
|
+
# @param [String] data
|
36
36
|
# @return [Calyx::Grammar]
|
37
37
|
def self.load_json(data)
|
38
38
|
require 'json'
|
data/lib/calyx/grammar.rb
CHANGED
@@ -23,7 +23,7 @@ module Calyx
|
|
23
23
|
# Accepts a JSON or YAML file path, identified by its extension (`.json`
|
24
24
|
# or `.yml`).
|
25
25
|
#
|
26
|
-
# @param
|
26
|
+
# @param [String] filename
|
27
27
|
# @return [Calyx::Grammar]
|
28
28
|
def load(filename)
|
29
29
|
Format.load(filename)
|
@@ -31,22 +31,22 @@ module Calyx
|
|
31
31
|
|
32
32
|
# DSL helper method for registering a modifier module with the grammar.
|
33
33
|
#
|
34
|
-
# @param
|
34
|
+
# @param [Module] module_name
|
35
35
|
def modifier(module_name)
|
36
36
|
registry.modifier(module_name)
|
37
37
|
end
|
38
38
|
|
39
39
|
# DSL helper method for registering a paired mapping regex.
|
40
40
|
#
|
41
|
-
# @param
|
42
|
-
# @param
|
41
|
+
# @param [Symbol] name
|
42
|
+
# @param [Hash<Regex,String>] pairs
|
43
43
|
def mapping(name, pairs)
|
44
44
|
registry.mapping(name, pairs)
|
45
45
|
end
|
46
46
|
|
47
47
|
# DSL helper method for registering the given block as a string filter.
|
48
48
|
#
|
49
|
-
# @param
|
49
|
+
# @param [Symbol] name
|
50
50
|
# @block block with a single string argument returning a modified string.
|
51
51
|
def filter(name, &block)
|
52
52
|
registry.mapping(name, &block)
|
@@ -56,8 +56,8 @@ module Calyx
|
|
56
56
|
#
|
57
57
|
# Not usually used directly, as the method missing API is less verbose.
|
58
58
|
#
|
59
|
-
# @param
|
60
|
-
# @param
|
59
|
+
# @param [Symbol] name
|
60
|
+
# @param [Array] productions
|
61
61
|
def rule(name, *productions)
|
62
62
|
registry.rule(name, *productions)
|
63
63
|
end
|
@@ -68,8 +68,8 @@ module Calyx
|
|
68
68
|
# This must be bypassed by calling `#rule` directly if the name of the
|
69
69
|
# desired rule clashes with an existing helper method.
|
70
70
|
#
|
71
|
-
# @param
|
72
|
-
# @param
|
71
|
+
# @param [Symbol] name
|
72
|
+
# @param [Array] productions
|
73
73
|
def method_missing(name, *productions)
|
74
74
|
registry.rule(name, *productions)
|
75
75
|
end
|
@@ -77,7 +77,7 @@ module Calyx
|
|
77
77
|
# Hook for combining the registry of a parent grammar into the child that
|
78
78
|
# inherits from it.
|
79
79
|
#
|
80
|
-
# @param
|
80
|
+
# @param [Calyx::Registry] child_registry
|
81
81
|
def inherit_registry(child_registry)
|
82
82
|
registry.combine(child_registry) unless child_registry.nil?
|
83
83
|
end
|
@@ -87,7 +87,7 @@ module Calyx
|
|
87
87
|
#
|
88
88
|
# This is automatically called by the Ruby engine.
|
89
89
|
#
|
90
|
-
# @param
|
90
|
+
# @param [Class] subclass
|
91
91
|
def inherited(subclass)
|
92
92
|
subclass.inherit_registry(registry)
|
93
93
|
end
|
@@ -98,7 +98,7 @@ module Calyx
|
|
98
98
|
# Grammar rules can be constructed on the fly when the passed-in block is
|
99
99
|
# evaluated.
|
100
100
|
#
|
101
|
-
# @param
|
101
|
+
# @param [Number] seed
|
102
102
|
def initialize(seed=nil, &block)
|
103
103
|
@seed = seed || Random.new_seed
|
104
104
|
srand(@seed)
|
@@ -113,8 +113,9 @@ module Calyx
|
|
113
113
|
|
114
114
|
# Produces a string as an output of the grammar.
|
115
115
|
#
|
116
|
-
# @
|
117
|
-
#
|
116
|
+
# @overload
|
117
|
+
# @param [Symbol] start_symbol
|
118
|
+
# @param [Hash] rules_map
|
118
119
|
# @return [String]
|
119
120
|
def generate(*args)
|
120
121
|
start_symbol, rules_map = map_default_args(*args)
|
@@ -126,8 +127,9 @@ module Calyx
|
|
126
127
|
|
127
128
|
# Produces a syntax tree of nested list nodes as an output of the grammar.
|
128
129
|
#
|
129
|
-
# @
|
130
|
-
#
|
130
|
+
# @overload
|
131
|
+
# @param [Symbol] start_symbol
|
132
|
+
# @param [Hash] rules_map
|
131
133
|
# @return [Array]
|
132
134
|
def evaluate(*args)
|
133
135
|
start_symbol, rules_map = map_default_args(*args)
|
data/lib/calyx/modifiers.rb
CHANGED
@@ -8,8 +8,8 @@ module Calyx
|
|
8
8
|
#
|
9
9
|
# If an invalid modifier function is given, returns the raw input string.
|
10
10
|
#
|
11
|
-
# @param
|
12
|
-
# @param
|
11
|
+
# @param [Symbol] name
|
12
|
+
# @param [String] value
|
13
13
|
# @return [String]
|
14
14
|
def transform(name, value)
|
15
15
|
if respond_to?(name)
|
@@ -6,8 +6,8 @@ module Calyx
|
|
6
6
|
# Parse a list of productions and return a choice node which is the head
|
7
7
|
# of a syntax tree of child nodes.
|
8
8
|
#
|
9
|
-
# @param
|
10
|
-
# @param
|
9
|
+
# @param [Array] productions
|
10
|
+
# @param [Calyx::Registry] registry
|
11
11
|
def self.parse(productions, registry)
|
12
12
|
choices = productions.map do |choice|
|
13
13
|
if choice.is_a?(String)
|
@@ -27,7 +27,7 @@ module Calyx
|
|
27
27
|
|
28
28
|
# Initialize a new choice with a list of child nodes.
|
29
29
|
#
|
30
|
-
# @param
|
30
|
+
# @param [Array] collection
|
31
31
|
def initialize(collection)
|
32
32
|
@collection = collection
|
33
33
|
end
|
@@ -13,8 +13,8 @@ module Calyx
|
|
13
13
|
#
|
14
14
|
# Returns a concat node which is the head of a tree of child nodes.
|
15
15
|
#
|
16
|
-
# @param
|
17
|
-
# @param
|
16
|
+
# @param [String] production
|
17
|
+
# @param [Calyx::Registry] registry
|
18
18
|
def self.parse(production, registry)
|
19
19
|
expansion = production.split(EXPRESSION).map do |atom|
|
20
20
|
if atom.is_a?(String)
|
@@ -42,7 +42,7 @@ module Calyx
|
|
42
42
|
# Initialize the concat node with an expansion of terminal and
|
43
43
|
# non-terminal fragments.
|
44
44
|
#
|
45
|
-
# @param
|
45
|
+
# @param [Array] expansion
|
46
46
|
def initialize(expansion)
|
47
47
|
@expansion = expansion
|
48
48
|
end
|
@@ -4,9 +4,9 @@ module Calyx
|
|
4
4
|
class Expression
|
5
5
|
# Constructs a node representing a single template substitution.
|
6
6
|
#
|
7
|
-
# @param
|
8
|
-
# @param
|
9
|
-
# @param
|
7
|
+
# @param [#evaluate] production
|
8
|
+
# @param [Array] methods
|
9
|
+
# @param [Calyx::Registry] registry
|
10
10
|
def initialize(production, methods, registry)
|
11
11
|
@production = production
|
12
12
|
@methods = methods.map { |m| m.to_sym }
|
@@ -8,8 +8,8 @@ module Calyx
|
|
8
8
|
# Construct a memoized rule, given the symbol to lookup and the registry
|
9
9
|
# to look it up in.
|
10
10
|
#
|
11
|
-
# @param
|
12
|
-
# @param
|
11
|
+
# @param [Symbol] symbol
|
12
|
+
# @param [Calyx::Registry] registry
|
13
13
|
def initialize(symbol, registry)
|
14
14
|
@symbol = symbol.slice(1, symbol.length-1).to_sym
|
15
15
|
@registry = registry
|
@@ -6,8 +6,8 @@ module Calyx
|
|
6
6
|
# Construct a non-terminal node, given the symbol to lookup and the
|
7
7
|
# registry to look it up in.
|
8
8
|
#
|
9
|
-
# @param
|
10
|
-
# @param
|
9
|
+
# @param [Symbol] symbol
|
10
|
+
# @param [Calyx::Registry] registry
|
11
11
|
def initialize(symbol, registry)
|
12
12
|
@symbol = symbol.to_sym
|
13
13
|
@registry = registry
|
@@ -7,8 +7,8 @@ module Calyx
|
|
7
7
|
# Parse a given list or hash of productions into a syntax tree of weighted
|
8
8
|
# choices.
|
9
9
|
#
|
10
|
-
# @param
|
11
|
-
# @param
|
10
|
+
# @param [Array<Array>, Hash<#to_s, Float>] productions
|
11
|
+
# @param [Calyx::Registry] registry
|
12
12
|
def self.parse(productions, registry)
|
13
13
|
weights_sum = productions.reduce(0) do |memo, choice|
|
14
14
|
memo += choice.last
|
@@ -29,7 +29,7 @@ module Calyx
|
|
29
29
|
|
30
30
|
# Initialize a new choice with a list of child nodes.
|
31
31
|
#
|
32
|
-
# @param
|
32
|
+
# @param [Array] collection
|
33
33
|
def initialize(collection)
|
34
34
|
@collection = collection
|
35
35
|
end
|
data/lib/calyx/registry.rb
CHANGED
@@ -12,30 +12,30 @@ module Calyx
|
|
12
12
|
|
13
13
|
# Hook for defining rules without explicitly calling the `#rule` method.
|
14
14
|
#
|
15
|
-
# @param
|
16
|
-
# @param
|
15
|
+
# @param [Symbol] name
|
16
|
+
# @param [Array] productions
|
17
17
|
def method_missing(name, *arguments)
|
18
18
|
rule(name, *arguments)
|
19
19
|
end
|
20
20
|
|
21
21
|
# Attaches a modifier module to this instance.
|
22
22
|
#
|
23
|
-
# @param
|
23
|
+
# @param [Module] module_name
|
24
24
|
def modifier(name)
|
25
25
|
modifiers.extend(name)
|
26
26
|
end
|
27
27
|
|
28
28
|
# Registers a paired mapping regex.
|
29
29
|
#
|
30
|
-
# @param
|
31
|
-
# @param
|
30
|
+
# @param [Symbol] name
|
31
|
+
# @param [Hash<Regex,String>] pairs
|
32
32
|
def mapping(name, pairs)
|
33
33
|
transforms[name.to_sym] = construct_mapping(pairs)
|
34
34
|
end
|
35
35
|
|
36
36
|
# Registers the given block as a string filter.
|
37
37
|
#
|
38
|
-
# @param
|
38
|
+
# @param [Symbol] name
|
39
39
|
# @block block with a single string argument returning a modified string.
|
40
40
|
def filter(name, callable=nil, &block)
|
41
41
|
if block_given?
|
@@ -47,23 +47,23 @@ module Calyx
|
|
47
47
|
|
48
48
|
# Registers a new grammar rule.
|
49
49
|
#
|
50
|
-
# @param
|
51
|
-
# @param
|
50
|
+
# @param [Symbol] name
|
51
|
+
# @param [Array] productions
|
52
52
|
def rule(name, *productions)
|
53
53
|
rules[name.to_sym] = construct_rule(productions)
|
54
54
|
end
|
55
55
|
|
56
56
|
# Expands the given rule symbol to its production.
|
57
57
|
#
|
58
|
-
# @param
|
58
|
+
# @param [Symbol] symbol
|
59
59
|
def expand(symbol)
|
60
60
|
rules[symbol] || context[symbol]
|
61
61
|
end
|
62
62
|
|
63
63
|
# Applies the given modifier function to the given value to transform it.
|
64
64
|
#
|
65
|
-
# @param
|
66
|
-
# @param
|
65
|
+
# @param [Symbol] name
|
66
|
+
# @param [String] value
|
67
67
|
# @return [String]
|
68
68
|
def transform(name, value)
|
69
69
|
if transforms.key?(name)
|
@@ -76,7 +76,7 @@ module Calyx
|
|
76
76
|
# Expands a memoized rule symbol by evaluating it and storing the result
|
77
77
|
# for later.
|
78
78
|
#
|
79
|
-
# @param
|
79
|
+
# @param [Symbol] symbol
|
80
80
|
def memoize_expansion(symbol)
|
81
81
|
memos[symbol] ||= expand(symbol).evaluate
|
82
82
|
end
|
@@ -86,7 +86,7 @@ module Calyx
|
|
86
86
|
# This is only needed at compile time, so that child classes can easily
|
87
87
|
# inherit the set of rules decared by their parent.
|
88
88
|
#
|
89
|
-
# @param
|
89
|
+
# @param [Calyx::Registry] registry
|
90
90
|
def combine(registry)
|
91
91
|
@rules = rules.merge(registry.rules)
|
92
92
|
end
|
@@ -96,8 +96,8 @@ module Calyx
|
|
96
96
|
#
|
97
97
|
# Produces a syntax tree of nested list nodes.
|
98
98
|
#
|
99
|
-
# @param
|
100
|
-
# @param
|
99
|
+
# @param [Symbol] start_symbol
|
100
|
+
# @param [Hash] rules_map
|
101
101
|
# @return [Array]
|
102
102
|
def evaluate(start_symbol=:start, rules_map={})
|
103
103
|
reset_evaluation_context
|
@@ -135,7 +135,12 @@ module Calyx
|
|
135
135
|
def construct_mapping(pairs)
|
136
136
|
mapper = -> (input) {
|
137
137
|
match, target = pairs.detect { |match, target| input =~ match }
|
138
|
-
|
138
|
+
|
139
|
+
if match && target
|
140
|
+
input.gsub(match, target)
|
141
|
+
else
|
142
|
+
input
|
143
|
+
end
|
139
144
|
}
|
140
145
|
end
|
141
146
|
|
data/lib/calyx/version.rb
CHANGED
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.11.
|
4
|
+
version: 0.11.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rickerby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|