Module: Pray::Literal
- Defined in:
- lib/pray/literal.rb
Defined Under Namespace
Classes: LiteralValue, Parser
Class Method Summary
collapse
Class Method Details
.find_top_level(input, token) ⇒ Object
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
|
# File 'lib/pray/literal.rb', line 56
def find_top_level(input, token)
depth = 0
quote = nil
escaped = false
index = 0
while index < input.length
character = input[index]
if quote
if escaped
escaped = false
elsif character == "\\"
escaped = true
elsif character == quote
quote = nil
end
index += 1
next
end
case character
when '"', "'"
quote = character
when "[", "{", "("
depth += 1
when "]", "}", ")"
depth -= 1
else
return index if depth.zero? && input[index..].start_with?(token)
end
index += 1
end
nil
end
|
.is_balanced?(input) ⇒ Boolean
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/pray/literal.rb', line 91
def is_balanced?(input)
depth = 0
quote = nil
escaped = false
input.each_char do |character|
if quote
if escaped
escaped = false
elsif character == "\\"
escaped = true
elsif character == quote
quote = nil
end
next
end
case character
when '"', "'"
quote = character
when "[", "{", "("
depth += 1
when "]", "}", ")"
depth -= 1
end
end
depth.zero? && quote.nil?
end
|
.parse_literal(input) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/pray/literal.rb', line 121
def parse_literal(input)
parser = Parser.new(input)
value = parser.parse_value
parser.skip_whitespace
unless parser.finished?
raise Error.parse("literal", "unexpected trailing input near #{parser.remaining.inspect}")
end
value
end
|
.parse_literal_array(input) ⇒ Object
138
139
140
141
142
143
|
# File 'lib/pray/literal.rb', line 138
def parse_literal_array(input)
value = parse_literal(input)
raise Error.parse("literal", "expected array literal, found #{value.inspect}") unless value.kind == :array
value.value
end
|
.parse_literal_map(input) ⇒ Object
131
132
133
134
135
136
|
# File 'lib/pray/literal.rb', line 131
def parse_literal_map(input)
value = parse_literal(input)
raise Error.parse("literal", "expected map literal, found #{value.inspect}") unless value.kind == :map
value.value
end
|
.prepare_parser_line(line) ⇒ Object
149
150
151
|
# File 'lib/pray/literal.rb', line 149
def prepare_parser_line(line)
(line).rstrip
end
|
.prepare_parser_lines(text) ⇒ Object
145
146
147
|
# File 'lib/pray/literal.rb', line 145
def prepare_parser_lines(text)
text.lines.map { |line| prepare_parser_line(line) }
end
|
.split_top_level(input, separator) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/pray/literal.rb', line 16
def split_top_level(input, separator)
output = []
start = 0
depth = 0
quote = nil
escaped = false
input.each_char.with_index do |character, index|
if quote
if escaped
escaped = false
elsif character == "\\"
escaped = true
elsif character == quote
quote = nil
end
next
end
case character
when '"', "'"
quote = character
when "[", "{", "("
depth += 1
when "]", "}", ")"
depth -= 1
else
if character == separator && depth.zero?
segment = input[start...index].strip
output << segment unless segment.empty?
start = index + 1
end
end
end
tail = input[start..].strip
output << tail unless tail.empty?
output
end
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/pray/literal.rb', line 153
def (line)
quote = nil
escaped = false
line.each_char.with_index do |character, index|
if quote
if escaped
escaped = false
elsif character == "\\"
escaped = true
elsif character == quote
quote = nil
end
next
end
case character
when '"', "'"
quote = character
when "#"
return line[0...index]
end
end
line
end
|