Module: TL1::AST
- Defined in:
- lib/tl1/ast.rb
Overview
Namespace for AST Nodes in input and output formats
Defined Under Namespace
Classes: ColonSeparatedVariables, CommaSeparatedKeywordVariables, CommaSeparatedVariables, Literal, Node, Variable
Constant Summary
collapse
- NODES_BY_NAME =
[
ColonSeparatedVariables,
CommaSeparatedKeywordVariables,
CommaSeparatedVariables,
Literal,
Variable
].map { |n| [n.to_s.split('::').last, n] }.to_h.freeze
- NODES_BY_CLASS =
NODES_BY_NAME.invert.freeze
Class Method Summary
collapse
Class Method Details
.colon_separated_element(source) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/tl1/ast.rb', line 11
module_function def colon_separated_element(source)
return from_json(source) if source.is_a?(Hash)
raise "Unparseable element #{source}" unless source.is_a?(String)
if source.include?('=')
CommaSeparatedKeywordVariables.parse(source)
elsif source.include?(',')
CommaSeparatedVariables.parse(source)
elsif source.start_with?('<') && source.end_with?('>')
Variable.parse(source)
else
Literal.parse(source)
end
end
|
.from_json(source) ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/tl1/ast.rb', line 26
module_function def from_json(source)
node = NODES_BY_NAME.fetch(source['node']) do
raise "Unknown node type #{source['node']}"
end
node.parse(source['fields'])
end
|
.remove_quotes(string) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/tl1/ast.rb', line 68
module_function def remove_quotes(string)
if string.start_with?('"') && string.end_with?('"')
string[1..-2]
else
string
end
end
|
.split(string, delimiter) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/tl1/ast.rb', line 34
module_function def split(string, delimiter)
scanner = StringScanner.new(string)
array = [String.new]
loop do
return array if scanner.eos?
char = scanner.getch
case char
when delimiter
array << String.new
when '"'
array.last << split_quoted(scanner)
else
array.last << char
end
end
end
|
.split_quoted(scanner) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/tl1/ast.rb', line 52
module_function def split_quoted(scanner)
string = String.new('"')
loop do
raise 'Unexpected end of quoted string' if scanner.eos?
char = scanner.getch
case char
when '"'
string << '"'
return string
else
string << char
end
end
end
|