-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
111 lines (93 loc) · 2.64 KB
/
grammar.js
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
module.exports = grammar({
name: 'mediawiki',
extras: $ => [ $.comment ],
rules: {
source_file: $ => repeat($._node),
_node: $ => choice(
$._inlinemarkup,
$._blockmarkup
),
_inlinemarkup: $ => choice(
$.extlink,
$.wikilink,
$.template,
$.parameter,
$.htmltag,
$.htmlentity,
/./
),
_blockmarkup: $ => choice(
$.heading,
/.|\n/
),
htmltag: $ => seq(
'<',
optional('/'),
field('name', $.tagname),
repeat($._node),
'>'
),
htmlentity: $ => /&(#[0-9]{1,4}|#x[0-9a-fA-F]{1,4}|[a-zA-Z]+);/,
comment: $ => seq( '<!--', repeat(/.|\n/), '-->' ),
extlink: $ => seq(
'[',
field('target', $.url),
optional(seq(
' ',
optional(field('label', $.linklabel))
)),
']'
),
wikilink: $ => seq(
'[[',
field('target', $.linktarget),
optional(
field('anchor', $.linkanchor)
),
optional(seq(
'|',
optional(field('label', $.linklabel))
)),
']]'
),
template: $ => seq(
'{{',
field('name', $.templatename),
optional(seq(':', repeat1($._node))), // argument for parser functions
repeat(seq('|', optional($.templateparam))),
'}}'
),
// parameter in a template, e.g. {{{name|default}}}
parameter: $ => seq(
'{{{',
field('name', $.paramname),
optional(seq('|', optional($._paramdefault))),
'}}}'
),
heading: $ => choice( $.h1, $.h2, $.h3, $.h4, $.h5, $.h6 ),
h1: $ => seq('\n=', /[^=\n]+/, '='),
h2: $ => seq('\n==', /[^=\n]+/, '=='),
h3: $ => seq('\n===', /[^=\n]+/, '==='),
h4: $ => seq('\n====', /[^=\n]+/, '===='),
h5: $ => seq('\n=====', /[^=\n]+/, '====='),
h6: $ => seq('\n======', /[^=\n]+/, '======'),
tagname: $ => /[a-zA-Z][\w-]*/,
url: $ => seq(
/([a-zA-Z0-9.-]+:|\/\/)/, // URI scheme or protocol relative
repeat1($._inlinemarkup)
),
linktarget: $ => repeat1($._inlinemarkup),
linkanchor: $ => seq('#', repeat1($._inlinemarkup)),
linklabel: $ => repeat1($._inlinemarkup),
templatename: $ => /[\s:]*[^|\n:<}]+/,
templateparam: $ => choice(
field('key', $.templatekey), // just |key=
field('value', $.templatevalue), // just |value
seq(field('key', $.templatekey), field('value', $.templatevalue)), // both
),
templatekey: $ => /[^|}]+?=/,
templatevalue: $ => repeat1($._node),
paramname: $ => /[^|}]+/, // everything until first "|" or "}" char
_paramdefault: $ => field('value', repeat1($._node))
}
});