1 /* 2 * Copyright (c) 2013-2014 Nonki Takahashi. All rights reserved. 3 * 4 * History: 5 * 0.3 2014-09-19 Method text is added. 6 * 0.2 2014-01-09 Interface changed and methods sq, sp are added. 7 * 0.1 2013-06-26 Created. Ported from GoSimulator03.smallbasic . 8 */ 9 10 /** 11 * @fileOverview Lex - Lexical Analyzer Object 12 * @version 0.3 13 * @author Nonki Takahashi 14 */ 15 16 const DIGIT0 = 48; 17 const DIGIT9 = 57; 18 const UPPERA = 65; 19 const UPPERZ = 90; 20 const LOWERA = 97; 21 const LOWERZ = 122; 22 23 /** 24 * Lexical Analyzer Object 25 * @class Represents Lexical Analyzer Object 26 * @this {Lex} 27 * @param {String} src source string to analyze 28 * @since 0.1 29 */ 30 function Lex(src) { 31 Source.call(this, src); 32 }; 33 34 Lex.prototype = new Source(); 35 36 Lex.prototype.constructor = Lex; 37 38 /** 39 * Analyze Digit 40 * @return {String} digit if matched, null if not matched 41 * @since 0.2 42 */ 43 Lex.prototype.digit = function() { 44 var code = this.buf.charCodeAt(this.ptr); 45 if (DIGIT0 <= code && code <= DIGIT9) 46 return this.buf.charAt(this.ptr++); 47 else 48 return null; 49 }; 50 51 /** 52 * Analyze Upper Case Character 53 * @return {String} character if matched, null if not matched 54 * @since 0.2 55 */ 56 Lex.prototype.upper = function() { 57 var code = this.buf.charCodeAt(this.ptr); 58 if (UPPERA <= code && code <= UPPERZ) 59 return this.buf.charAt(this.ptr++); 60 else 61 return null; 62 }; 63 64 /** 65 * Analyze Lower Case Character 66 * @return {String} character if matched, null if not matched 67 * @since 0.2 68 */ 69 Lex.prototype.lower = function() { 70 var code = this.buf.charCodeAt(this.ptr); 71 if (LOWERA <= code && code <= LOWERZ) 72 return this.buf.charAt(this.ptr++); 73 else 74 return null; 75 }; 76 77 /** 78 * Given Character 79 * @param {Char} ch given character 80 * @return {String} character if matched, null if not matched 81 * @since 0.2 82 */ 83 Lex.prototype.ch = function(ch) { 84 if (this.buf.charAt(this.ptr) == ch) { 85 this.ptr++; 86 return ch; 87 } else 88 return null; 89 }; 90 91 /** 92 * Analyze Single Quote 93 * @return {String} single quote if matched, null if not matched 94 * @since 0.2 95 */ 96 Lex.prototype.sq = function() { 97 return this.ch("'"); 98 }; 99 100 /** 101 * Analyze Space 102 * @return {String} space if matched, null if not matched 103 * @since 0.2 104 */ 105 Lex.prototype.sp = function() { 106 var match = false; 107 while (this.ch(" ") || this.ch("\t") || this.ch("\n")) { 108 match = true; 109 } 110 if (match) 111 return " "; 112 else 113 return null; 114 }; 115 116 /** 117 * Analyze Text 118 * @param {Boolean} text given text 119 * @param {Boolean} ignoreCase true if ignore case 120 * @return {String} text if matched, null if not matched 121 * @since 0.3 122 */ 123 Lex.prototype.text = function(text, ignoreCase) { 124 var text2 = text; 125 if (ignoreCase) 126 text2 = text2.toLowerCase(); 127 var text1 = this.buf.substr(this.ptr, text.length); 128 if (ignoreCase) 129 text1 = text1.toLowerCase(); 130 if (text1 == text2) { 131 var retval = this.buf.substr(this.ptr, text.length); 132 this.ptr += text.length; 133 return retval; 134 } else 135 return null; 136 }; 137