import {abstract, copyWithoutDuplicates} from './common.js';import * as pexprs from './pexprs-main.js';
function isRestrictedJSIdentifier(str) { return /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str);}
function resolveDuplicatedNames(argumentNameList) { const count = Object.create(null); argumentNameList.forEach(argName => { count[argName] = (count[argName] || 0) + 1; });
Object.keys(count).forEach(dupArgName => { if (count[dupArgName] <= 1) { return; }
let subscript = 1; argumentNameList.forEach((argName, idx) => { if (argName === dupArgName) { argumentNameList[idx] = argName + '_' + subscript++; } }); });}
pexprs.PExpr.prototype.toArgumentNameList = abstract('toArgumentNameList');
pexprs.any.toArgumentNameList = function(firstArgIndex, noDupCheck) { return ['any'];};
pexprs.end.toArgumentNameList = function(firstArgIndex, noDupCheck) { return ['end'];};
pexprs.Terminal.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { if (typeof this.obj === 'string' && /^[_a-zA-Z0-9]+$/.test(this.obj)) { return ['_' + this.obj]; } else { return ['$' + firstArgIndex]; }};
pexprs.Range.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { let argName = this.from + '_to_' + this.to; if (!isRestrictedJSIdentifier(argName)) { argName = '_' + argName; } if (!isRestrictedJSIdentifier(argName)) { argName = '$' + firstArgIndex; } return [argName];};
pexprs.Alt.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { const termArgNameLists = this.terms.map(term => term.toArgumentNameList(firstArgIndex, true), );
const argumentNameList = []; const numArgs = termArgNameLists[0].length; for (let colIdx = 0; colIdx < numArgs; colIdx++) { const col = []; for (let rowIdx = 0; rowIdx < this.terms.length; rowIdx++) { col.push(termArgNameLists[rowIdx][colIdx]); } const uniqueNames = copyWithoutDuplicates(col); argumentNameList.push(uniqueNames.join('_or_')); }
if (!noDupCheck) { resolveDuplicatedNames(argumentNameList); } return argumentNameList;};
pexprs.Seq.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { let argumentNameList = []; this.factors.forEach(factor => { const factorArgumentNameList = factor.toArgumentNameList(firstArgIndex, true); argumentNameList = argumentNameList.concat(factorArgumentNameList);
firstArgIndex += factorArgumentNameList.length; }); if (!noDupCheck) { resolveDuplicatedNames(argumentNameList); } return argumentNameList;};
pexprs.Iter.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { const argumentNameList = this.expr .toArgumentNameList(firstArgIndex, noDupCheck) .map(exprArgumentString => exprArgumentString[exprArgumentString.length - 1] === 's' ? exprArgumentString + 'es' : exprArgumentString + 's', ); if (!noDupCheck) { resolveDuplicatedNames(argumentNameList); } return argumentNameList;};
pexprs.Opt.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { return this.expr.toArgumentNameList(firstArgIndex, noDupCheck).map(argName => { return 'opt' + argName[0].toUpperCase() + argName.slice(1); });};
pexprs.Not.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { return [];};
pexprs.Lookahead.prototype.toArgumentNameList = pexprs.Lex.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { return this.expr.toArgumentNameList(firstArgIndex, noDupCheck); };
pexprs.Apply.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { return [this.ruleName];};
pexprs.UnicodeChar.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { return ['$' + firstArgIndex];};
pexprs.Param.prototype.toArgumentNameList = function(firstArgIndex, noDupCheck) { return ['param' + this.index];};