@@ -196,7 +196,7 @@ Syntax.extractMatches = function() {
196
196
197
197
if ( match [ index ] . length > 0 ) {
198
198
if ( rule . brush ) {
199
- matches . push ( Syntax . brushes [ rule . brush ] . buildTree ( match [ index ] , RegExp . indexOf ( match , index ) ) ) ;
199
+ matches . push ( Syntax . Brush . buildTree ( rule , match [ index ] , RegExp . indexOf ( match , index ) ) ) ;
200
200
} else {
201
201
var expression = jQuery . extend ( { owner : expr . owner } , rule ) ;
202
202
@@ -789,8 +789,8 @@ Syntax.Brush = function () {
789
789
Syntax . Brush . prototype . derives = function ( name ) {
790
790
this . parents . push ( name ) ;
791
791
this . rules . push ( {
792
- apply : function ( text , expr , offset ) {
793
- return Syntax . brushes [ name ] . getMatches ( text , offset ) ;
792
+ apply : function ( text , expr ) {
793
+ return Syntax . brushes [ name ] . getMatches ( text ) ;
794
794
}
795
795
} ) ;
796
796
}
@@ -828,6 +828,32 @@ Syntax.Brush.convertStringToTokenPattern = function (pattern, escape) {
828
828
return prefix + pattern + postfix ;
829
829
}
830
830
831
+ Syntax . Brush . MatchPattern = function ( text , rule ) {
832
+ if ( ! rule . pattern )
833
+ return ;
834
+
835
+ // Duplicate the pattern so that the function is reentrant.
836
+ var matches = [ ] , pattern = new RegExp ;
837
+ pattern . compile ( rule . pattern ) ;
838
+
839
+ while ( ( match = pattern . exec ( text ) ) !== null ) {
840
+ if ( rule . matches ) {
841
+ matches = matches . concat ( rule . matches ( match , rule ) ) ;
842
+ } else if ( rule . brush ) {
843
+ matches . push ( Syntax . Brush . buildTree ( rule , match [ 0 ] , match . index ) ) ;
844
+ } else {
845
+ matches . push ( new Syntax . Match ( match . index , match [ 0 ] . length , rule , match [ 0 ] ) ) ;
846
+ }
847
+
848
+ if ( rule . incremental ) {
849
+ // Don't start scanning from the end of the match..
850
+ pattern . lastIndex = match . index + 1 ;
851
+ }
852
+ }
853
+
854
+ return matches ;
855
+ }
856
+
831
857
Syntax . Brush . prototype . push = function ( ) {
832
858
if ( jQuery . isArray ( arguments [ 0 ] ) ) {
833
859
var patterns = arguments [ 0 ] , rule = arguments [ 1 ] ;
@@ -862,6 +888,9 @@ Syntax.Brush.prototype.push = function () {
862
888
if ( typeof ( XRegExp ) !== 'undefined' ) {
863
889
rule . pattern = new XRegExp ( rule . pattern ) ;
864
890
}
891
+
892
+ // Default pattern extraction algorithm
893
+ rule . apply = rule . apply || Syntax . Brush . MatchPattern ;
865
894
866
895
if ( rule . pattern && rule . pattern . global || typeof ( rule . pattern ) == 'undefined' ) {
867
896
this . rules . push ( jQuery . extend ( { owner : this } , rule ) ) ;
@@ -871,45 +900,16 @@ Syntax.Brush.prototype.push = function () {
871
900
}
872
901
} ;
873
902
874
- Syntax . Brush . prototype . getMatchesForRule = function ( text , rule , offset ) {
903
+ Syntax . Brush . prototype . getMatchesForRule = function ( text , rule ) {
875
904
var matches = [ ] , match = null ;
876
905
877
906
// Short circuit (user defined) function:
878
907
if ( typeof ( rule . apply ) != 'undefined' ) {
879
- return rule . apply ( text , rule , offset ) ;
880
- }
881
-
882
- if ( typeof ( rule . pattern ) == 'undefined' ) {
883
- return matches ;
884
- }
885
-
886
- // Duplicate the pattern so that the function is reentrant.
887
- var pattern = new RegExp ;
888
- pattern . compile ( rule . pattern ) ;
889
-
890
- while ( ( match = pattern . exec ( text ) ) !== null ) {
891
- if ( rule . matches ) {
892
- matches = matches . concat ( rule . matches ( match , rule ) ) ;
893
- } else if ( rule . brush ) {
894
- matches . push ( Syntax . brushes [ rule . brush ] . buildTree ( match [ 0 ] , match . index ) ) ;
895
- } else {
896
- matches . push ( new Syntax . Match ( match . index , match [ 0 ] . length , rule , match [ 0 ] ) ) ;
897
- }
898
-
899
- if ( rule . incremental ) {
900
- // Don't start scanning from the end of the match..
901
- pattern . lastIndex = match . index + 1 ;
902
- }
903
- }
904
-
905
- if ( offset && offset > 0 ) {
906
- for ( var i = 0 ; i < matches . length ; i += 1 ) {
907
- matches [ i ] . shift ( offset ) ;
908
- }
908
+ matches = rule . apply ( text , rule ) ;
909
909
}
910
910
911
911
if ( rule . debug ) {
912
- Syntax . log ( "matches" , matches ) ;
912
+ Syntax . log ( "Syntax matches:" , rule , text , matches ) ;
913
913
}
914
914
915
915
return matches ;
@@ -935,13 +935,27 @@ Syntax.Brush.prototype.getMatches = function(text, offset) {
935
935
return matches ;
936
936
} ;
937
937
938
+ Syntax . Brush . buildTree = function ( rule , text , offset , additionalMatches ) {
939
+ var match = Syntax . brushes [ rule . brush ] . buildTree ( text , offset , additionalMatches ) ;
940
+
941
+ jQuery . extend ( match . expression , rule ) ;
942
+
943
+ return match ;
944
+ }
945
+
938
946
Syntax . Brush . prototype . buildTree = function ( text , offset , additionalMatches ) {
939
947
offset = offset || 0 ;
940
948
941
949
// Fixes code that uses \r\n for line endings. /$/ matches both \r\n, which is a problem..
942
950
text = text . replace ( / \r / g, '' ) ;
943
951
944
- var matches = this . getMatches ( text , offset ) ;
952
+ var matches = this . getMatches ( text ) ;
953
+
954
+ if ( offset && offset > 0 ) {
955
+ for ( var i = 0 ; i < matches . length ; i += 1 ) {
956
+ matches [ i ] . shift ( offset ) ;
957
+ }
958
+ }
945
959
946
960
var top = new Syntax . Match ( offset , text . length , { klass : this . allKlasses ( ) . join ( " " ) , allow : '*' , owner : this } , text ) ;
947
961
0 commit comments