Class: Livetext::ParseSet
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#eos?, #grab, #lookahead, #peek, #prev, #remainder, #skip_spaces, #ungrab
Constructor Details
#initialize(line) ⇒ ParseSet
13
14
15
|
# File 'lib/livetext/parser/set.rb', line 13
def initialize(line) super
end
|
Instance Attribute Details
Returns the value of attribute eos.
7
8
9
|
# File 'lib/livetext/parser/set.rb', line 7
def eos
@eos
end
|
Returns the value of attribute i.
7
8
9
|
# File 'lib/livetext/parser/set.rb', line 7
def i
@i
end
|
Returns the value of attribute len.
7
8
9
|
# File 'lib/livetext/parser/set.rb', line 7
def len
@len
end
|
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
#assignment ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/livetext/parser/set.rb', line 49
def assignment pair = nil
var = value = nil
return if eos?
var = get_var
skip_equal
value = get_value
pair = [var, value]
Livetext::Vars[var.to_sym] = value
pair
end
|
93
94
95
96
|
# File 'lib/livetext/parser/set.rb', line 93
def escaped
grab grab end
|
#get_value ⇒ Object
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
|
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? case char
when /[a-zA-Z_\.0-9]/
name << grab
next
when /[ =]/
return name
else
raise BadVariableName, char, name
end
end
raise NoEqualSign
end
|
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? raise "Expected alpha to start var name" unless char =~ /[a-z]/i
pairs << assignment
char = skip_spaces
break if eos? case char
when nil when ","
char = grab char = skip_spaces
else
raise "Expected comma or end of string (found #{char.inspect})"
end
end
pairs
end
|
#quote?(char) ⇒ Boolean
132
133
134
|
# File 'lib/livetext/parser/set.rb', line 132
def quote?(char)
char == ?" || char == ?'
end
|
#quoted_value ⇒ Object
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 value = ""
char = nil
loop do
char = grab
break if eos?
break if char == quote
char = escaped if char == "\\"
value << char
end
if char == quote
return value
end
raise BadQuotedString, quote + value
end
|
#skip_equal ⇒ Object
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_spaces return peek rescue StopIteration
raise NoEqualSign unless found
return nil
end
|
#unquoted_value ⇒ Object
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 "| " @i.times { TTY.print "-" }
TTY.puts "^"
TTY.puts
end
|