-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmh_grammar.leg
674 lines (532 loc) · 26.8 KB
/
pmh_grammar.leg
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
%{
/* PEG Markdown Highlight
* Copyright 2011 Ali Rantakari -- http://hasseg.org
* Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
*
* pmh_grammar.leg
*
* This is a slightly adapted version of the PEG grammar from John MacFarlane's
* peg-markdown compiler.
*/
/// header_code_here
#define elem(x) mk_element((parser_data *)G->data, x, thunk->begin, thunk->end)
#define elem_s(x) mk_element((parser_data *)G->data, x, s->pos, thunk->end)
#define mk_sep mk_element((parser_data *)G->data, pmh_SEPARATOR, 0,0)
#define mk_notype mk_element((parser_data *)G->data, pmh_NO_TYPE, 0,0)
#define etext(x) mk_etext((parser_data *)G->data, x)
#define ADD(x) add((parser_data *)G->data, x)
#define EXT(x) extension((parser_data *)G->data, x)
#define REF_EXISTS(x) reference_exists((parser_data *)G->data, x)
#define GET_REF(x) get_reference((parser_data *)G->data, x)
#define PARSING_REFERENCES ((parser_data *)G->data)->parsing_only_references
#define FREE_LABEL(l) { free(l->label); l->label = NULL; }
#define FREE_ADDRESS(l) { free(l->address); l->address = NULL; }
// This gives us the text matched with < > as it appears in the original input:
#define COPY_YYTEXT_ORIG() copy_input_span((parser_data *)G->data, thunk->begin, thunk->end)
%}
Doc = ( Block )*
# placeholder for marking locations
LocMarker = &. { $$ = elem(pmh_NO_TYPE); }
Block = BlankLine*
( BlockQuote
| Verbatim
| Note
| Reference
| HorizontalRule
| Heading
| OrderedList
| BulletList
| HtmlBlock
| StyleBlock
| Para
| Plain )
Para = NonindentSpace Inlines BlankLine+
Plain = Inlines
AtxInline = !Newline !(Sp? '#'* Sp Newline) Inline
AtxStart = < ( "######" | "#####" | "####" | "###" | "##" | "#" ) >
{ $$ = elem((pmh_element_type)(pmh_H1 + (strlen(yytext) - 1))); }
AtxHeading = < s:AtxStart Sp? ( AtxInline )+ (Sp? '#'* Sp)? Newline >
{ ADD(elem_s(s->type)); }
SetextHeading = SetextHeading1 | SetextHeading2
SetextBottom1 = "===" '='* Newline
SetextBottom2 = "---" '-'* Newline
SetextHeading1 = &(RawLine SetextBottom1)
s:LocMarker
< ( !Endline Inline )+ Sp? Newline
SetextBottom1 >
{ ADD(elem_s(pmh_H1)); }
SetextHeading2 = &(RawLine SetextBottom2)
s:LocMarker
< ( !Endline Inline )+ Sp? Newline
SetextBottom2 >
{ ADD(elem_s(pmh_H2)); }
Heading = SetextHeading | AtxHeading
BlockQuote = a:BlockQuoteRaw
{ pmh_realelement *rawlist = mk_element((parser_data *)G->data, pmh_RAW_LIST, 0,0);
rawlist->children = reverse(a);
ADD(rawlist);
}
BlockQuoteRaw = a:StartList
(( < '>' ' '? > { ADD(elem(pmh_BLOCKQUOTE)); } Line { a = cons($$, a); } )
( !'>' !BlankLine Line { a = cons($$, a); } )*
( < BlankLine > { a = cons(etext("\n"), a); } )*
)+
{ $$ = a; }
NonblankIndentedLine = !BlankLine IndentedLine
VerbatimChunk = ( BlankLine )*
( NonblankIndentedLine )+
Verbatim = < s:LocMarker
( VerbatimChunk )+ >
{ ADD(elem_s(pmh_VERBATIM)); }
HorizontalRule = < NonindentSpace
( '*' Sp '*' Sp '*' (Sp '*')*
| '-' Sp '-' Sp '-' (Sp '-')*
| '_' Sp '_' Sp '_' (Sp '_')*)
Sp Newline > BlankLine+
{ ADD(elem(pmh_HRULE)); }
Bullet = !HorizontalRule NonindentSpace < ('+' | '*' | '-') > Spacechar+
{ ADD(elem(pmh_LIST_BULLET)); }
BulletList = &Bullet (ListTight | ListLoose)
ListTight = a:StartList
( ListItemTight
{ pmh_realelement *el = mk_notype;
el->children = $$;
a = cons(el, a);
} )+
BlankLine* !(Bullet | Enumerator)
{ pmh_realelement *cur = a;
while (cur != NULL) {
pmh_realelement *rawlist = mk_element((parser_data *)G->data, pmh_RAW_LIST, 0,0);
rawlist->children = reverse(cur->children);
ADD(rawlist);
cur = cur->next;
}
}
ListLoose = a:StartList
( b:ListItem BlankLine*
{ b = cons(etext("\n\n"), b); /* In loose list, \n\n added to end of each element */
pmh_realelement *el = mk_notype;
el->children = b;
a = cons(el, a);
} )+
{ pmh_realelement *cur = a;
while (cur != NULL) {
pmh_realelement *rawlist = mk_element((parser_data *)G->data, pmh_RAW_LIST, 0,0);
rawlist->children = reverse(cur->children);
ADD(rawlist);
cur = cur->next;
}
}
ListItem = ( Bullet | Enumerator )
a:StartList
ListBlock { a = cons($$, a); }
( ListContinuationBlock { a = cons($$, a); } )*
{ $$ = a; }
ListItemTight =
( Bullet | Enumerator )
a:StartList
ListBlock { a = cons($$, a); }
( !BlankLine
ListContinuationBlock { a = cons($$, a); } )*
!ListContinuationBlock
{ $$ = a; }
ListBlock = a:StartList
!BlankLine Line { a = cons($$, a); }
( ListBlockLine { a = cons(elem(pmh_RAW), a); } )*
{ $$ = a; }
ListContinuationBlock = a:StartList
( < BlankLine* >
{ if (*yytext == '\0') /* if strlen(yytext) == 0 */
a = cons(elem(pmh_SEPARATOR), a);
else
a = cons(elem(pmh_RAW), a);
} )
( Indent ListBlock { a = cons($$, a); } )+
{ $$ = a; }
Enumerator = NonindentSpace < [0-9]+ '.' > Spacechar+
{ ADD(elem(pmh_LIST_ENUMERATOR)); }
OrderedList = &Enumerator (ListTight | ListLoose)
ListBlockLine = !BlankLine
!( Indent? (Bullet | Enumerator) )
!HorizontalRule
OptionallyIndentedLine
# Parsers for different kinds of block-level HTML content.
# This is repetitive due to constraints of PEG grammar.
HtmlBlockOpenAddress = '<' Spnl ("address" | "ADDRESS") Spnl HtmlAttribute* '>'
HtmlBlockCloseAddress = '<' Spnl '/' ("address" | "ADDRESS") Spnl '>'
HtmlBlockAddress = HtmlBlockOpenAddress (HtmlBlockAddress | !HtmlBlockCloseAddress .)* HtmlBlockCloseAddress
HtmlBlockOpenBlockquote = '<' Spnl ("blockquote" | "BLOCKQUOTE") Spnl HtmlAttribute* '>'
HtmlBlockCloseBlockquote = '<' Spnl '/' ("blockquote" | "BLOCKQUOTE") Spnl '>'
HtmlBlockBlockquote = HtmlBlockOpenBlockquote (HtmlBlockBlockquote | !HtmlBlockCloseBlockquote .)* HtmlBlockCloseBlockquote
HtmlBlockOpenCenter = '<' Spnl ("center" | "CENTER") Spnl HtmlAttribute* '>'
HtmlBlockCloseCenter = '<' Spnl '/' ("center" | "CENTER") Spnl '>'
HtmlBlockCenter = HtmlBlockOpenCenter (HtmlBlockCenter | !HtmlBlockCloseCenter .)* HtmlBlockCloseCenter
HtmlBlockOpenDir = '<' Spnl ("dir" | "DIR") Spnl HtmlAttribute* '>'
HtmlBlockCloseDir = '<' Spnl '/' ("dir" | "DIR") Spnl '>'
HtmlBlockDir = HtmlBlockOpenDir (HtmlBlockDir | !HtmlBlockCloseDir .)* HtmlBlockCloseDir
HtmlBlockOpenDiv = '<' Spnl ("div" | "DIV") Spnl HtmlAttribute* '>'
HtmlBlockCloseDiv = '<' Spnl '/' ("div" | "DIV") Spnl '>'
HtmlBlockDiv = HtmlBlockOpenDiv (HtmlBlockDiv | !HtmlBlockCloseDiv .)* HtmlBlockCloseDiv
HtmlBlockOpenDl = '<' Spnl ("dl" | "DL") Spnl HtmlAttribute* '>'
HtmlBlockCloseDl = '<' Spnl '/' ("dl" | "DL") Spnl '>'
HtmlBlockDl = HtmlBlockOpenDl (HtmlBlockDl | !HtmlBlockCloseDl .)* HtmlBlockCloseDl
HtmlBlockOpenFieldset = '<' Spnl ("fieldset" | "FIELDSET") Spnl HtmlAttribute* '>'
HtmlBlockCloseFieldset = '<' Spnl '/' ("fieldset" | "FIELDSET") Spnl '>'
HtmlBlockFieldset = HtmlBlockOpenFieldset (HtmlBlockFieldset | !HtmlBlockCloseFieldset .)* HtmlBlockCloseFieldset
HtmlBlockOpenForm = '<' Spnl ("form" | "FORM") Spnl HtmlAttribute* '>'
HtmlBlockCloseForm = '<' Spnl '/' ("form" | "FORM") Spnl '>'
HtmlBlockForm = HtmlBlockOpenForm (HtmlBlockForm | !HtmlBlockCloseForm .)* HtmlBlockCloseForm
HtmlBlockOpenH1 = '<' Spnl ("h1" | "H1") Spnl HtmlAttribute* '>'
HtmlBlockCloseH1 = '<' Spnl '/' ("h1" | "H1") Spnl '>'
HtmlBlockH1 = < s:LocMarker HtmlBlockOpenH1 (HtmlBlockH1 | !HtmlBlockCloseH1 .)* HtmlBlockCloseH1 >
{ ADD(elem_s(pmh_H1)); }
HtmlBlockOpenH2 = '<' Spnl ("h2" | "H2") Spnl HtmlAttribute* '>'
HtmlBlockCloseH2 = '<' Spnl '/' ("h2" | "H2") Spnl '>'
HtmlBlockH2 = < s:LocMarker HtmlBlockOpenH2 (HtmlBlockH2 | !HtmlBlockCloseH2 .)* HtmlBlockCloseH2 >
{ ADD(elem_s(pmh_H2)); }
HtmlBlockOpenH3 = '<' Spnl ("h3" | "H3") Spnl HtmlAttribute* '>'
HtmlBlockCloseH3 = '<' Spnl '/' ("h3" | "H3") Spnl '>'
HtmlBlockH3 = < s:LocMarker HtmlBlockOpenH3 (HtmlBlockH3 | !HtmlBlockCloseH3 .)* HtmlBlockCloseH3 >
{ ADD(elem_s(pmh_H3)); }
HtmlBlockOpenH4 = '<' Spnl ("h4" | "H4") Spnl HtmlAttribute* '>'
HtmlBlockCloseH4 = '<' Spnl '/' ("h4" | "H4") Spnl '>'
HtmlBlockH4 = < s:LocMarker HtmlBlockOpenH4 (HtmlBlockH4 | !HtmlBlockCloseH4 .)* HtmlBlockCloseH4 >
{ ADD(elem_s(pmh_H4)); }
HtmlBlockOpenH5 = '<' Spnl ("h5" | "H5") Spnl HtmlAttribute* '>'
HtmlBlockCloseH5 = '<' Spnl '/' ("h5" | "H5") Spnl '>'
HtmlBlockH5 = < s:LocMarker HtmlBlockOpenH5 (HtmlBlockH5 | !HtmlBlockCloseH5 .)* HtmlBlockCloseH5 >
{ ADD(elem_s(pmh_H5)); }
HtmlBlockOpenH6 = '<' Spnl ("h6" | "H6") Spnl HtmlAttribute* '>'
HtmlBlockCloseH6 = '<' Spnl '/' ("h6" | "H6") Spnl '>'
HtmlBlockH6 = < s:LocMarker HtmlBlockOpenH6 (HtmlBlockH6 | !HtmlBlockCloseH6 .)* HtmlBlockCloseH6 >
{ ADD(elem_s(pmh_H6)); }
HtmlBlockOpenMenu = '<' Spnl ("menu" | "MENU") Spnl HtmlAttribute* '>'
HtmlBlockCloseMenu = '<' Spnl '/' ("menu" | "MENU") Spnl '>'
HtmlBlockMenu = HtmlBlockOpenMenu (HtmlBlockMenu | !HtmlBlockCloseMenu .)* HtmlBlockCloseMenu
HtmlBlockOpenNoframes = '<' Spnl ("noframes" | "NOFRAMES") Spnl HtmlAttribute* '>'
HtmlBlockCloseNoframes = '<' Spnl '/' ("noframes" | "NOFRAMES") Spnl '>'
HtmlBlockNoframes = HtmlBlockOpenNoframes (HtmlBlockNoframes | !HtmlBlockCloseNoframes .)* HtmlBlockCloseNoframes
HtmlBlockOpenNoscript = '<' Spnl ("noscript" | "NOSCRIPT") Spnl HtmlAttribute* '>'
HtmlBlockCloseNoscript = '<' Spnl '/' ("noscript" | "NOSCRIPT") Spnl '>'
HtmlBlockNoscript = HtmlBlockOpenNoscript (HtmlBlockNoscript | !HtmlBlockCloseNoscript .)* HtmlBlockCloseNoscript
HtmlBlockOpenOl = '<' Spnl ("ol" | "OL") Spnl HtmlAttribute* '>'
HtmlBlockCloseOl = '<' Spnl '/' ("ol" | "OL") Spnl '>'
HtmlBlockOl = HtmlBlockOpenOl (HtmlBlockOl | !HtmlBlockCloseOl .)* HtmlBlockCloseOl
HtmlBlockOpenP = '<' Spnl ("p" | "P") Spnl HtmlAttribute* '>'
HtmlBlockCloseP = '<' Spnl '/' ("p" | "P") Spnl '>'
HtmlBlockP = HtmlBlockOpenP (HtmlBlockP | !HtmlBlockCloseP .)* HtmlBlockCloseP
HtmlBlockOpenPre = '<' Spnl ("pre" | "PRE") Spnl HtmlAttribute* '>'
HtmlBlockClosePre = '<' Spnl '/' ("pre" | "PRE") Spnl '>'
HtmlBlockPre = HtmlBlockOpenPre (HtmlBlockPre | !HtmlBlockClosePre .)* HtmlBlockClosePre
HtmlBlockOpenTable = '<' Spnl ("table" | "TABLE") Spnl HtmlAttribute* '>'
HtmlBlockCloseTable = '<' Spnl '/' ("table" | "TABLE") Spnl '>'
HtmlBlockTable = HtmlBlockOpenTable (HtmlBlockTable | !HtmlBlockCloseTable .)* HtmlBlockCloseTable
HtmlBlockOpenUl = '<' Spnl ("ul" | "UL") Spnl HtmlAttribute* '>'
HtmlBlockCloseUl = '<' Spnl '/' ("ul" | "UL") Spnl '>'
HtmlBlockUl = HtmlBlockOpenUl (HtmlBlockUl | !HtmlBlockCloseUl .)* HtmlBlockCloseUl
HtmlBlockOpenDd = '<' Spnl ("dd" | "DD") Spnl HtmlAttribute* '>'
HtmlBlockCloseDd = '<' Spnl '/' ("dd" | "DD") Spnl '>'
HtmlBlockDd = HtmlBlockOpenDd (HtmlBlockDd | !HtmlBlockCloseDd .)* HtmlBlockCloseDd
HtmlBlockOpenDt = '<' Spnl ("dt" | "DT") Spnl HtmlAttribute* '>'
HtmlBlockCloseDt = '<' Spnl '/' ("dt" | "DT") Spnl '>'
HtmlBlockDt = HtmlBlockOpenDt (HtmlBlockDt | !HtmlBlockCloseDt .)* HtmlBlockCloseDt
HtmlBlockOpenFrameset = '<' Spnl ("frameset" | "FRAMESET") Spnl HtmlAttribute* '>'
HtmlBlockCloseFrameset = '<' Spnl '/' ("frameset" | "FRAMESET") Spnl '>'
HtmlBlockFrameset = HtmlBlockOpenFrameset (HtmlBlockFrameset | !HtmlBlockCloseFrameset .)* HtmlBlockCloseFrameset
HtmlBlockOpenLi = '<' Spnl ("li" | "LI") Spnl HtmlAttribute* '>'
HtmlBlockCloseLi = '<' Spnl '/' ("li" | "LI") Spnl '>'
HtmlBlockLi = HtmlBlockOpenLi (HtmlBlockLi | !HtmlBlockCloseLi .)* HtmlBlockCloseLi
HtmlBlockOpenTbody = '<' Spnl ("tbody" | "TBODY") Spnl HtmlAttribute* '>'
HtmlBlockCloseTbody = '<' Spnl '/' ("tbody" | "TBODY") Spnl '>'
HtmlBlockTbody = HtmlBlockOpenTbody (HtmlBlockTbody | !HtmlBlockCloseTbody .)* HtmlBlockCloseTbody
HtmlBlockOpenTd = '<' Spnl ("td" | "TD") Spnl HtmlAttribute* '>'
HtmlBlockCloseTd = '<' Spnl '/' ("td" | "TD") Spnl '>'
HtmlBlockTd = HtmlBlockOpenTd (HtmlBlockTd | !HtmlBlockCloseTd .)* HtmlBlockCloseTd
HtmlBlockOpenTfoot = '<' Spnl ("tfoot" | "TFOOT") Spnl HtmlAttribute* '>'
HtmlBlockCloseTfoot = '<' Spnl '/' ("tfoot" | "TFOOT") Spnl '>'
HtmlBlockTfoot = HtmlBlockOpenTfoot (HtmlBlockTfoot | !HtmlBlockCloseTfoot .)* HtmlBlockCloseTfoot
HtmlBlockOpenTh = '<' Spnl ("th" | "TH") Spnl HtmlAttribute* '>'
HtmlBlockCloseTh = '<' Spnl '/' ("th" | "TH") Spnl '>'
HtmlBlockTh = HtmlBlockOpenTh (HtmlBlockTh | !HtmlBlockCloseTh .)* HtmlBlockCloseTh
HtmlBlockOpenThead = '<' Spnl ("thead" | "THEAD") Spnl HtmlAttribute* '>'
HtmlBlockCloseThead = '<' Spnl '/' ("thead" | "THEAD") Spnl '>'
HtmlBlockThead = HtmlBlockOpenThead (HtmlBlockThead | !HtmlBlockCloseThead .)* HtmlBlockCloseThead
HtmlBlockOpenTr = '<' Spnl ("tr" | "TR") Spnl HtmlAttribute* '>'
HtmlBlockCloseTr = '<' Spnl '/' ("tr" | "TR") Spnl '>'
HtmlBlockTr = HtmlBlockOpenTr (HtmlBlockTr | !HtmlBlockCloseTr .)* HtmlBlockCloseTr
HtmlBlockOpenScript = '<' Spnl ("script" | "SCRIPT") Spnl HtmlAttribute* '>'
HtmlBlockCloseScript = '<' Spnl '/' ("script" | "SCRIPT") Spnl '>'
HtmlBlockScript = HtmlBlockOpenScript (!HtmlBlockCloseScript .)* HtmlBlockCloseScript
HtmlBlockInTags = HtmlBlockAddress
| HtmlBlockBlockquote
| HtmlBlockCenter
| HtmlBlockDir
| HtmlBlockDiv
| HtmlBlockDl
| HtmlBlockFieldset
| HtmlBlockForm
| HtmlBlockH1
| HtmlBlockH2
| HtmlBlockH3
| HtmlBlockH4
| HtmlBlockH5
| HtmlBlockH6
| HtmlBlockMenu
| HtmlBlockNoframes
| HtmlBlockNoscript
| HtmlBlockOl
| HtmlBlockP
| HtmlBlockPre
| HtmlBlockTable
| HtmlBlockUl
| HtmlBlockDd
| HtmlBlockDt
| HtmlBlockFrameset
| HtmlBlockLi
| HtmlBlockTbody
| HtmlBlockTd
| HtmlBlockTfoot
| HtmlBlockTh
| HtmlBlockThead
| HtmlBlockTr
| HtmlBlockScript
HtmlBlock = < s:LocMarker
( HtmlBlockInTags | HtmlComment | HtmlBlockSelfClosing ) >
BlankLine+
{ ADD(elem_s(pmh_HTMLBLOCK)); }
HtmlBlockSelfClosing = '<' Spnl HtmlBlockType Spnl HtmlAttribute* '/' Spnl '>'
HtmlBlockType = "address" | "blockquote" | "center" | "dir" | "div" | "dl" | "fieldset" | "form" | "h1" | "h2" | "h3" |
"h4" | "h5" | "h6" | "hr" | "isindex" | "menu" | "noframes" | "noscript" | "ol" | "p" | "pre" | "table" |
"ul" | "dd" | "dt" | "frameset" | "li" | "tbody" | "td" | "tfoot" | "th" | "thead" | "tr" | "script" |
"ADDRESS" | "BLOCKQUOTE" | "CENTER" | "DIR" | "DIV" | "DL" | "FIELDSET" | "FORM" | "H1" | "H2" | "H3" |
"H4" | "H5" | "H6" | "HR" | "ISINDEX" | "MENU" | "NOFRAMES" | "NOSCRIPT" | "OL" | "P" | "PRE" | "TABLE" |
"UL" | "DD" | "DT" | "FRAMESET" | "LI" | "TBODY" | "TD" | "TFOOT" | "TH" | "THEAD" | "TR" | "SCRIPT"
StyleOpen = '<' Spnl ("style" | "STYLE") Spnl HtmlAttribute* '>'
StyleClose = '<' Spnl '/' ("style" | "STYLE") Spnl '>'
InStyleTags = StyleOpen (!StyleClose .)* StyleClose
StyleBlock = < s:LocMarker InStyleTags >
BlankLine*
{ ADD(elem_s(pmh_HTMLBLOCK)); }
Inlines = ( !Endline Inline
| Endline &Inline )+ Endline?
Inline = Str
| Endline
| UlOrStarLine
| Space
| Strong
| Emph
| Image
| Link
| NoteReference
| InlineNote
| Code
| RawHtml
| Entity
| EscapedChar
| Symbol
Space = Spacechar+
Str = NormalChar (NormalChar | '_'+ &Alphanumeric)*
EscapedChar = '\\' !Newline [-\\`|*_{}[\]()#+.!><]
Entity = < s:LocMarker
( HexEntity | DecEntity | CharEntity ) >
{ ADD(elem_s(pmh_HTML_ENTITY)); }
Endline = LineBreak | TerminalEndline | NormalEndline
NormalEndline = Sp Newline !BlankLine !'>' !AtxStart
!(Line ("===" '='* | "---" '-'*) Newline)
TerminalEndline = Sp Newline Eof
LineBreak = " " NormalEndline
Symbol = SpecialChar
# This keeps the parser from getting bogged down on long strings of '*' or '_',
# or strings of '*' or '_' with space on each side:
UlOrStarLine = (UlLine | StarLine)
StarLine = "****" '*'* | Spacechar '*'+ &Spacechar
UlLine = "____" '_'* | Spacechar '_'+ &Spacechar
Emph = EmphStar | EmphUl
OneStarOpen = !StarLine < '*' > !Spacechar !Newline { $$ = elem(pmh_NO_TYPE); }
OneStarClose = !Spacechar !Newline Inline !StrongStar < '*' > { $$ = elem(pmh_NO_TYPE); }
EmphStar = s:OneStarOpen
( !OneStarClose Inline )*
OneStarClose
{ ADD(elem_s(pmh_EMPH)); }
OneUlOpen = !UlLine < '_' > !Spacechar !Newline { $$ = elem(pmh_NO_TYPE); }
OneUlClose = !Spacechar !Newline Inline !StrongUl < '_' > !Alphanumeric { $$ = elem(pmh_NO_TYPE); }
EmphUl = s:OneUlOpen
( !OneUlClose Inline )*
OneUlClose
{ ADD(elem_s(pmh_EMPH)); }
Strong = StrongStar | StrongUl
TwoStarOpen = !StarLine < "**" > !Spacechar !Newline { $$ = elem(pmh_NO_TYPE); }
TwoStarClose = !Spacechar !Newline Inline < "**" > { $$ = elem(pmh_NO_TYPE); }
StrongStar = s:TwoStarOpen
( !TwoStarClose Inline )*
TwoStarClose
{ ADD(elem_s(pmh_STRONG)); }
TwoUlOpen = !UlLine < "__" > !Spacechar !Newline { $$ = elem(pmh_NO_TYPE); }
TwoUlClose = !Spacechar !Newline Inline < "__" > !Alphanumeric { $$ = elem(pmh_NO_TYPE); }
StrongUl = s:TwoUlOpen
( !TwoUlClose Inline )*
TwoUlClose
{ ADD(elem_s(pmh_STRONG)); }
Image = '!' ( ExplicitLink | ReferenceLink )
{
if ($$ != NULL) {
$$->type = pmh_IMAGE;
$$->pos -= 1;
ADD($$);
}
}
Link = ( ExplicitLink | ReferenceLink | AutoLink )
{ if ($$) ADD($$); } # AutoLink does not return $$
ReferenceLink = ReferenceLinkDouble | ReferenceLinkSingle
ReferenceLinkDouble = < s:Label Spnl !"[]" l:Label >
{
pmh_realelement *reference = GET_REF(l->label);
if (reference) {
$$ = elem_s(pmh_LINK);
$$->label = strdup(l->label);
$$->address = strdup(reference->address);
} else
$$ = NULL;
FREE_LABEL(s);
FREE_LABEL(l);
}
ReferenceLinkSingle = < s:Label (Spnl "[]")? >
{
pmh_realelement *reference = GET_REF(s->label);
if (reference) {
$$ = elem_s(pmh_LINK);
$$->label = strdup(s->label);
$$->address = strdup(reference->address);
} else
$$ = NULL;
FREE_LABEL(s);
}
ExplicitLink = < s:Label Spnl '(' Sp l:Source Spnl Title Sp ')' >
{
$$ = elem_s(pmh_LINK);
if (l->address != NULL)
$$->address = strdup(l->address);
FREE_LABEL(s);
FREE_ADDRESS(l);
}
Source = { $$ = mk_notype; }
( '<' < SourceContents > { $$->address = COPY_YYTEXT_ORIG(); } '>'
| < SourceContents > { $$->address = COPY_YYTEXT_ORIG(); } )
SourceContents = ( ( !'(' !')' !'>' Nonspacechar )+ | '(' SourceContents ')')*
Title = ( TitleSingle | TitleDouble | "" )
TitleSingle = '\'' ( !( '\'' Sp ( ')' | Newline ) ) . )* '\''
TitleDouble = '"' ( !( '"' Sp ( ')' | Newline ) ) . )* '"'
AutoLink = AutoLinkUrl | AutoLinkEmail
AutoLinkUrl = < s:LocMarker { s->type = pmh_AUTO_LINK_URL; }
'<'
< [A-Za-z]+ "://" ( !Newline !'>' . )+ >
{ s->address = COPY_YYTEXT_ORIG(); }
'>' >
{
s->end = thunk->end;
ADD(s);
}
AutoLinkEmail = < s:LocMarker { s->type = pmh_AUTO_LINK_EMAIL; }
'<'
( "mailto:" )? < [-A-Za-z0-9+_./!%~$]+ '@' ( !Newline !'>' . )+ >
{ s->address = COPY_YYTEXT_ORIG(); }
'>' >
{
s->end = thunk->end;
ADD(s);
}
Reference = < s:LocMarker
NonindentSpace !"[]" l:Label ':' Spnl r:RefSrc RefTitle > BlankLine+
{
pmh_realelement *el = elem_s(pmh_REFERENCE);
el->label = strdup(l->label);
el->address = strdup(r->address);
ADD(el);
FREE_LABEL(l);
FREE_ADDRESS(r);
}
Label = < s:LocMarker
'[' ( !'^' &{ EXT(pmh_EXT_NOTES) } | &. &{ !EXT(pmh_EXT_NOTES) } )
< ( !']' Inline )* >
{ s->label = COPY_YYTEXT_ORIG(); }
']' >
{
s->pos = s->pos;
s->end = thunk->end;
$$ = s;
}
RefSrc = < Nonspacechar+ >
{ $$ = mk_notype; $$->address = COPY_YYTEXT_ORIG(); }
RefTitle = ( RefTitleSingle | RefTitleDouble | RefTitleParens | EmptyTitle )
EmptyTitle = ""
RefTitleSingle = Spnl '\'' ( !('\'' Sp Newline | Newline ) . )* '\''
RefTitleDouble = Spnl '"' ( !('"' Sp Newline | Newline) . )* '"'
RefTitleParens = Spnl '(' ( !(')' Sp Newline | Newline) . )* ')'
# Starting point for parsing only references:
References = ( Reference | SkipBlock )*
Ticks1 = < "`" > !'`' { $$ = elem(pmh_NO_TYPE); }
Ticks2 = < "``" > !'`' { $$ = elem(pmh_NO_TYPE); }
Ticks3 = < "```" > !'`' { $$ = elem(pmh_NO_TYPE); }
Ticks4 = < "````" > !'`' { $$ = elem(pmh_NO_TYPE); }
Ticks5 = < "`````" > !'`' { $$ = elem(pmh_NO_TYPE); }
Code = < ( s:Ticks1 Sp ( ( !'`' Nonspacechar )+ | !Ticks1 '`'+ | !( Sp Ticks1 ) ( Spacechar | Newline !BlankLine ) )+ Sp Ticks1
| s:Ticks2 Sp ( ( !'`' Nonspacechar )+ | !Ticks2 '`'+ | !( Sp Ticks2 ) ( Spacechar | Newline !BlankLine ) )+ Sp Ticks2
| s:Ticks3 Sp ( ( !'`' Nonspacechar )+ | !Ticks3 '`'+ | !( Sp Ticks3 ) ( Spacechar | Newline !BlankLine ) )+ Sp Ticks3
| s:Ticks4 Sp ( ( !'`' Nonspacechar )+ | !Ticks4 '`'+ | !( Sp Ticks4 ) ( Spacechar | Newline !BlankLine ) )+ Sp Ticks4
| s:Ticks5 Sp ( ( !'`' Nonspacechar )+ | !Ticks5 '`'+ | !( Sp Ticks5 ) ( Spacechar | Newline !BlankLine ) )+ Sp Ticks5
) >
{ ADD(elem_s(pmh_CODE)); }
RawHtml = < s:LocMarker (HtmlComment | HtmlBlockScript | HtmlTag) >
{ ADD(elem_s(pmh_HTML)); }
BlankLine = Sp Newline
Quoted = '"' (!'"' .)* '"' | '\'' (!'\'' .)* '\''
HtmlAttribute = (AlphanumericAscii | '-')+ Spnl ('=' Spnl (Quoted | (!'>' Nonspacechar)+))? Spnl
HtmlComment = < s:LocMarker "<!--" (!"-->" .)* "-->" >
{ ADD(elem_s(pmh_COMMENT)); }
HtmlTag = '<' Spnl '/'? AlphanumericAscii+ Spnl HtmlAttribute* '/'? Spnl '>'
Eof = !.
Spacechar = ' ' | '\t'
Nonspacechar = !Spacechar !Newline .
Newline = '\n' | '\r' '\n'?
Sp = Spacechar*
Spnl = Sp (Newline Sp)?
SpecialChar = '*' | '_' | '`' | '&' | '[' | ']' | '(' | ')' | '<' | '!' | '#' | '\\' | '\'' | '"' | ExtendedSpecialChar
NormalChar = !( SpecialChar | Spacechar | Newline ) .
# Not used anywhere in grammar:
#NonAlphanumeric = [\000-\057\072-\100\133-\140\173-\177]
Alphanumeric = [0-9A-Za-z] | '\200' | '\201' | '\202' | '\203' | '\204' | '\205' | '\206' | '\207' | '\210' | '\211' | '\212' | '\213' | '\214' | '\215' | '\216' | '\217' | '\220' | '\221' | '\222' | '\223' | '\224' | '\225' | '\226' | '\227' | '\230' | '\231' | '\232' | '\233' | '\234' | '\235' | '\236' | '\237' | '\240' | '\241' | '\242' | '\243' | '\244' | '\245' | '\246' | '\247' | '\250' | '\251' | '\252' | '\253' | '\254' | '\255' | '\256' | '\257' | '\260' | '\261' | '\262' | '\263' | '\264' | '\265' | '\266' | '\267' | '\270' | '\271' | '\272' | '\273' | '\274' | '\275' | '\276' | '\277' | '\300' | '\301' | '\302' | '\303' | '\304' | '\305' | '\306' | '\307' | '\310' | '\311' | '\312' | '\313' | '\314' | '\315' | '\316' | '\317' | '\320' | '\321' | '\322' | '\323' | '\324' | '\325' | '\326' | '\327' | '\330' | '\331' | '\332' | '\333' | '\334' | '\335' | '\336' | '\337' | '\340' | '\341' | '\342' | '\343' | '\344' | '\345' | '\346' | '\347' | '\350' | '\351' | '\352' | '\353' | '\354' | '\355' | '\356' | '\357' | '\360' | '\361' | '\362' | '\363' | '\364' | '\365' | '\366' | '\367' | '\370' | '\371' | '\372' | '\373' | '\374' | '\375' | '\376' | '\377'
AlphanumericAscii = [A-Za-z0-9]
HexEntity = '&' '#' [Xx] [0-9a-fA-F]+ ';'
DecEntity = '&' '#' [0-9]+ ';'
CharEntity = '&' [A-Za-z0-9]+ ';'
NonindentSpace = " " | " " | " " | ""
Indent = "\t" | " "
IndentedLine = Indent Line
OptionallyIndentedLine = Indent? Line
# StartList starts a list data structure that can be added to with cons:
StartList = &.
{ $$ = NULL; }
Line = RawLine
{ $$ = mk_element((parser_data *)G->data, pmh_RAW, $$->pos, $$->end); }
RawLine = ( < (!'\r' !'\n' .)* Newline > | < .+ > Eof )
{ $$ = elem(pmh_RAW); }
SkipBlock = ( !BlankLine RawLine )+ BlankLine*
| BlankLine+
# Syntax extensions
ExtendedSpecialChar = &{ EXT(pmh_EXT_NOTES) } ( '^' )
NoteReference = &{ EXT(pmh_EXT_NOTES) }
RawNoteReference
RawNoteReference = "[^" ( !Newline !']' . )+ ']'
Note = &{ EXT(pmh_EXT_NOTES) }
NonindentSpace RawNoteReference ':' Sp
( RawNoteBlock )
( &Indent RawNoteBlock )*
InlineNote = &{ EXT(pmh_EXT_NOTES) }
"^["
( !']' Inline )+
']'
# Not used anywhere in grammar:
#Notes = ( Note | SkipBlock )*
RawNoteBlock = ( !BlankLine OptionallyIndentedLine )+
( BlankLine* )
%%