Best JavaScript code snippet using wpt
parser.js
Source:parser.js
...61 }62 function consumeKeyword(expected) {63 return consume('keyword', expected);64 }65 function consumeIdentifier(category, isDefinition) {66 const token = consume('identifier', null);67 if (isDefinition) {68 if (category === 'class') {69 currentClassSymbolTable = symbolTable(token.value);70 } else if (category === 'subroutine') {71 currentSubroutineSymbolTable = symbolTable(token.value);72 // kinda hacky but it's ok because it's never used73 if (mostRecentSubroutineType.value === 'method') {74 currentSubroutineSymbolTable.add('this', 'object', 'argument');75 }76 } else if (category === 'static' || category === 'field') {77 currentClassSymbolTable.add(token.value, mostRecentTypeDefinition, category);78 } else {79 currentSubroutineSymbolTable.add(token.value, mostRecentTypeDefinition, category);80 }81 } else {82 let symbol = getSymbol(token.value);83 Object.assign(token, symbol);84 }85 return token;86 }87 function consumeIdentifierOrThis(category) {88 if (nextTypeIs('keyword')) {89 return consumeKeyword('this');90 } else {91 return consumeIdentifier(category, false);92 }93 }94 function consumeSymbol(expected) {95 return consume('symbol', expected);96 }97 function consumeType() {98 const type = consumeAlternatives([99 { type: 'keyword', value: returnTypes },100 { type: 'identifier', value: null }101 ]);102 mostRecentTypeDefinition = type.value;103 return type;104 }105 function zeroOrMoreTimes(func) {106 let output = [];107 // As long as func returns a result, add it to the output108 // Once func returns null, return the array of output109 while(true) {110 let nextResult = func();111 if (nextResult) {112 if (Array.isArray(nextResult)) {113 output = output.concat(nextResult);114 } else {115 output.push(nextResult);116 }117 } else {118 return output;119 }120 }121 }122 // (, func())*123 function consumeZeroOrMoreListItems(func) {124 return zeroOrMoreTimes(() => {125 if (nextValueIs(',')) {126 return [127 consumeSymbol(','),128 ...func()129 ];130 } else {131 return null;132 }133 });134 }135 function consumeVarDec(type, varTypes) {136 if (nextValueIsOneOf(varTypes)) {137 let varType = getToken().value;138 return {139 type: type,140 content: [141 consumeKeyword(varTypes),142 consumeType(),143 consumeIdentifier(varType, true),144 ...consumeZeroOrMoreListItems(() =>145 [146 consumeIdentifier(varType, true)147 ]148 ),149 consumeSymbol(';')150 ]151 };152 } else {153 return null;154 }155 }156 function consumeClassVarDec() {157 return consumeVarDec('classVarDec', ['static', 'field']);158 }159 function consumeSubroutineVarDec() {160 return consumeVarDec('varDec', ['var']);161 }162 function consumeParameterList() {163 function consumeParameterListContent() {164 // empty parameter list165 if (nextValueIs(')')) {166 return [];167 } else {168 return [169 consumeType(),170 consumeIdentifier('argument', true),171 ...consumeZeroOrMoreListItems(() =>172 [173 consumeType(),174 consumeIdentifier('argument', true)175 ]176 )177 ];178 }179 }180 return {181 type: 'parameterList',182 content: consumeParameterListContent()183 }184 }185 function consumeOptionalArraySubscript() {186 if (nextValueIs('[')) {187 return [188 consumeSymbol('['),189 consumeExpression(),190 consumeSymbol(']')191 ];192 } else {193 return [];194 }195 }196 function consumeSubroutineCall() {197 function consumeOptionalMethodDereference() {198 if (nextValueIs('.')) {199 return [200 consumeSymbol('.'),201 consumeIdentifier('subroutine', false)202 ];203 } else {204 return [];205 }206 }207 return [208 consumeIdentifier(null, false),209 ...consumeOptionalMethodDereference(),210 consumeSymbol('('),211 consumeExpressionList(),212 consumeSymbol(')')213 ]214 }215 function consumeUnaryTerm() {216 return [217 consumeSymbol(unaryOperators),218 consumeTerm()219 ];220 }221 function consumeArrayAccess() {222 return [223 consumeIdentifier(null, false),224 ...consumeOptionalArraySubscript()225 ];226 }227 function consumeParenthesizedExpression() {228 return [229 consumeSymbol('('),230 consumeExpression(),231 consumeSymbol(')')232 ];233 }234 function consumeTerm() {235 function consumeTermContent() {236 if (nextValueIsOneOf(unaryOperators)) {237 return consumeUnaryTerm();238 } else if (nextTypeIs('integerConstant')) {239 return [consume('integerConstant', null)];240 } else if (nextTypeIs('stringConstant')) {241 return [consume('stringConstant', null)];242 } else if (nextValueIsOneOf(['true', 'false', 'null', 'this'])) {243 return [consumeKeyword(null)];244 } else if (nextValueIs('(')) {245 return consumeParenthesizedExpression();246 } else if (nextTypeIs('identifier') || nextValueIs('this')) {247 if (peekToken().value === '[') {248 return consumeArrayAccess();249 } else if (peekToken().value === '.' || peekToken().value === '(') {250 return consumeSubroutineCall();251 } else {252 return [consumeIdentifierOrThis(null)];253 }254 } else {255 throw new Error('Unknown term');256 }257 }258 return {259 type: 'term',260 content: consumeTermContent()261 };262 }263 function consumeOperator() {264 return consumeSymbol(binaryOperators);265 }266 function consumeExpression() {267 function consumeOptionalOperatorAndTerm() {268 if (nextValueIsOneOf(binaryOperators)) {269 return [270 consumeOperator(),271 consumeExpression()272 ];273 } else {274 return [];275 }276 }277 return {278 type: 'expression',279 content: [280 consumeTerm(),281 ...consumeOptionalOperatorAndTerm()282 ]283 };284 }285 function consumeExpressionList() {286 function consumeExpressionListContent() {287 if (nextValueIs(')')) {288 return []289 } else {290 return [291 consumeExpression(),292 ...consumeZeroOrMoreListItems(() => [ consumeExpression() ])293 ]294 }295 }296 return {297 type: 'expressionList',298 content: consumeExpressionListContent()299 };300 }301 function consumeLetStatement() {302 return {303 type: 'letStatement',304 content: [305 consumeKeyword('let'),306 consumeIdentifier('var', false),307 ...consumeOptionalArraySubscript(),308 consumeSymbol('='),309 consumeExpression(),310 consumeSymbol(';')311 ]312 };313 }314 function consumeIfStatement() {315 function consumeOptionalElseClause() {316 if (nextValueIs('else')) {317 return [318 consumeKeyword('else'),319 consumeSymbol('{'),320 consumeStatements(),321 consumeSymbol('}')322 ];323 } else {324 return [];325 }326 }327 return {328 type: 'ifStatement',329 content: [330 consumeKeyword('if'),331 consumeSymbol('('),332 consumeExpression(),333 consumeSymbol(')'),334 consumeSymbol('{'),335 consumeStatements(),336 consumeSymbol('}'),337 ...consumeOptionalElseClause()338 ]339 };340 }341 function consumeWhileStatement() {342 return {343 type: 'whileStatement',344 content: [345 consumeKeyword('while'),346 consumeSymbol('('),347 consumeExpression(),348 consumeSymbol(')'),349 consumeSymbol('{'),350 consumeStatements(),351 consumeSymbol('}'),352 ]353 };354 }355 function consumeDoStatement() {356 return {357 type: 'doStatement',358 content: [359 consumeKeyword('do'),360 ...consumeSubroutineCall(),361 consumeSymbol(';')362 ]363 };364 }365 function consumeReturnStatement() {366 function consumeOptionalReturnExpression() {367 if (nextValueIs(';')) {368 return [];369 } else {370 return [371 consumeExpression(),372 ];373 }374 }375 return {376 type: 'returnStatement',377 content: [378 consumeKeyword('return'),379 ...consumeOptionalReturnExpression(),380 consumeSymbol(';')381 ]382 };383 }384 function consumeStatement() {385 const statementTypes = {386 'let': consumeLetStatement,387 'if': consumeIfStatement,388 'while': consumeWhileStatement,389 'do': consumeDoStatement,390 'return': consumeReturnStatement391 };392 const consumeNextStatement = statementTypes[getToken().value];393 if (consumeNextStatement) {394 return consumeNextStatement();395 } else {396 return null;397 }398 }399 function consumeStatements() {400 return {401 type: 'statements',402 content: zeroOrMoreTimes(consumeStatement)403 }404 }405 function consumeSubroutineBody() {406 return {407 type: 'subroutineBody',408 content: [409 consumeSymbol('{'),410 ...zeroOrMoreTimes(consumeSubroutineVarDec),411 consumeStatements(),412 consumeSymbol('}')413 ]414 };415 }416 function consumeSubroutineDec() {417 const subroutineTypes = ['constructor', 'function', 'method'];418 function consumeSubroutineReturnType() {419 return consumeAlternatives([420 { type: 'keyword', value: returnTypes },421 { type: 'identifier', value: null }422 ]);423 }424 if (nextValueIsOneOf(subroutineTypes)) {425 mostRecentSubroutineType = consumeKeyword(subroutineTypes);426 return {427 type: 'subroutineDec',428 content: [429 mostRecentSubroutineType,430 consumeSubroutineReturnType(),431 consumeIdentifier('subroutine', true),432 consumeSymbol('('),433 consumeParameterList(),434 consumeSymbol(')'),435 consumeSubroutineBody()436 ]437 };438 } else {439 return null;440 }441 }442 function consumeClass() {443 const cls = {444 type: 'class',445 content: [446 consumeKeyword('class'),447 consumeIdentifier('class', true),448 consumeSymbol('{'),449 ...zeroOrMoreTimes(consumeClassVarDec),450 ...zeroOrMoreTimes(consumeSubroutineDec),451 consumeSymbol('}'),452 ]453 };454 cls.symbolTable = currentClassSymbolTable.table;455 return cls;456 }457 let parseTree = [consumeClass()];458 return parseTree;459}...
tokenize.ts
Source:tokenize.ts
...36 } else if (DELIMS.includes(c)) {37 tokens.push(c);38 i++;39 } else if (SIGILS.includes(c)) {40 const j = consumeIdentifier(line, i + 1);41 tokens.push(`${line[i]}${line.slice(i + 1, j)}`);42 i = j;43 }44 switch (line[i]) {45 case "#":46 break loop;47 case ";":48 tokens.push(";");49 i++;50 break;51 case "$":52 case "@":53 case "%":54 const j = consumeIdentifier(line, i + 1);55 tokens.push(`${line[i]}${line.slice(i + 1, j)}`);56 i = j;57 break;58 case ",":59 tokens.push(",");60 i += 1;61 break;62 case "(":63 tokens.push("(");64 i += 1;65 break;66 case ")":67 tokens.push(")");68 i += 1;69 break;70 case "[":71 tokens.push("[");72 i += 1;73 break;74 case "]":75 tokens.push("]");76 i += 1;77 break;78 case "{":79 tokens.push("{");80 i += 1;81 break;82 case "}":83 tokens.push("}");84 i += 1;85 break;86 }87 }88 return tokens;89}90// processLineByLine();91function startsWithDigit(s: string) {92 return s.charCodeAt(0) >= 48 && s.charCodeAt(0) < 58;93};94function startsWithLetter(s: string) {95 const c = s.charCodeAt(0);96 return (c >= 65 && c < 91) || (c >= 97 && c < 123);97};98function consumeIdentifier(s: string, i: number) {99 let j: number;100 for (j = i; j < s.length; j++) {101 if (!startsWithDigit(s[j]) && !startsWithLetter(s[j]) && s[j] !== "_") {102 break;103 }104 }105 return j;106}107let line = ";; ($my_ident ) [@hi_mom123; ]{}%hash; $";108let i = 0;109let tokens = tokenizeLine(line);...
mention.ts
Source:mention.ts
...6 const { pos } = state;7 if (state.consume(Codes.At)) {8 // РазÑеÑаем поглоÑиÑÑ ÑамоÑÑоÑÑелÑнÑй Ñимвол `@`, ÑÑÐ¾Ð±Ñ Ð¿Ð¾ÐºÐ°Ð·ÑваÑÑ9 // его в ÑедакÑоÑе и пÑи необÑ
одимоÑÑи вÑвеÑÑи авÑокомплиÑ10 if (consumeIdentifier(state) || isDelimiter(state.peek())) {11 state.push({12 type: TokenType.Mention,13 format: TokenFormat.None,14 value: state.substring(pos),15 });16 return true;17 }18 }19 state.pos = pos;20 }21 return false;...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.getLocations(function(err, data) {4 if (err) return console.error(err);5 console.dir(data);6});7var wpt = require('webpagetest');8var wpt = new WebPageTest('www.webpagetest.org');9wpt.getLocations().then(function(data) {10 console.dir(data);11}).catch(function(err) {12 console.error(err);13});14var wpt = require('webpagetest');15var wpt = new WebPageTest('www.webpagetest.org');16try {17 const data = await wpt.getLocations();18 console.dir(data);19} catch (err) {20 console.error(err);21}
Using AI Code Generation
1var fs = require('fs');2var wptokenizer = require('wptokenizer');3var tokenizer = new wptokenizer();4var stream = fs.createReadStream('enwiki-latest-pages-articles.xml');5var wikiStream = tokenizer.createWikiStream(stream);6var count = 0;7var title = '';8var text = '';9wikiStream.on('data', function(data) {10 if (data.type === 'page') {11 title = data.title;12 text = data.text;13 }14 if (data.type === 'wikitext') {15 var tokens = tokenizer.tokenize(data.text);16 for (var i = 0; i < tokens.length; i++) {17 if (tokens[i].type === 'identifier') {18 count++;19 }20 }21 }22});23wikiStream.on('end', function() {24 console.log('Total number of identifiers in the stream: ' + count);25});26var fs = require('fs');27var wptokenizer = require('wptokenizer');28var tokenizer = new wptokenizer();29var stream = fs.createReadStream('enwiki-latest-pages-articles.xml');30var wikiStream = tokenizer.createWikiStream(stream);31wikiStream.on('data', function(data) {32 if (data.type === 'page') {33 console.log('Title: ' + data.title);34 console.log('Text: ' + data.text);35 }36});37wikiStream.on('end', function() {38 console.log('End of stream');39});
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.consumeIdentifier(url, function(err, data) {3 if(err) {4 console.log(err);5 } else {6 console.log(data);7 }8});
Using AI Code Generation
1var fs = require('fs');2var wptokenizer = require('wptokenizer');3var tokenizer = new wptokenizer();4var file = fs.readFileSync("test.txt", "utf8");5tokenizer.consume(file);6var token = tokenizer.consumeIdentifier();7console.log(token);
Using AI Code Generation
1var fs = require('fs');2var WPTokenizer = require('./wptokenizer.js');3var tokenizer = new WPTokenizer(fs.readFileSync('test.html', 'utf8'));4var token;5while (token = tokenizer.next()) {6 console.log(token);7}
Using AI Code Generation
1var fs = require('fs');2var WPTokenizer = require('./wptokenizer.js');3var tokenizer = new WPTokenizer();4var text = fs.readFileSync('./test2.txt', 'utf8');5tokenizer.consume(text);6var tokens = tokenizer.getTokens();7console.log(tokens);
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('Albert Einstein');3wp.getTemplate('Infobox scientist').then(function(result) {4 console.log(result);5});6var wptools = require('wptools');7var wp = new wptools('Albert Einstein');8wp.getTemplate('Infobox scientist').then(function(result) {9 console.log(result);10});11### wptools#getTemplate(template, callback)12**Kind**: instance method of <code>[wptools](#module_wptools)</code> 13var wptools = require('wptools');14var wp = new wptools('Albert Einstein');15wp.getTemplate('Infobox scientist', function(err, result) {16 console.log(result);17});18var wptools = require('wptools');19var wp = new wptools('Albert Einstein');20wp.getTemplate('Infobox scientist', function(err, result) {21 console.log(result);22});23### wptools#getTemplates(callback)24**Kind**: instance method of <code>[wptools](#module_wptools)</code> 25var wptools = require('wptools');26var wp = new wptools('Albert Einstein');27wp.getTemplates(function(err, result) {28 console.log(result);29});30var wptools = require('wptools');31var wp = new wptools('Albert Einstein');32wp.getTemplates(function(err, result) {
Using AI Code Generation
1var fs = require('fs');2var tokenizer = require('./wptokenizer.js');3var token = new tokenizer();4var file = fs.readFileSync('test.html','utf8');5token.tokenize(file);6var tokenArray = token.getTokenArray();7var identifierArray = [];8for(var i=0;i<tokenArray.length;i++)9{10 if(tokenArray[i].type == 'identifier')11 {12 identifierArray.push(tokenArray[i]);13 }14}15console.log(identifierArray);16var identifierSet = new Set();17for(var i=0;i<identifierArray.length;i++)18{19 identifierSet.add(identifierArray[i].value);20}21identifierArray = Array.from(identifierSet);22console.log(identifierArray);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!