Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigovilina committed Aug 2, 2024
1 parent b9ab343 commit 56272bc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 31 deletions.
28 changes: 5 additions & 23 deletions lib/risk/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,11 @@ def self.read(io)
end

def self.parse(input)
parse_unit(input)
.bind_none { parse_atom(input) }
.bind_none { parse_empty(input) }
end

def self.parse_unit(input)
Farseer::And.new(
Farseer::Char::L_PARENS,
Farseer::Regexp::WS,
Farseer::Char::R_PARENS
).parse(input).map { S.new }
end

def self.parse_atom(input)
Farseer::Chars::DIGITS.parse(input)
.map { |digit_result| S.new(digit_result.token) }
end

def self.parse_empty(input)
case input
when '' then Maybe.return S.new
else Maybe.none
end
Unit.parse(input)
.bind_none { Atom.parse(input) }
.bind_none { List.parse(input) }
.bind_none { Empty.parse(input) }
end
end
end

15 changes: 15 additions & 0 deletions lib/risk/read/atom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Risk
module Read
module Atom
def self.parse(input)
parser.parse(input)
end

def self.parser
Farseer::Map.new(Farseer::Chars::DIGITS) { |digit| S.new(digit) }
end
end
end
end
15 changes: 15 additions & 0 deletions lib/risk/read/empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Risk
module Read
module Empty
def self.parse(input)
parser.parse(input)
end

def self.parser
Farseer::Map.new(Farseer::Empty.new) { S.new }
end
end
end
end
17 changes: 17 additions & 0 deletions lib/risk/read/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Risk
module Read
module List
def self.parse(input)
Farseer::Char::L_PARENS.parse(input).bind do |res|
Read.parse(res.rest).bind do |rres|
Farseer::Char::R_PARENS.parse(rres.rest).map do
rres
end
end
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/risk/read/unit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Risk
module Read
module Unit
def self.parse(input)
parser.parse(input)
end

def self.parser
parser = Farseer::And.new(Farseer::Char::L_PARENS, Farseer::Regexp::WS, Farseer::Char::R_PARENS)
Farseer::Map.new(parser) { S.new }
end
end
end
end

22 changes: 14 additions & 8 deletions spec/risk/read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@
RSpec.describe Risk::Read do
describe '.parse' do
specify do
expect(described_class.parse('').value!).to eq S.new
expect(described_class.parse('').value!.token).to eq S.new
end

specify 'does not parse an open parenthesis', :aggregate_failures do
result = described_class.parse('(').value!
expect(result.token).to eq S.new
expect(result.rest).to eq '('
end

specify do
expect { described_class.parse('(').value! }
.to raise_error Maybe::UnwrappingError
expect(described_class.parse('()').value!.token).to eq S.new
end

specify do
expect(described_class.parse('()').value!).to eq S.new
expect(described_class.parse('( )').value!.token).to eq S.new
end

specify do
expect(described_class.parse('( )').value!).to eq S.new
expect(described_class.parse('( )').value!.token).to eq S.new
end

specify do
expect(described_class.parse('( )').value!).to eq S.new
expect(described_class.parse('1').value!.token).to eq S.new('1')
end

specify do
expect(described_class.parse('1').value!).to eq S.new('1')
expect(described_class.parse('(1)').value!.token).to eq S.new('1')
end
end

Expand All @@ -35,7 +40,8 @@
chomp_double = double(chomp: '')
io = double(gets: chomp_double)

expect(described_class.read(io)).to eq Maybe.return(S.new)
expect(described_class.read(io).value!.token).to eq S.new

expect(io).to have_received(:gets)
expect(chomp_double).to have_received(:chomp)
end
Expand Down

0 comments on commit 56272bc

Please sign in to comment.