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