It is less trivial that it seems, at least for function definitions, because Chris chose not to prefix them with something, unlike most languages I know (Fun, Sub, function, return type...). So it is quite hard to distinguish a simple function call (ie. with only variable names as parameters in the call) from a function definition. The only hint we have is that a function definition is eventually followed by an opening brace.
The problem is that lot of stuff can go between the function name and this opening brace... So if you want to handle all legal cases, the script has to be a bit complex. I wouldn't have tried to do it without the help of regular expressions, code would have been much longer!
The script being more than 200 lines long, I just uploaded it. Get ListAHKFunctions.ahk and, if you want, SyntaxFunctions.ahk which attempts to make a legal files exploring all the limits of the syntax.
What is the use of such script?Found in 'SyntaxFunctions.ahk':
# 5 functions:
[25] Function1()
[36] Function2(a, [bb], ?c="", d=8, f=1.6)
[48] Function3( _a, ByRef @b, c?="",__d__=65536, #f=-3.14 )
[65] Function4( )
[70] Function5( x, y )
# 2 labels:
[41] Labels!Can_Be/In\Functions...
[81] X
# 8 hotkeys, hotstrings and key remappings:
[88] Numpad0 & F1
[90] Escape
[91] a
[92] q
[93] z
[94] w
[95] ::rtfm
[96] :*c:kk
You can use it to grab quickly the API of an unknown library, to get the functions of you own library to document it, etc.
Or, primary purpose, if augmented of some code to get the source of the currently edited script, to navigate your source in your favorite editor.
This as been done before: Active GoTo v4
I must admit I didn't tried it. I just took a look at the source now, it seems not to support parameters on several lines nor additional lines between function definition and opening brace.
Of course, this covers most scripts -- not some in the manual...
And Rajat's script can be changed to use my code for better function detection.
Limitation: I was lazy and didn't tried to match only legal hotkeys and such. It will just list all lines with double : char in them, including occurrences in strings (not in comments nor continuation sections, though).
I was planning to make an AHK script to parse the source in the current buffer of SciTE as an example of use, but finally I prefer to rewrite this algorithm in Lua, so a simple Lua script will present a menu to jump to a section.
Work in progress, I will publish the result here.
[EDIT] I rewrote the code to be cleaner, hiding the gory details of state handling. If you want better performance (?) or just want to compare to the previous version, just get it: ListAHKFunctions-.ahk
Some details on implementation: I used an automaton to manage the states of the lexer/parser.
I used, of course, regular expressions to verify what kind of line I have.
If it matches, I set a state indicating the line is in a comment, a continuation section, in a function parameter list or before the opening brace.
Transitions between states are made by regex matching or non-matching: if state is INPARAMS, if the current line doesn't match the syntax of parameter list, we know we don't have a function definition so we return to DEFAULT state.
Project history:
1.03.000 -- 2006/11/30 (toralf & PL) -- Better detection of hotstuff,
splitted between hotkeys (and key remapping) and hotstrings.
Use GUI instead of MsgBox to display results.
1.02.001 -- 2006/11/29 (PL) -- Making labels more tolerant.
1.02.000 -- 2006/11/21 (PL) -- Encapsulation of state handling.
1.01.000 -- 2006/11/18 (PL) -- Added listing of labels and hotstuff.
1.00.000 -- 2006/11/17 (PL) -- Creation.




