Class: SDL4R::Parser::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/sdl4r/parser/token.rb

Overview

An SDL token.

Author:

  • Daniel Leuck, Philippe Vosges

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, line = -1,, position = -1)) ⇒ Token

:nodoc: all



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
97
98
99
100
101
102
103
104
# File 'lib/sdl4r/parser/token.rb', line 33

def initialize(text, line = -1, position = -1)
  @text = text
  @line = line
  @position = position
  @size = text.length

  begin
    @type = nil
    @object = nil

    if text =~ /^["`]/
      @type = :STRING
      @object = Parser.parse_string(text)

    elsif text =~ /^'/
      @type = :CHARACTER
      @object = text[1...-1]

    elsif text == "null"
      @type = :NULL
      @object = nil

    elsif text =~ /^true$|^on$/
      @type = :BOOLEAN
      @object = true

    elsif text =~ /^false$|^off$/
      @type = :BOOLEAN
      @object = false

    elsif text =~ /^\[/
      @type=:BINARY
      @object = Parser.parse_binary(text)

    elsif text =~ /^\d+\/\d+\/\d+$/
      @type = :DATE;
      @object = Parser.parse_date_time(text)

    elsif text =~ /^-?\d+d?:\d+/
      @type = :TIME
      @object = parse_time_span_with_zone(text)

    elsif text =~ /^[\d\-\.]/
      @type = :NUMBER
      @object = Parser.parse_number(text)

    else
      case text[0]
      when ?{
        @type = :START_BLOCK
      when ?}
        @type = :END_BLOCK
      when ?=
        @type = :EQUALS
      when ?:
        @type = :COLON
      when ?;
        @type = :SEMICOLON
      end
    end

  rescue ArgumentError
    raise SdlParseError.new($!.message, @line, @position)
  end

  @type = :IDENTIFIER if @type.nil? # if all hope is lost, it's an identifier

  @punctuation =
    @type == :COLON || @type == :SEMICOLON || @type == :EQUALS ||
    @type == :START_BLOCK || @type == :END_BLOCK
  @literal = @type != :IDENTIFIER && !@punctuation
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



106
107
108
# File 'lib/sdl4r/parser/token.rb', line 106

def line
  @line
end

#positionObject (readonly)

Returns the value of attribute position.



106
107
108
# File 'lib/sdl4r/parser/token.rb', line 106

def position
  @position
end

#textObject (readonly)

Returns the value of attribute text.



106
107
108
# File 'lib/sdl4r/parser/token.rb', line 106

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



106
107
108
# File 'lib/sdl4r/parser/token.rb', line 106

def type
  @type
end

Instance Method Details

#literal?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/sdl4r/parser/token.rb', line 108

def literal?
  @literal
end

#object_for_literalObject

Returns the Ruby object corresponding to this literal (or nil if it is not a literal).



114
115
116
# File 'lib/sdl4r/parser/token.rb', line 114

def object_for_literal
  return @object
end

#parse_time_span_with_zone(literal) ⇒ Object

This special parse method is used only by the Token class for tokens which are ambiguously either a TimeSpan or the time component of a date/time type



125
126
127
128
129
130
131
132
# File 'lib/sdl4r/parser/token.rb', line 125

def parse_time_span_with_zone(literal)
  raise ArgumentError("time span or date literal is nil") if literal.nil?

  days, hours, minutes, seconds, time_zone_offset =
    Parser.parse_time_span_and_time_zone(literal, true, true)

  return Parser::TimeSpanWithZone.new(days, hours, minutes, seconds, time_zone_offset)
end

#to_sObject



118
119
120
# File 'lib/sdl4r/parser/token.rb', line 118

def to_s
  @type.to_s + " " + @text + " pos:" + @position.to_s
end