ParseScript

Compiles script code without running it and returns any compiler errors.

This function is exported by the KS module.

Errors := ParseScript(Code)

Parameters

Code

Type: String

Either the path of a script file or the script source itself. If a file of that name exists it is parsed, otherwise the value is parsed as source.

Return Value

Type: String

Returns an empty string if the code compiles. Otherwise returns the compiler error text, which is suitable for display.

Remarks

Only compilation is performed: the code is never executed, no hotkeys are registered and the auto-execute section does not run. This makes ParseScript suitable for validating code a script generated or received before writing it to disk.

The --validate command-line option performs the same check on a file from outside a script.

Examples

Validates generated code before saving it.

#Import "Ks" { ParseScript }

Code := "MsgBox 'hello'"
if (Errs := ParseScript(Code))
    MsgBox "Generated code is invalid:`n" Errs
else
    FileAppend Code, A_ScriptDir "\generated.ks"

Checks an existing file.

#Import "Ks" { ParseScript }

MsgBox ParseScript(A_ScriptDir "\other.ks") || "No errors"

Command-Line Options, How a Script Executes, RunScript