我们如何使用 msscript.ocx 以编程方式知道语法错误? [英] How can we programmatically know the syntax error using msscript.ocx?

查看:16
本文介绍了我们如何使用 msscript.ocx 以编程方式知道语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 c# 实现了 msscript.ocx,它适用于 VBScript.

I have implemented msscript.ocx using c# and it works for VBScript.

考虑以下 VBScript 代码:

Consider the following VBScript code:

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)的行中是否存在错误?

How can we tell if there is an error in line containing If (missing Then) without running the script?

推荐答案

加载时出现错误.

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

Vbs

filter vbs "text of a vbs script"

使用冒号分隔语句和行.使用单引号代替双引号,如果您需要单引号,请使用 chr(39).使用 ^ 字符转义括号和与号.如果您需要插入符号,请使用 chr(136).

Use colons to seperate statements and lines. Use single quotes in place of double quotes, if you need a single quote use chr(39). Escape brackets and ampersand with the ^ character. If you need a caret use chr(136).

该函数称为 UF(用于 UserFunction).它有两个参数,L 包含当前行和 LC 包含行数.将脚本的结果设置为 UF.参见示例.

The function is called UF (for UserFunction). It has two parameters, L which contains the current line and LC which contains the linecount. Set the results of the script to UF. See example.

有三个可用的全局对象.一个未声明的全局变量 gU 来维护状态.如果您需要多个变量,请将其用作数组.用于保存和访问前几行的字典对象 gdU.以及一个可以使用的 RegExp 对象 greU.

There are three global objects available. An undeclared global variable gU to maintain state. Use it as an array if you need more than one variable. A Dictionary object gdU for saving and accessing previous lines. And a RegExp object greU ready for use.

示例此 vbs 脚本插入行号并将该行设置为 Filter 打印的函数 UF.

Example This vbs script inserts the line number and sets the line to the function UF which Filter prints.

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

如果有语法错误过滤器会显示调试细节.

If there is a syntax error Filter will display debugging details.

这篇关于我们如何使用 msscript.ocx 以编程方式知道语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆