Tuesday, October 02, 2007

Updated Varscoper.cfc CFSCRIPT Support

Here's an updated version of my varscoper mods. Dan Wilson found a bug with the way I was handling the // comments.

The code will now strip all // comments and will also correctly handle code with Unix line endings (I convert windows style CRLF to LF and process the code based only on LF)

Mike Schierberl, the author of Varscoper replied and said he will add my code to the code base on Riaforge when he finds some time!


<cfset var variableCFScriptDelim=" #chr(13)##chr(9)##chr(8)#">
<cfset var variableCFScriptStart = ""/>
<cfset var variableCFScriptEND = ""/>

<cfset var variableCFScriptText = ""/>
<cfset var variableCFScriptTextTmp = ""/>
<cfset var variableCFScriptCmd = ""/>
<cfset var variableCFcommentSTART = "" />

<cfset var variableCFcommentEND =""/>
<!--- Now start looping over all cfscript statements to identify variables that are being set --->

<cfset currentPositionVariableFind = 1 />
<cfset variableCFScriptStart = FindNoCase("<CFS"&"CRIPT>",functionInnerText,currentPositionVariableFind)/>

<cfloop condition="variableCFScriptStart NEQ 0">

<!--- Identify the cfscript statement --->
<cfset variableCFScriptEND = FindNoCase("</CFS"&"CRIPT>",functionInnerText, variableCFScriptStart+1)/>


<cfif variableCFScriptEND neq 0>
<cfset variableCFScriptText=""/>

<cfset variableCFScriptTextTmp =mid(functionInnerText, variableCFScriptSTART + len('<cfs'&'cript>'),
variableCFScriptEND-variableCFScriptSTART- len('<cfsc'&'ript>') )/>


<!--- change to use unix style lines endings --->
<cfset variableCFScriptTextTmp =Replace(variableCFScriptTextTmp,"#chr(13)##chr(10)#",chr(13),"ALL")/>

<!--- strip line // comments as they don't end in a ; --->
<cfset variableCFcommentSTART=find("//",variableCFScriptTextTmp,1)>

<cfloop condition="variableCFcommentSTART neq 0">
<cfset variableCFcommentEND=find("#chr(13)#",variableCFScriptTextTmp,variableCFcommentSTART+2)>
<cfif variableCFcommentEND gt 0>
<cfset variableCFScriptTextTmp=
Mid(variableCFScriptTextTmp,1,variableCFcommentSTART-2) & chr(13)
& Mid(variableCFScriptTextTmp,variableCFcommentEND+2,len(variableCFScriptTextTmp) )/>

</cfif>
<cfset variableCFcommentSTART=find("//",variableCFScriptTextTmp,variableCFcommentSTART+2)>
</cfloop>

<!--- strip out /* type comments */ --->
<cfset variableCFcommentSTART=find("/*",variableCFScriptTextTmp,1)>

<cfloop condition="variableCFcommentSTART neq 0">
<cfset variableCFcommentEND=find("*/",variableCFScriptTextTmp,variableCFcommentSTART+2)>
<cfif variableCFcommentEND gt 0>
<cfset variableCFScriptTextTmp=
Mid(variableCFScriptTextTmp,1,variableCFcommentSTART-2)
& Mid(variableCFScriptTextTmp,variableCFcommentEND+2,len(variableCFScriptTextTmp) )/>

</cfif>
<cfset variableCFcommentSTART=find("/*",variableCFScriptTextTmp,variableCFcommentSTART+2)>
</cfloop>

<cfloop index="variableCFScriptCmd" list="#variableCFScriptTextTmp#" delimiters=";">

<cfset variableCFScriptCmd=ListFirst(ListChangeDelims(variableCFScriptCmd," ",variableCFScriptDelim),"=")/>
<cfset variableCFScriptCmd=trim(variableCFScriptCmd)>
<cfif ListFirst(trim(variableCFScriptCmd), " ") eq "var">
<cfset VariableNameCfset=mid(variableCFScriptCmd,3,len(variableCFScriptCmd))/>

<cfset VariableNameCfset=trim(ListGetAt(variableCFScriptCmd,2, " "))/>
<!--- Update the var-ed struct to note that we are using this var --->
<cfset tempVaredStruct["#VariableNameCfset#"] = "not used">
</cfif>

</cfloop>
<!--- Update the current parsing position to start from the end of the cfset statement --->
<cfset currentPositionVariableFind = variableCFScriptEND>
<cfset variableCFScriptStart = FindNoCase("<CFS"&"CRIPT>",functionInnerText,currentPositionVariableFind)>
<cfelse>
<cfset variableCFScriptStart=variableCFScriptStart+len("<CFS"&"CRIPT>")>

</cfif>
</cfloop>

<cfset currentPositionVariableFind = arguments.positionToStart />

No comments: