calyx 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/calyx.rb +2 -0
- data/lib/calyx/file_converter.rb +59 -0
- data/lib/calyx/grammar/production/choices.rb +5 -1
- data/lib/calyx/grammar/production/memo.rb +18 -0
- data/lib/calyx/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 870b438756c9199fdf30b72058d42ce178cf1b82
|
4
|
+
data.tar.gz: 7c6b71e8512d6a26d3f2643dc5efa126a03212bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 259a6fdcca7b525cbb6fa095e6d7b7be54248c145f34213d8f3a725716cebe93814a76cd054d6f5ef79fd694cf5e2679242289e195510360dba881775d0b95fa
|
7
|
+
data.tar.gz: 5773c867c7364309c162aa5c0de4a02dd72663aea2ff5444a67fad59dcb09cd2afbb9faef0719312ff51642f1e8196e63e7937a6a1c40d8ff08ec8dd2f8270d0
|
data/README.md
CHANGED
@@ -252,7 +252,7 @@ grammar = Calyx::Grammar.new do
|
|
252
252
|
end
|
253
253
|
|
254
254
|
grammar.evaluate
|
255
|
-
# => [:start, [:
|
255
|
+
# => [:start, [:choice, [:concat, [[:atom, "Riddle me ree."]]]]]
|
256
256
|
```
|
257
257
|
|
258
258
|
__Note: This feature is still experimental. The tree structure is likely to change so it’s probably best not to rely on it for anything big at this stage.__
|
data/lib/calyx.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require_relative 'calyx/version'
|
2
2
|
require_relative 'calyx/grammar'
|
3
|
+
require_relative 'calyx/file_converter'
|
3
4
|
require_relative 'calyx/grammar/registry'
|
5
|
+
require_relative 'calyx/grammar/production/memo'
|
4
6
|
require_relative 'calyx/grammar/production/choices'
|
5
7
|
require_relative 'calyx/grammar/production/concat'
|
6
8
|
require_relative 'calyx/grammar/production/expression'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Calyx
|
5
|
+
class Grammar
|
6
|
+
class << self
|
7
|
+
def load(filename)
|
8
|
+
file = File.read(filename)
|
9
|
+
extension = File.extname(filename)
|
10
|
+
if extension == ".yml"
|
11
|
+
load_yml(file)
|
12
|
+
elsif extension == ".json"
|
13
|
+
load_json(file)
|
14
|
+
else
|
15
|
+
raise "Cannot convert #{extension} files."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def load_yml(file)
|
20
|
+
yaml = YAML.load(file)
|
21
|
+
create_calyx_class(yaml)
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_json(file)
|
25
|
+
json = JSON.parse(file)
|
26
|
+
json.each do |key, value|
|
27
|
+
if value[0] == ":"
|
28
|
+
json[key] = convert_to_symbol(value)
|
29
|
+
elsif value.is_a?(Array)
|
30
|
+
json[key] = parse_array(value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
create_calyx_class(json)
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_calyx_class(hash)
|
37
|
+
klass = Class.new(Calyx::Grammar)
|
38
|
+
hash.each do |rule_name, rule_productions|
|
39
|
+
klass.send(rule_name, *rule_productions)
|
40
|
+
end
|
41
|
+
klass.new
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse_array(array)
|
45
|
+
array.map do |element|
|
46
|
+
if element[0] == ":"
|
47
|
+
convert_to_symbol(element)
|
48
|
+
else
|
49
|
+
element
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def convert_to_symbol(value)
|
55
|
+
value[1..-1].to_sym
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -7,7 +7,11 @@ module Calyx
|
|
7
7
|
if choice.is_a?(String)
|
8
8
|
Concat.parse(choice, registry)
|
9
9
|
elsif choice.is_a?(Symbol)
|
10
|
-
|
10
|
+
if choice[0] == Memo::SIGIL
|
11
|
+
Memo.new(choice, registry)
|
12
|
+
else
|
13
|
+
NonTerminal.new(choice, registry)
|
14
|
+
end
|
11
15
|
end
|
12
16
|
end
|
13
17
|
self.new(choices)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Calyx
|
2
|
+
class Grammar
|
3
|
+
module Production
|
4
|
+
class Memo
|
5
|
+
SIGIL = '@'.freeze
|
6
|
+
|
7
|
+
def initialize(symbol, registry)
|
8
|
+
@symbol = symbol.slice(1, symbol.length-1).to_sym
|
9
|
+
@registry = registry
|
10
|
+
end
|
11
|
+
|
12
|
+
def evaluate
|
13
|
+
@result ||= [@symbol, @registry.expand(@symbol).evaluate]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
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.9.
|
4
|
+
version: 0.9.1
|
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-04-
|
11
|
+
date: 2016-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,10 +68,12 @@ files:
|
|
68
68
|
- README.md
|
69
69
|
- calyx.gemspec
|
70
70
|
- lib/calyx.rb
|
71
|
+
- lib/calyx/file_converter.rb
|
71
72
|
- lib/calyx/grammar.rb
|
72
73
|
- lib/calyx/grammar/production/choices.rb
|
73
74
|
- lib/calyx/grammar/production/concat.rb
|
74
75
|
- lib/calyx/grammar/production/expression.rb
|
76
|
+
- lib/calyx/grammar/production/memo.rb
|
75
77
|
- lib/calyx/grammar/production/non_terminal.rb
|
76
78
|
- lib/calyx/grammar/production/terminal.rb
|
77
79
|
- lib/calyx/grammar/production/weighted_choices.rb
|