0.0
The project is in a healthy, maintained state
SAX/Stream style parse helpers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

StreamParser

StreamParser is a Ruby Module to help build fast and efficent parsers.

Installation

Add gem 'stream_parser' to your Gemfile or install via RubyGems (gem install stream_parser)

Usage Example:

Below is an example to extract all quoted strings from a string:

class QuotedStringFinder
  include StreamParser
  
  def parse
    @stack = []
    @results = []
    
    while !eos?
        case @stack.last
        when :double_quoted_string
            if scan_until('"') # Can only use pre_match is there is a match
              @results << pre_match
              @stack.pop
            end
        when :single_quoted_string
            if scan_until("'") # Can only use pre_match is there is a match
              @results << pre_match
              @stack.pop
            end
        else
            scan_until(/['"]/)
            if match == '"'
              @stack << :double_quoted_string
            elsif match == "'"
              @stack << :single_quoted_string
            end
        end
    end
    
    raise SyntaxError.new("Unbalanced Quotes in string") if !@stack.empty?
    
    @results
  end
end

QuotedStringFinder.parse(%q{Here "are" a few 'examples' for "you"})
# => ["are", "examples", "you"]

QuotedStringFinder.parse(%q{Here "ar})
# => SyntaxError "Unbalanced Quotes in string"

Although we grab quoted values ourselfs in this example there is a quoted_value helper as well as a StreamParser::HTML which provides additional helpers such as next_tag, scan_for_tag, next_end_tag and others.