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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 679eb1cd31045fb4f4603dc28566d7d833486c62
4
- data.tar.gz: ba1a2640bc31bfc67d5b354576444e901eb1280f
3
+ metadata.gz: 870b438756c9199fdf30b72058d42ce178cf1b82
4
+ data.tar.gz: 7c6b71e8512d6a26d3f2643dc5efa126a03212bf
5
5
  SHA512:
6
- metadata.gz: 1ad76734b34a1e5483b8da959ec2b8d9658c20e174f86cf1f000d2db0a958e7261519731b2bc46059406d38b540c372f278b619fb78c2cc6c77e288a38d059e1
7
- data.tar.gz: 85d7f3bfd3236406c0a27c075a427527c8dc48c7d2c212187b5af386227ff39138215b58d7b9284959838461a2d5d37d4cfacbe24d633b040f142641474f424d
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, [:term, "Riddle me ree."]]
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
- NonTerminal.new(choice, registry)
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
@@ -1,3 +1,3 @@
1
1
  module Calyx
2
- VERSION = '0.9.0'.freeze
2
+ VERSION = '0.9.1'.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.9.0
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-08 00:00:00.000000000 Z
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