Skip to content

Commit

Permalink
pegjs#30: add support for delimiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Sep 22, 2013
1 parent e14add5 commit b809cd0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -293,7 +293,7 @@ expression as many times as possible.
Try to match the expression. If the match succeeds, return its match result,
otherwise return an empty string.

#### *expression* |count|<br> *expression* |min..max|
#### *expression* |count|<br> *expression* |count,delimiter|<br> *expression* |min..max|<br> *expression* |min..max,delimiter|

Match exact `count` repetitions of `expression`. If the match succeeds, return
their match results in an array.
Expand All @@ -306,6 +306,10 @@ be omitted. If `min` omitted, then suppose to `0`. If `max` is omitted, then
suppose to infinity. `expression |..|` equivalently `expression |0..|` and
`expression *`. `expression |1..|` equivalently `expression +`.

Optionally, you can specify delimiter as rule reference or arbitrary rule
expression. Delimiter must appear between elements in parsed input. Results
of delimiter expression will be dropped.

`count`, `min` and `max` must be positive integers.

#### & *expression*
Expand Down
24 changes: 20 additions & 4 deletions lib/compiler/passes/generate-bytecode.js
Expand Up @@ -546,17 +546,33 @@ module.exports = function(ast, options) {
var emptyArrayIndex = addConst('[]');
var nullIndex = addConst('null');
// expressionCode put result on stack
expressionCode = generate(node.expression, {
var expressionCode = generate(node.expression, {
sp: context.sp + 1,
env: { },
action: null
});
/*minIndex = generate(node.min, {
var expressionCode2 = node.delimiter ?
buildSequence(
generate(node.delimiter, {
sp: context.sp + 1,
env: { },
action: null
}), // item = delim();
buildCondition(
[op.IF_NOT_ERROR], // if (item != null)
buildSequence(
op.POP,
expressionCode // item = expr()
),
[]
)
) : expressionCode;
/*var minIndex = generate(node.min, {
sp: context.sp + 1,
env: { },
action: null
});
maxIndex = generate(node.max, {
var maxIndex = generate(node.max, {
sp: context.sp + 1,
env: { },
action: null
Expand All @@ -577,7 +593,7 @@ module.exports = function(ast, options) {
[op.IF_ARRLEN_MAX], // stack:[ [elem...] ]
// push `null` - this break loop on next iteration.
[op.PUSH, nullIndex], // elem = null; stack:[ [elem...], null ]
expressionCode // } else {
expressionCode2 // } else {
) // elem = expr(); stack:[ [elem...], elem ]
) // }
), // } stack:[ [elem...], elem ]
Expand Down

0 comments on commit b809cd0

Please sign in to comment.