import { toCamelCaseFromTokens, toCamelCase } from '../string'; import { Token, TokenGroup } from '../../types/Token'; import { ScriptLineDescriptor } from '../../types/script/ScriptLineDescriptor'; import { parseAjax } from './methods/ajax'; import { parseReturn } from './methods/return'; import { CodeSection } from '../../types/CodeSection'; import { parseAlert } from './methods/alert'; import { parsePrompt } from './methods/prompt'; import { parseStore } from './methods/store'; import { parseIf } from './methods/if'; import { ScriptDescriptor } from '../../types/script/ScriptDescriptor'; export function processScript( body: any[] ): CodeSection { const head = body[0]; const lines = body[1]; let functionNameSymbols = []; let inName = false; for( let i=0; i < head.length; i++ ) { if( inName && head[i].group !== TokenGroup.Custom ) { break; } if( !inName && head[i].group === TokenGroup.Custom ) { inName = true; } if( inName ) { functionNameSymbols.push( head[i] ); } } const functionName = toCamelCaseFromTokens( functionNameSymbols ); const inputs = getInputVariables( head ); return { type: 'script', content: { name: functionName, inputs, isAsync: lines.some( ( line: ScriptLineDescriptor ) => line.isAsync ), lines: generateBlock( lines ) } as ScriptDescriptor } } function generateBlock( lines: any ) { let block = []; let children: ScriptLineDescriptor[] = []; for( let i=0; i < lines.length; i++ ) { const hasChildren = lines.length > i + 1 && lines[i + 1][0]['length']; const method = lines[i].find( (x: Token) => x.group === 'script' ); if( !method ) continue; if( hasChildren ) { children = generateBlock( lines[i + 1] ); } const generated = generateOperation( method.key, lines[i], children ); if( generated ) { block.push( generated ); } if( hasChildren ) { i++; } } return block; } function generateOperation( opType: string, tokens: Token[], content: ScriptLineDescriptor[] ) { switch( opType ) { case 'alert': return parseAlert( tokens ); case 'prompt': return parsePrompt( tokens ); case 'store': return parseStore( tokens ); case 'if': return parseIf( tokens, content ); case 'ajax': return parseAjax( tokens ); case 'return': return parseReturn( tokens ); } return null; } /** * Locates the parameters defined in a function descriptor. Obeys comma separation and camal cases in between */ function getInputVariables( tokens: Token[] ) { const refIndex = tokens.findIndex( x => x.group === TokenGroup.Generic && x.section === 'reference' ); if( refIndex === -1 ) return []; let inputs = []; for( let i = refIndex + 1; i < tokens.length; i++ ) { const token = tokens[ i ]; if( token.group !== TokenGroup.Custom ) break; inputs.push( token.key ); } type NullableString = string | null; const nameGroups = inputs.reduce( ( all: NullableString[], word: string ) => { if( word.indexOf(',') === word.length - 1 ) { return [ ...all, word.substr( 0, word.length - 1 ), null ]; } else { return [ ...all, word ]; } }, [] ); inputs = []; let tempName: string[] = []; for( let i =0; i < nameGroups.length; i++ ) { if( nameGroups[i] === null && tempName.length > 0 ) { inputs.push( toCamelCase( tempName ) ); tempName = []; } else { tempName.push( nameGroups[ i ] as string ); } } if( tempName.length > 0 ) { inputs.push( toCamelCase( tempName ) ); } return inputs; }