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
|
package org.zwobble.mammoth.styles;
import com.google.common.collect.ImmutableList;
import org.parboiled.BaseParser;
import org.parboiled.Parboiled;
import org.parboiled.Rule;
import org.parboiled.parserunners.ReportingParseRunner;
import org.parboiled.support.ParsingResult;
import org.parboiled.support.Var;
import java.util.List;
import static org.zwobble.mammoth.util.MammothLists.list;
import static org.zwobble.mammoth.util.MammothMaps.map;
public class StyleMapParser extends BaseParser<Object> {
public static HtmlPath parseHtmlPath(String value) {
StyleMapParser parser = Parboiled.createParser(StyleMapParser.class);
ReportingParseRunner runner = new ReportingParseRunner(parser.HtmlPath());
ParsingResult<?> result = runner.run(value);
if (result.hasErrors()) {
// TODO: wrap in result
throw new RuntimeException("Parse error");
} else {
return (HtmlPath) result.valueStack.peek();
}
}
Rule HtmlPath() {
ListVar<HtmlPathElement> elements = new ListVar<>();
return Sequence(HtmlPathElements(elements), EOI, push(new HtmlPath(elements.build())));
}
Rule HtmlPathElements(ListVar<HtmlPathElement> elements) {
Var<HtmlPathElement> element = new Var<>();
return FirstOf(
Sequence(
HtmlPathElement(element),
elements.append(element),
ZeroOrMore(Sequence(Whitespace(), Ch('>'), Whitespace(), HtmlPathElement(element), elements.append(element)))),
toRule(push(ImmutableList.builder())));
}
Rule HtmlPathElement(Var<HtmlPathElement> element) {
ListVar<String> tagNames = new ListVar<>();
ListVar<String> classNames = new ListVar<>();
Var<Boolean> fresh = new Var<>();
return Sequence(
TagNames(tagNames),
ClassNames(classNames),
Fresh(fresh),
element.set(new HtmlPathElement(
tagNames.build(),
classNames.isEmpty() ? map() : map("class", String.join(" ", classNames.build())),
!fresh.get())));
}
Rule TagNames(ListVar<String> tagNames) {
return Sequence(
Identifier(),
tagNames.append(match()),
ZeroOrMore(Sequence("|", Identifier(), tagNames.append(match()))));
}
Rule ClassNames(ListVar<String> classNames) {
Var<String> className = new Var<>();
return ZeroOrMore(Sequence(ClassName(className), classNames.append(className)));
}
Rule ClassName(Var<String> className) {
return Sequence(".", Identifier(), className.set(match()));
}
Rule Fresh(Var<Boolean> fresh) {
return Sequence(fresh.set(false), Optional(":fresh", fresh.set(true)));
}
Rule Identifier() {
return Sequence(
FirstOf(CharRange('a', 'z'), CharRange('A', 'Z')),
ZeroOrMore(FirstOf(CharRange('a', 'z'), CharRange('A', 'Z'), CharRange('0', '9'), Ch('-'), Ch('_'))));
}
Rule Whitespace() {
return ZeroOrMore(AnyOf(" \t"));
}
class ListVar<T> extends Var<ImmutableList.Builder<T>> {
boolean append(Var<T> element) {
return append(element.get());
}
boolean append(T element) {
if (get() == null) {
set(ImmutableList.builder());
}
return set(get().add(element));
}
public List<T> build() {
if (get() == null) {
return list();
} else {
return get().build();
}
}
public boolean isEmpty() {
return get() == null;
}
}
}
|