Class SDL4R::Parser
In: lib/sdl4r/parser.rb
lib/sdl4r/parser/reader.rb
lib/sdl4r/parser/tokenizer.rb
lib/sdl4r/parser/token.rb
lib/sdl4r/parser/time_span_with_zone.rb
Parent: Object

The SDL parser.

In Ruby 1.8, in order to enable UTF-8 support, you may have to declare the following lines:

  $KCODE = 'u'
  require 'jcode'

This will give you correct input and output and correct UTF-8 "general" sorting. Alternatively you can use the following options when launching the Ruby interpreter:

  /path/to/ruby -Ku -rjcode

Authors

Daniel Leuck, Philippe Vosges

Methods

new   new_date_time   parse  

Constants

UNKNOWN_POSITION = -2   Passed to parse_error() in order to specify an error that occured on no specific position (column).

Public Class methods

Creates an SDL parser on the specified IO.

  IO.open("path/to/sdl_file") { |io|
    parser = SDL4R::Parser.new(io)
    tags = parser.parse()
  }

[Source]

# File lib/sdl4r/parser.rb, line 59
    def initialize(io)
      raise ArgumentError, "io == nil" if io.nil?
                        
      @tokenizer = Tokenizer.new(io)
    end

Public Instance methods

Creates and returns the object representing a datetime (DateTime in the default implementation). Can be overriden.

  def new_date_time(year, month, day, hour, min, sec, time_zone_offset)
    Time.utc(year, month, day, hour, min, sec)
  end

[Source]

# File lib/sdl4r/parser.rb, line 105
    def new_date_time(year, month, day, hour, min, sec, time_zone_offset)
      SDL4R::new_date_time(year, month, day, hour, min, sec, time_zone_offset)
    end

Parses the underlying IO and returns an Array of Tag.

Errors

IOError
If a problem is encountered with the IO
SdlParseError
If the document is malformed

[Source]

# File lib/sdl4r/parser.rb, line 70
    def parse
      tags = []
                        
      while tokens = @tokenizer.read_line_tokens()
        if tokens.last.type == :START_BLOCK
          # tag with a block
          tag = construct_tag(tokens[0...-1])
          add_children(tag)
          tags << tag

        elsif tokens.first.type == :END_BLOCK
          # we found an block end token that should have been consumed by
          # add_children() normally
          parse_error(
            "No opening block ({) for close block (}).",
            tokens.first.line,
            tokens.first.position)
        else
          # tag without block
          tags << construct_tag(tokens)
        end
      end
                        
      @tokenizer.close()
                        
      return tags
    end

[Validate]