Skip to content

Commit

Permalink
Handle block comments
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Oct 17, 2015
1 parent 25e8744 commit 8ea06d9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
74 changes: 51 additions & 23 deletions lib/streams/commentFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var any = require('../consumers/any.js');
var constrainAcceptance = require('../consumers/constrainAcceptance.js');
var constrainValidity = require('../consumers/constrainValidity.js');
var consumerFilter = require('./consumerFilter.js');
var defer = require('../util/defer.js');
var many = require('../consumers/many.js');
var optional = require('../consumers/optional.js');
var or = require('../consumers/or.js');
Expand All @@ -13,28 +14,55 @@ var single = require('../consumers/single.js');
var string = require('../consumers/string.js');
var transform = require('../consumers/transform.js');

module.exports = function(inputStream) {
return consumerFilter(
or(
transform(
sequence(
string('//'),
many(
constrainAcceptance(any, function(value) { return value !== '\n'; })
),
constrainValidity(
optional(single('\n')),
function(value) {
return value.set;
}
)
),
function() {
return '\n';
}
),
any
var lineCommentAndNewline = transform(
sequence(
string('//'),
many(
constrainAcceptance(any, function(value) { return value !== '\n'; })
),
inputStream
);
constrainValidity(
optional(single('\n')),
function(value) {
return value.set;
}
)
),
function() {
return '\n';
}
);

var blockCommentAndNext = undefined;

var nextChar = or(
lineCommentAndNewline,
defer('blockCommentAndNext', function() { return blockCommentAndNext; }),
any
);

var blockComment = sequence(
string('/*'),
many(or(
defer('blockComment', function() { return blockComment; }),
constrainAcceptance(any, function(value) { return value !== '*'; }),
sequence(
single('*'),
constrainAcceptance(any, function(value) { return value !== '/'; })
)
)),
string('*/')
);

blockCommentAndNext = transform(
sequence(
blockComment,
nextChar
),
function(value) {
return value[1];
}
);

module.exports = function(inputStream) {
return consumerFilter(nextChar, inputStream);
};
34 changes: 33 additions & 1 deletion test/streams/commentFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var describeStream = require('../describers/Stream.js');
var LineStream = require('../../lib/streams/lineStream.js');

describe('commentFilter', function() {
it('filters out comments', function() {
it('filters out line comments', function() {
var stream = commentFilter(
LineStream('test', [
'// this should be ignored',
Expand All @@ -24,6 +24,38 @@ describe('commentFilter', function() {
assert.equal(stream.next(), 'b');
});

it('filters out block comments', function() {
var stream = commentFilter(
LineStream('test', [
'/* this should be ignored */',
'a/* more ignored *//* stuff */',
'b'
].join('\n'))
);

assert.equal(stream.next(), '\n');
assert.equal(stream.next(), 'a');
assert.equal(stream.next(), '\n');
assert.equal(stream.next(), 'b');
});

it('filters out nested block comments', function() {
var stream = commentFilter(
LineStream('test', [
'/* this should be ignored',
' code = here;',
' ',
' /* this inner block comment shouldn\'t interfere with the outer block comment */',
' more.code = here',
'*/',
'b'
].join('\n'))
);

assert.equal(stream.next(), '\n');
assert.equal(stream.next(), 'b');
});

it('hasNext when starting on a comment and there is a newline', function() {
var stream = commentFilter(
LineStream('test', '// foobar\n')
Expand Down

0 comments on commit 8ea06d9

Please sign in to comment.