Skip to content

Commit

Permalink
+ prsnt? -> present?, absnt? -> absent?
Browse files Browse the repository at this point in the history
Thanks to txus (Josep M. Bach) for nudging me ever so slightly into that
direction and for providing a patch. I rewrote it to have it exactly my
way, which is not saying that the patch was bad. It just testifies my
perfectionism. Thanks a lot, it makes sense to have it this way.
  • Loading branch information
kschiess committed Mar 2, 2011
1 parent 04bbe35 commit 39fc3d9
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 31 deletions.
5 changes: 5 additions & 0 deletions HISTORY.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
= 2.0 / ?? (future release changes, like a reminder to self)

- prsnt? and absnt? are now finally banned into oblivion. Wasting vocals for
the win.

= 1.2.0 / ???

+ Parslet::Parser is now also a grammar atom, it can be composed freely with
Expand Down
4 changes: 2 additions & 2 deletions example/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class ALanguage < Parslet::Parser
rule(:spaces) { space.repeat }
rule(:space) { multiline_comment | line_comment | str(' ') }

rule(:line_comment) { (str('//') >> (newline.absnt? >> any).repeat).as(:line) }
rule(:multiline_comment) { (str('/*') >> (str('*/').absnt? >> any).repeat >> str('*/')).as(:multi) }
rule(:line_comment) { (str('//') >> (newline.absent? >> any).repeat).as(:line) }
rule(:multiline_comment) { (str('/*') >> (str('*/').absent? >> any).repeat >> str('*/')).as(:multi) }
end

code = %q(
Expand Down
4 changes: 2 additions & 2 deletions example/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
require 'parslet'

class ErbParser < Parslet::Parser
rule(:ruby) { (str('%>').absnt? >> any).repeat.as(:ruby) }
rule(:ruby) { (str('%>').absent? >> any).repeat.as(:ruby) }

rule(:expression) { (str('=') >> ruby).as(:expression) }
rule(:comment) { (str('#') >> ruby).as(:comment) }
rule(:code) { ruby.as(:code) }
rule(:erb) { expression | comment | code }

rule(:erb_with_tags) { str('<%') >> erb >> str('%>') }
rule(:text) { (str('<%').absnt? >> any).repeat(1) }
rule(:text) { (str('<%').absent? >> any).repeat(1) }

rule(:text_with_ruby) { (text.as(:text) | erb_with_tags).repeat.as(:text) }
root(:text_with_ruby)
Expand Down
2 changes: 1 addition & 1 deletion example/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# S = E2 | E1 S

def this(name, &block); return Parslet::Atoms::Entity.new(name, &block) end
def epsilon; any.absnt? end
def epsilon; any.absent? end

# Traditional repetition will try as long as the pattern can be matched and
# then give up. This is greedy and blind.
Expand Down
2 changes: 1 addition & 1 deletion example/minilisp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Parser < Parslet::Parser
rule(:string) {
str('"') >> (
str('\\') >> any |
str('"').absnt? >> any
str('"').absent? >> any
).repeat.as(:string) >> str('"') >> space?
}
end
Expand Down
2 changes: 1 addition & 1 deletion example/readme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
parser = str('"') >>
(
str('\\') >> any |
str('"').absnt? >> any
str('"').absent? >> any
).repeat.as(:string) >>
str('"')

Expand Down
2 changes: 1 addition & 1 deletion example/simple_xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def tag(opts={})

parslet = str('<')
parslet = parslet >> str('/') if close
parslet = parslet >> (str('>').absnt? >> match("[a-zA-Z]")).repeat(1).as(:name)
parslet = parslet >> (str('>').absent? >> match("[a-zA-Z]")).repeat(1).as(:name)
parslet = parslet >> str('>')

parslet
Expand Down
2 changes: 1 addition & 1 deletion example/string_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LiteralsParser < Parslet::Parser
str('"') >>
(
(str('\\') >> any) |
(str('"').absnt? >> any)
(str('"').absent? >> any)
).repeat.as(:string) >>
str('"')
end
Expand Down
4 changes: 2 additions & 2 deletions experiments/heredoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class HereDocs < Parslet::Parser
# a doc_line is either the stop tag followed by nothing
# or just any kind of line.
rule(:doc_line) {
(end_tag.absnt? >> gobble_eol).repeat >> end_tag
(end_tag.absent? >> gobble_eol).repeat >> end_tag
}
rule(:end_tag) { tag.matches(:tag) >> space? >> eol }
# eats anything until an end of line is found
rule(:gobble_eol) { (eol.absnt? >> any).repeat >> eol }
rule(:gobble_eol) { (eol.absent? >> any).repeat >> eol }

rule(:eol) { match['\n\r'].repeat(1) }
rule(:space?) { str(' ').repeat }
Expand Down
16 changes: 12 additions & 4 deletions lib/parslet/atoms/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def |(parslet)
#
# Example:
# # Only proceed the parse if 'a' is absent.
# str('a').absnt?
# str('a').absent?
#
def absnt?
def absent?
Parslet::Atoms::Lookahead.new(self, false)
end

Expand All @@ -61,11 +61,19 @@ def absnt?
#
# Example:
# # Only proceed the parse if 'a' is present.
# str('a').prsnt?
# str('a').present?
#
def prsnt?
def present?
Parslet::Atoms::Lookahead.new(self, true)
end

# Alias for present? that will disappear in 2.0 (deprecated)
#
alias prsnt? present?

# Alias for absent? that will disappear in 2.0 (deprecated)
#
alias absnt? absent?

# Marks a parslet atom as important for the tree output. This must be used
# to achieve meaningful output from the #parse method.
Expand Down
2 changes: 1 addition & 1 deletion lib/parslet/atoms/lookahead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Example:
#
# str('foo').prsnt? # matches when the input contains 'foo', but leaves it
# str('foo').present? # matches when the input contains 'foo', but leaves it
#
class Parslet::Atoms::Lookahead < Parslet::Atoms::Base
attr_reader :positive
Expand Down
4 changes: 2 additions & 2 deletions lib/parslet/expression/treetop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Parser < Parslet::Parser # :nodoc:
rule(:char_class) {
(str('[') >>
(str('\\') >> any |
str(']').absnt? >> any).repeat(1) >>
str(']').absent? >> any).repeat(1) >>
str(']')).as(:match) >> space?
}

Expand All @@ -45,7 +45,7 @@ class Parser < Parslet::Parser # :nodoc:
str('\'') >>
(
(str('\\') >> any) |
(str("'").absnt? >> any)
(str("'").absent? >> any)
).repeat.as(:string) >>
str('\'') >> space?
}
Expand Down
6 changes: 3 additions & 3 deletions spec/acceptance/regression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ArgumentListParser
str('"') >>
(
str('\\') >> any |
str('"').absnt? >> any
str('"').absent? >> any
).repeat.as(:string) >>
str('"') >> space?
end
Expand Down Expand Up @@ -118,8 +118,8 @@ class ALanguage < Parslet::Parser
rule(:space?) { space.repeat }
rule(:space) { multiline_comment.as(:multi) | line_comment.as(:line) | str(' ') }

rule(:line_comment) { str('//') >> (match["\n\r"].absnt? >> any).repeat }
rule(:multiline_comment) { str('/*') >> (str('*/').absnt? >> any).repeat >> str('*/') }
rule(:line_comment) { str('//') >> (match["\n\r"].absent? >> any).repeat }
rule(:multiline_comment) { str('/*') >> (str('*/').absent? >> any).repeat >> str('*/') }
end
describe ALanguage do
def remove_indent(s)
Expand Down
17 changes: 17 additions & 0 deletions spec/parslet/atoms/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Parslet::Atoms::DSL do
describe "deprecated methods" do
let(:parslet) { Parslet.str('foo') }
describe "<- #absnt?" do
subject { parslet.absnt? }
its(:bound_parslet) { should == parslet }
its(:positive) { should == false }
end
describe "<- #prsnt?" do
subject { parslet.prsnt? }
its(:bound_parslet) { should == parslet }
its(:positive) { should == true }
end
end
end
2 changes: 1 addition & 1 deletion spec/parslet/atoms/transform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def visit_lookahead(positive, parslet)
it "should transform a positive lookahead into a negative lookahead" do
apply(
# The order of the sequence gets reversed as well...
str('foo') >> str('foo').absnt?
str('foo') >> str('foo').absent?
).should parse('oof')
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/parslet/atoms/visitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
end
end
describe Parslet::Atoms::Lookahead do
let(:parslet) { str('a').absnt? }
let(:parslet) { str('a').absent? }
it "should call back visitor" do
visitor.should_receive(:visit_lookahead).with(false, Parslet::Atoms::Base).once

Expand Down
14 changes: 7 additions & 7 deletions spec/parslet/atoms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ def src(str); Parslet::Source.new str; end
parslet.inspect.should == "'foo' / 'bar'"
end
end
describe "str('foo').prsnt? (positive lookahead)" do
describe "str('foo').present? (positive lookahead)" do
attr_reader :parslet
before(:each) do
@parslet = str('foo').prsnt?
@parslet = str('foo').present?
end

it "should inspect as &'foo'" do
Expand All @@ -203,10 +203,10 @@ def src(str); Parslet::Source.new str; end
end
end
end
describe "str('foo').absnt? (negative lookahead)" do
describe "str('foo').absent? (negative lookahead)" do
attr_reader :parslet
before(:each) do
@parslet = str('foo').absnt?
@parslet = str('foo').absent?
end

it "should inspect as !'foo'" do
Expand All @@ -232,7 +232,7 @@ def src(str); Parslet::Source.new str; end
attr_reader :parslet
before(:each) do
# repeat will always succeed, since it has a minimum of 0. It will not
# modify input position in that case. absnt? will, depending on
# modify input position in that case. absent? will, depending on
# implementation, match as much as possible and call its inner element
# again. This leads to an infinite loop. This example tests for the
# absence of that loop.
Expand Down Expand Up @@ -350,9 +350,9 @@ def src(str); Parslet::Source.new str; end
parslet.parse('ab').should == { :a => 'b' }
end
end
context "str('a').absnt?" do
context "str('a').absent?" do
it "should return something in merge, even though it is nil" do
(str('a').absnt? >> str('b').as(:b)).
(str('a').absent? >> str('b').as(:b)).
parse('b').should == {:b => 'b'}
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/parslet/export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MiniLisp < Parslet::Parser
rule(:string) {
str('"') >> (
str('\\') >> any |
str('"').absnt? >> any
str('"').absent? >> any
).repeat.as(:string) >> str('"') >> space?
}
end
Expand Down

0 comments on commit 39fc3d9

Please sign in to comment.