Skip to content

Commit

Permalink
Regin 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jun 22, 2010
1 parent 0c1f68d commit 29e3648
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 207 deletions.
20 changes: 10 additions & 10 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
= Reginald
= Regin

Reginald is a Ruby Reginald syntax parser.
Regin allows you to introspect on Ruby Regexps.

Its parser and tokenizer are written in racc/rexical. Since racc is part of Ruby's standard library, there are no extra runtime dependencies.
Powered by an over the top regexp syntax parser written in racc/rexical.

=== Examples

Determine if a Regexp could be treated as a literal String

Reginald.parse(/foo/).literal? # => true
Reginald.parse(/ba./).literal? # => false
Regin.parse(/foo/).literal? # => true
Regin.parse(/ba./).literal? # => false

Determine whether a character could match a part of a Regexp

Reginald.parse(/foo\/bar/).include?("/") # => true
Reginald.parse(/foo.bar/).include?("/") # => true
Reginald.parse(/foo[a-z]bar/).include?("/") # => false
Regin.parse(/foo\/bar/).include?("/") # => true
Regin.parse(/foo.bar/).include?("/") # => true
Regin.parse(/foo[a-z]bar/).include?("/") # => false

Extract a substring of a Regexp

Reginald.parse(/foobar/)[3..6] => #<Expression "bar">
Reginald.parse(/fo{2}[bB]ar/)[2..5] => #<Expression "[bB]ar">
Regin.parse(/foobar/)[3..6] => #<Expression "bar">
Regin.parse(/fo{2}[bB]ar/)[2..5] => #<Expression "[bB]ar">
10 changes: 5 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
task :default => :test

task :compile => [
'lib/reginald/parser.rb',
'lib/reginald/tokenizer.rb'
'lib/regin/parser.rb',
'lib/regin/tokenizer.rb'
]

file 'lib/reginald/parser.rb' => 'lib/reginald/parser.y' do |t|
file 'lib/regin/parser.rb' => 'lib/regin/parser.y' do |t|
sh "racc -l -o #{t.name} #{t.prerequisites.first}"
sh "sed -i '' -e 's/ class Parser < Racc::Parser/ class Parser < Racc::Parser #:nodoc: all/' #{t.name}"
sh "sed -i '' -e 's/ end # module Reginald/end # module Reginald/' #{t.name}"
sh "sed -i '' -e 's/ end # module Regin/end # module Regin/' #{t.name}"
end

file 'lib/reginald/tokenizer.rb' => 'lib/reginald/tokenizer.rex' do |t|
file 'lib/regin/tokenizer.rb' => 'lib/regin/tokenizer.rex' do |t|
sh "rex -o #{t.name} #{t.prerequisites.first}"
end

Expand Down
8 changes: 4 additions & 4 deletions benchmark/bm_parse.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'reginald'
require 'regin'
require 'benchmark'

TIMES = 10_000.to_i

Benchmark.bmbm do |x|
x.report('foo') { TIMES.times { Reginald.parse(/foo/) } }
x.report('foo(/bar)?') { TIMES.times { Reginald.parse(/foo(\/bar)?/) } }
x.report('foo(/bar(/baz)?)?') { TIMES.times { Reginald.parse(/foo(\/bar(\/baz)?)?/) } }
x.report('foo') { TIMES.times { Regin.parse(/foo/) } }
x.report('foo(/bar)?') { TIMES.times { Regin.parse(/foo(\/bar)?/) } }
x.report('foo(/bar(/baz)?)?') { TIMES.times { Regin.parse(/foo(\/bar(\/baz)?)?/) } }
end

# user system total real
Expand Down
24 changes: 12 additions & 12 deletions lib/reginald.rb → lib/regin.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module Reginald
autoload :Alternation, 'reginald/alternation'
autoload :Anchor, 'reginald/anchor'
autoload :Atom, 'reginald/atom'
autoload :Character, 'reginald/character'
autoload :CharacterClass, 'reginald/character_class'
autoload :Collection, 'reginald/collection'
autoload :Expression, 'reginald/expression'
autoload :Group, 'reginald/group'
autoload :Options, 'reginald/options'
autoload :Parser, 'reginald/parser'
module Regin
autoload :Alternation, 'regin/alternation'
autoload :Anchor, 'regin/anchor'
autoload :Atom, 'regin/atom'
autoload :Character, 'regin/character'
autoload :CharacterClass, 'regin/character_class'
autoload :Collection, 'regin/collection'
autoload :Expression, 'regin/expression'
autoload :Group, 'regin/group'
autoload :Options, 'regin/options'
autoload :Parser, 'regin/parser'

class << self
begin
Expand All @@ -34,7 +34,7 @@ def parse(regexp)

# Recompiles Regexp by parsing it and turning it back into a Regexp.
#
# (In the future Reginald will perform some Regexp optimizations
# (In the future Regin will perform some Regexp optimizations
# such as removing unnecessary captures and options)
def compile(source)
regexp = Regexp.compile(source)
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/alternation.rb → lib/regin/alternation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Alternation < Collection
def self.reduce(alternation_or_expression, expression) #:nodoc:
if alternation_or_expression.first.is_a?(Alternation)
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/anchor.rb → lib/regin/anchor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Anchor < Atom
end
end
4 changes: 2 additions & 2 deletions lib/reginald/atom.rb → lib/regin/atom.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Atom
attr_reader :value, :ignorecase

Expand Down Expand Up @@ -33,7 +33,7 @@ def to_s(parent = false)
end

def inspect #:nodoc:
"#<#{self.class.to_s.sub('Reginald::', '')} #{to_s.inspect}>"
"#<#{self.class.to_s.sub('Regin::', '')} #{to_s.inspect}>"
end

def ==(other) #:nodoc:
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/character.rb → lib/regin/character.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Character < Atom
attr_reader :quantifier

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class CharacterClass < Character
def initialize(value, options = {})
@negate = options[:negate]
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/collection.rb → lib/regin/collection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Collection
include Enumerable

Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/expression.rb → lib/regin/expression.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Expression < Collection
attr_reader :ignorecase
attr_accessor :multiline, :extended
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/group.rb → lib/regin/group.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Group
attr_reader :expression, :quantifier, :capture, :index, :name

Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/options.rb → lib/regin/options.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Reginald
module Regin
class Options
def self.from_int(flags)
multiline = flags & Regexp::MULTILINE != 0
Expand Down
6 changes: 3 additions & 3 deletions lib/reginald/parser.rb → lib/regin/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/reginald/parser.y → lib/regin/parser.y
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Reginald::Parser
class Regin::Parser
rule
expression: expression BAR branch { result = Expression.new(Alternation.reduce(val[0], val[2])) }
| branch { result = Expression.reduce(val[0]) }
Expand Down Expand Up @@ -87,4 +87,4 @@ def initialize
end

---- footer
require 'reginald/tokenizer'
require 'regin/tokenizer'
4 changes: 2 additions & 2 deletions lib/reginald/tokenizer.rb → lib/regin/tokenizer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#--
# DO NOT MODIFY!!!!
# This file is automatically generated by rex 1.0.5.beta1
# from lexical definition file "lib/reginald/tokenizer.rex".
# from lexical definition file "lib/regin/tokenizer.rex".
#++

require 'racc/parser'
class Reginald::Parser < Racc::Parser
class Regin::Parser < Racc::Parser
require 'strscan'

class ScanError < StandardError ; end
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/tokenizer.rex → lib/regin/tokenizer.rex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Reginald::Parser
class Regin::Parser
macro
CTYPES alnum|alpha|ascii|blank|cntrl|digit|graph|lower|print|punct|space|upper|word|xdigit
rule
Expand Down
2 changes: 1 addition & 1 deletion lib/reginald/version.rb → lib/regin/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Reginald
module Regin
Version = '0.2.0'
end
28 changes: 14 additions & 14 deletions spec/alternation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'test_helper'

describe Reginald::Alternation do
describe Regin::Alternation do
context "a|b|c" do
before do
@alternation = Reginald::Alternation.new(
Reginald::Expression.new(Reginald::Character.new('a')),
Reginald::Expression.new(Reginald::Character.new('b')),
Reginald::Expression.new(Reginald::Character.new('c'))
@alternation = Regin::Alternation.new(
Regin::Expression.new(Regin::Character.new('a')),
Regin::Expression.new(Regin::Character.new('b')),
Regin::Expression.new(Regin::Character.new('c'))
)
end

Expand Down Expand Up @@ -65,25 +65,25 @@
end

it "should dup with ignorecase" do
@alternation.dup(:ignorecase => true).should == Reginald::Alternation.new(
Reginald::Expression.new(Reginald::Character.new('a')),
Reginald::Expression.new(Reginald::Character.new('b')),
Reginald::Expression.new(Reginald::Character.new('c')),
@alternation.dup(:ignorecase => true).should == Regin::Alternation.new(
Regin::Expression.new(Regin::Character.new('a')),
Regin::Expression.new(Regin::Character.new('b')),
Regin::Expression.new(Regin::Character.new('c')),
:ignorecase => true
)
end

it "should dup with ignorecase and apply to children" do
@alternation.dup(:ignorecase => true).first.should == Reginald::Expression.new(Reginald::Character.new('a', :ignorecase => true), :ignorecase => true)
@alternation.dup(:ignorecase => true).first.should == Regin::Expression.new(Regin::Character.new('a', :ignorecase => true), :ignorecase => true)
end
end

context "case insensitive a|b|c" do
before do
@alternation = Reginald::Alternation.new(
Reginald::Expression.new(Reginald::Character.new('a')),
Reginald::Expression.new(Reginald::Character.new('b')),
Reginald::Expression.new(Reginald::Character.new('c')),
@alternation = Regin::Alternation.new(
Regin::Expression.new(Regin::Character.new('a')),
Regin::Expression.new(Regin::Character.new('b')),
Regin::Expression.new(Regin::Character.new('c')),
:ignorecase => true
)
end
Expand Down
Loading

0 comments on commit 29e3648

Please sign in to comment.