From e773fe76b74ac5d88f3318ff2de08e42f592e2b9 Mon Sep 17 00:00:00 2001 From: Jorge Luis Martinez Gomez Date: Sun, 20 Sep 2020 12:33:04 -0700 Subject: [PATCH] remove Parsby::Token... It's better not to have it since we're more dependent on the labels generated by define_combinator now. --- lib/parsby.rb | 33 ++--------------------------- lib/parsby/combinators.rb | 7 +------ spec/parsby/combinators_spec.rb | 6 ------ spec/parsby_spec.rb | 37 +++------------------------------ 4 files changed, 6 insertions(+), 77 deletions(-) diff --git a/lib/parsby.rb b/lib/parsby.rb index cf54509..3880a00 100644 --- a/lib/parsby.rb +++ b/lib/parsby.rb @@ -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 @@ -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 diff --git a/lib/parsby/combinators.rb b/lib/parsby/combinators.rb index cd43504..9d28b63 100644 --- a/lib/parsby/combinators.rb +++ b/lib/parsby/combinators.rb @@ -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. @@ -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 diff --git a/spec/parsby/combinators_spec.rb b/spec/parsby/combinators_spec.rb index a1b61e4..3418051 100644 --- a/spec/parsby/combinators_spec.rb +++ b/spec/parsby/combinators_spec.rb @@ -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 diff --git a/spec/parsby_spec.rb b/spec/parsby_spec.rb index 3e6fa96..22b7d6b 100644 --- a/spec/parsby_spec.rb +++ b/spec/parsby_spec.rb @@ -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 "" - 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 "" - end - end - end - describe Parsby::BackedIO do let(:pipe) { IO.pipe } let(:r) { pipe[0] } @@ -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 @@ -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 "" + 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 "" + expect(Parsby.new.label).to eq "unknown" end end