[packages/ruby-syntax] up to 1.0.0, use rubygem as source
glen
glen at pld-linux.org
Thu Apr 25 21:41:25 CEST 2013
commit 333259f7332f5dc5cee28081f5bc6f79ddf36fba
Author: Elan Ruusamäe <glen at delfi.ee>
Date: Thu Apr 25 22:41:07 2013 +0300
up to 1.0.0, use rubygem as source
ruby-syntax.spec | 45 ++---
syntax.rb | 490 -------------------------------------------------------
2 files changed, 24 insertions(+), 511 deletions(-)
---
diff --git a/ruby-syntax.spec b/ruby-syntax.spec
index 6d3c7d4..d969ba9 100644
--- a/ruby-syntax.spec
+++ b/ruby-syntax.spec
@@ -1,15 +1,23 @@
+#
+# Conditional build:
+%bcond_without tests # build without tests
+
+%define pkgname syntax
Summary: Syntax classes for specifying BNF-like grammar in Ruby
Summary(pl.UTF-8): Klasy składni do opisu gramatyk typu BNF w języku Ruby
-Name: ruby-syntax
-Version: 0.1
-Release: 2
-License: GPL
-Group: Development/Libraries
-Source0: syntax.rb
-URL: http://raa.ruby-lang.org/project/syntax/
+Name: ruby-%{pkgname}
+Version: 1.0.0
+Release: 1
+License: Public Domain
+Group: Development/Languages
+Source0: http://gems.rubyforge.org/gems/%{pkgname}-%{version}.gem
+# Source0-md5: d9d2eabc03bc937adfa00e35f228f9a8
+URL: http://syntax.rubyforge.org/
BuildRequires: rpm-rubyprov
BuildRequires: rpmbuild(macros) >= 1.656
-BuildRequires: setup.rb
+%if %{with tests}
+BuildRequires: ruby-minitest
+%endif
BuildArch: noarch
BuildRoot: %{tmpdir}/%{name}-%{version}-root-%(id -u -n)
@@ -20,29 +28,22 @@ Syntax classes for specifying BNF-like grammar in Ruby.
Klasy składni do opisu gramatyk typu BNF w języku Ruby.
%prep
-%setup -qcT
-install -d lib
-cp -p %{SOURCE0} lib
-cp -p %{_datadir}/setup.rb .
+%setup -q -n %{pkgname}-%{version}
%build
-ruby setup.rb config \
- --site-ruby=%{ruby_vendorlibdir} \
- --so-dir=%{ruby_vendorarchdir}
-ruby setup.rb setup
+%if %{with tests}
+ruby -Itest test/ALL-TESTS.rb
+%endif
rdoc --inline-source --op rdoc lib
rdoc --ri --op ri lib
-rm -r ri/{Array,RandomAccessStream,Range,String}
rm ri/cache.ri
rm ri/created.rid
%install
rm -rf $RPM_BUILD_ROOT
-install -d $RPM_BUILD_ROOT{%{ruby_rubylibdir},%{ruby_ridir}}
-ruby setup.rb install \
- --prefix=$RPM_BUILD_ROOT
-
+install -d $RPM_BUILD_ROOT{%{ruby_vendorlibdir},%{ruby_ridir}}
+cp -a lib/* $RPM_BUILD_ROOT%{ruby_vendorlibdir}
cp -a ri/* $RPM_BUILD_ROOT%{ruby_ridir}
%clean
@@ -51,4 +52,6 @@ rm -rf $RPM_BUILD_ROOT
%files
%defattr(644,root,root,755)
%{ruby_vendorlibdir}/syntax.rb
+%{ruby_vendorlibdir}/syntax
+
%{ruby_ridir}/Syntax
diff --git a/syntax.rb b/syntax.rb
deleted file mode 100644
index 80df035..0000000
--- a/syntax.rb
+++ /dev/null
@@ -1,490 +0,0 @@
-
-# classes in this module allow one to define BNF-like grammar directly in Ruby
-# Author: Eric Mahurin
-# license: free, but you are at your own risk if you use it
-
-module Syntax
-
- # base class where common operators are defined
- class Base
- def |(other)
- Alteration.new(self,other)
- end
- def +(other)
- Sequence.new(self,other)
- end
- def *(multiplier)
- Repeat.new(self,multiplier)
- end
- def +@
- Positive.new(self)
- end
- def -@
- Negative.new(self)
- end
- def qualify(*args,&code)
- Qualify.new(self,*args,&code)
- end
- end
-
- # just passes the syntax through - needed for recursive syntax
- class Pass < Base
- def initialize(syntax=NULL)
- @syntax =
- if (syntax.kind_of?Base)
- syntax
- else
- Verbatim.new(syntax)
- end
- end
- def <<(syntax)
- initialize(syntax)
- end
- def ===(stream)
- @syntax===stream
- end
- end
-
- # generic code matches to the stream (first arg to the code)
- # [] operator allows additional arguments to be passed to the code
- class Code < Base
- def initialize(*args,&code)
- @args = args
- @code = code
- end
- def ===(stream,*args) # passing args here will bypass creating a new object
- (match = @code[stream,*(@args+args)]) ||
- stream.buffered || raise(Error.new(stream,"a semantic error"))
- match
- end
- def [](*args)
- self.class.new(*(@args+args),&@code)
- end
- end
-
- # qualify the match with some code that takes the match
- # [] operator allows additional arguments to be passed to the code
- class Qualify < Base
- def initialize(syntax=NULL,*args,&code)
- @syntax =
- if (syntax.kind_of?Base)
- syntax
- else
- Verbatim.new(syntax)
- end
- @args = args
- @code = code
- end
- def ===(stream,*args) # passing args here will bypass creating a new object
- (match = (@syntax===stream)) || (return match)
- (match = @code[match,*(@args+args)]) ||
- stream.buffered || raise(Error.new(stream,"a semantic qualification error"))
- match
- end
- def [](*args)
- self.class.new(@syntax,*(@args+args),&@code)
- end
- end
-
- # sequence of syntaxes
- class Sequence < Base
- def initialize(*syntaxes)
- @syntax = syntaxes.collect do |syntax|
- if (syntax.kind_of?Base)
- syntax
- else
- Verbatim.new(syntax)
- end
- end
- end
- def +(other)
- self.class.new(*(@syntax+[other])) # pull in multiple sequence items
- end
- def <<(other)
- @syntax << ((other.kind_of?Base)?other:Verbatim.new(other))
- end
- def ===(stream)
- matches = []
- @syntax.each do |syntax|
- match = (syntax===stream)
- if (!match)
- matches=NIL
- break
- end
- matches << match if match!=TRUE
- end
- matches
- end
- end
-
- # alternative syntaxes
- class Alteration < Base
- def initialize(*syntaxes)
- @syntax = syntaxes.collect do |syntax|
- if (syntax.kind_of?Base)
- syntax
- else
- Verbatim.new(syntax)
- end
- end
- end
- def |(other)
- self.class.new(*(@syntax+[other])) # pull in multiple alteration items
- end
- def <<(other)
- @syntax << ((other.kind_of?Base)?other:Verbatim.new(other))
- end
- def ===(stream)
- match = nil
- @syntax.detect do |syntax|
- match = stream.buffer { |stream| syntax===stream }
- end
- match || stream.buffered || raise(Error.new(stream,nil,"an alteration"))
- match
- end
- alias =~ ===
- end
-
- # repeating syntax
- class Repeat < Base
- def initialize(syntax,multiplier)
- @syntax =
- if (syntax.kind_of?Base)
- syntax
- else
- Verbatim.new(syntax)
- end
- @repeat =
- if (multiplier.kind_of?Proc)
- multiplier
- else
- lambda do |matches|
- compare = (multiplier<=>(matches.length+1))
- if (compare==0)
- compare = (multiplier<=>matches.length)
- end
- compare
- end
- end
- end
- def ===(stream)
- matches = []
- while ((compare=@repeat[matches])>=0)
- if (compare>0)
- unless (match = (@syntax===stream))
- return NIL
- end
- else
- unless (match = stream.buffer { |stream| @syntax===stream })
- break
- end
- end
- matches << match
- end
- # filter out simple TRUE elements
- matches = matches.find_all { |match| match!=TRUE }
- matches
- end
- end
-
- # positive syntactic predicate
- class Positive < Base
- def initialize(syntax)
- @syntax =
- if (syntax.kind_of?Base)
- syntax
- else
- Verbatim.new(syntax)
- end
- end
- def ===(stream)
- stream.buffer { |stream| match = (@syntax===stream); FALSE }
- if (match)
- TRUE
- else
- stream.buffered || raise(Error.new(stream,nil,"a positive syntatic predicate"))
- FALSE
- end
- end
- end
-
- # negative syntactic predicate
- class Negative < Positive
- def ===(stream)
- stream.buffer { |stream| match = (@syntax===stream); FALSE }
- if (!match)
- TRUE
- else
- stream.buffered || raise(Error.new(stream,nil,"a negative syntatic predicate"))
- FALSE
- end
- end
- end
-
- # all atoms can also use ~ to invert what's matches
-
- # element match (uses === to match)
- class Atom < Base
- def initialize(pattern,length=NIL,invert=FALSE)
- @pattern = pattern
- @length = length
- @invert = invert
- end
- def ~@
- new(pattern,length,!invert)
- end
- def ===(stream)
- element = stream.get(@length)
- match = (@pattern===element)
- match = !match if (@invert)
- if (match==TRUE)
- element || TRUE
- else
- match || begin
- stream.buffered || raise(Error.new(stream,element.inspect, at pattern.inspect))
- FALSE
- end
- end
- end
- end
-
- # element set (uses include? to match)
- class Set < Atom
- def ===(stream)
- element = stream.get(@length)
- match = @pattern.include?(element)
- match = !match if (@invert)
- if (match==TRUE)
- element || TRUE
- else
- match || begin
- stream.buffered || raise(Error.new(stream,element.inspect,"one of these: #{@pattern.to_s}"))
- FALSE
- end
- end
- end
- end
-
- # element lookup array or hash (uses [] to match)
- # translation will occur if the lookup returns anything but TRUE
- class Lookup < Atom
- def =~(stream)
- element = stream.get(@length)
- match = @pattern[element]
- match = !match if (@invert)
- if (match==TRUE)
- element || TRUE
- else
- match || begin
- stream.buffered || raise(Error.new(stream,element.inspect,"one of these: #{@pattern.keys.to_s}"))
- FALSE
- end
- end
- end
- end
-
- # element sequence that knows its length
- class Verbatim < Atom
- def initialize(pattern,invert=FALSE)
- @pattern = pattern
- @invert = invert
- end
- def ~@
- new(pattern,!invert)
- end
- def ===(stream)
- element = stream.get(@pattern.length)
- if (element)
- match = (@pattern===element)
- match = !match if (@invert)
- else
- match = FALSE
- end
- if (match==TRUE)
- element || TRUE
- else
- match || begin
- stream.buffered || raise(Error.new(stream,element.inspect, at pattern.inspect))
- FALSE
- end
- end
- end
- end
-
- # any element
- class Any < Atom
- def initialize(length=NIL,invert=FALSE)
- @length = length
- @invert = invert
- end
- def ~@ # create a never matching Atom
- new(length,!invert)
- end
- def ===(stream)
- element = stream.get(@length)
- !@invert && element
- end
- end
- ANY = Any.new
-
-
- # zero length constants
- FLUSH = Code.new { |stream| stream.flush; TRUE }
- FAIL = Code.new { FALSE }
- NULL = Code.new { TRUE }
- NULLS = Code.new { [] }
- EOF = Code.new { !(element = stream.get) }
-
- # exception class for handling syntax errors
- class Error < RuntimeError
- attr_accessor(:stream,:found,:expected)
- def initialize(stream=nil,found=nil,expected=nil)
- @stream = stream
- @found = found
- @expected = expected
- end
- def to_s
- err = [super]
- err << "found #{found.to_s}" if found
- err << "expected #{expected.to_s}" if expected
- err << stream.location.to_s if stream
- err * ", "
- end
- end
-
-end
-
-
-# class acts like an iterator over a string/array/etc
-# except that using buffer allows one go back to a certain point
-# another class could be designed to work on an IO/File
-class RandomAccessStream
- def initialize(s,pos=0)
- @s = s
- @pos = pos
- @buffered = NIL
- self
- end
- def get(termination=NIL)
- if (@pos>=@s.length)
- # end of file/string/array
- element = NIL
- elsif (!termination)
- # read one character/element
- element = @s[@pos]
- @pos += 1
- else
- # read a sub-string/sub-array
- pos1 = (termination.kind_of?(Integer)) ? @pos+termination :
- (t = @s.index(termination, at pos)) ? t+termination.length :
- @s.length
- element = @s[@pos...pos1]
- @pos = pos1
- end
- element
- end
- def buffer(&code)
- old_buffered = @buffered
- @buffered = @pos if (!@buffered || @pos<@buffered)
- pos = @pos
- match = NIL
- match = code[self]
- if (@buffered && @buffered<=pos)
- @buffered = old_buffered
- elsif (!match)
- raise(IndexError,"need to rewind buffer, but it was flushed")
- end
- @pos = pos if !match
- match
- end
- def flush
- @buffered = NIL
- end
- def buffered
- @buffered ? TRUE : FALSE
- end
- def location
- "index #{@pos} in #{@s.inspect}"
- end
-end
-
-
-# put stuff in String to have Syntax objects magically appear
-class String
- def |(other)
- Syntax::Verbatim.new(self)|other
- end
- def +@
- +Syntax::Verbatim.new(self)
- end
- def -@
- -Syntax::Verbatim.new(self)
- end
- alias _repeat *
- def *(other)
- if (other.kind_of?Numeric)
- _repeat(other)
- else
- Syntax::Verbatim.new(self)*other
- end
- end
- alias _concat +
- def +(other)
- if (other.kind_of?String)
- _concat(other)
- else
- Syntax::Verbatim.new(self)+other
- end
- end
- def ===(other)
- if (other.kind_of?String)
- self==other
- else
- Syntax::Verbatim.new(self)===other
- end
- end
- def qualify(&code)
- Syntax::Verbatim.new(self).qualify(&code)
- end
-end
-
-# allow an Array to look more like a Hash with keys and values
-class Array
- def keys
- (0...length).find_all { |i| self[i] }
- end
- def values
- find_all { | element | element }
- end
-end
-
-# make things fully comparable to Ranges
-# also * makes a Syntax
-class Range
- include Comparable
- def <=>(other)
- if (other<self.begin)
- +1
- elsif (if exclude_end? then other>=self.end else other>self.end end)
- -1
- else
- 0
- end
- end
- alias _old_equal ==
- def ==(other)
- if (other.kind_of?Range)
- # undocumented previous functionality
- _old_equal(other)
- else
- (self<=>other)==0
- end
- end
- def *(other)
- Syntax::Atom.new(self,1)*other
- end
-end
-
-
-
-
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/packages/ruby-syntax.git/commitdiff/333259f7332f5dc5cee28081f5bc6f79ddf36fba
More information about the pld-cvs-commit
mailing list