List of rules used by v2 parser

Share your ideas as to how the documentation can be improved.
evanzieg
Posts: 1
Joined: 10 Jul 2023, 09:57

List of rules used by v2 parser

Post by evanzieg » 10 Jul 2023, 10:09

Apologies if this is a duplicate topic, but I haven't found something like this in my searching so far

Can the list of rules that are used by the v2 parser be included in the docs somewhere? I run into the issue almost every time I update my scripts where I write something the parser doesn't like and it will usually cite a rule like v1-char or v1-def with no indication of what that means and what could be triggering it. I end up spending way longer googling to see if someone has posted a similar issue, or just changing all sorts of stuff in my script until it finally accepts it, but I don't really know what I changed that made it change its mind.

If the rules were posted somewhere, they don't have to be super exposed, just something that can be found by searching "rules" in the docs or something like "ahk v2 rules" in google, it would be super helpful for narrowing down what may be triggering the error(s).

Something else that might be helpful, though not a documentation suggestion, is when the rule is triggered, if either the version error popup or some kind of log file could show which line triggered the error, and potentially which character(s) specifically.

Open to suggestions, or if someone knows where the list is posted if it already exists, feel free to let me know!

just me
Posts: 9505
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: List of rules used by v2 parser

Post by just me » 11 Jul 2023, 02:36

evanzieg wrote:... where I write something the parser doesn't like and it will usually cite a rule like v1-char or v1-def with no indication of what that means and what could be triggering it.
Would you please show an example?

lexikos
Posts: 9625
Joined: 30 Sep 2013, 04:07
Contact:

Re: List of rules used by v2 parser

Post by lexikos » 17 Jul 2023, 06:14

... it will usually cite a rule like v1-char or v1-def with no indication of what that means and what could be triggering it.
That would be on a dialog like this:
It looks like the script you are trying to run requires AutoHotkey v1, which is not installed.

Script: test.ahk
Rule: v1-pct

We can try to download and install AutoHotkey v1.1.37.01 for you, while retaining the ability to use the versions already installed.

Download and install AutoHotkey v1.1.37.01?
This is obviously not an error message. This is about recognizing v1 syntax, which is not something that "the v2 parser" does. It is done by the launcher (which is a script), when "Try to identify version based on syntax" is enabled in the launcher settings. This is primarily intended to detect old scripts that both use v1 syntax and are meant to run on v1. It can't work well if you are incorrectly using v1 syntax in a script intended for v2, or vice versa.

I would suggest that you instruct the launcher which version you are trying to use, by declaring it at the top of the script:

Code: Select all

#Requires AutoHotkey v2
The launcher will execute the v2 interpreter, which will then tell you exactly where the first syntax error is.

The "rules" are subject to change, but are currently as follows for v1 syntax (cases which would be invalid in v2):
  • v1-pct: The percent-space prefix used in v1 to force an expression. v2 would interpret this as the start of a double-deref, missing its ending percent sign.
  • v1-ref: ByRef in a function declaration. v2 uses &.
  • v1-def: a=b in a function declaration, defining a parameter's default value. v2 requires :=, which is also valid in v1.
  • v1-hk: A double-colon hotkey which is not followed by an open-brace or another hotkey. v2 requires the body of each multi-line hotkey to be defined by braces.
  • v1-lbl: A label containing characters that cannot be used in a label in v2.
  • v1-dir: A directive like #NoEnv or #If, only valid in v1.
  • v1-char: A character used in a way that would only be valid in v1. For example, Run C:\ is valid in v1 but definitely invalid in v2 because \ cannot appear in an expression, unless it is quoted.
  • v1-kw: The local, global or static keyword used in a way that is only valid in v1, such as local.x := y (use a different name) or local x = y (use :=).
  • v1-if: if in/contains/between.
  • v1-ass: legacy assignment - a = b.
  • v1-cmd: xxx, where xxx is any name, assumed to be a valid v1 command. The comma makes it invalid in v2.
  • v1-send: Something like Send ^c or Send {key}, which must be quoted in v2.
For v2 syntax (cases which would be invalid in v1):
  • v2-sq: A 'single-quoted string' in an expression.
  • v2-cle: Line continuation by placing an expression operator at the end of the line.
  • v2-cbe: Continuation by enclosure.
  • v2-pct: Double-deref containing something other than a plain variable name; e.g. a := b%n+1%.
  • v2-fat: Fat arrow function.
  • v2-dir: #HotIf.
  • v2-ref: Function parameter marked with & (byref).
  • v2-vfn: A variadic function where the extra parameters are discarded; e.g. fn(*).
  • v2-kw: Something like static method() or static property { (or something invalid using local/global).
The rules are defined in identify-build.ahk with the functions mark_v1, mark_v2, end_v1 and end_v2, or the pattern (*: ... ), which is short for (*MARK: ... ), a PCRE construct.

User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

Re: List of rules used by v2 parser

Post by andymbody » 12 Apr 2024, 16:04

Thank you for this!

Post Reply

Return to “Suggestions on Documentation Improvements”