Best JavaScript code snippet using playwright-internal
SubframeSelectorEvaluator.js
Source:SubframeSelectorEvaluator.js
1// ----------------------------------------------------------------------------2// PixInsight JavaScript Runtime API - PJSR Version 1.03// ----------------------------------------------------------------------------4// SubframeSelectorEvaluator.js - Released 2018-11-05T16:53:08Z5// ----------------------------------------------------------------------------6//7// This file is part of SubframeSelector Script version 1.128//9// Copyright (C) 2012-2018 Mike Schuster. All Rights Reserved.10// Copyright (C) 2003-2018 Pleiades Astrophoto S.L. All Rights Reserved.11//12// Redistribution and use in both source and binary forms, with or without13// modification, is permitted provided that the following conditions are met:14//15// 1. All redistributions of source code must retain the above copyright16// notice, this list of conditions and the following disclaimer.17//18// 2. All redistributions in binary form must reproduce the above copyright19// notice, this list of conditions and the following disclaimer in the20// documentation and/or other materials provided with the distribution.21//22// 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names23// of their contributors, may be used to endorse or promote products derived24// from this software without specific prior written permission. For written25// permission, please contact info@pixinsight.com.26//27// 4. All products derived from this software, in any form whatsoever, must28// reproduce the following acknowledgment in the end-user documentation29// and/or other materials provided with the product:30//31// "This product is based on software from the PixInsight project, developed32// by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."33//34// Alternatively, if that is where third-party acknowledgments normally35// appear, this acknowledgment must be reproduced in the product itself.36//37// THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS38// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED39// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR40// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS41// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,42// EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS43// INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,44// DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN45// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)46// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE47// POSSIBILITY OF SUCH DAMAGE.48// ----------------------------------------------------------------------------49function numberCompare(a, b) {50 return a < b ? -1 : a > b ? 1 : 0;51}52function medianMeanDeviationOfArray(values) {53 if (values.length == 0) {54 return [0.0, 0.0];55 }56 values.sort(numberCompare);57 var i = Math.floor(0.5 * values.length);58 var median = (2 * i == values.length) ?59 0.5 * (values[i - 1] + values[i]) :60 values[i];61 var deviations = new Array(values.length);62 for (var i = 0; i != values.length; ++i) {63 deviations[i] = Math.abs(median - values[i]);64 }65 var dispersion = 0.0;66 for (var i = 0; i != deviations.length; ++i) {67 dispersion += deviations[i];68 }69 dispersion = dispersion / deviations.length;70 return [median, dispersion];71}72function medianMedianDeviationOfArray(values) {73 if (values.length == 0) {74 return [0.0, 0.0];75 }76 values.sort(numberCompare);77 var i = Math.floor(0.5 * values.length);78 var median = (2 * i == values.length) ?79 0.5 * (values[i - 1] + values[i]) :80 values[i];81 var deviations = new Array(values.length);82 for (var i = 0; i != values.length; ++i) {83 deviations[i] = Math.abs(median - values[i]);84 }85 deviations.sort(numberCompare);86 var i = Math.floor(0.5 * deviations.length);87 var dispersion = (2 * i == deviations.length) ?88 0.5 * (deviations[i - 1] + deviations[i]) :89 deviations[i];90 return [median, dispersion];91}92function sigmaNormalize(value, median, dispersion) {93 return (value - median) / (dispersion != 0.0 ? dispersion : 1.0);94}95function sigmaDenormalize(value, median, dispersion) {96 return value * (dispersion != 0.0 ? dispersion : 1.0) + median;97}98function findMedianDispersionDescription(descriptions) {99 var values = [];100 for (var i = 0; i != descriptions.length; ++i) {101 values.push(descriptions[i].weight);102 }103 var weight = medianMeanDeviationOfArray(values);104 var values = [];105 for (var i = 0; i != descriptions.length; ++i) {106 values.push(descriptions[i].FWHM);107 }108 var FWHM = medianMeanDeviationOfArray(values);109 var values = [];110 for (var i = 0; i != descriptions.length; ++i) {111 values.push(descriptions[i].FWHMMeanDeviation);112 }113 var FWHMMeanDeviation = medianMeanDeviationOfArray(values);114 var values = [];115 for (var i = 0; i != descriptions.length; ++i) {116 values.push(descriptions[i].eccentricity);117 }118 var eccentricity = medianMeanDeviationOfArray(values);119 var values = [];120 for (var i = 0; i != descriptions.length; ++i) {121 values.push(descriptions[i].eccentricityMeanDeviation);122 }123 var eccentricityMeanDeviation = medianMeanDeviationOfArray(values);124 var values = [];125 for (var i = 0; i != descriptions.length; ++i) {126 values.push(descriptions[i].SNRWeight);127 }128 var SNRWeight = medianMeanDeviationOfArray(values);129 var values = [];130 for (var i = 0; i != descriptions.length; ++i) {131 values.push(descriptions[i].median);132 }133 var median = medianMeanDeviationOfArray(values);134 var values = [];135 for (var i = 0; i != descriptions.length; ++i) {136 values.push(descriptions[i].noise);137 }138 var noise = medianMeanDeviationOfArray(values);139 var values = [];140 for (var i = 0; i != descriptions.length; ++i) {141 values.push(descriptions[i].dispersion);142 }143 var dispersion = medianMeanDeviationOfArray(values);144 var values = [];145 for (var i = 0; i != descriptions.length; ++i) {146 values.push(descriptions[i].starSupport);147 }148 var starSupport = medianMeanDeviationOfArray(values);149 var values = [];150 for (var i = 0; i != descriptions.length; ++i) {151 values.push(descriptions[i].starResidual);152 }153 var starResidual = medianMeanDeviationOfArray(values);154 var values = [];155 for (var i = 0; i != descriptions.length; ++i) {156 values.push(descriptions[i].starResidualMeanDeviation);157 }158 var starResidualMeanDeviation = medianMeanDeviationOfArray(values);159 var values = [];160 for (var i = 0; i != descriptions.length; ++i) {161 values.push(descriptions[i].noiseSupport);162 }163 var noiseSupport = medianMeanDeviationOfArray(values);164 return new evaluationDescription(165 null,166 true,167 false,168 false,169 0,170 weight,171 FWHM,172 FWHMMeanDeviation,173 eccentricity,174 eccentricityMeanDeviation,175 SNRWeight,176 median,177 noise,178 dispersion,179 starSupport,180 starResidual,181 starResidualMeanDeviation,182 noiseSupport,183 "",184 []185 );186}187function selectorExpressionEvaluator(188 expression, description, descriptions, subframeScale, cameraGain, cameraResolution, statistics189) {190 this.expression = expression;191 this.description = description;192 this.descriptions = descriptions;193 this.subframeScale = subframeScale;194 this.cameraGain = cameraGain;195 this.cameraResolution = cameraResolution;196 this.position = 0;197 this.value = true;198 this.errorPosition = 0;199 this.error = null;200 this.savePosition = function() {201 return [this.position, this.errorPosition, this.error];202 }203 this.restorePosition = function(description) {204 this.position = description[0];205 this.errorPosition = description[1];206 this.error = description[2];207 }208 this.medianDispersionDescription =209 findMedianDispersionDescription(this.descriptions);210 this.scaleMedianDispersion = function(medianDispersion, scale) {211 return [scale * medianDispersion[0], scale * medianDispersion[1]];212 }213 this.setError = function(error) {214 if (this.error != null) {215 return;216 }217 this.errorPosition = this.position;218 this.error = error;219 };220 this.setErrorContext = function(error) {221 if (this.error != null) {222 return;223 }224 var prefixMaxLength = 5;225 var prefix = this.expression.substring(0, this.position);226 if (prefix.length > prefixMaxLength) {227 prefix =228 "..." +229 prefix.substring(prefix.length - prefixMaxLength, prefix.length);230 }231 var suffixMaxLength = 20;232 var suffix = this.expression.substring(this.position, this.expression.length);233 if (suffix.length > suffixMaxLength) {234 suffix = suffix.substring(0, suffixMaxLength) + "...";235 }236 this.errorPosition = this.position;237 this.error = error + ": " + prefix + "|" + suffix;238 }239 this.isChar = function(character) {240 return this.position != this.expression.length &&241 this.expression.charAt(this.position) == character;242 };243 this.isString = function(string) {244 return this.position <= this.expression.length - string.length &&245 this.expression.substring(this.position, this.position + string.length) ==246 string;247 };248 this.isDigit = function() {249 if (this.position == this.expression.length) {250 return false;251 }252 return "0" <= this.expression.charAt(this.position) &&253 this.expression.charAt(this.position) <= "9";254 };255 this.isLetter = function() {256 if (this.position == this.expression.length) {257 return false;258 }259 return "a" <= this.expression.charAt(this.position) &&260 this.expression.charAt(this.position) <= "z" ||261 "A" <= this.expression.charAt(this.position) &&262 this.expression.charAt(this.position) <= "Z";263 };264 this.isLetterString = function(string) {265 if (!this.isString(string)) {266 return false;267 }268 this.position += string.length;269 var isLetter = this.isLetter();270 this.position -= string.length;271 return !isLetter;272 }273 this.skipWhiteSpace = function() {274 for (; this.position != this.expression.length;) {275 var c = this.expression.charAt(this.position);276 if (c == " " || c == "\n" || c == "\r" || c == "\t") {277 ++this.position;278 }279 else {280 break;281 }282 }283 };284 this.evaluateNumber = function() {285 var digits = 0;286 var number = 0.0;287 for (; this.isDigit();) {288 ++digits;289 number = number * 10 +290 this.expression.charCodeAt(this.position) - "0".charCodeAt(0);291 ++this.position;292 }293 var dotExponent = 0;294 if (this.isChar(".")) {295 ++this.position;296 for (; this.isDigit();) {297 ++digits;298 number = number * 10 +299 this.expression.charCodeAt(this.position) - "0".charCodeAt(0);300 --dotExponent;301 ++this.position;302 }303 }304 if (digits == 0) {305 this.setErrorContext("number expected");306 return 0.0;307 }308 var eExponent = 0;309 if (this.isChar("e") || this.isChar("E")) {310 ++this.position;311 var sign = 1.0;312 if (this.isChar("-")) {313 sign = -1.0;314 ++this.position;315 }316 else if (this.isChar("+")) {317 sign = 1.0;318 ++this.position;319 }320 else {321 sign = 1.0;322 }323 if (!this.isDigit()) {324 this.setErrorContext("exponent expected");325 return 0.0;326 }327 for (; this.isDigit();) {328 eExponent = eExponent * 10 +329 this.expression.charCodeAt(this.position) - "0".charCodeAt(0);330 ++this.position;331 }332 eExponent = sign * eExponent;333 }334 return number * Math.pow(10.0, dotExponent + eExponent);335 };336 this.evaluateProperty = function(isSelector) {337 var property = "";338 for (; this.isLetter();) {339 property = property + this.expression.charAt(this.position);340 ++this.position;341 }342 this.skipWhiteSpace();343 var value;344 var medianDispersion;345 if (property == "Index") {346 return this.description.index + 1;347 }348 else if (isSelector && property == "Weight") {349 return this.description.weight;350 }351 else if (isSelector && property == "WeightSigma") {352 value = this.description.weight;353 medianDispersion = this.medianDispersionDescription.weight;354 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);355 }356 else if (property == "FWHM") {357 return this.subframeScale * this.description.FWHM;358 }359 else if (property == "FWHMMaximum") {360 return this.subframeScale * statistics.FWHMMaximum;361 }362 else if (property == "FWHMMinimum") {363 return this.subframeScale * statistics.FWHMMinimum;364 }365 else if (property == "FWHMSigma") {366 value = this.subframeScale * this.description.FWHM;367 medianDispersion = this.scaleMedianDispersion(368 this.medianDispersionDescription.FWHM, this.subframeScale369 );370 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);371 }372 else if (property == "FWHMMeanDev") {373 return this.subframeScale * this.description.FWHMMeanDeviation;374 }375 else if (property == "FWHMMeanDevSigma") {376 value = this.subframeScale * this.description.FWHMMeanDeviation;377 medianDispersion = this.scaleMedianDispersion(378 this.medianDispersionDescription.FWHMMeanDeviation, this.subframeScale379 );380 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);381 }382 else if (property == "Eccentricity") {383 return this.description.eccentricity;384 }385 else if (property == "EccentricityMaximum") {386 return statistics.eccentricityMaximum;387 }388 else if (property == "EccentricityMinimum") {389 return statistics.eccentricityMinimum;390 }391 else if (property == "EccentricitySigma") {392 value = this.description.eccentricity;393 medianDispersion = this.medianDispersionDescription.eccentricity;394 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);395 }396 else if (property == "EccentricityMeanDev") {397 return this.description.eccentricityMeanDeviation;398 }399 else if (property == "EccentricityMeanDevSigma") {400 value = this.description.eccentricityMeanDeviation;401 medianDispersion = this.medianDispersionDescription.eccentricityMeanDeviation;402 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);403 }404 else if (property == "SNRWeight") {405 return this.description.SNRWeight;406 }407 else if (property == "SNRWeightMaximum") {408 return statistics.SNRWeightMaximum;409 }410 else if (property == "SNRWeightMinimum") {411 return statistics.SNRWeightMinimum;412 }413 else if (property == "SNRWeightSigma") {414 value = this.description.SNRWeight;415 medianDispersion = this.medianDispersionDescription.SNRWeight;416 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);417 }418 else if (property == "Median") {419 return this.cameraResolution * this.cameraGain *420 pedestalFunction(this.description.median);421 }422 else if (property == "MedianSigma") {423 value = this.cameraResolution * this.cameraGain *424 pedestalFunction(this.description.median);425 medianDispersion = this.scaleMedianDispersion(426 [427 pedestalFunction(this.medianDispersionDescription.median[0]),428 this.medianDispersionDescription.median[1]429 ],430 this.cameraResolution * this.cameraGain431 );432 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);433 }434 else if (property == "Noise") {435 return this.cameraResolution * this.cameraGain * this.description.noise;436 }437 else if (property == "NoiseSigma") {438 value = this.cameraResolution * this.cameraGain * this.description.noise;439 medianDispersion = this.scaleMedianDispersion(440 this.medianDispersionDescription.noise,441 this.cameraResolution * this.cameraGain442 );443 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);444 }445 else if (property == "MeanDeviation") {446 return this.cameraResolution * this.cameraGain * this.description.dispersion;447 }448 else if (property == "MeanDeviationSigma") {449 value = this.cameraResolution * this.cameraGain * this.description.dispersion;450 medianDispersion = this.scaleMedianDispersion(451 this.medianDispersionDescription.dispersion,452 this.cameraResolution * this.cameraGain453 );454 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);455 }456 else if (property == "StarSupport") {457 return this.description.starSupport;458 }459 else if (property == "StarSupportSigma") {460 value = this.description.starSupport;461 medianDispersion = this.medianDispersionDescription.starSupport;462 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);463 }464 else if (property == "StarResidual") {465 return this.description.starResidual;466 }467 else if (property == "StarResidualSigma") {468 value = this.description.starResidual;469 medianDispersion = this.medianDispersionDescription.starResidual;470 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);471 }472 else if (property == "StarResidualMeanDev") {473 return this.description.starResidualMeanDeviation;474 }475 else if (property == "StarResidualMeanDevSigma") {476 value = this.description.starResidualMeanDeviation;477 medianDispersion = this.medianDispersionDescription.starResidualMeanDeviation;478 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);479 }480 else if (property == "NoiseSupport") {481 return this.description.noiseSupport;482 }483 else if (property == "NoiseSupportSigma") {484 value = this.description.noiseSupport;485 medianDispersion = this.medianDispersionDescription.noiseSupport;486 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);487 }488 else if (property != "") {489 this.setError("unknown property: " + property);490 return 0.0;491 }492 else {493 this.setErrorContext("property expected");494 return 0.0;495 }496 };497 this.evaluateTerm = function(isSelector) {498 if (this.isDigit() || this.isChar(".")) {499 var number = this.evaluateNumber();500 this.skipWhiteSpace();501 return number;502 }503 else {504 var property = this.evaluateProperty(isSelector);505 this.skipWhiteSpace();506 return property;507 }508 };509 this.evaluateWeight = function(isSelector) {510 var sign = 1.0;511 if (this.isChar("-")) {512 sign = -1.0;513 ++this.position;514 this.skipWhiteSpace();515 }516 var value;517 if (this.isChar("(")) {518 ++this.position;519 this.skipWhiteSpace();520 value = this.evaluateAddWeight(isSelector);521 this.skipWhiteSpace();522 if (!this.isChar(")")) {523 this.setErrorContext(") expected");524 return 0.0;525 }526 ++this.position;527 this.skipWhiteSpace();528 }529 else {530 value = this.evaluateTerm(isSelector);531 this.skipWhiteSpace();532 }533 var exponent = 1.0;534 if (this.isChar("^")) {535 ++this.position;536 this.skipWhiteSpace();537 exponent = this.evaluateWeight(isSelector);538 this.skipWhiteSpace();539 }540 return sign * (exponent == 1.0 ? value : Math.pow(value, exponent));541 };542 this.evaluateMultiplyWeight = function(isSelector) {543 var value = this.evaluateWeight(isSelector);544 this.skipWhiteSpace();545 for (; this.isChar("*") || this.isChar("/");) {546 var multiply = this.isChar("*");547 ++this.position;548 this.skipWhiteSpace();549 var weight = this.evaluateWeight(isSelector);550 this.skipWhiteSpace();551 value = multiply ? value * weight : value / weight;552 }553 return value;554 };555 this.evaluateAddWeight = function(isSelector) {556 var value = this.evaluateMultiplyWeight(isSelector);557 this.skipWhiteSpace();558 for (; this.isChar("+") || this.isChar("-");) {559 var add = this.isChar("+");560 ++this.position;561 this.skipWhiteSpace();562 var multiplyWeight = this.evaluateMultiplyWeight(isSelector);563 this.skipWhiteSpace();564 value = add ? value + multiplyWeight : value - multiplyWeight;565 }566 return !isNaN(value) && isFinite(value) ? value : 0.0;567 };568 this.evaluateRelation = function() {569 var left = this.evaluateAddWeight(true);570 this.skipWhiteSpace();571 var relation = 0;572 if (this.isString("<=")) {573 ++this.position;574 ++this.position;575 this.skipWhiteSpace();576 relation = 0;577 }578 else if (this.isString(">=")) {579 ++this.position;580 ++this.position;581 this.skipWhiteSpace();582 relation = 1;583 }584 else if (this.isString("==")) {585 ++this.position;586 ++this.position;587 this.skipWhiteSpace();588 relation = 2;589 }590 else if (this.isString("!=")) {591 ++this.position;592 ++this.position;593 this.skipWhiteSpace();594 relation = 3;595 }596 else if (this.isChar("<")) {597 ++this.position;598 this.skipWhiteSpace();599 relation = 5;600 }601 else if (this.isChar(">")) {602 ++this.position;603 this.skipWhiteSpace();604 relation = 6;605 }606 else {607 this.setErrorContext("relational operator expected");608 return false;609 }610 var right = this.evaluateAddWeight(true);611 this.skipWhiteSpace();612 if (relation == 0) {613 return left <= right;614 }615 else if (relation == 1) {616 return left >= right;617 }618 else if (relation == 2) {619 return left == right;620 }621 else if (relation == 3) {622 return left != right;623 }624 else if (relation == 4) {625 return false;626 }627 else if (relation == 5) {628 return left < right;629 }630 else if (relation == 6) {631 return left > right;632 }633 else if (relation == 7) {634 return true;635 }636 else {637 this.setErrorContext("relational operator expected");638 return false;639 }640 }641 this.evaluateConstraint = function() {642 if (this.isChar("!")) {643 ++this.position;644 this.skipWhiteSpace();645 if (!this.isChar("(")) {646 this.setErrorContext("( expected");647 return false;648 }649 ++this.position;650 this.skipWhiteSpace();651 var orSelector = this.evaluateOrSelector();652 this.skipWhiteSpace();653 if (this.isChar(")")) {654 ++this.position;655 this.skipWhiteSpace();656 }657 else {658 this.setErrorContext(") expected");659 return false;660 }661 return !orSelector;662 }663 else if (this.isChar("(")) {664 var position = this.savePosition();665 ++this.position;666 this.skipWhiteSpace();667 var orSelector = this.evaluateOrSelector();668 if (this.error != null) {669 this.restorePosition(position);670 var relation = this.evaluateRelation();671 this.skipWhiteSpace();672 return relation;673 }674 this.skipWhiteSpace();675 if (this.isChar(")")) {676 ++this.position;677 this.skipWhiteSpace();678 }679 else {680 this.setErrorContext(") expected");681 return false;682 }683 return orSelector;684 }685 else if (this.isLetterString("true")) {686 this.position += 4;687 this.skipWhiteSpace();688 return true;689 }690 else if (this.isLetterString("false")) {691 this.position += 5;692 this.skipWhiteSpace();693 return false;694 }695 else {696 var relation = this.evaluateRelation();697 this.skipWhiteSpace();698 return relation;699 }700 };701 this.evaluateAndSelector = function() {702 var value = this.evaluateConstraint();703 this.skipWhiteSpace();704 for (; this.isString("&&");) {705 ++this.position;706 ++this.position;707 this.skipWhiteSpace();708 var constraint = this.evaluateConstraint();709 this.skipWhiteSpace();710 value = value && constraint;711 }712 return value;713 };714 this.evaluateOrSelector = function() {715 var value = this.evaluateAndSelector();716 this.skipWhiteSpace();717 for (; this.isString("||");) {718 ++this.position;719 ++this.position;720 this.skipWhiteSpace();721 var andSelector = this.evaluateAndSelector();722 this.skipWhiteSpace();723 value = value || andSelector;724 }725 return value;726 };727 this.evaluate = function() {728 this.position = 0;729 this.errorPosition = 0;730 this.error = null;731 var value = true;732 this.skipWhiteSpace();733 if (this.position != this.expression.length) {734 value = this.evaluateOrSelector();735 this.skipWhiteSpace();736 if (this.position != this.expression.length) {737 this.setErrorContext("end of expression expected");738 }739 }740 this.value = this.error == null ? value : true;741 return this.value;742 };743}744function weightingExpressionEvaluator(745 expression, description, descriptions, subframeScale, cameraGain, cameraResolution, statistics746) {747 this.expression = expression;748 this.description = description;749 this.descriptions = descriptions;750 this.subframeScale = subframeScale;751 this.cameraGain = cameraGain;752 this.cameraResolution = cameraResolution;753 this.position = 0;754 this.value = 1.0;755 this.errorPosition = 0;756 this.error = null;757 this.medianDispersionDescription =758 findMedianDispersionDescription(this.descriptions);759 this.scaleMedianDispersion = function(medianDispersion, scale) {760 return [scale * medianDispersion[0], scale * medianDispersion[1]];761 }762 this.setError = function(error) {763 if (this.error != null) {764 return;765 }766 this.errorPosition = this.position;767 this.error = error;768 };769 this.setErrorContext = function(error) {770 if (this.error != null) {771 return;772 }773 var prefixMaxLength = 5;774 var prefix = this.expression.substring(0, this.position);775 if (prefix.length > prefixMaxLength) {776 prefix =777 "..." +778 prefix.substring(prefix.length - prefixMaxLength, prefix.length);779 }780 var suffixMaxLength = 20;781 var suffix = this.expression.substring(this.position, this.expression.length);782 if (suffix.length > suffixMaxLength) {783 suffix = suffix.substring(0, suffixMaxLength) + "...";784 }785 this.errorPosition = this.position;786 this.error = error + ": " + prefix + "|" + suffix;787 }788 this.isChar = function(character) {789 return this.position != this.expression.length &&790 this.expression.charAt(this.position) == character;791 };792 this.isString = function(string) {793 return this.position <= this.expression.length - string.length &&794 this.expression.substring(this.position, this.position + string.length) ==795 string;796 };797 this.isDigit = function() {798 if (this.position == this.expression.length) {799 return false;800 }801 return "0" <= this.expression.charAt(this.position) &&802 this.expression.charAt(this.position) <= "9";803 };804 this.isLetter = function() {805 if (this.position == this.expression.length) {806 return false;807 }808 return "a" <= this.expression.charAt(this.position) &&809 this.expression.charAt(this.position) <= "z" ||810 "A" <= this.expression.charAt(this.position) &&811 this.expression.charAt(this.position) <= "Z";812 };813 this.isLetterString = function(string) {814 if (!this.isString(string)) {815 return false;816 }817 this.position += string.length;818 var isLetter = this.isLetter();819 this.position -= string.length;820 return !isLetter;821 }822 this.skipWhiteSpace = function() {823 for (; this.position != this.expression.length;) {824 var c = this.expression.charAt(this.position);825 if (c == " " || c == "\n" || c == "\r" || c == "\t") {826 ++this.position;827 }828 else {829 break;830 }831 }832 };833 this.evaluateNumber = function() {834 var digits = 0;835 var number = 0.0;836 for (; this.isDigit();) {837 ++digits;838 number = number * 10 +839 this.expression.charCodeAt(this.position) - "0".charCodeAt(0);840 ++this.position;841 }842 var dotExponent = 0;843 if (this.isChar(".")) {844 ++this.position;845 for (; this.isDigit();) {846 ++digits;847 number = number * 10 +848 this.expression.charCodeAt(this.position) - "0".charCodeAt(0);849 --dotExponent;850 ++this.position;851 }852 }853 if (digits == 0) {854 this.setErrorContext("number expected");855 return 0.0;856 }857 var eExponent = 0;858 if (this.isChar("e") || this.isChar("E")) {859 ++this.position;860 var sign = 1.0;861 if (this.isChar("-")) {862 sign = -1.0;863 ++this.position;864 }865 else if (this.isChar("+")) {866 sign = 1.0;867 ++this.position;868 }869 else {870 sign = 1.0;871 }872 if (!this.isDigit()) {873 this.setErrorContext("exponent expected");874 return 0.0;875 }876 for (; this.isDigit();) {877 eExponent = eExponent * 10 +878 this.expression.charCodeAt(this.position) - "0".charCodeAt(0);879 ++this.position;880 }881 eExponent = sign * eExponent;882 }883 return number * Math.pow(10.0, dotExponent + eExponent);884 };885 this.evaluateProperty = function(isSelector) {886 var property = "";887 for (; this.isLetter();) {888 property = property + this.expression.charAt(this.position);889 ++this.position;890 }891 this.skipWhiteSpace();892 var value;893 var medianDispersion;894 if (property == "Index") {895 return this.description.index + 1;896 }897 else if (isSelector && property == "Weight") {898 return this.description.weight;899 }900 else if (isSelector && property == "WeightSigma") {901 value = this.description.weight;902 medianDispersion = this.medianDispersionDescription.weight;903 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);904 }905 else if (property == "FWHM") {906 return this.subframeScale * this.description.FWHM;907 }908 else if (property == "FWHMMaximum") {909 return this.subframeScale * statistics.FWHMMaximum;910 }911 else if (property == "FWHMMinimum") {912 return this.subframeScale * statistics.FWHMMinimum;913 }914 else if (property == "FWHMSigma") {915 value = this.subframeScale * this.description.FWHM;916 medianDispersion = this.scaleMedianDispersion(917 this.medianDispersionDescription.FWHM, this.subframeScale918 );919 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);920 }921 else if (property == "FWHMMeanDev") {922 return this.subframeScale * this.description.FWHMMeanDeviation;923 }924 else if (property == "FWHMMeanDevSigma") {925 value = this.subframeScale * this.description.FWHMMeanDeviation;926 medianDispersion = this.scaleMedianDispersion(927 this.medianDispersionDescription.FWHMMeanDeviation, this.subframeScale928 );929 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);930 }931 else if (property == "Eccentricity") {932 return this.description.eccentricity;933 }934 else if (property == "EccentricityMaximum") {935 return statistics.eccentricityMaximum;936 }937 else if (property == "EccentricityMinimum") {938 return statistics.eccentricityMinimum;939 }940 else if (property == "EccentricitySigma") {941 value = this.description.eccentricity;942 medianDispersion = this.medianDispersionDescription.eccentricity;943 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);944 }945 else if (property == "EccentricityMeanDev") {946 return this.description.eccentricityMeanDeviation;947 }948 else if (property == "EccentricityMeanDevSigma") {949 value = this.description.eccentricityMeanDeviation;950 medianDispersion = this.medianDispersionDescription.eccentricityMeanDeviation;951 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);952 }953 else if (property == "SNRWeight") {954 return this.description.SNRWeight;955 }956 else if (property == "SNRWeightMaximum") {957 return statistics.SNRWeightMaximum;958 }959 else if (property == "SNRWeightMinimum") {960 return statistics.SNRWeightMinimum;961 }962 else if (property == "SNRWeightSigma") {963 value = this.description.SNRWeight;964 medianDispersion = this.medianDispersionDescription.SNRWeight;965 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);966 }967 else if (property == "Median") {968 return this.cameraResolution * this.cameraGain *969 pedestalFunction(this.description.median);970 }971 else if (property == "MedianSigma") {972 value = this.cameraResolution * this.cameraGain *973 pedestalFunction(this.description.median);974 medianDispersion = this.scaleMedianDispersion(975 [976 pedestalFunction(this.medianDispersionDescription.median[0]),977 this.medianDispersionDescription.median[1]978 ],979 this.cameraResolution * this.cameraGain980 );981 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);982 }983 else if (property == "Noise") {984 return this.cameraResolution * this.cameraGain * this.description.noise;985 }986 else if (property == "NoiseSigma") {987 value = this.cameraResolution * this.cameraGain * this.description.noise;988 medianDispersion = this.scaleMedianDispersion(989 this.medianDispersionDescription.noise,990 this.cameraResolution * this.cameraGain991 );992 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);993 }994 else if (property == "MeanDeviation") {995 return this.cameraResolution * this.cameraGain * this.description.dispersion;996 }997 else if (property == "MeanDeviationSigma") {998 value = this.cameraResolution * this.cameraGain * this.description.dispersion;999 medianDispersion = this.scaleMedianDispersion(1000 this.medianDispersionDescription.dispersion,1001 this.cameraResolution * this.cameraGain1002 );1003 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);1004 }1005 else if (property == "StarSupport") {1006 return this.description.starSupport;1007 }1008 else if (property == "StarSupportSigma") {1009 value = this.description.starSupport;1010 medianDispersion = this.medianDispersionDescription.starSupport;1011 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);1012 }1013 else if (property == "StarResidual") {1014 return this.description.starResidual;1015 }1016 else if (property == "StarResidualSigma") {1017 value = this.description.starResidual;1018 medianDispersion = this.medianDispersionDescription.starResidual;1019 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);1020 }1021 else if (property == "StarResidualMeanDev") {1022 return this.description.starResidualMeanDeviation;1023 }1024 else if (property == "StarResidualMeanDevSigma") {1025 value = this.description.starResidualMeanDeviation;1026 medianDispersion = this.medianDispersionDescription.starResidualMeanDeviation;1027 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);1028 }1029 else if (property == "NoiseSupport") {1030 return this.description.noiseSupport;1031 }1032 else if (property == "NoiseSupportSigma") {1033 value = this.description.noiseSupport;1034 medianDispersion = this.medianDispersionDescription.noiseSupport;1035 return sigmaNormalize(value, medianDispersion[0], medianDispersion[1]);1036 }1037 else if (property != "") {1038 this.setError("unknown property: " + property);1039 return 0.0;1040 }1041 else {1042 this.setErrorContext("property expected");1043 return 0.0;1044 }1045 };1046 this.evaluateTerm = function(isSelector) {1047 if (this.isDigit() || this.isChar(".")) {1048 var number = this.evaluateNumber();1049 this.skipWhiteSpace();1050 return number;1051 }1052 else {1053 var property = this.evaluateProperty(isSelector);1054 this.skipWhiteSpace();1055 return property;1056 }1057 };1058 this.evaluateWeight = function(isSelector) {1059 var sign = 1.0;1060 if (this.isChar("-")) {1061 sign = -1.0;1062 ++this.position;1063 this.skipWhiteSpace();1064 }1065 var value;1066 if (this.isChar("(")) {1067 ++this.position;1068 this.skipWhiteSpace();1069 value = this.evaluateAddWeight(isSelector);1070 this.skipWhiteSpace();1071 if (!this.isChar(")")) {1072 this.setErrorContext(") expected");1073 return 0.0;1074 }1075 ++this.position;1076 this.skipWhiteSpace();1077 }1078 else {1079 value = this.evaluateTerm(isSelector);1080 this.skipWhiteSpace();1081 }1082 var exponent = 1.0;1083 if (this.isChar("^")) {1084 ++this.position;1085 this.skipWhiteSpace();1086 exponent = this.evaluateWeight(isSelector);1087 this.skipWhiteSpace();1088 }1089 return sign * (exponent == 1.0 ? value : Math.pow(value, exponent));1090 };1091 this.evaluateMultiplyWeight = function(isSelector) {1092 var value = this.evaluateWeight(isSelector);1093 this.skipWhiteSpace();1094 for (; this.isChar("*") || this.isChar("/");) {1095 var multiply = this.isChar("*");1096 ++this.position;1097 this.skipWhiteSpace();1098 var weight = this.evaluateWeight(isSelector);1099 this.skipWhiteSpace();1100 value = multiply ? value * weight : value / weight;1101 }1102 return value;1103 };1104 this.evaluateAddWeight = function(isSelector) {1105 var value = this.evaluateMultiplyWeight(isSelector);1106 this.skipWhiteSpace();1107 for (; this.isChar("+") || this.isChar("-");) {1108 var add = this.isChar("+");1109 ++this.position;1110 this.skipWhiteSpace();1111 var multiplyWeight = this.evaluateMultiplyWeight(isSelector);1112 this.skipWhiteSpace();1113 value = add ? value + multiplyWeight : value - multiplyWeight;1114 }1115 return !isNaN(value) && isFinite(value) ? value : 0.0;1116 };1117 this.evaluate = function() {1118 this.position = 0;1119 this.errorPosition = 0;1120 this.error = null;1121 var value = 0.0;1122 this.skipWhiteSpace();1123 if (this.position != this.expression.length) {1124 value = this.evaluateAddWeight(false);1125 this.skipWhiteSpace();1126 if (this.position != this.expression.length) {1127 this.setErrorContext("end of expression expected");1128 }1129 }1130 this.value = this.error == null ? value : 0.0;1131 return this.value;1132 };1133}1134function selectorExpressionIsValid(expression) {1135 var selectorEvaluator = new selectorExpressionEvaluator(1136 expression,1137 nullEvaluationDescription,1138 [nullEvaluationDescription],1139 parameters.actualSubframeScale(),1140 parameters.actualCameraGain(),1141 parameters.cameraResolutionValues[parameters.cameraResolution],1142 nullEvaluationDescriptionStatistics1143 );1144 selectorEvaluator.evaluate();1145 return selectorEvaluator.error == null;1146}1147function weightingExpressionIsValid(expression) {1148 var weightEvaluator = new weightingExpressionEvaluator(1149 expression,1150 nullEvaluationDescription,1151 [nullEvaluationDescription],1152 parameters.actualSubframeScale(),1153 parameters.actualCameraGain(),1154 parameters.cameraResolutionValues[parameters.cameraResolution],1155 nullEvaluationDescriptionStatistics1156 );1157 weightEvaluator.evaluate();1158 return weightEvaluator.error == null;1159}1160// ----------------------------------------------------------------------------...
MetaCompiler.js
Source:MetaCompiler.js
1/**2 * Wheel, copyright (c) 2017 - present by Arno van der Vegt3 * Distributed under an MIT license: https://arnovandervegt.github.io/wheel/license.txt4**/5const path = require('../../../shared/lib/path');6const t = require('../tokenizer/tokenizer');7const errors = require('../errors');8const err = require('../errors').errors;9const removePadding = function(s) {10 return s.substr(1, s.length - 2);11 };12exports.checkRestTokens = function(iterator, after) {13 let restTokens = iterator.nextUntilLexeme([t.LEXEME_NEWLINE]);14 if (restTokens.tokens.length) {15 throw errors.createError(err.UNEXPECTED_CODE_AFTER_META, restTokens.tokens[0], 'Unexpected code after "#' + after + '".');16 }17 };18exports.MetaCompiler = class {19 constructor(opts) {20 this._globalDefines = opts.globalDefines;21 this._defines = opts.defines;22 this._resources = opts.resources;23 this._linter = opts.linter;24 }25 compileDefine(iterator, token, tokenFilename) {26 token.done = true;27 token = iterator.skipWhiteSpace().next();28 if (token.cls !== t.TOKEN_IDENTIFIER) {29 token.filename = tokenFilename;30 throw errors.createError(err.IDENTIFIER_EXPECTED, token, 'Identifier expected.');31 }32 this._linter && this._linter.addDefine(token);33 let defineKey;34 token.done = true;35 defineKey = token.lexeme;36 token = iterator.skipWhiteSpace().next();37 token.done = true;38 if ([t.TOKEN_NUMBER, t.TOKEN_STRING].indexOf(token.cls) === -1) {39 token.filename = tokenFilename;40 throw errors.createError(err.NUMBER_OR_STRING_CONSTANT_EXPECTED, token, 'Number or string constant expected.');41 }42 if (token.cls === t.TOKEN_NUMBER) {43 this._defines.add(token, defineKey, token.value);44 } else {45 this._defines.add(token, defineKey, token.lexeme);46 }47 exports.checkRestTokens(iterator, 'define');48 }49 compileImage(iterator, token, tokenFilename) {50 token.done = true;51 token = iterator.skipWhiteSpace().next();52 token.done = true;53 if (token.cls !== t.TOKEN_STRING) {54 token.filename = tokenFilename;55 throw errors.createError(err.FILENAME_EXPECTED, token, 'Filename expected.');56 }57 let filename = removePadding(token.lexeme);58 if (filename.substr(-4) !== '.rgf') {59 token.filename = tokenFilename;60 throw errors.createError(err.RGF_EXTENSION_EXPECTED, token, '".rgf" Extension expected.');61 }62 let p = iterator.skipWhiteSpace().peek();63 if ((p.cls !== t.TOKEN_META) || (p.lexeme !== t.LEXEME_META_DATA)) {64 token.filename = tokenFilename;65 throw errors.createError(err.DATA_EXPECTED, token, '"' + t.LEXEME_META_DATA + '" Expected.');66 }67 let hasData = true;68 let dataWidth = null;69 let image = {filename: filename, data: []};70 while (hasData) {71 token = iterator.skipWhiteSpace().next();72 token.done = true;73 token = iterator.skipWhiteSpace().next();74 if (token.cls !== t.TOKEN_STRING) {75 token.filename = tokenFilename;76 throw errors.createError(err.DATA_STRING_EXPECTED, token, 'Data string expected.');77 }78 token.done = true;79 let dataString = removePadding(token.lexeme);80 if (!dataString.length) {81 token.filename = tokenFilename;82 throw errors.createError(err.DATA_STRING_EMPTY, token, 'Data string is empty.');83 }84 if (dataWidth === null) {85 dataWidth = dataString.length;86 } else if (dataString.length !== dataWidth) {87 token.filename = tokenFilename;88 throw errors.createError(err.DATA_STRING_LENGTH_MISMATCH, token, 'Data string length mismatch.');89 }90 let dataLine = [];91 for (let i = 0; i < dataString.length; i++) {92 if ('01'.indexOf(dataString[i]) === -1) {93 token.filename = tokenFilename;94 throw errors.createError(err.DATA_STRING_INVALID_CHARACTER, token, 'Data string can only contain "0" or "1".');95 }96 dataLine.push(dataString[i] === '0' ? 0 : 1);97 }98 image.data.push(dataLine);99 hasData = (iterator.skipWhiteSpace().peek().lexeme === t.LEXEME_META_DATA);100 }101 this._resources.add(image.filename, image.data, token);102 }103 compileText(iterator, token, tokenFilename) {104 token.done = true;105 token = iterator.skipWhiteSpace().next();106 token.done = true;107 if (token.cls !== t.TOKEN_STRING) {108 token.filename = tokenFilename;109 throw errors.createError(err.FILENAME_EXPECTED, token, 'Filename expected.');110 }111 let filename = removePadding(token.lexeme);112 if (filename.substr(-4) !== '.rtf') {113 token.filename = tokenFilename;114 throw errors.createError(err.RTF_EXTENSION_EXPECTED, token, '".rtf" Extension expected.');115 }116 let p = iterator.skipWhiteSpace().peek();117 if ((p.cls !== t.TOKEN_META) || (p.lexeme !== t.LEXEME_META_LINE)) {118 token.filename = tokenFilename;119 throw errors.createError(err.LINE_EXPECTED, token, '"' + t.LEXEME_META_LINE + '" Expected.');120 }121 let hasLines = true;122 let text = {filename: filename, lines: []};123 while (hasLines) {124 token = iterator.skipWhiteSpace().next();125 token.done = true;126 token = iterator.skipWhiteSpace().next();127 if (token.cls !== t.TOKEN_STRING) {128 token.filename = tokenFilename;129 throw errors.createError(err.LINE_STRING_EXPECTED, token, 'Line string expected.');130 }131 token.done = true;132 text.lines.push(removePadding(token.lexeme));133 hasLines = (iterator.skipWhiteSpace().peek().lexeme === t.LEXEME_META_LINE);134 }135 this._resources.add(text.filename, text.lines.join(String.fromCharCode(0x0D)), token);136 }137 compileResource(iterator, token, tokenFilename) {138 token.done = true;139 token = iterator.skipWhiteSpace().next();140 token.done = true;141 if (token.cls !== t.TOKEN_STRING) {142 token.filename = tokenFilename;143 throw errors.createError(err.FILENAME_EXPECTED, token, 'Filename expected.');144 }145 let filename = removePadding(token.lexeme);146 if (['.rgf', '.rsf', '.rtf', '.rbf', '.wfrm'].indexOf(path.getExtension(filename)) === -1) {147 token.filename = tokenFilename;148 throw errors.createError(err.INVALID_RESOURCE, token, 'Invalid resource.');149 }150 this._resources.add(filename, null, token);151 }152 compileIfdef(iterator, token, defines) {153 token.done = true;154 token = iterator.skipWhiteSpace().next();155 token.done = true;156 let defined = (defines.get(token.lexeme) !== false);157 let done = false;158 let doneElse = false;159 exports.checkRestTokens(iterator, 'ifdef');160 while (true) {161 token = iterator.skipWhiteSpace().next();162 if (token === null) {163 break;164 } else if (token.lexeme === t.LEXEME_META_IFDEF) {165 this.compileIfdef(iterator, token, defines);166 } else if (token.lexeme === t.LEXEME_META_ELSE) {167 if (doneElse) {168 throw errors.createError(err.UNEXPECTED_META_ELSE, token, 'Duplicate #else.');169 }170 exports.checkRestTokens(iterator, 'else');171 doneElse = true;172 token.done = true;173 defined = !defined;174 } else {175 if (!defined) {176 token.done = true;177 }178 if (token.lexeme === t.LEXEME_META_END) {179 token.done = true;180 exports.checkRestTokens(iterator, 'end');181 break;182 }183 }184 }185 }...
jsonlite.js
Source:jsonlite.js
...18 t: '\t'19 };20 var text = source;21 var result = readValue();22 skipWhitespace();23 if(ch) {24 raiseError("Syntax error");25 }26 return result;27 function raiseError(m) {28 throw {29 name: 'SyntaxError',30 message: m,31 at: at,32 text: text33 };34 }35 function next(c) {36 if(c && c !== ch) {37 raiseError("Expected '" + c + "' instead of '" + ch + "'");38 }39 ch = text.charAt(at);40 at += 1;41 return ch;42 }43 function readString() {44 var s = '';45 if(ch === '"') {46 while(next()) {47 if(ch === '"') {48 next();49 return s;50 }51 if(ch === '\\') {52 next();53 if(ch === 'u') {54 var uffff = 0;55 for(var i = 0; i < 4; i += 1) {56 var hex = parseInt(next(), 16);57 if(!isFinite(hex)) {58 break;59 }60 uffff = uffff * 16 + hex;61 }62 s += String.fromCharCode(uffff);63 } else if(typeof escapee[ch] === 'string') {64 s += escapee[ch];65 } else {66 break;67 }68 } else {69 s += ch;70 }71 }72 }73 raiseError("Bad string");74 }75 function skipWhitespace() {76 while(ch && ch <= ' ') {77 next();78 }79 }80 function readWord() {81 var s = '';82 while(allowedInWord()) {83 s += ch;84 next();85 }86 if(s === "true") {87 return true;88 }89 if(s === "false") {90 return false;91 }92 if(s === "null") {93 return null;94 }95 if(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {96 return parseFloat(s);97 }98 return s;99 }100 function readArray() {101 var array = [];102 if(ch === '[') {103 next('[');104 skipWhitespace();105 if(ch === ']') {106 next(']');107 return array;108 }109 while(ch) {110 array.push(readValue());111 skipWhitespace();112 if(ch === ']') {113 next(']');114 return array;115 }116 next(',');117 skipWhitespace();118 }119 }120 raiseError("Bad array");121 }122 function readObject() {123 var o = {124 };125 if(ch === object_start) {126 next(object_start);127 skipWhitespace();128 if(ch === object_end) {129 next(object_end);130 return o;131 }132 while(ch) {133 var key = ch === '"' ? readString() : readWord();134 if(typeof key !== 'string') {135 raiseError('Bad object key: ' + key);136 }137 skipWhitespace();138 next(pair_seperator);139 if(Object.hasOwnProperty.call(o, key)) {140 raiseError('Duplicate key: "' + key + '"');141 }142 o[key] = readValue();143 skipWhitespace();144 if(ch === object_end) {145 next(object_end);146 return o;147 }148 next(',');149 skipWhitespace();150 }151 }152 raiseError("Bad object");153 }154 function readValue() {155 skipWhitespace();156 switch(ch) {157 case object_start:158 return readObject();159 case '[':160 return readArray();161 case '"':162 return readString();163 default:164 return readWord();165 }166 }167 function allowedInWord() {168 switch(ch) {169 case '"':...
modernizr-2.6.2.min.js
Source:modernizr-2.6.2.min.js
...18 t: '\t'19 };20 var text = source;21 var result = readValue();22 skipWhitespace();23 if(ch) {24 raiseError("Syntax error");25 }26 return result;27 function raiseError(m) {28 throw {29 name: 'SyntaxError',30 message: m,31 at: at,32 text: text33 };34 }35 function next(c) {36 if(c && c !== ch) {37 raiseError("Expected '" + c + "' instead of '" + ch + "'");38 }39 ch = text.charAt(at);40 at += 1;41 return ch;42 }43 function readString() {44 var s = '';45 if(ch === '"') {46 while(next()) {47 if(ch === '"') {48 next();49 return s;50 }51 if(ch === '\\') {52 next();53 if(ch === 'u') {54 var uffff = 0;55 for(var i = 0; i < 4; i += 1) {56 var hex = parseInt(next(), 16);57 if(!isFinite(hex)) {58 break;59 }60 uffff = uffff * 16 + hex;61 }62 s += String.fromCharCode(uffff);63 } else if(typeof escapee[ch] === 'string') {64 s += escapee[ch];65 } else {66 break;67 }68 } else {69 s += ch;70 }71 }72 }73 raiseError("Bad string");74 }75 function skipWhitespace() {76 while(ch && ch <= ' ') {77 next();78 }79 }80 function readWord() {81 var s = '';82 while(allowedInWord()) {83 s += ch;84 next();85 }86 if(s === "true") {87 return true;88 }89 if(s === "false") {90 return false;91 }92 if(s === "null") {93 return null;94 }95 if(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {96 return parseFloat(s);97 }98 return s;99 }100 function readArray() {101 var array = [];102 if(ch === '[') {103 next('[');104 skipWhitespace();105 if(ch === ']') {106 next(']');107 return array;108 }109 while(ch) {110 array.push(readValue());111 skipWhitespace();112 if(ch === ']') {113 next(']');114 return array;115 }116 next(',');117 skipWhitespace();118 }119 }120 raiseError("Bad array");121 }122 function readObject() {123 var o = {124 };125 if(ch === object_start) {126 next(object_start);127 skipWhitespace();128 if(ch === object_end) {129 next(object_end);130 return o;131 }132 while(ch) {133 var key = ch === '"' ? readString() : readWord();134 if(typeof key !== 'string') {135 raiseError('Bad object key: ' + key);136 }137 skipWhitespace();138 next(pair_seperator);139 if(Object.hasOwnProperty.call(o, key)) {140 raiseError('Duplicate key: "' + key + '"');141 }142 o[key] = readValue();143 skipWhitespace();144 if(ch === object_end) {145 next(object_end);146 return o;147 }148 next(',');149 skipWhitespace();150 }151 }152 raiseError("Bad object");153 }154 function readValue() {155 skipWhitespace();156 switch(ch) {157 case object_start:158 return readObject();159 case '[':160 return readArray();161 case '"':162 return readString();163 default:164 return readWord();165 }166 }167 function allowedInWord() {168 switch(ch) {169 case '"':...
index.js
Source:index.js
...13 token.assignment = false14 token.declaration = false15 if (type !== 'ident' && type !== 'builtin') continue16 idx = i + 117 skipWhitespace(+1)18 if (tokens[idx].type !== 'operator') continue19 if (!assignments[tokens[idx].data]) continue20 token.assignment = true21 }22 // Determine if a value is being defined, e.g.23 // float x;24 // float x, y, z;25 // float x, y = vec3(sin(1.0 + 3.0)), z;26 // float[3][2] x, y = vec3(sin(1.0 + 3.0)), z;27 // float[][2] x, y = vec3(sin(1.0 + 3.0)), z;28 // float x[2], y = vec3(sin(1.0 + 3.0)), z[4];29 // float x(float y, float z) {};30 // float x(float y[2], Thing[3] z) {};31 // Thing x[2], y = Another(sin(1.0 + 3.0)), z[4];32 for (var i = 0; i < tokens.length; i++) {33 var datatype = tokens[i]34 var type = datatype.type35 var data = datatype.data36 datatype.declaration = false37 if (type === 'keyword') {38 if (ignoredKeywords[data]) continue39 } else40 if (type !== 'ident') continue41 idx = i + 142 skipArrayDimensions()43 if (tokens[idx].type !== 'ident') continue44 tokens[idx++].declaration = true45 skipArrayDimensions()46 // Function arguments/parameters47 if (tokens[idx].data === '(') {48 idx++49 skipWhitespace(+1)50 while (tokens[idx] && tokens[idx].data !== ')') {51 if (tokens[idx].type !== 'keyword' && tokens[idx].type !== 'ident') break52 idx++53 skipWhitespace(+1)54 if (tokens[idx].type !== 'ident') continue55 tokens[idx++].declaration = true56 skipWhitespace(+1)57 skipArrayDimensions()58 skipWhitespace(+1)59 if (tokens[idx].data !== ',') continue60 idx++61 skipWhitespace(+1)62 }63 i = idx64 continue65 }66 // Declaration Lists67 while (tokens[idx] && tokens[idx].data !== ';') {68 if (tokens[idx].data === ',') {69 idx++70 skipWhitespace(+1)71 if (tokens[idx].declaration = tokens[idx].type === 'ident') idx++72 } else {73 skipWhitespace(+1)74 skipParens()75 skipWhitespace(+1)76 idx++77 }78 }79 i = idx80 }81 // Handle struct declarations:82 // struct declaration {83 // float x, y, z;84 // Other w;85 // } declaration;86 for (var i = 0; i < tokens.length; i++) {87 var token = tokens[i]88 if (token.type !== 'keyword') continue89 if (token.data !== 'struct') continue90 idx = i + 191 skipWhitespace(+1)92 if (tokens[idx].type !== 'ident') continue93 idx++94 skipWhitespace(+1)95 if (tokens[idx++].data !== '{') continue96 skipWhitespace(+1)97 while (tokens[idx].type === 'ident' || tokens[idx].type === 'keyword') {98 do {99 idx++100 skipWhitespace(+1)101 tokens[idx].structMember = true102 tokens[idx].declaration = false103 idx++104 skipArrayDimensions()105 } while (tokens[idx].data === ',')106 if (tokens[idx].data === ';') idx++107 skipWhitespace()108 }109 idx++110 skipWhitespace(+1)111 if (tokens[idx].type !== 'ident') continue112 tokens[idx].declaration = true113 skipWhitespace(+1)114 while (tokens[++idx].data === ',') {115 skipWhitespace(+1)116 idx++117 skipWhitespace(+1)118 if (tokens[idx].type === 'ident') tokens[idx].declaration = true119 skipWhitespace(+1)120 }121 }122 return tokens123 function skipWhitespace(n) {124 while (tokens[idx] && tokens[idx].type === 'whitespace') idx++125 }126 function skipArrayDimensions() {127 while (tokens[idx] && (128 tokens[idx].type === 'integer'129 || tokens[idx].data === '['130 || tokens[idx].data === ']'131 || tokens[idx].type === 'whitespace'132 )) idx++133 }134 function skipParens() {135 if (!tokens[idx]) return136 if (tokens[idx].data !== '(') return137 var depth = 0...
JSON.js
Source:JSON.js
1const CustomJSON={2 parse:function(str,options) {3 this.str = str;4 this.i = 0;5 this.nfirstchars = '-0123456789.';6 this.nchars = '-0123456789.eE';7 this.n8 const res =this.parseValue();9 console.log(res);10 return res;11 },12 isWhiteSpace:function(c) {13 return c==' ' || c=='\r' || c=='\n' || c=='\t';14 },15 skipWhiteSpace:function() {16 while(this.i < this.str.length && this.isWhiteSpace(this.str.charAt(this.i))) this.i++;17 },18 parseValue:function() {19 this.skipWhiteSpace();20 if (this.i == this.str.length) return null;21 var c = this.str.charAt(this.i);22 console.log(c, this.i);23 if (c == '{') return this.parseObject();24 else if (c == '[') return this.parseArray();25 else if (c == '\"') return this.parseString('\"');26 else if (c == '\'') return this.parseString('\'');27 else if (this.nfirstchars.indexOf(c) != -1) return this.parseNumber();28 else if (c == 't') return this.parseLiteral('true',true);29 else if (c == 'f') return this.parseLiteral('false',false);30 else if (c == 'n') return this.parseLiteral('null',null);31 else if (c == 'N') return this.parseLiteral('NaN',NaN);32 else if (c == 'u') return this.parseLiteral('undefined',undefined);33 throw "Invalid json";34 },35 parseLiteral:function(literal, value) {36 if (literal.length > this.str.length - this.i) {37 throw "Expecting " + literal;38 }39 for (var i = 0; i < literal.length; i++) { 40 if (literal.charAt(i) != this.str.charAt(this.i++)) {41 throw "Expecting " + literal;42 }43 }44 return value;45 },46 parseNumber:function() {47 var startIndex = this.i;48 var c;49 while(this.nchars.indexOf(this.str.charAt(this.i)) != -1) {50 this.i++;51 }52 return new Number(this.str.substring(startIndex,this.i));53 },54 parseString:function(quote) {55 var startIndex = ++this.i;56 var c;57 var s = '';58 while((c = this.str.charAt(this.i)) != quote) {59 if (c == '\\') {60 this.i++;61 c = this.str.charAt(this.i);62 if (c == 'r') s += '\r';63 else if (c == 'n') s += '\n';64 else if (c == 't') s += '\t';65 else if (c == 'f') s += '\f';66 // Note escaped unicode not handled67 else s += c;68 } else s += c;69 this.i++;70 }71 this.i++;72 return s;73 },74 parseObject:function() {75 this.i++;76 this.skipWhiteSpace();77 if (this.str.charAt(this.i) == '}') { 78 this.i++;79 return {};80 }81 var o = {};82 var c;83 while(true) {84 var name = this.parseValue();85 console.log('key ',name);86 this.skipWhiteSpace();87 c = this.str.charAt(this.i);88 if (c != ':') throw "Expecting :";89 this.i++;90 this.skipWhiteSpace();91 var value = this.parseValue();92 console.log('value ', value);93 this.skipWhiteSpace();94 o[name] = value;95 c = this.str.charAt(this.i);96 if (c == ',') {97 this.i++;98 this.skipWhiteSpace();99 } else break;100 }101 if ( c != '}' ) {102 Response.Write(this.str.substring(this.i));103 throw "Expecting }";104 }105 this.i++;106 return o;107 },108 parseArray:function() {109 this.i++;110 this.skipWhiteSpace();111 if (this.str.charAt(this.i) == ']') {112 this.i++;113 return [];114 }115 var a = [];116 var c;117 while(true) {118 var value = this.parseValue();119 a.push(value);120 this.skipWhiteSpace();121 c = this.str.charAt(this.i);122 if (c == ',') {123 this.i++;124 this.skipWhiteSpace();125 } else break;126 }127 if ( c != ']' ) {128 throw "Expecting ]";129 }130 this.i++;131 return a;132 }
...
Using AI Code Generation
1const { skipWhitespace } = require('playwright/lib/utils/whitespace');2const { readFileSync } = require('fs');3const { join } = require('path');4const html = readFileSync(join(__dirname, 'test.html'), 'utf8');5console.log(skipWhitespace(html));6const { skipWhitespace } = require('playwright/lib/utils/whitespace');7const { readFileSync } = require('fs');8const { join } = require('path');9const html = readFileSync(join(__dirname, 'test.html'), 'utf8');10console.log(skipWhitespace(html));11const { skipWhitespace } = require('playwright/lib/utils/whitespace');12const { readFileSync } = require('fs');13const { join } = require('path');14const html = readFileSync(join(__dirname, 'test.html'), 'utf8');15console.log(skipWhitespace(html));16const { skipWhitespace } = require('playwright/lib/utils/whitespace');17const { readFileSync } = require('fs');18const { join } = require('path');19const html = readFileSync(join(__dirname, 'test.html'), 'utf8');20console.log(skipWhitespace(html));
Using AI Code Generation
1const { skipWhitespace } = require('playwright/lib/utils/lineWrapper');2const { test } = require('@playwright/test');3test('skipWhitespace', async ({ page }) => {4 World`;5 console.log(skipWhitespace(text, 0));6 console.log(skipWhitespace(text, 1));7 console.log(skipWhitespace(text, 2));8 console.log(skipWhitespace(text, 3));9 console.log(skipWhitespace(text, 4));10 console.log(skipWhitespace(text, 5));11 console.log(skipWhitespace(text, 6));12 console.log(skipWhitespace(text, 7));13 console.log(skipWhitespace(text, 8));14 console.log(skipWhitespace(text, 9));15 console.log(skipWhitespace(text, 10));16 console.log(skipWhitespace(text, 11));17 console.log(skipWhitespace(text, 12));18 console.log(skipWhitespace(text, 13));19 console.log(skipWhitespace(text, 14));20 console.log(skipWhitespace(text, 15));21 console.log(skipWhitespace(text, 16));22 console.log(skipWhitespace(text, 17));23 console.log(skipWhitespace(text, 18));24 console.log(skipWhitespace(text, 19));25 console.log(skipWhitespace(text, 20));26 console.log(skipWhitespace(text, 21));27 console.log(skipWhitespace(text, 22));28 console.log(skipWhitespace(text, 23));29 console.log(skipWhitespace(text, 24));30 console.log(skipWhitespace(text, 25));31 console.log(skipWhitespace(text, 26));32 console.log(skipWhitespace(text, 27));33 console.log(skipWhitespace(text, 28));34 console.log(skipWhitespace(text, 29));35 console.log(skipWhitespace(text, 30));36 console.log(skipWhitespace(text, 31));37 console.log(skipWhitespace(text, 32));38 console.log(skipWhitespace(text, 33));39 console.log(skipWhitespace(text, 34));40 console.log(skipWhitespace(text, 35));41 console.log(skipWhitespace(text, 36));42 console.log(skipWhitespace(text, 37));43 console.log(skipWhitespace(text, 38));44 console.log(skipWhitespace(text, 39));45 console.log(skipWhitespace(text, 40));46 console.log(skipWhitespace(text, 41));47 console.log(skipWhitespace(text, 42));48 console.log(skipWhitespace(text
Using AI Code Generation
1const { skipWhitespace } = require('playwright/lib/utils');2const input = ' hello world';3console.log(skipWhitespace(input, 0, 3));4const { skipWhitespace } = require('playwright/lib/utils');5const input = 'hello world';6console.log(skipWhitespace(input, 0, 0));7const { skipWhitespace } = require('playwright/lib/utils');8const input = 'hello world';9console.log(skipWhitespace(input, 0, 5));10const { skipWhitespace } = require('playwright/lib/utils');11const input = 'hello world';12console.log(skipWhitespace(input, 0, 10));13const { skipWhitespace } = require('playwright/lib/utils');14const input = 'hello world';15console.log(skipWhitespace(input, 0, 11));16const { skipWhitespace } = require('playwright/lib/utils');17const input = 'hello world';18console.log(skipWhitespace(input, 0, 12));19const { skipWhitespace } = require('playwright/lib/utils');20const input = 'hello world';21console.log(skipWhitespace(input, 0, 13));22const { skipWhitespace } = require('playwright/lib/utils');23const input = 'hello world';24console.log(skipWhitespace(input, 0, 14));25const { skipWhitespace } = require('playwright/lib/utils');26const input = 'hello world';27console.log(skipWhitespace(input, 0, 15));28const { skipWhitespace } = require('playwright/lib/utils');29const input = 'hello world';30console.log(skipWhitespace(input, 0, 16));31const { skipWhitespace } = require('playwright/lib/utils');32const input = 'hello world';33console.log(skipWhitespace(input, 0, 17));
Using AI Code Generation
1const { skipWhitespace } = require('@playwright/test/lib/utils/terminalUtils');2const str = ' hello world';3const skipped = skipWhitespace(str);4console.log(skipped);5const { skipWhitespace } = require('@playwright/test/lib/utils/terminalUtils');6const str = ' hello world';7const skipped = skipWhitespace(str);8console.log(skipped);9const { currentTestInfo } = require('@playwright/test/lib/utils/testInfo');10const testInfo = currentTestInfo();11console.log(testInfo.title);12const { currentTestInfo } = require('@playwright/test/lib/utils/testInfo');13const testInfo = currentTestInfo();14console.log(testInfo.title);15const { currentTestInfo } = require('@playwright/test/lib/utils/testInfo');16const testInfo = currentTestInfo();17console.log(testInfo.status);18const { currentTestInfo } = require('@playwright/test/lib/utils/testInfo');19const testInfo = currentTestInfo();20console.log(testInfo.status
Using AI Code Generation
1const { skipWhitespace } = require('playwright/lib/protocol/serializers');2const input = "Hello World";3const output = skipWhitespace(input);4console.log(output);5isWhitespace(char: string): boolean;6const { isWhitespace } = require('playwright/lib/protocol/serializers');7const output = isWhitespace(" ");8console.log(output);9isLineTerminator(char: string): boolean;10const { isLineTerminator } = require('playwright/lib/protocol/serializers');11const output = isLineTerminator("12");13console.log(output);14isDigit(char: string): boolean;15const { isDigit } = require('playwright/lib/protocol/serializers');16const output = isDigit("1");17console.log(output);18isHexDigit(char: string): boolean;19const { isHexDigit } = require('playwright/lib/protocol/serializers');20const output = isHexDigit("A");21console.log(output);22isIdentifierStart(char: string): boolean;23const { isIdentifierStart } = require('playwright/lib/protocol/serializers');24const output = isIdentifierStart("A");25console.log(output);
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!