Class: Livetext::ParseSet

Inherits:
StringParser show all
Defined in:
lib/livetext/parser/set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StringParser

#eos?, #grab, #lookahead, #peek, #prev, #remainder, #skip_spaces, #ungrab

Constructor Details

#initialize(line) ⇒ ParseSet

Livetext::ParseSet



13
14
15
# File 'lib/livetext/parser/set.rb', line 13

def initialize(line)   # Livetext::ParseSet
  super
end

Instance Attribute Details

#eosObject (readonly)

Returns the value of attribute eos.



7
8
9
# File 'lib/livetext/parser/set.rb', line 7

def eos
  @eos
end

#iObject (readonly)

Returns the value of attribute i.



7
8
9
# File 'lib/livetext/parser/set.rb', line 7

def i
  @i
end

#lenObject (readonly)

Returns the value of attribute len.



7
8
9
# File 'lib/livetext/parser/set.rb', line 7

def len
  @len
end

#lineObject (readonly)

Returns the value of attribute line.



7
8
9
# File 'lib/livetext/parser/set.rb', line 7

def line
  @line
end

Class Method Details

.parse(str) ⇒ Object



9
10
11
# File 'lib/livetext/parser/set.rb', line 9

def self.parse(str)
  self.new(str).parse
end

Instance Method Details

#assignmentObject

one single var=value



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/livetext/parser/set.rb', line 49

def assignment   # one single var=value
  pair = nil
  var = value = nil
  return if eos?
  var = get_var
  skip_equal
  value = get_value
  # value = Livetext.interpolate(value)
  pair = [var, value]
  Livetext::Vars[var.to_sym] = value
  pair
end

#escapedObject



93
94
95
96
# File 'lib/livetext/parser/set.rb', line 93

def escaped
  grab   # skip backslash
  grab   # return following char
end

#get_valueObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/livetext/parser/set.rb', line 136

def get_value
  char = peek
  flag = quote?(char)
  if flag
    value = quoted_value 
  else
    value = unquoted_value
  end
  value
end

#get_varObject

Raises:

  • (NoEqualSign)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/livetext/parser/set.rb', line 62

def get_var
  name = ""
  loop do
    char = peek
    break if eos?   # end of string
    case char
      when /[a-zA-Z_\.0-9]/
        name << grab
        next
      when /[ =]/
        return name
    else
      raise BadVariableName, char, name
    end
  end
  raise NoEqualSign
end

#parseObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/livetext/parser/set.rb', line 27

def parse
  pairs = []
  char = nil
  loop do
    char = skip_spaces
    break if eos?   # end of string
    raise "Expected alpha to start var name" unless char =~ /[a-z]/i
    pairs << assignment
    char = skip_spaces
    break if eos?   # end of string
    case char
      when nil  # end of string
      when ","
        char = grab  # skip comma
        char = skip_spaces
    else
      raise "Expected comma or end of string (found #{char.inspect})"
    end
  end
  pairs
end

#quote?(char) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/livetext/parser/set.rb', line 132

def quote?(char)
  char == ?" || char == ?'
end

#quoted_valueObject

Raises:

  • (BadQuotedString)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/livetext/parser/set.rb', line 100

def quoted_value
  quote = grab   # opening quote...
  value = ""
  char = nil
  loop do
    char = grab
    break if eos?
    break if char == quote
    char = escaped if char == "\\"
    value << char
  end
  if char == quote
    # char = grab
    return value
  end
  raise BadQuotedString, quote + value
end

#skip_equalObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/livetext/parser/set.rb', line 80

def skip_equal
  found = false
  skip_spaces
  raise NoEqualSign unless peek == "="
  found = true
  grab         # skip =
  skip_spaces  # skip spaces too
  return peek  # just for testing
rescue StopIteration
  raise NoEqualSign unless found
  return nil
end

#unquoted_valueObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/livetext/parser/set.rb', line 118

def unquoted_value
  value = ""
  char = nil
  loop do
    char = peek
    break if char.nil?
    break if eos?
    break if char == " " || char == ","
    value << char
    char = grab
  end
  value
end

#wtf(note = "") ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/livetext/parser/set.rb', line 17

def wtf(note="")
  TTY.puts "| PARSER: @i = #@i   @len = #@len"
  TTY.puts "|#{note}"
  TTY.puts "| [" + @line.gsub(" ", "_") + "]"
  TTY.print "|  "    # 0-based (one extra space)
  @i.times { TTY.print "-" }
  TTY.puts "^"
  TTY.puts
end