1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
require "../spec_helper.cr"
include Parcom
# Adapted from: https://bodil.lol/parser-combinators/
struct Attribute
getter name, value
def initialize(@name : String, @value : String)
end
def to_s(io : IO)
io << @name << "=" << @value
end
end
struct Element
getter name, attributes, children
def initialize(@name : String, @attributes : Array(Attribute), @children : Array(Element))
end
def to_s(io : IO)
io << String.build do |str|
str << "<" << @name << " " << @attributes.join(" ")
if @children.empty?
str << "/>"
else
str << ">"
@children.each { |c| str << c }
str << "</" << @name << ">"
end
end
end
end
def match_literal(s : String) : Parser(Char, String)
Parser(Char, Char).token_sequence(s.chars).map(&.join)
end
def letter
Parser(Char, Char).satisfy(&.letter?)
end
def alphanum_dash
Parser(Char, Char).satisfy { |c| c == '-' || c.alphanumeric? }
end
def identifier
(letter + alphanum_dash.many).map do |c, cs|
cs.unshift(c).join
end
end
def equal
Parser.token('=')
end
def quote
Parser.token('"')
end
def string_body
Parser(Char, Char).satisfy(&.!=('"')).many.map(&.join)
end
def string_literal
quote >> string_body << quote
end
def ws_char
Parser(Char, Char).satisfy(&.whitespace?)
end
def within_ws(p)
ws_char.many >> p << ws_char.many
end
def attr_pair
(identifier + (equal >> string_literal)).map do |name, value|
Attribute.new(name, value)
end
end
def attributes
(ws_char.some >> attr_pair).many
end
def element_start
match_literal("<") >> (identifier + attributes)
end
def single_element
(element_start << match_literal("/>")).map do |name, attributes|
Element.new(name, attributes, [] of Element)
end
end
def open_element
(element_start << match_literal(">")).map do |name, attributes|
Element.new(name, attributes, [] of Element)
end
end
def element
within_ws(single_element | parent_element)
end
def close_element(name : String)
match_literal("</") >> match_literal(name) << match_literal(">")
end
def parent_element : Parser(Char, Element)
open_element.and_then do |base_element|
(element.many << close_element(base_element.name)).map do |elems|
Element.new(
base_element.name,
base_element.attributes,
elems
)
end.as(Parser(Char, Element))
end
end
# Adapted from: https://bodil.lol/parser-combinators/
describe "markup language similar to XML" do
expected_value = Element.new(
"top",
[Attribute.new("label", "Top")],
[
Element.new(
"semi-bottom",
[Attribute.new("label", "Bottom")],
[] of Element
),
Element.new(
"middle",
[] of Attribute,
[
Element.new(
"bottom",
[Attribute.new("label", "another bottom")],
[] of Element
)
],
)
]
)
xml = "
<top label=\"Top\">
<semi-bottom label=\"Bottom\"/>
<middle>
<bottom label=\"another bottom\"/>
</middle>
</top>"
result = element.parse(Tokens.from_string(xml))
result.value.should eq(expected_value)
end
|