/
laydown.treetop
98 lines (82 loc) · 1.95 KB
/
laydown.treetop
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
grammar LayDown
# root node
# chomps extra newlines
rule laydown
(header / codeblock / paragraph)* ([\n] / eof) <Laydown>
end
# A paragraph
rule paragraph
line+ ([\n] / eof) <Paragraph>
end
# Lines are text or inline code plus a newline
rule line
(text / inlineblock / inlinemath)+ ([\n] / eof) <Line>
end
# Text is everything that doesn't contain an unescaped ` or $.
rule text
('\`' / '\$' / [^$`\n])+ <Text>
end
# An inline block looks like one of the following:
# ``
# `some inline stuff`
# `inline stuff with escaped \``
# `code-directive, actual code`
# `code-directive, actual code, with escaped \``
rule inlineblock
'`' ([^\s,]+ ',')? ('\`' / [^`\n])* '`' <InlineBlock>
end
# Inline math looks like
# $ math syntax $
# $$
rule inlinemath
'$' ('\$' / [^$\n])* '$' <InlineMath>
end
# A header
rule header
(h1 / h2 / h3 / h4 / h5 / h6) [\n]* <Header>
end
# H1 can be denoted either as
# line of text
# ============
# -or-
# # line of text
# -or-
# # line of text #
rule h1
(line '='+ [\n]) / ('#' [\s]+ (text / inlineblock)+ '#'* [\n]) <H1>
end
# H2 can be denoted either as
# line of text
# ------------
# -or-
# ## line of text
# -or-
# ## line of text ####
rule h2
(line '-'+ [\n]) / ('##' [\s]+ (text / inlineblock)+ '#'* [\n]) <H2>
end
# H3 and on can be written as
# ### line of text
# -or-
# ### line of text #####
rule h3
'###' [\s]+ (text / inlineblock)+ '#'* [\n] <H3>
end
rule h4
'####' [\s]+ (text / inlineblock)+ '#'* [\n] <H4>
end
rule h5
'#####' [\s]+ (text / inlineblock)+ '#'* [\n] <H5>
end
rule h6
'######' [\s]+ (text / inlineblock)+ '#'* [\n] <H6>
end
# Code Blocks are either denoted with ``` on it's own line or indented four spaces/one tab
rule codeblock
('```' [^\s\n]* "\n" (!("\n```\n\n") . )* "\n```\n\n") <CodeBlock> / (((' ' / [\t]) (![\n] .)* [\n])+ [\n]) <CodeBlock>
end
# End of file just means there's nothing left to parse
rule eof
!.
end
end