1 /*
  2 * Copyright (c) 2014 Nonki Takahashi. 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  * @property {Number} stack[] stack for buffer pointer
 22  * @since 0.1
 23  */
 24 Source = function(src) {
 25 	// buffer
 26 	if (!src)
 27 		this.buf = "";
 28 	else
 29 		this.buf = src;
 30 	// pointer
 31 	this.ptr = 0;
 32 	// for push and pop pointer
 33 	this.stack = [];
 34 };
 35 
 36 Source.prototype = {
 37 
 38 	/**
 39 	 * Set Source
 40 	 * @param {String} src source string
 41 	 * @since 0.1
 42 	 */
 43 	set : function(src) {
 44 		if (!src)
 45 			this.buf = "";
 46 		else
 47 			this.buf = src;
 48 		this.rewind();
 49 	},
 50 
 51 	/**
 52 	 * Push Pointer
 53 	 * @since 0.1
 54 	 */
 55 	push: function() {
 56 		this.stack.push(this.ptr);
 57 	},
 58 
 59 	/**
 60 	 * Pop Pointer
 61 	 * @param {Boolean} pop true if pop pushed pointer, false if remove pushed pointer 
 62 	 * @since 0.1
 63 	 */
 64 	pop: function(pop) {
 65 		if (pop)
 66 			this.ptr = this.stack.pop();
 67 		else
 68 			this.stack.pop();
 69 	},
 70 
 71 	/**
 72 	 * End of Data
 73 	 * @return {Boolean} true if buffer pointer comes at end of data
 74 	 * @since 0.1
 75 	 */
 76 	eod : function() {
 77 		if (this.ptr < this.buf.length)
 78 			return false;
 79 		else
 80 			return true;
 81 	},
 82 
 83 	/**
 84 	 * Rewind
 85 	 * @since 0.1
 86 	 */
 87 	rewind : function() {
 88 		this.ptr = 0;
 89 	}
 90 };
 91