all files / lib/stream/ parserstream.js

100% Statements 21/21
100% Branches 4/4
100% Functions 7/7
100% Lines 21/21
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                              23× 23× 23×       23729×       3043×       10353×       10333×   10333× 10332× 10332×         23×      
/*
 * Parsec
 * https://github.com/d-plaindoux/parsec
 *
 * Copyright (c) 2016 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */
 
 module.exports = (function() {
     
     'use strict';
     
     var stream = require('./stream.js');
      
     /**
      * Parser stream class
      */
     function ParserStream(parser, source) {
         this.source = parser;
         this.input = source;
         this.offsets = { };
     }
     
     ParserStream.prototype = stream();
     
     ParserStream.prototype.getOffset = function(index) {
         return this.offsets[index] || index;
     };
               
     // Stream 'a => number -> number
     ParserStream.prototype.location = function(index) {
         return this.input.location(this.getOffset(index-1) + 1);
     };
     
     // ParserStream 'a => unit -> boolean
     ParserStream.prototype.endOfStream = function(index) {
         return this.input.endOfStream(this.getOffset(index));
     };
     
     // ParserStream 'a => number -> 'a <+> error
     ParserStream.prototype.unsafeGet = function(index) {
         var result = this.source.parse(this.input, this.getOffset(index));
 
         if (result.isAccepted()) {
             this.offsets[index+1] = result.offset;
             return result.value;
         } else {
             throw new Error();
         }
     };
 
     return function(parser, source) {
         return new ParserStream(parser, source);
     };
     
 }());