Skip to content

Commit

Permalink
#164: line for string stream
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-zozol committed Dec 2, 2020
1 parent b9fb9b4 commit d53b140
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/parsec/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class ParserResponse {
*
* flatMap is a specialization of fold
*/
line(){
return this.input.lineAt(this.getOffset())
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/lib/stream/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import atry from '../data/try';
/**
* Abstract methods:
* - unsafeGet(index)
* - lineAt(offset)
*/
class Stream {
constructor() {}
Expand Down Expand Up @@ -44,6 +45,8 @@ class Stream {

return true;
}

lineAt(offset){return 0}
}

export default Stream;
5 changes: 5 additions & 0 deletions src/lib/stream/stringstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class StringStream extends Stream {
unsafeGet(index) {
return this.source.charAt(index);
}

lineAt(offset){
const str= this.source.substring(0, offset+1);
return (str.match(/\n/g) || '').length + 1
}
}

function factory(source) {
Expand Down
61 changes: 61 additions & 0 deletions src/test/parsec/line-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import stream from '../../lib/stream/index';
import {C,N} from '../../lib/parsec/index';

const eol = C.char('\n')

export default {
setUp: function (done) {
done();
},

'expect line 1 is ok': function (test) {
const string = '007';
// tests here
const parser = N.integer();
const response = parser.parse(stream.ofString(string));

test.equal(1, response.line());
test.done();
},
'expect line 2 is ok': function (test) {
const string = '007\n12';
// tests here
const parser = N.integer().then(eol).then(N.integer());
const response = parser.parse(stream.ofString(string));

test.equal(2, response.line());
test.done();
},
'multiline is ok': function (test) {
const string = '007\n\n12';
// tests here
const parser = N.integer().then(eol).then(N.integer());
const response = parser.parse(stream.ofString(string));

test.equal(3, response.line());
test.done();
},
'not finished parser is ok': function (test) {
const string = '007\nab\n12';
// tests here
const parser = N.integer().then(eol).then(N.integer());
const response = parser.parse(stream.ofString(string));

test.equal(2, response.line());
test.equal(false, response.isAccepted());
test.done();
},
'first lines is ok and windows': function (test) {
const string = '\n007\nab\n12';
// tests here
const parser = eol.then(N.integer()).then(eol).then(N.integer());
const response = parser.parse(stream.ofString(string));

test.equal(3, response.line());
test.equal(false, response.isAccepted());
test.done();
}



}
4 changes: 3 additions & 1 deletion src/test/parsec/parser-package-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import charsBundleTest from './chars-bundle-test';
import numberBundleTest from './number-bundle-test';
import fLayerTest from './f-layer-test';
import tupleParserTest from './tuple-parser-test';
import lineTest from './line-test';

export default {
parserChainTest,
Expand All @@ -21,5 +22,6 @@ export default {
charsBundleTest,
numberBundleTest,
fLayerTest,
tupleParserTest
tupleParserTest,
lineTest
}

0 comments on commit d53b140

Please sign in to comment.