All files parser.ts

98.6% Statements 425/431
97.53% Branches 79/81
100% Functions 21/21
98.6% Lines 425/431

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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 4321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 211x 211x 211x 1x 1x 1x 1x 1x 1x 119x 119x 119x 1x 1x 1x 1x 1x 1x 1x 1x 1x 107x 107x 11x 11x 11x 107x 107x 1x 1x 1x 1x 1x 1x 1x 1x 1245x 1245x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2253x 2253x 2253x 1040x 1040x 1040x 2253x 2253x 1x 1x 1x 293x 293x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 293x 293x 293x 1x 1x 1x 1x 1x 1x 145x 145x 145x 147x 147x 147x 145x 145x 129x 129x 145x 145x 145x 1x 1x 1x 1x 1x 1x 1x 1x 1x 211x 211x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 24x 24x 13x 13x 13x 13x 13x 13x 24x 11x 11x 24x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x             24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 11x 11x 11x 11x 11x 11x 10x 10x 10x 10x 10x 10x 11x 36x 25x 25x 25x 36x 36x 36x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 211x 211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 496x 496x 496x 496x 496x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2007x 2007x 2007x 2007x 2007x 2007x 1x 1x 1x 1x 1x 1x 1x 1x 1x 508x 508x 508x 508x 508x 508x 508x 1x 1x 1x 1x 1x 1x 1x 1x 1x 376x 376x 376x 371x 371x 371x 1630x 1630x 1630x 371x 376x 339x 339x 376x 376x 1x 1x 1x 1x 1x 1x 1x 1x 1x 114x 114x 114x 102x 102x 102x 406x 406x 406x 102x 114x 91x 91x 114x 114x 1x 1x 1x 1x 1x 1x 1x 1x 1x 221x 221x 221x 114x 102x 102x 102x 102x 114x 221x 221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 739x 739x 739x 219x 100x 219x 116x 116x 96x 89x 89x 96x 96x 116x 739x 374x 374x 739x 739x 1x 1x 1x 1x 1x 1x 1x 1x 1x 173x 173x 173x 562x 562x 173x 173x 1x 1x  
/**
 * Copyright © 2020 2021 2022 7thCode.(http://seventh-code.com/)
 * This software is released under the MIT License.
 * opensource.org/licenses/mit-license.php
 */
 
"use strict";
 
import {BaseHandler} from './handler';
import {ParserStream} from './stream';
 
export enum TokenType {
	operator,
	number,
	name,
	index
}
 
/**
 * BaseParser
 * @remarks
 */
export abstract class BaseParser {
 
	protected stream: ParserStream;
	protected handler: BaseHandler | null;
 
	/**
	 *
	 * @remarks
	 */
	protected constructor(handler: BaseHandler | null, stream: ParserStream) {
		this.stream = stream;
		this.handler = handler;
	}
 
	/**
	 * is_s
	 * s ::=  | \t
	 */
	protected is_s(): boolean {
		const code: number = this.stream.charCode;
		return (code === 32) || (code === 9);
	}
 
	/**
	 * is_s
	 *
	 * S ::= ( " " | tab ) *
	 *
	 * @remarks Spaces or Tabs
	 */
	protected parse_s(): boolean {
		let result: boolean = false;
		while (this.is_s()) {
			this.stream.next();
			result = true;
		}
		return result;
	}
 
	/**
	 * is_terminal
	 *
	 * @remarks
	 *
	 */
	protected is_terminal(): boolean {
		return this.stream.is_terminal;
	}
 
	/**
	 * is_char
	 *
	 * @param c
	 *
	 * @remarks
	 */
	protected is_char(c: string): boolean {
		let result: boolean = false;
		const char: string = this.stream.char;
		if (char === c) {
			this.stream.next();
			result = true;
		}
		return result;
	}
 
	/**/
	private between(data: number, s: number, e: number): boolean {
		return ((s <= data) && (data <= e));
	}
 
	/**
	 * is_symbol
	 * symbol ::= "." | "[" | "]" | "'" | '"' | "+" | "-" | "*" | "/" | "%" | "~" | "&" | "|" | "^" | ">" | "<" | "!" | "=" | "`" | "(" | ")" | "{" | "}" | "?" | ":" | ";" | ","
	 * @remarks
	 */
 
	/*
	protected is_symbol(): boolean {
		let result: boolean = false;
		const code: number = this.stream.charCode;
		switch (code) {
			case 33: // "!"
			case 34: // "'"
			case 91: // "["
			case 93: // "]"
			case 94: // "^"
			case 96: // "`"
				this.stream.next();
				result = true;
				break;
			default: // '%&"()*+,-./'    ":;<=>?"    "{|}~"
				if (this.between(code, 37, 47) || this.between(code, 58, 63) || this.between(code, 123, 126)) {
					this.stream.next();
					result = true;
				}
				break;
		}
		return result;
	}
	*/
 
	/**
	 * is_digit
	 * digit ::= ( 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 )
	 */
	protected is_digit(): boolean {
		const code: number = this.stream.charCode;
		return this.between(code, 48, 57);
	}
 
	/**
	 * parse_number
	 * number ::= digit *
	 */
	protected parse_number(): boolean {
		let result: boolean = false;
		this.stream.restore_point();
		while (this.is_digit()) {
			this.stream.next();
			result = true;
		}
 
		if (this.handler) {
			this.handler.token(TokenType.number, this.stream.current, this.is_terminal());
		}
 
		return result;
	}
}
 
export class FormulaParser extends BaseParser {
 
	/**
	 *
	 * @remarks
	 */
	constructor(handler: BaseHandler | null, stream: ParserStream) {
		super(handler, stream);
	}
 
	/**
	 *
	 * expr ::= term { S { "+" | "-" } S term } *
	 * @remarks
	 * */
	protected is_expr(): boolean {
		let result = false;
		this.stream.restore_point();
		this.parse_s();
		if (this.is_term()) {
			result = true;
			while (true) {
				const char = this.stream.char;
				if (this.is_char("+") || this.is_char("-")) {
					if (this.handler) {
						this.handler.token(TokenType.operator, char, this.is_terminal());
					}
					if (this.is_term()) {
						result = true;
					}
				} else {
					break;
				}
			}
		}
		return result;
	}
 
	/**
	 * is_term
	 *
	 * @remarks
	 * term ::= factor { S { "*" | "/" } S factor } *
	 *
	 *
	 * */
	protected is_term(): boolean {
		let result = false;
		this.stream.restore_point();
		this.parse_s();
		if (this.is_factor()) {
			result = true;
			while (true) {
				const char = this.stream.char;
				if (this.is_char("*") || this.is_char("/")) {
					if (this.handler) {
						this.handler.token(TokenType.operator, char, this.is_terminal());
					}
					if (this.is_factor()) {
						result = true;
					}
				} else {
					break;
				}
			}
		}
		return result;
	}
 
	/**
	 * is_factor
	 *
	 * @remarks
	 * factor ::= "(" S expr S ")" | number
	 *
	 * */
	protected is_factor(): boolean {
		let result = false;
		this.stream.restore_point();
		this.parse_s();
		const char = this.stream.char;
		if (this.is_char("(")) {
			if (this.handler) {
				this.handler.token(TokenType.operator, char, this.is_terminal());
			}
			if (this.is_expr()) {
				const char = this.stream.char;
				if (this.is_char(")")) {
					if (this.handler) {
						this.handler.token(TokenType.operator, char, this.is_terminal());
					}
					this.parse_s();
					result = true;
				}
			}
		} else if (this.parse_number()) {
			this.parse_s();
			result = true;
		}
 
		return result;
	}
 
}
 
/**
 * Parse semantic strings of object references in JavaScript.
 *
 */
export class AttributeParser extends FormulaParser {
 
	/**
	 *
	 * @remarks
	 *
	 */
	constructor(handler: BaseHandler | null, stream: ParserStream) {
		super(handler, stream);
	}
 
	/**
	 * is_reading
	 *
	 * @remarks
	 * reading ::= ( alpha | "_" | "$" ) *
	 *
	 */
	protected is_reading(): boolean {
		const code: number = this.stream.charCode;
		return (((0x3040 <= code) && (code <= 0x2FFFF)) || //
			((65 <= code) && (code <= 90)) || ((97 <= code) && (code <= 122)) || // Alphabet
			(code === 95) || (code === 36)); // _ $
	}
 
	/**
	 * is_trailing
	 *
	 * @remarks
	 * trailing ::= ( alpha | "_" | "$" | digit ) *
	 *
	 */
	protected is_trailing(): boolean {
		const code: number = this.stream.charCode;
		return (((0x3040 <= code) && (code <= 0x2FFFF)) || //
			((65 <= code) && (code <= 90)) || ((97 <= code) && (code <= 122)) || // Alphabet
			((48 <= code) && (code <= 57)) || // Number
			(code === 95) || (code === 36)); // _ $
	}
 
	/**
	 * is_subscript_trailing
	 *
	 * @remarks
	 * subscript_trailing ::= ( alpha | "_" | "$" | digit | "." ) *
	 *
	 */
	protected is_subscript_trailing(): boolean {
		const code: number = this.stream.charCode;
		return (((0x3040 <= code) && (code <= 0x2FFFF)) || //
			((65 <= code) && (code <= 90)) || ((97 <= code) && (code <= 122)) || // Alphabet
			((48 <= code) && (code <= 57)) || // Number
			(code === 95) || (code === 36) || // _ $
			(code === 46)); // .
	}
 
	/**
	 * parse_name
	 *
	 * @remarks
	 * name ::= reading [ trailing ]
	 *
	 */
	protected parse_name(): boolean {
		let result: boolean = false;
		this.stream.restore_point();
		if (this.is_reading()) {
			this.stream.next();
			result = true;
			while (this.is_trailing()) {
				this.stream.next();
				result = true;
			}
		}
		if (this.handler) {
			this.handler.token(TokenType.name, this.stream.current, this.is_terminal());
		}
		return result;
	}
 
	/**
	 * parse_subscript_name
	 *
	 * @remarks
	 * subscript_name ::= subscript_reading [ subscript_trailing ]
	 *
	 */
	protected parse_subscript_name(): boolean {
		let result: boolean = false;
		this.stream.restore_point();
		if (this.is_reading()) {
			this.stream.next();
			result = true;
			while (this.is_subscript_trailing()) {
				this.stream.next();
				result = true;
			}
		}
		if (this.handler) {
			this.handler.token(TokenType.name, this.stream.current, this.is_terminal());
		}
		return result;
	}
 
	/**
	 * parse_subscript_string
	 *
	 * @remarks
	 * subscript_string = "'" subscript_mame "'" | '"' subscript_mame '"'
	 *
	 */
	protected parse_subscript_string(): boolean {
		let result: boolean = false;
		this.stream.restore_point();
		if (this.is_char("'") || this.is_char('"')) {
			if (this.parse_subscript_name()) {
				if ((this.is_char("'") || this.is_char('"'))) {
					result = true;
				}
			}
		}
		return result;
	}
 
	/**
	 * parse_attr
	 *
	 * @remarks
	 * attr ::= "." name | '[' subscript_string | number ']'
	 *
	 */
	protected parse_attr(): boolean {
		let result: boolean = false;
		this.stream.restore_point();
		if (this.is_char("[")) {
			if (this.parse_subscript_string()) {
				result = this.is_char("]");
			} else if (this.parse_number()) {
				const word = this.stream.current;
				if (this.is_char("]")) {
					if (this.handler) {
						this.handler.token(TokenType.index, word, this.is_terminal());
					}
					result = true;
				}
			}
		} else if (this.is_char(".")) {
			result = this.parse_name();
		}
		return result;
	}
 
	/**
	 * parse_path
	 *
	 * @remarks
	 * path ::= attr *
	 *
	 */
	public parse_path(): boolean {
		let result: boolean = false;
		this.stream.restore_point();
		while (this.parse_attr()) {
			result = (this.is_terminal());
		}
		return result;
	}
 
}