簡體   English   中英

我們如何使用 msscript.ocx 以編程方式知道語法錯誤?

[英]How can we programmatically know the syntax error using msscript.ocx?

我已經使用 c# 實現了 msscript.ocx,它適用於 VBScript。

考慮以下 VBScript 代碼:

For i = 0 To 5

  'The following line has missing 'Then'. It should show an error.
  If i = 2
    Exit For
  End If

Next

我們如何在不運行腳本的情況下判斷包含If (缺少Then )的行中是否存在錯誤?

加載時出現錯誤。

Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

Sub VBSCmd
    RawScript = Arg(1)
    'Remove ^ from quoting command line and replace : with vbcrlf so get line number if error
    Script = Replace(RawScript, "^", "")
    Script = Replace(Script, "'", chr(34))
    Script = Replace(Script, ":", vbcrlf)
    'Building the script with predefined statements and the user's code
    Script = "Dim gU" & vbcrlf & "Dim gdU" & vbcrlf & "Set gdU = CreateObject(" & chr(34) & "Scripting.Dictionary" & chr(34) & ")" & vbcrlf & "Function UF(L, LC)" & vbcrlf & "Set greU = New RegExp" & vbcrlf & "On Error Resume Next" & vbcrlf & Script & vbcrlf & "End Function" & vbcrlf

    'Testing the script for syntax errors
    On Error Resume Next
    set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)
        With ScriptControl1
            .Language = "VBScript"
            .UseSafeSubset = False
            .AllowUI = True
        .AddCode Script
    End With
    With ScriptControl1.Error
        If .number <> 0 then
            Outp.WriteBlankLines(1)
            Outp.WriteLine "User function syntax error"
            Outp.WriteLine "=========================="
            Outp.WriteBlankLines(1)
            Outp.Write NumberScript(Script)
            Outp.WriteBlankLines(2)
            Outp.WriteLine "Error " & .number & " " & .description
            Outp.WriteLine "Line " & .line & " " & "Col " & .column
            Exit Sub
        End If
    End With

    ExecuteGlobal(Script)

    'Remove the first line as the parameters are the first line
    'Line=Inp.readline  
    Do Until Inp.AtEndOfStream
        Line=Inp.readline
        LineCount = Inp.Line 

        temp = UF(Line, LineCount)
        If err.number <> 0 then 
            outp.writeline ""
            outp.writeline ""
            outp.writeline "User function runtime error"
            outp.writeline "==========================="
            Outp.WriteBlankLines(1)
            Outp.Write NumberScript(Script)
            Outp.WriteBlankLines(2)
            Outp.WriteLine "Error " & err.number & " " & err.description
            Outp.WriteLine "Source " & err.source

            Outp.WriteLine "Line number and column not available for runtime errors"
            wscript.quit
        End If
        outp.writeline temp
    Loop
End Sub

VB

filter vbs "text of a vbs script"

使用冒號分隔語句和行。 使用單引號代替雙引號,如果您需要單引號,請使用 chr(39)。 使用 ^ 字符轉義括號和與號。 如果您需要插入符號,請使用 chr(136)。

該函數稱為 UF(用於 UserFunction)。 它有兩個參數,L 包含當前行和 LC 包含行數。 將腳本的結果設置為 UF。 參見示例。

有三個可用的全局對象。 一個未聲明的全局變量 gU 來維護狀態。 如果您需要多個變量,請將其用作數組。 用於保存和訪問前幾行的字典對象 gdU。 和一個可以使用的 RegExp 對象 greU。

示例 此 vbs 腳本插入行號並將該行設置為 Filter 打印的函數 UF。

filter vbs "uf=LC ^& ' ' ^& L"<"%systemroot%\win.ini"

這是它在內存中的樣子

Dim gU
Set gdU = CreateObject("Scripting.Dictionary")
Set greU = New RegExp

Function UF(L, LC)

---from command line---
uf=LC & " " & L
---end from command line---

End Function

如果有語法錯誤過濾器將顯示調試細節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM