#132358 - 2005-01-13 07:05 PM
Re: Lexer for KiX
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
lonk has a lokenizer for his script... he writes it with real vars and tabs and spacers and such, then lonkenizes it to convert $temp to $!, $loop to $_, etc...
|
|
Top
|
|
|
|
#132362 - 2005-01-14 06:08 PM
Re: Lexer for KiX
|
jtokach
Seasoned Scripter
   
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
This sucks. Here's what I've got so far. Anyone bored to tears feel free to pitch in.
Code:
; Based on Jooel's VBS2Kix Script
Break On Kix2VBS("test.kix","test.vbs")
function Kix2VBS($src,$dst) dim $file,$,$Counter,$LineLength,$String,$t,$u,$Character $file=freefilehandle if not $file exit 1 endif
$=open($file,$src) if @error exit @error endif
$=readline($file) redim $src while @error=0 $u=ubound($src)+1 redim preserve $src[$u] $src[$u]=$ $=readline($file) loop $=close($file)
$file=0+freefilehandle if not $file exit 1 endif
if exist($dst) del $dst endif
$=open($file,$dst,5) if @error exit @error endif redim $dst[$u]
for $=0 to $u $Line=$src[$] ? "Line: >"+$Line+"<" $String="" $Counter=0
$Line=Replace($Line, "@@Error", "Err.Number") $Line=Replace($Line, "@@SError", "Err.Description") $Line=Replace($Line, "EndIf", "End If") $Line=Replace($Line, "EndFunction", "End Function") $Line=Replace($Line, "$$", "var") $Line=Replace($Line, "?", "wscript.echo")
? "Line: >"+$Line+"<" $LineLength=len($Line)
for $Counter=1 to $LineLength $Character=substr($Line,$Counter,1) ;? "Current beginning char is >"+$Character+"<" select ; Strings encased in dbl quotes case $Character='"' $String=$String+$Character do if $Counter>=$LineLength $dst[$]=$String $=$+1 $Line=$src[$] $String='' $Counter=0 $LineLength=len($Line) endif do $Counter=$Counter+1 $Character=substr($Line,$Counter,1) $String=$String+$Character until $Character='"' or $Counter=>$LineLength until $Character='"' or $=$u
; Spaces case $Character=" " or $Character=" " $String=$String+$Character
; Concantenation Char case $Character="+" $String=$String+"&"
; Comments case $Character=";" ? "Found comment" $String=$String+"'"
case 1 $t="" $bitDQ=0 $bitSQ=0 $TTP=""
;? "LL "$LineLength ;? "CT "$Counter For $i=1 to $LineLength $Char=Substr($Line,$i,1) ;? "Char>"+$Char+"<" $TTP=$TTP+$Char Select ; Apostrophe ;1=DQ active ;2=DQ master ;4=SQ active ;8=SQ master
;3=DQ active master ;7=DQ active master, SQ active lesser ;12=SQ active master ;13=SQ active master, DQ active lesser
Case $Char="'" and $bitDQ=1 and $bitSQ=1
Case $Char='"' and $bitDQ=0 ;out of a dq block $bitDQ=1 ? "Entering a DQ string>"+$TTP+"<"
Case $Char="'" and $bitSQ=0 ;out of a sq block $bitSQ=1 ? "Entering a SQ string>"+$TTP+"<" Case $Char='"' and $bitDQ=1 ;in a dq block $bitDQ=0 ? "Exiting a DQ string>"+$TTP+"<" Case $Char="'" and $bitSQ=1 ;in a sq block $bitSQ=0 ? "Exiting a SQ string>"+$TTP+"<" Case 1
EndSelect Next
do $t=$t+$Character $Counter=$Counter+1 $Character=substr($Line,$Counter,1) until $Counter=>$LineLength+1;instr(" ",$Character); $Counter=$Counter-1
? "T is >"+$t+"<"
select case $t="_" or $t="then" or $t="wscript.echo"
; Remove Break On case Substr(Ltrim($t),1,8)="Break On" $t=Replace($t,"Break On","") $String=$String+$t ;+" ' VBC: Removed Break On"
; Global to Dim case Substr(Ltrim($t),1,6)="global" $t=Replace($t,"global","Dim") ? "G" ? "T is Now>"+$t+"<" $String=$String+$t+" ' VBC: Converted from a global"
; Add Set to objects Case instr($t,"CreateObject") or instr($t,"GetObject") $String=$String+"Set "+$t
; Handle Optionals Case Substr(Ltrim($t),1,8)="Function" ; While Instr($t,"Optional")<>0 ; $pstart=Instr($t,"Optional") ; $parm=Substr($t,$pstart) ; $parm=Trim(Substr($t,9)) ; ? "parm>"+$parm+"<" ; ; Loop
;If Not IsNull(optViewOutput) Then ;End If $String=$String+"dim"
case $t="set" do $Counter=$Counter+1 $Character=substr($Line,$Counter,1) until $Character<>" " and $Character<>" " $Counter=$Counter-1 $String=$String+"$$"
case $t="quit" $String=$String+"wscript.quit"
case $t="not 0" $String=$String+"true"
case $t="not 1" $String=$String+"false"
case $t+substr($Line,$Counter,3)="endif" $String=$String+"end if"
case 1 $String=$String+$t endselect endselect next $dst[$]=$String next
for $=0 to $u $Counter=writeline($file,$dst[$]+@crlf) next
endfunction
; Function Replace() ; ; Author Randy Watson (aerathistorm@yahoo.com) ; ; Action Replaces first or all occurrences of one string with another ; ; Syntax Replace(Source, SearchString, ReplaceString, First, CaseSensitive) ; ; Parameters ; Source ; String to search ; SearchString ; String being replaced ; ReplaceString ; New string ; First (Optional) ; Pass the value 1 to replace only first occurrence. Default is replace all. ; CaseSensitive (Optional) ; Pass the value 1 to turn on case sensitivity. Default is off. ; ; Returns ; Returns the new string. ; ; Remarks ; ; ; Dependencies ; None ; ; Example(s) ; Replace all occurences of a non case sensative string with another: ;$StringSource = "Three Apples + four apples makes seven apples" ;$StringSearch = "apple" ;$StringReplace = "orange" ;$StringReplaced = Replace($StringSource, $StringSearch, $StringReplace) ;? "Original string: $StringSource" ;? "New string: $StringReplaced" ; Replace only the first case sensative occurrence: ;$StringReplaced = Replace($StringSource, $StringSearch, $StringReplace, 1, 1) ;? "Original string: $StringSource" ;? "New string: $StringReplaced"; Source: ;Exit Function Replace($SourceString, $SearchString, $ReplaceString, Optional $First, Optional $CaseSensitive) Dim $String1 Dim $String2 Dim $Finished $Finished = 0 $Counter = 0 $String1 = $SourceString If $CaseSensitive $PreviousState = SetOption("CaseSensitivity", "On") EndIf While Not $Finished $String2 = $String1 $Location = InStr($String1, $SearchString) If $Location > 0 $String1 = Substr($String1, 1, $Location - 1) + $ReplaceString + Substr($String1, $Location + Len($SearchString)) Else $Finished = 1 EndIf If $First $Finished = 1 EndIf If $String1 = $String2 $Finished = 1 EndIf Loop $Replace = $String1 $Nul = SetOption("CaseSensitivity", $PreviousState) EndFunction
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
|
Top
|
|
|
|
#132368 - 2005-01-14 10:27 PM
Re: Lexer for KiX
|
jtokach
Seasoned Scripter
   
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
All good points. To Jooel's point regarding freeform, I just about gave up yesterday when I tried figured out how to parse KiX's ability to $ret=shell('cmd /c CD "\My Folder\Jim's Stuff" & Dir /b & Pause') ...Emphasis on the apostrophe and that doesn't even cover multiline strings.
To Maciep's point, there is just some stuff it's not worth converting, such as Select Case statements where the methodolgy behind the command is completely different. In those cases I'm just going to slap a comment at the end of the line stating that it needs a manual approach.
Shawn, I'm way ahead of you buddy. I started a project on SF to do exactly that. vbscript.sf.net
But I am going to do everything: commands, functions, macros and selected udf's. The CVS is mess and I have a ticket in for complete removal so I can start clean. Check back next week sometime. I'm just about finished with the commands and am moving on to functions as we speak. My intent is to have a library to emulate kix in vbs do to vbs sucking so hard. (For instance, the Set commands (set,setm,setl) are proving a bit difficult to convert cleanly...)
Les, my dr. evil avatar is far from my actual appearance (unfortunately for me) and the hair pulling is in full stride. The great thing about using VBScript is that you actually learn to despise it once you start using it a lot hence my need to create a "KiX like" experience. My hands are tied.
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|