| 1 |
/* PEG grammar for a subset of wikitext, useful in the MediaWiki frontend */ |
| 2 |
|
| 3 |
start |
| 4 |
= e:expression* { return e.length > 1 ? [ "CONCAT" ].concat(e) : e[0]; } |
| 5 |
|
| 6 |
expression |
| 7 |
= template |
| 8 |
/ link |
| 9 |
/ extlink |
| 10 |
/ replacement |
| 11 |
/ literal |
| 12 |
|
| 13 |
paramExpression |
| 14 |
= template |
| 15 |
/ link |
| 16 |
/ extlink |
| 17 |
/ replacement |
| 18 |
/ literalWithoutBar |
| 19 |
|
| 20 |
template |
| 21 |
= "{{" t:templateContents "}}" { return t; } |
| 22 |
|
| 23 |
templateContents |
| 24 |
= twr:templateWithReplacement p:templateParam* { return twr.concat(p) } |
| 25 |
/ twr:templateWithOutReplacement p:templateParam* { return twr.concat(p) } |
| 26 |
/ twr:templateWithOutFirstParameter p:templateParam* { return twr.concat(p) } |
| 27 |
/ t:templateName p:templateParam* { return p.length ? [ t, p ] : [ t ] } |
| 28 |
|
| 29 |
templateWithReplacement |
| 30 |
= t:templateName ":" r:replacement { return [ t, r ] } |
| 31 |
|
| 32 |
templateWithOutReplacement |
| 33 |
= t:templateName ":" p:paramExpression { return [ t, p ] } |
| 34 |
|
| 35 |
templateWithOutFirstParameter |
| 36 |
= t:templateName ":" { return [ t, "" ] } |
| 37 |
|
| 38 |
templateParam |
| 39 |
= "|" e:paramExpression* { return e.length > 1 ? [ "CONCAT" ].concat(e) : e[0]; } |
| 40 |
|
| 41 |
templateName |
| 42 |
= tn:[A-Za-z_]+ { return tn.join('').toUpperCase() } |
| 43 |
|
| 44 |
/* TODO: Update to reflect separate piped and unpiped handling */ |
| 45 |
link |
| 46 |
= "[[" w:expression "]]" { return [ 'WLINK', w ]; } |
| 47 |
|
| 48 |
extlink |
| 49 |
= "[" url:url whitespace text:expression "]" { return [ 'LINK', url, text ] } |
| 50 |
|
| 51 |
url |
| 52 |
= url:[^ ]+ { return url.join(''); } |
| 53 |
|
| 54 |
whitespace |
| 55 |
= [ ]+ |
| 56 |
|
| 57 |
replacement |
| 58 |
= '$' digits:digits { return [ 'REPLACE', parseInt( digits, 10 ) - 1 ] } |
| 59 |
|
| 60 |
digits |
| 61 |
= [0-9]+ |
| 62 |
|
| 63 |
literal |
| 64 |
= lit:escapedOrRegularLiteral+ { return lit.join(''); } |
| 65 |
|
| 66 |
literalWithoutBar |
| 67 |
= lit:escapedOrLiteralWithoutBar+ { return lit.join(''); } |
| 68 |
|
| 69 |
escapedOrRegularLiteral |
| 70 |
= escapedLiteral |
| 71 |
/ regularLiteral |
| 72 |
|
| 73 |
escapedOrLiteralWithoutBar |
| 74 |
= escapedLiteral |
| 75 |
/ regularLiteralWithoutBar |
| 76 |
|
| 77 |
escapedLiteral |
| 78 |
= "\\" escaped:. { return escaped; } |
| 79 |
|
| 80 |
regularLiteral |
| 81 |
= [^{}\[\]$\\] |
| 82 |
|
| 83 |
regularLiteralWithoutBar |
| 84 |
= [^{}\[\]$\\|] |
| 85 |
|