1 /* 2 * Copyright (C) 2012-2013 Nonki Takkahashi. All rights reserved. 3 * 4 * History: 5 * 0.1 2013-06-26 Created. Ported from GoSimulator03.smallbasic . 6 */ 7 8 /** 9 * @fileOverview Lex - Lexical Analyzer Object 10 * @version 0.1 11 * @author Nonki Takahashi 12 */ 13 14 const DIGIT0 = 48; 15 const DIGIT9 = 57; 16 const UPPERA = 65; 17 const UPPERZ = 90; 18 const LOWERA = 97; 19 const LOWERZ = 122; 20 21 /** 22 * Lexical Analyzer Object 23 * @class Represents Lexical Analyzer Object 24 * @this {Lex} 25 * @param {String} src source string to analyze 26 * @property {String} sBuf source string buffer 27 * @property {Number} iBufPtr buffer pointer (0 origin) 28 * @since 0.1 29 */ 30 Lex = function(src) { 31 this.sBuf = src; 32 this.iBufPtr = 0; 33 }; 34 35 Lex.prototype = { 36 37 /** 38 * Analyze Digit 39 * @returns {Char} digit or null if not matched 40 * @since 0.1 41 */ 42 digit : function() { 43 var iCode = this.sBuf.charCodeAt(this.iBufPtr); 44 if (DIGIT0 <= iCode && iCode <= DIGIT9) 45 return this.sBuf.charAt(this.iBufPtr++); 46 else 47 return null; 48 }, 49 50 /** 51 * Analyze Upper Case Character 52 * @returns {Char} upper case character or null if not matched 53 * @since 0.1 54 */ 55 upper : function() { 56 var iCode = this.sBuf.charCodeAt(this.iBufPtr); 57 if (UPPERA <= iCode && iCode <= UPPERZ) 58 return this.sBuf.charAt(this.iBufPtr++); 59 else 60 return null; 61 }, 62 63 /** 64 * Analyze Lower Case Character 65 * @returns {Char} lower case character or null if not matched 66 * @since 0.1 67 */ 68 lower : function() { 69 var iCode = this.sBuf.charCodeAt(this.iBufPtr); 70 if (LOWERA <= iCode && iCode <= LOWERZ) 71 return this.sBuf.charAt(this.iBufPtr++); 72 else 73 return null; 74 }, 75 76 /** 77 * Given Character 78 * @param {Char} cChar given character 79 * @returns {Char} character or null if not matched 80 * @since 0.1 81 */ 82 ch : function(cChar) { 83 if (this.sBuf.charAt(this.iBufPtr) == cChar) 84 return this.sBuf.charAt(this.iBufPtr++); 85 else 86 return null; 87 }, 88 89 /** 90 * End of Data 91 * @returns {Boolean} true if buffer pointer comes at end of data 92 * @since 0.1 93 */ 94 eod : function() { 95 var iCode = this.sBuf.charCodeAt(this.iBufPtr); 96 if (this.iBufPtr < this.sBuf.length) 97 return false; 98 else 99 return true; 100 }, 101 102 /** 103 * Rewind 104 * @since 0.1 105 */ 106 rewind : function() { 107 this.iBufPtr = 0; 108 } 109 110 }; 111