Skip to content

Commit

Permalink
remove Parsby::Token...
Browse files Browse the repository at this point in the history
It's better not to have it since we're more dependent on the labels
generated by define_combinator now.
  • Loading branch information
jolmg committed Sep 20, 2020
1 parent 01762e0 commit e773fe7
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 77 deletions.
33 changes: 2 additions & 31 deletions lib/parsby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,31 +374,6 @@ def message
end
end

class Token
attr_reader :name

# Makes a token with the given name.
def initialize(name)
@name = name
end

# Renders token name by surrounding it in angle brackets.
def to_s
"<#{name}>"
end

# Compare tokens
def ==(t)
t.is_a?(self.class) && t.name == name
end

# Flipped version of Parsby#%, so you can specify the token of a parser
# at the beginning of a parser expression.
def %(p)
p % self
end
end

class Backup < StringIO
def with_saved_pos(&b)
saved = pos
Expand Down Expand Up @@ -620,14 +595,10 @@ def furthest_parsed_range

# The parser's label. It's an "unknown" token by default.
def label
@label || Token.new("unknown")
@label || "unknown"
end

# Assign label to parser. If given a symbol, it'll be turned into a
# Parsby::Token.
def label=(name)
@label = name.is_a?(Symbol) ? Token.new(name) : name
end
attr_writer :label

# Initialize parser with optional label argument, and parsing block. The
# parsing block is given an IO as argument, and its result is the result
Expand Down
7 changes: 1 addition & 6 deletions lib/parsby/combinators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def included(base)

# Parses a decimal number as matched by \d+.
define_combinator :decimal do
many_1(decimal_digit).fmap {|ds| ds.join.to_i } % token("number")
many_1(decimal_digit).fmap {|ds| ds.join.to_i }
end

# This is taken from the Json subparser for numbers.
Expand Down Expand Up @@ -375,10 +375,5 @@ def splicer
end
end
end

# Makes a token with the given name.
def token(name)
Parsby::Token.new name
end
end
end
6 changes: 0 additions & 6 deletions spec/parsby/combinators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,4 @@ def self.parenthesis
expect { eof.parse("x") }.to raise_error Parsby::ExpectationFailed
end
end

describe "#token" do
it "builds a token with the given name" do
expect(token "foo").to be_a(Parsby::Token).and satisfy {|t| t.name == "foo"}
end
end
end
37 changes: 3 additions & 34 deletions spec/parsby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -728,33 +728,6 @@ def tree(e, &b)
end
end

describe Parsby::Token do
describe "#to_s" do
it "wraps name in angle brackets" do
expect(Parsby::Token.new("foo").to_s).to eq "<foo>"
end
end

describe "#initialize" do
it "takes name as argument" do
expect(Parsby::Token.new("foo").name).to eq "foo"
end
end

describe "#==" do
it "compares tokens" do
expect(Parsby::Token.new("foo")).to eq Parsby::Token.new("foo")
end
end

describe "#%" do
it "is the flipped version of Parsby's" do
expect((Parsby::Token.new("foo") % lit("foo")).label.to_s)
.to eq "<foo>"
end
end
end

describe Parsby::BackedIO do
let(:pipe) { IO.pipe }
let(:r) { pipe[0] }
Expand Down Expand Up @@ -994,8 +967,7 @@ def piped(s, &b)
end

it "when label is not provided, it's an unknown token" do
expect(Parsby.new.label.class).to eq Parsby::Token
expect(Parsby.new.label.name).to eq "unknown"
expect(Parsby.new.label).to eq "unknown"
end

it "takes block that provides a BackedIO as argument, and which result is the result of #parse" do
Expand Down Expand Up @@ -1030,16 +1002,13 @@ def piped(s, &b)
describe "#label=" do
it "assigns strings as is" do
expect(Parsby.new.tap {|p| p.label = "foo"}.label.to_s).to eq "foo"
end

it "turns it into a token if it's a symbol" do
expect(Parsby.new.tap {|p| p.label = :foo}.label.to_s).to eq "<foo>"
expect(Parsby.new.tap {|p| p.label = :foo}.label.to_s).to eq "foo"
end
end

describe "#label" do
it "defaults to unknown token" do
expect(Parsby.new.label.to_s).to eq "<unknown>"
expect(Parsby.new.label).to eq "unknown"
end
end

Expand Down

0 comments on commit e773fe7

Please sign in to comment.