speculate_about 0.2.3 → 0.3.0

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
  SHA256:
3
- metadata.gz: a3d2e27062babfecbf03e34c59b76b495cbb1a9f27d3cbd0f4b23501d3be9181
4
- data.tar.gz: bdde751f7fa2c532dc95551f06ecc22f44fba32ad0f70103371e1743f62ca347
3
+ metadata.gz: d53e888a2cad71c7c20205f0f34d7ededa3df3feceef7d567f051d362a36ccb8
4
+ data.tar.gz: ee00700ca0a19000b8098cb3022b462da0537477d58ee912f250aba6b02e3e7c
5
5
  SHA512:
6
- metadata.gz: 95b2fd95e73b0b95d8c9237200bdb86c198f8584987d1f0a54968b742b46c1a06ffff29eb6352962a14e54c9d9d7e047a02f98c57b78a8894fc5c14a4f941d8c
7
- data.tar.gz: bc56ce52b6e4bc87fbff62c5fb5583ad6fe683b4dd78e18bc8b6d6da28bfbdedf46d3397e297c70878dff8633e3c8c0aa15cb89c871d01ff3c84a8c32c3ccf12
6
+ metadata.gz: 3ced515885a50ff873cc4b471fe03eab69dc3e7c4f409e4e5ff392a91bfd94a08946aae7eb866607b27ecad62104beee68d8a52230fa2e216e1a544800a4ef0e
7
+ data.tar.gz: 792619744dab6387ad2e70c7fa53eb49c3b8b9f26470a5de7c92477c3cc274075d7cff720b5fb8dc0a87bf71d2af016517e72449d56298824d3f865253b64192
@@ -3,11 +3,11 @@ require 'rspec'
3
3
  require 'speculations/parser'
4
4
 
5
5
  module SpeculateAbout
6
- def speculate_about file
6
+ def speculate_about file, alternate_syntax: false
7
7
  paths = _find_files(file, File.dirname( caller.first ))
8
8
  raise ArgumentError, "no files found for pattern #{file}" if paths.empty?
9
9
  paths.each do |path|
10
- code = _compile path, _readable(path)
10
+ code = _compile path, _readable(path), alternate_syntax: alternate_syntax
11
11
  ENV["SPECULATE_ABOUT_DEBUG"] ? _show(code, path) : instance_eval(code, path)
12
12
  end
13
13
  end
@@ -15,8 +15,8 @@ module SpeculateAbout
15
15
 
16
16
  private
17
17
 
18
- def _compile path, file
19
- ast = Speculations::Parser.new.parse_from_file(path, file)
18
+ def _compile path, file, alternate_syntax: false
19
+ ast = Speculations::Parser.new.parse_from_file(path, file, alternate_syntax: alternate_syntax)
20
20
  ast.to_code
21
21
  end
22
22
  def _readable(path)
@@ -3,7 +3,7 @@ class Speculations::Parser
3
3
  require_relative './parser/context'
4
4
  require_relative './parser/state'
5
5
 
6
- attr_reader :filename, :input, :orig_filename, :root, :state
6
+ attr_reader :alternate_syntax, :filename, :input, :orig_filename, :root, :state
7
7
 
8
8
  def self.parsers
9
9
  @__parsers__ ||= {
@@ -14,7 +14,8 @@ class Speculations::Parser
14
14
  }
15
15
  end
16
16
 
17
- def parse_from_file file, orig_filename = nil
17
+ def parse_from_file file, orig_filename = nil, alternate_syntax: false
18
+ @alternate_syntax = alternate_syntax
18
19
  @filename = file
19
20
  @orig_filename = orig_filename || file
20
21
  @input = File
@@ -31,7 +32,7 @@ class Speculations::Parser
31
32
  end
32
33
 
33
34
  def parse!
34
- root = node = Context.new(name: "Speculations from #{@filename}", lnb: 0, filename: @filename, orig_filename: orig_filename, parent: nil)
35
+ root = node = Context.new(alternate_syntax: alternate_syntax, name: "Speculations from #{@filename}", lnb: 0, filename: @filename, orig_filename: orig_filename, parent: nil)
35
36
  input.each_with_index do |line, lnb|
36
37
  @state, node = self.class.parsers.fetch(@state).parse(line, lnb.succ, node)
37
38
  end
@@ -3,11 +3,11 @@ class Speculations::Parser::Context
3
3
  require_relative './context/example'
4
4
  require_relative './context/setup'
5
5
 
6
- attr_reader :filename, :level, :lnb, :name, :orig_filename, :parent, :potential_name, :setup
6
+ attr_reader :alternate_syntax, :filename, :level, :lnb, :name, :orig_filename, :parent, :potential_name, :setup
7
7
 
8
8
  def add_child(name:, lnb:)
9
9
  raise "Illegal nesting" if parent
10
- children << self.class.new(name: name, lnb: lnb, parent: self)
10
+ children << self.class.new(alternate_syntax: alternate_syntax, name: name, lnb: lnb, parent: self)
11
11
  children.last
12
12
  end
13
13
 
@@ -60,8 +60,9 @@ class Speculations::Parser::Context
60
60
 
61
61
  private
62
62
 
63
- def initialize(lnb:, name:, filename: nil, orig_filename: nil, parent: nil)
63
+ def initialize(alternate_syntax: false, lnb:, name:, filename: nil, orig_filename: nil, parent: nil)
64
64
  _init_from_parent filename, orig_filename, parent
65
+ @alternate_syntax = alternate_syntax
65
66
  @level = parent ? parent.level.succ : 1
66
67
  @lnb = lnb
67
68
  @setup = nil
@@ -9,7 +9,9 @@ module Speculations::Parser::State extend self
9
9
  EOBLOCK_RGX = %r[\A\s{0,3}```\s*\z]
10
10
  EXAMPLE_RGX = %r[\A\s{0,3}```.*\s:example]
11
11
  INCLUDE_RGX = %r[\A\s{0,3}```.*\s:include]
12
- NAME_RGX = %r[\A\s{0,3}Example:\s+(.*)]
12
+ NAME_RGX = %r[\A\s{0,3}Example:?\s+(.*)]i
13
+ RUBY_RGX = %r[\A\s{0,3}```ruby]
14
+ WS_RGX = %r[\A\s*\z]
13
15
 
14
16
  def before_match line
15
17
  BEFORE_RGX =~ line
@@ -36,4 +38,12 @@ module Speculations::Parser::State extend self
36
38
  def potential_name line
37
39
  NAME_RGX.match(line)
38
40
  end
41
+
42
+ def ruby_match line
43
+ RUBY_RGX =~ line
44
+ end
45
+
46
+ def ws_match line
47
+ WS_RGX =~ line
48
+ end
39
49
  end
@@ -4,6 +4,42 @@ module Speculations
4
4
  module Out extend self
5
5
 
6
6
  def parse line, lnb, node
7
+ if node.alternate_syntax
8
+ _parse_alternate line, lnb, node
9
+ else
10
+ _parse_classical line, lnb, node
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def _parse_alternate line, lnb, node
17
+ case
18
+ when name = State.context_match(line)
19
+ node = node.parent if node.parent
20
+ new_node = node.add_child(name: name, lnb: lnb)
21
+ new_node.set_name(nil)
22
+ [:out, new_node]
23
+ when State.ws_match(line)
24
+ [:out, node]
25
+ when name = State.potential_name(line)
26
+ node.set_name(name[1])
27
+ [:out, node]
28
+ when State.ruby_match(line)
29
+ if node.potential_name
30
+ node = node.add_example(lnb: lnb, line: line)
31
+ [:exa, node]
32
+ else
33
+ node = node.add_include(lnb: lnb)
34
+ [:inc, node]
35
+ end
36
+ else
37
+ node.set_name(nil)
38
+ [:out, node]
39
+ end
40
+ end
41
+
42
+ def _parse_classical line, lnb, node
7
43
  case
8
44
  when name = State.context_match(line)
9
45
  node = node.parent if node.parent
@@ -1,3 +1,3 @@
1
1
  module SpeculateAbout
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: speculate_about
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-12 00:00:00.000000000 Z
11
+ date: 2020-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec