File: tiny_markdown.kpeg

package info (click to toggle)
ruby-kpeg 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: ruby: 11,839; makefile: 10
file content (199 lines) | stat: -rw-r--r-- 5,805 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
%% name = TinyMarkdown::Parser
%% ast-location = ::TinyMarkdown
%% document = ast DocumentNode(compiler, position, content)
%% para = ast ParaNode(compiler, position, content)
%% plain = ast PlainNode(compiler, position, content)
%% text = ast TextNode(compiler, position, content)
%% headline = ast HeadlineNode(compiler, position, level, content)
%% block_quote = ast BlockQuoteNode(compiler, position, content)
%% verbatim = ast VerbatimNode(compiler, position, content)
%% horizontal_rule = ast HorizontalRuleNode(compiler, position)
%% bullet_list = ast BulletListNode(compiler, position, content)
%% list = ast ListNode(compiler, position, content)
%% bullet_list_item = ast BulletListItemNode(compiler, position, content)
%% linebreak = ast LineBreakNode(compiler, position)
%% inline_element = ast InlineElementNode(compiler, position, name, content)

%% {
  attr_reader :ast

  class Position
    attr_accessor :pos, :line, :col
    def initialize(compiler)
      @pos = compiler.pos
      @line = compiler.current_line
      @col = compiler.current_column
    end
  end

  def position
    Position.new(self)
  end
}

root = Start

Start = &. Doc:c { @ast = c  }

Doc =       Block*:c
            ~document(self, position, c)

Block =     BlankLine*
            ( BlockQuote
            | Verbatim
            | HorizontalRule
            | Heading
            | BulletList
            | Para
            | Plain )

Para =      NonindentSpace Inlines:a BlankLine+ ~para(self, position, a)

Plain =     Inlines:a ~plain(self, position, a)

AtxInline = !Newline !(Sp '#'* Sp Newline) Inline:c { c }

AtxStart =  < /######|#####|####|###|##|#/ > { text.length }

AtxHeading = AtxStart:level Sp AtxInline+:c (Sp "#"* Sp)?  Newline ~headline(self, position, level, c)

Heading = AtxHeading

BlockQuote = BlockQuoteRaw:c ~block_quote(self, position, c)

BlockQuoteRaw =  ( '>' ' '? Line:c { c })+:cc { cc }

NonblankIndentedLine = !BlankLine IndentedLine:c { c }

VerbatimChunk =  (BlankLine { text(self,position,"\n") } )*:c1
                 (NonblankIndentedLine:c { [c, text(self,position,"\n")] })+:c2 { c1 + c2.flatten }

Verbatim =     VerbatimChunk+:cc ~verbatim(self, position, cc.flatten)

HorizontalRule = NonindentSpace
                 ( '*' Sp '*' Sp '*' (Sp '*')*
                 | '-' Sp '-' Sp '-' (Sp '-')*
                 | '_' Sp '_' Sp '_' (Sp '_')*)
                 Sp Newline BlankLine+ ~horizontal_rule(self, position)

Bullet = !HorizontalRule NonindentSpace ('+' | '*' | '-') Spacechar+

BulletList = &Bullet ListTight:c ~bullet_list(self, position, c)

ListTight = ListItemTight+:cc
            BlankLine* !Bullet
            { cc }

ListItemTight = Bullet ListBlock:c ~bullet_list_item(self, position, c)

ListBlock = !BlankLine Line:c ListBlockLine*:cc { cc.unshift(c) }

ListBlockLine = !BlankLine
                !( Indent? Bullet )
                !HorizontalRule
                OptionallyIndentedLine



Inlines  =  ( !Endline Inline:c { c }
            | Endline:c &Inline { c } )+:cc Endline?
            { cc }

Inline  = Str
        | Endline
        | Space
        | Strong
        | Emph
        | Code
        | Symbol

Space = Spacechar+:c ~text(self, position, c.join(""))

Str = NormalChar+:c1
      StrChunk*:c2
      ~text(self, position, (c1+c2).join(""))

StrChunk = (NormalChar:c { [c] } | '_'+:c1 NormalChar:c2 { c1.push(c2) } )+:cc  { cc.flatten }


Endline =   LineBreak | TerminalEndline | NormalEndline

NormalEndline =   Sp Newline !BlankLine !'>' !AtxStart
                  !(Line ('='+ | '-'+) Newline)
                  ~text(self, position, "\n")

TerminalEndline = Sp Newline Eof ~text(self, position, "\n")

LineBreak = "  " NormalEndline ~linebreak(self, position)

Symbol =    SpecialChar:c ~text(self, position, c)


Emph =      EmphStar | EmphUl

Whitespace = Spacechar | Newline

EmphStar =  '*' !Whitespace
            ( !'*' Inline:b { b }
            | StrongStar:b  { b }
            )+:c
            '*'
            ~inline_element(self, position, :em, c)

EmphUl =    '_' !Whitespace
            ( !'_' Inline:b { b }
            | StrongUl:b  { b }
            )+:c
            '_'
            ~inline_element(self, position, :em, c)

Strong = StrongStar | StrongUl

StrongStar =    "**" !Whitespace
                ( !"**" Inline:b { b })+:c
                "**"
                ~inline_element(self, position, :strong, c)

StrongUl   =    "__" !Whitespace
                ( !"__" Inline:b { b })+:c
                "__"
                ~inline_element(self, position, :strong, c)



Ticks1 = < /`/ > !'`' { text }
Ticks2 = < /``/ > !'`' { text }

Code =  ( Ticks1 Sp
           ( !'`' Nonspacechar )+:c
           Sp Ticks1  ~text(self, position, c.join(""))
        | Ticks2 Sp
           ( !'``' Nonspacechar )+:c
           Sp Ticks2 ~text(self, position, c.join(""))
        ):cc
       ~inline_element(self, position, :code, [cc])


BlankLine =     Sp Newline

Quoted =        '"' (!'"' .)* '"' | '\'' (!'\'' .)* '\''
Eof =           !.
Spacechar =     < / |\t/ > { text }
Nonspacechar =  !Spacechar !Newline <.> { text }
Newline =       "\n" | "\r" "\n"?
Sp =            Spacechar*
Spnl =          Sp (Newline Sp)?
##SpecialChar =   '~' | '*' | '_' | '`' | '&' | '[' | ']' | '(' | ')' | '<' | '!' | '#' | "\\" | "'" | '"'
SpecialChar =   < /[~*_`&\[\]()<!#\\'"]/ > { text }
NormalChar =    !( SpecialChar | Spacechar | Newline ) <.> { text }
AlphanumericAscii =  < /[A-Za-z0-9]/ > { text }
Digit =  < /[0-9]/ > { text }

NonindentSpace =    < /   |  | |/ > { text }
Indent =            < /\t|    / > { text }
IndentedLine =      Indent Line:c { c }
OptionallyIndentedLine = Indent? Line

Line =  RawLine:c { c }
RawLine = ((  < /[^\r\n]*/ > ) Newline { text } |  < /.+/ > Eof { text }):c  ~text(self, position, c)