All files / mframejs/binding/ast tokenizer.ts

100% Statements 131/131
100% Branches 81/81
100% Functions 11/11
100% Lines 130/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 31729x     29x           29x       326x 326x 326x 326x 326x 326x                 326x 326x 326x 326x 326x 326x 326x 326x                 326x 326x   326x                 326x 326x     81x   326x 326x         34x 34x   34x 34x   2x 2x   2x   57x 57x 57x 57x   2x 2x 2x     108x   91x 91x   17x     108x       5422x       81x     326x                 326x 326x   326x         225x 225x 225x 225x       225x 225x 225x         521x       4998x 4998x       326x                 6384x   6384x               5448x 5448x                   4998x 1992x       1992x 1992x                   326x         226x   238x 238x   226x 226x   225x 225x 225x   589x 589x   225x 225x 225x   1050x 1050x 1050x 1050x 1050x   491x   3121x 3121x   491x     1992x         1768x 1768x       326x 326x   1810x         491x   374x   491x 491x 491x       225x 225x 225x       868x 868x     30x 30x 30x 30x 30x   122x 122x 122x 122x           26x     16x 16x   674x 674x           868x       226x   26x 26x 26x 26x   226x 226x              
import { CharCodes } from './charcode';
 
// create charCode class we can use everytime
const charCode = new CharCodes();
 
/**
 * Class for taking a expression and generating tokens for ast
 *
 */
export class Tokenizer {
    private baseTokenNo: number;
    private token: any;
    private expression: string;
    private baseTokens: any[] = [];
    private tokens: any[] = [];
    private chars: any[] = [];
    private curChar: any = null;
    private curCharNo = 0;
    private curtype: string = null;
    private expressionLength: number;
    private isMix: boolean; // if its a mix of text and interpolate expressions
    private isOutsideExpression: boolean; // if mix stuff outside ${} is text only
    private expressionOriginal: string;
 
 
 
    constructor(expression: string) {
        this.expressionOriginal = expression;
        this.isMix = expression.indexOf('${') !== -1;
        this.isMix = this.isMix ? true : expression.indexOf('@{') !== -1;
        this.isOutsideExpression = this.isMix;
        this.expression = this.setStrings(this.expressionOriginal);
        this.expression = this.removeWhitespaceExpressions(this.expression);
        this.curChar = this.expression.charCodeAt(this.curCharNo);
        this.expressionLength = this.expression.length;
    }
 
 
    /**
     * starts to parse and returns tokens
     *
     */
    public start(): any[] {
        this.generateBaseTokens();
        this.combineBaseTokens();
 
        return this.tokens;
    }
 
 
    /**
     * marks out text
     *
     */
    private setStrings(expression: string): string {
        let text = this.isOutsideExpression;
        let trimmed = '';
        // if text add: "
        if (text) {
            trimmed = '"';
        }
        let count = 0;
        for (let i = 0; i < expression.length; i++) {
            switch (true) {
 
                // if expressions start, add: " before
                case text && expression.charCodeAt(i) === '$'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0):
                    count++;
                    trimmed = trimmed + '"' + expression[i];
 
                    text = false;
                    break;
                case !text && expression.charCodeAt(i) === '$'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0):
                    count++;
                    trimmed = trimmed + expression[i];
 
                    break;
                case text && expression.charCodeAt(i) === '@'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0):
                    count++;
                    trimmed = trimmed + '"' + expression[i];
                    text = false;
                    break;
                case !text && expression.charCodeAt(i) === '@'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0):
                    count++;
                    trimmed = trimmed + expression[i];
                    break;
                // if expressions end, add: " before, if not end of string
                case !text && expression.charCodeAt(i) === '}'.charCodeAt(0):
                    count--;
                    if (!count) {
                        trimmed = trimmed + expression[i] + '"';
                        text = true;
                    } else {
                        trimmed = trimmed + expression[i];
                    }
 
                    break;
 
                // just add
                default:
                    trimmed = trimmed + expression[i];
            }
        }
        if (text) {
            trimmed = trimmed + '"';
        }
 
        return trimmed;
    }
 
 
    /**
     * trims down eveything thats not text so we dont need to check for whitespace later
     *
     */
    private removeWhitespaceExpressions(expression: string): string {
        let text = false;
        let trimmed = '';
        let stringcharType: any;
        for (let i = 0; i < expression.length; i++) {
            switch (true) {
 
                // turn on
                case !text && charCode.STRING_START_END.has(expression.charCodeAt(i)):
                    trimmed = trimmed + expression[i];
                    stringcharType = expression.charCodeAt(i);
                    text = true;
                    break;
 
                // turn of
                case text && (stringcharType === expression.charCodeAt(i)):
                    trimmed = trimmed + expression[i];
                    text = false;
                    break;
 
                // trim
                case !text && charCode.WHITESPACE.has(expression.charCodeAt(i)):
                    // skip only
                    break;
 
                // just add
                default:
                    trimmed = trimmed + expression[i];
                    break;
            }
        }
 
        return trimmed;
 
    }
 
    /**
     * checks if we are at the end of expression
     *
     */
    private parsedAllChars(): boolean {
        const done = this.curCharNo < this.expressionLength;
 
        return !done;
    }
 
    /**
     * goes to next char in expression
     *
     */
    private advanceChar(): void {
        this.curCharNo++;
        this.curChar = this.expression.charCodeAt(this.curCharNo);
    }
 
 
 
    /**
     * adds token
     *
     */
    private addToken(): void {
        const val = this.chars.map(a => String.fromCharCode(a)).join('');
        this.baseTokens.push({
            type: this.curtype,
            value: this.curtype === 'number' ? parseFloat(val) : val
        });
        this.chars = [];
        this.curtype = null;
    }
 
 
 
    /**
     * loops expression and create base tokens
     *
     */
    private generateBaseTokens(): void {
        let done = this.parsedAllChars();
 
        while (!done) {
            switch (true) {
                case this.curtype === null && charCode.NUMBER.has(this.curChar):
                    this.curtype = 'number';
                    while (charCode.NUMBER.has(this.curChar) && !this.parsedAllChars()) {
                        this.chars.push(this.curChar);
                        this.advanceChar();
                    }
                    this.addToken();
                    break;
                case this.curtype === null && charCode.STRING_START_END.has(this.curChar):
                    this.curtype = 'string';
                    const stringcharType = this.curChar;
                    this.advanceChar();
                    while ((stringcharType !== this.curChar) && !this.parsedAllChars()) {
                        this.chars.push(this.curChar);
                        this.advanceChar();
                    }
                    this.advanceChar();
                    this.addToken();
                    break;
                case this.curtype === null && charCode.OPERATOR.has(this.curChar):
                    this.curtype = 'operator';
                    this.chars.push(this.curChar);
                    this.advanceChar();
                    this.addToken();
                    break;
                default:
                    this.curtype = 'variable';
                    while (!charCode.OPERATOR.has(this.curChar) && !this.parsedAllChars()) {
                        this.chars.push(this.curChar);
                        this.advanceChar();
                    }
                    this.addToken();
 
            }
            done = this.parsedAllChars();
        }
    }
 
    private advanceNextBaseToken() {
        this.baseTokenNo++;
        this.token = this.baseTokens[this.baseTokenNo];
    }
 
    private combineBaseTokens() {
        this.tokens = [];
        this.baseTokenNo = 0;
        while (this.baseTokenNo < this.baseTokens.length) {
            this.token = this.baseTokens[this.baseTokenNo];
            switch (true) {
 
                // VARIABLE
                case this.token.type === 'variable':
                    const root = this.tokens[this.tokens.length - 1];
                    if (!root || root && root.value !== '.' && root.value !== '[') {
                        this.token.root = true;
                    }
                    this.tokens.push(this.token);
                    this.advanceNextBaseToken();
                    break;
 
                // STRING
                case this.token.type === 'string':
                    this.tokens.push(this.token);
                    this.advanceNextBaseToken();
                    break;
 
                // OPERATOR
                case this.token.type === 'operator':
                    const next1 = this.baseTokens[this.baseTokenNo + 1] ? this.baseTokens[this.baseTokenNo + 1] : undefined;
                    const next2 = this.baseTokens[this.baseTokenNo + 2] ? this.baseTokens[this.baseTokenNo + 2] : undefined;
                    if (next1 && next1.type === 'operator' && charCode.OPERATOR_COMBO.has(this.token.value + next1.value)) {
                        if (next2 && next2.type === 'operator' && charCode.OPERATOR_COMBO.has(this.token.value + next1.value + next2.value)) {
                            this.token.value = this.token.value + next1.value + next2.value;
                            this.tokens.push(this.token);
                            this.baseTokens.splice(this.baseTokenNo, 1);
                            this.baseTokens.splice(this.baseTokenNo, 1);
                            this.advanceNextBaseToken();
                        } else {
                            this.token.value = this.token.value + next1.value;
                            this.tokens.push(this.token);
                            this.baseTokens.splice(this.baseTokenNo, 1);
                            this.advanceNextBaseToken();
                        }
                    } else {
                        // check if operator and is minus, and if last was also optrator then set number
                        if ((this.token.value === '-' && next1.type === 'number')
                            && this.tokens.length > 0 && this.tokens[this.tokens.length - 1].type === 'operator') {
                            this.token.type = 'number';
                        } else {
                            if ((this.token.value === '$' || this.token.value === '_') && next1.type === 'variable') {
                                next1.value = this.token.value + next1.value;
                                this.baseTokens.splice(this.baseTokenNo, 1);
                            } else {
                                this.tokens.push(this.token);
                                this.advanceNextBaseToken();
                            }
                        }
 
 
                    }
                    break;
 
                // NUMBER
                case this.token.type === 'number':
                    let check = this.baseTokens[this.baseTokenNo + 1] ? this.baseTokens[this.baseTokenNo + 1] : undefined;
                    while (check && check.type === 'number') {
                        this.token.value = this.token.value + check.value;
                        this.token.value = this.token.value * 1;
                        this.baseTokens.splice(this.baseTokenNo, 1);
                        check = this.baseTokens[this.baseTokenNo + 1] ? this.baseTokens[this.baseTokenNo + 1] : undefined;
                    }
                    this.tokens.push(this.token);
                    this.advanceNextBaseToken();
            }
 
        }
 
    }
}