Class: SDL4R::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sdl4r/parser.rb,
lib/sdl4r/parser/token.rb,
lib/sdl4r/parser/reader.rb,
lib/sdl4r/parser/tokenizer.rb,
lib/sdl4r/parser/time_span_with_zone.rb

Overview

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

Defined Under Namespace

Classes: Reader, TimeSpanWithZone, Token, Tokenizer

Constant Summary collapse

UNKNOWN_POSITION =

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

-2

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Parser

Creates an SDL parser on the specified IO.

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

Raises:

  • (ArgumentError)


59
60
61
62
63
# File 'lib/sdl4r/parser.rb', line 59

def initialize(io)
  raise ArgumentError, "io == nil" if io.nil?
			
  @tokenizer = Tokenizer.new(io)
end

Instance Method Details

#new_date_time(year, month, day, hour, min, sec, time_zone_offset) ⇒ Object

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


105
106
107
# 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

#parseObject

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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