1 /* 2 * Copyright (c) 2014 Nonki Takkahashi. All rights reserved. 3 * 4 * History: 5 * 0.1 2014-01-09 Created. 6 */ 7 8 /** 9 * @fileOverview Source - Source Object for Lexer and Parser 10 * @version 0.1 11 * @author Nonki Takahashi 12 */ 13 14 /** 15 * Source Object 16 * @class Represents Source Object 17 * @this {Source} 18 * @param {String} src source string to analyze 19 * @property {String} buf source string buffer 20 * @property {Number} ptr buffer pointer (0 origin) 21 * @since 0.1 22 */ 23 Source = function(src) { 24 // buffer 25 if (!src) 26 this.buf = ""; 27 else 28 this.buf = src; 29 // pointer 30 this.ptr = 0; 31 // for push and pop pointer 32 this.stack = []; 33 }; 34 35 Source.prototype = { 36 37 /** 38 * Set Source 39 * @since 0.1 40 */ 41 set : function(src) { 42 if (!src) 43 this.buf = ""; 44 else 45 this.buf = src; 46 this.rewind(); 47 }, 48 49 /** 50 * Push Pointer 51 */ 52 push: function() { 53 this.stack.push(this.ptr); 54 }, 55 56 /** 57 * Pop Pointer 58 * @param {Boolean} pop true if pop pushed pointer, false if remove pushed pointer 59 */ 60 pop: function(pop) { 61 if (pop) 62 this.ptr = this.stack.pop(); 63 else 64 this.stack.pop(); 65 }, 66 67 /** 68 * End of Data 69 * @return {Boolean} true if buffer pointer comes at end of data 70 * @since 0.1 71 */ 72 eod : function() { 73 if (this.ptr < this.buf.length) 74 return false; 75 else 76 return true; 77 }, 78 79 /** 80 * Rewind 81 * @since 0.1 82 */ 83 rewind : function() { 84 this.ptr = 0; 85 } 86 }; 87