-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxeger.pegjs
136 lines (119 loc) · 3.62 KB
/
pxeger.pegjs
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
regex = root:full_expression
{
var bucket = [ ];
root( function ( text ) { bucket.push( text ); } );
return bucket;
}
full_expression = first:long_expression others:( '|' expression:long_expression { return expression; } )*
{
var expressions = [ first ].concat( others );
return function ( cb ) {
expressions.forEach( function ( expression ) {
expression( cb );
} );
};
}
long_expression = expressions:( modified_expression * )
{
return function ( cb ) {
var next = function ( index, string ) {
if ( ! expressions[ index ] ) {
cb( string );
} else {
expressions[ index ]( function ( part ) {
var temporaryString = string + part;
next( index + 1, temporaryString );
} );
}
};
next( 0, '' );
};
}
modified_expression = expression:expression modifier:modifier
{
return function ( cb ) {
modifier( cb, expression );
};
}
expression = range_expression
/ scope_expression
/ character_expression
range_expression = '[' ranges:(range *) ']'
{
return function ( cb ) {
ranges.forEach( function ( range ) {
range( cb );
} );
};
}
range = range_set
/ range_character
range_set = from:([^\]-]) '-' to:([^\]-])
{
var from = from.charCodeAt( 0 );
var to = to.charCodeAt( 0 );
to = Math.max( from, to );
return function ( cb ) {
for ( var t = from; t <= to; ++ t ) {
cb( String.fromCharCode( t ) );
}
};
}
range_character = character:([^\]])
{
return function ( cb ) {
cb( character );
};
}
scope_expression = '(' expression:full_expression ')'
{
return function ( cb ) {
expression( cb );
}
}
character_expression = character:( character:[^[\](){}|?\\] { return character; }
/ '\\' character:. { return character; } )
{
return function ( cb ) {
cb( character );
};
}
modifier = ornot_modifier
/ count_modifier
/ no_modifier
count_modifier = limits:( '{' from:([0-9]+) ',' to:([0-9]+) '}' { return [from.join( '' ), to.join( '' ) ]; }
/ '{' ',' to:([0-9]+) '}' { return ['0', to.join( '' ) ]; }
/ '{' from:([0-9]+) '}' { return [from.join( '' ), from.join( '' )]; } )
{
var min = parseInt( limits[ 0 ], 10 ) || 0;
var max = parseInt( limits[ 1 ], 10 );
max = Math.max( min, max );
return function ( cb, expression ) {
var next = function ( index, max, string ) {
if ( index === max ) {
cb( string );
} else {
expression( function ( part ) {
var temporaryString = string + part;
next( index + 1, max, temporaryString );
} );
}
};
for ( var t = min; t <= max; ++ t ) {
next( 0, t, '' );
}
};
}
ornot_modifier = '?'
{
return function ( cb, expression ) {
cb( '' );
expression( cb );
};
}
no_modifier =
{
return function ( cb, expression ) {
expression( cb );
};
}