r105285 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105284‎ | r105285 | r105286 >
Date:11:06, 6 December 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Convert heading rules to single rule that figures out the level. This saves a
lot of backtracking and inline break complexity.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/pegParser.pegjs.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/pegParser.pegjs.txt
@@ -550,14 +550,7 @@
551551 ! syntaxFlags.linkdesk); } ":" { return true; }
552552 / & { return syntaxFlags['extlink']; } "]" { return true; }
553553 / & { return syntaxFlags['linkdesc']; } link_end { return true; }
554 - / & { return syntaxFlags['h']; }
555 - ( & { return syntaxFlags['h1'] } '=' space* newline { return true; }
556 - / & { return syntaxFlags['h2'] } '==' space* newline { return true; }
557 - / & { return syntaxFlags['h3'] } '===' space* newline { return true; }
558 - / & { return syntaxFlags['h4'] } '====' space* newline { return true; }
559 - / & { return syntaxFlags['h5'] } '=====' space* newline { return true; }
560 - / & { return syntaxFlags['h6'] } '======' space* newline { return true; }
561 - )
 554+ / & { return syntaxFlags['h']; } '='+ space* newline { return true; }
562555
563556 inline
564557 = c:(urltext / inline_element / (!inline_breaks ch:. { return ch; }))+ {
@@ -620,88 +613,46 @@
621614 / extlink
622615 / quote
623616
624 -/* Headings
625 - *
626 - * Listed in reverse order on purpose ;) */
627 -h = h6 / h5 / h4 / h3 / h2 / h1
 617+/* Headings */
628618
629 -/* We might want to consider using a single rule for all headings, and
630 - * figuring out the level in the action. This saves quite some backtracking,
631 - * and the conversion of equal signs into text should not be a problem as
632 - * equals are not part of other syntax. */
633 -h1 = '='
634 - r:(
635 - & { setFlag('h'); return setFlag('h1') }
636 - c:inlineline '=' (space / comment)*
 619+h = & "=" // guard, to make sure '='+ will match.
 620+ // XXX: Also check to end to avoid inline parsing?
 621+ r:(
 622+ s:'='+ // moved in here to make s accessible to inner action
 623+ & { return setFlag('h'); }
 624+ c:inlineline
 625+ e:'='+
 626+ spc:(space / comment)*
637627 &eolf
638628 {
639 - clearFlag('h');
640 - clearFlag('h1');
641 - return [{type: 'TAG', name: 'h1'}]
642 - .concat(c, [{type: 'ENDTAG', name: 'h1'}]);
643 - }
644 - / & { dp('nomatch exit h1'); clearFlag('h'); clearFlag('h1'); return false } { return null }
645 - ) { return r }
 629+ clearFlag('h');
 630+ var level = Math.min(s.length, e.length);
 631+ // convert surplus equals into text
 632+ if(s.length > level) {
 633+ var extras = s.substr(0, s.length - level);
 634+ if(c[0].type == 'TEXT') {
 635+ c[0].value = extras + c[0].value;
 636+ } else {
 637+ c.unshift({type: 'TEXT', value: extras});
 638+ }
 639+ }
 640+ if(e.length > level) {
 641+ var extras = e.substr(0, s.length - level),
 642+ lastElem = c[c.length - 1];
 643+ if(lastElem.type == 'TEXT') {
 644+ lastElem.value = lastElem.value + extras;
 645+ } else {
 646+ c.push({type: 'TEXT', value: extras});
 647+ }
 648+ }
646649
647 -h2 = '=='
648 - r:(
649 - & { setFlag('h'); return setFlag('h2') }
650 - c:inlineline '==' (space / comment)* &newline {
651 - clearFlag('h');
652 - clearFlag('h2');
653 - return [{type: 'TAG', name: 'h2'}]
654 - .concat(c, [{type: 'ENDTAG', name: 'h2'}]);
 650+ return [{type: 'TAG', name: 'h' + level}]
 651+ .concat(c, [{type: 'ENDTAG', name: 'h' + level}]);
655652 }
656 - / & { clearFlag('h'); clearFlag('h2'); return false }
 653+ / & { dp('nomatch exit h'); clearFlag('h'); return false } { return null }
657654 ) { return r }
658655
659 -h3 = '==='
660 - r:(
661 - & { setFlag('h'); return setFlag('h3') }
662 - c:inlineline '===' comment? &newline {
663 - clearFlag('h');
664 - clearFlag('h3');
665 - return [{type: 'TAG', name: 'h3'}]
666 - .concat(c, [{type: 'ENDTAG', name: 'h3'}]);
667 - }
668 - / & { clearFlag('h'); clearFlag('h3'); return false }
669 - ) { return r }
670656
671 -h4 = '===='
672 - r:(
673 - & { setFlag('h'); return setFlag('h4') }
674 - c:inlineline '====' comment? &newline {
675 - clearFlag('h');
676 - clearFlag('h4');
677 - return [{type: 'TAG', name: 'h4'}]
678 - .concat(c, [{type: 'ENDTAG', name: 'h4'}]);
679 - }
680 - / & { clearFlag('h'); clearFlag('h4'); return false }
681 - ) { return r }
682 -
683 -h5 = '====='
684 - r:(& { setFlag('h'); return setFlag('h5') }
685 - c:inlineline '=====' comment? &newline {
686 - clearFlag('h');
687 - clearFlag('h5');
688 - return [{type: 'TAG', name: 'h5'}]
689 - .concat(c, [{type: 'ENDTAG', name: 'h5'}]);
690 - }
691 - / & { clearFlag('h'); clearFlag('h5'); return false }
692 - ) { return r }
693 -
694 -h6 = '======'
695 - r:(& { setFlag('h'); return setFlag('h6') }
696 - c:inlineline '======' comment? &newline {
697 - clearFlag('h');
698 - clearFlag('h6');
699 - return [{type: 'TAG', name: 'h6'}]
700 - .concat(c, [{type: 'ENDTAG', name: 'h6'}]);
701 - }
702 - / & { clearFlag('h'); clearFlag('h6'); return false }
703 - ) { return r }
704 -
705 -
706657 pre_indent
707658 = l:pre_indent_line ls:(sol pre_indent_line)* {
708659 return [{type: 'TAG', name: 'pre'}]

Status & tagging log