0

I am trying to create a regex expression that will create a function list for a propietary programming language inside of notepad++ using the functionList.xml file. The regex expression needs to capture all instances of any Method, Function or Macro following this syntax:

 Method Blah1(pParam1, pParam2)
   [New] dynamicVar = "string"
   [New] dynamicVar2 = 4
   [New] result = Bar2(dynamicVar, dynamicVar2)
 End Method

 Function Bar2(pParam3, pParam2)
   Foo3
   Return pParam3 && pParam2
 End Function

 Macro Foo3()
 End Macro

So the regexp should capture 3 instances for the above example.

Link to regexr: http://www.regexr.com/3fcd7

functionList.xml

        <parser
            displayName="MOX"
            id         ="mox_function"
            commentExpr="(?s:/\*.*?\*/)|(?m-s://.*?$)"
        >
            <function
                mainExpr="^(s|Method|Macro|Function)"
            >
                <functionName>
                    <nameExpr expr="[A-Za-z_]\w*\s*[=:]|[A-Za-z_]?\w*\s*\(" />
                    <nameExpr expr="[A-Za-z_]?\w*" />
                </functionName>
                <className>
                    <nameExpr expr="([A-Za-z_]\w*\.)*[A-Za-z_]\w*\." />
                    <nameExpr expr="([A-Za-z_]\w*\.)*[A-Za-z_]\w*" />
                </className>
            </function>
        </parser>
4
  • What question do you have ? Commented Feb 23, 2017 at 16:36
  • I need the regular expression that will select any and all instances of where there is ("Method" AND "End Method") OR ("Macro" AND "End Macro") OR ("Function" AND "End Function") Commented Feb 23, 2017 at 16:44
  • What if there is something between the Method and End? Should it still match? Commented Feb 23, 2017 at 18:09
  • Yes, there can be anything between Method and End Commented Feb 23, 2017 at 18:28

2 Answers 2

2

Try this.

(?m)^(Method|Macro).*\(.*\)\n.*\n?^\s?End.*$

https://regex101.com/r/pAkFDU/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, unfortunately this didn't create my function list for notepad++. here is a link to what i'm trying to achieve: notepad-plus-plus.org/features/function-list.html but I will will mark your answer as correct if i can't figure it out.
1

I've never been able to use FunctionList. It's been known to have stability issues for a long time and is even not listed, by default, in the plugin manager.

It's possible highly likely that a too-inclusive expression won't properly capture nested groups (unless FunctionList recurses through matches, or supports Recursion in Regex). Just something you should be aware of.

Too-inclusive regexes frequently stumble on scenarios like this

Function foo()
  Function refoo()
  End Function         // 1
End Function           // 2

Function solo(stuff,thing)
End Function

Function bar()
  sample = Function (x,y)
    return x & y
  End Function
End Function           // 3 

Either failing at points 1 or 3. It can be quite difficult to properly capture the function, unless you have recursion available. (Function|Method|Macro)[\s\S]*?End \1 captures too little; take the ? away and it captures too much.

Truly recursive ((?R)) Regular Expressions (or Balanced Pairs), which aren't widely supported, can properly capture to the right point, but without code to recurse through it, won't flag the nested functions.

(Function|Method|Macro) (\w+)\((.*)\)((?:(?R)|[\s\S]*?)+)End \1 would correctly stop at point 2.

You can use a similar expression with a lookahead: (Function|Method|Macro) (\w+)\((.*)\)(?=(?:(?R)|[\s\S]*?)+End \1) but it's also easy to fool. All it basically cares about is that End \1, follows it anywhere in the file.

If FunctionList does support recursive regex and doesn't recurse (code-side) through the results (I don't know, the plugin won't work for me), I would look at a simpler expression like below. Honestly, even if it does, you'll get the best performance out of a simple expression. It will work faster and you'll know exactly what it is and is not indicating. Anything else has too much overhead for relatively little gain.

^\s*(Function|Method|Macro) (\w+)\((.+)\)
Output: \2     Args:  \3

^\s*(Function|Method|Macro) (\w+)\(\)
Output: \2

The outputs are just examples, the plugin I use lets you customize the output. Others may not. Also, prefixing with ^\s* allows you to find functions not assigned to variables or commented-out.

Or both above can be captured in one expression by changing the first expression's + to a *. It won't tell you if there's a matching End, but it will find the part you're interested in for a document outline.


Personally, I've always had good luck with an a similar plugin ambiguously named "SourceCookifier" but it only parses the file as it was last saved/opened. If I add a new function, it's not listed until save/open.

The interface for adding new languages isn't very intuitive, packing a lot of features in a small area, but it works and it works well. Like FunctionList, and probably most other alternatives, you'll need to add rules for "Mox".

I'm not saying you should switch to this particular utility, but FunctionList hasn't been updated in 7 years.

2 Comments

I forgot to mention that all of these method/function/macro have parameters (updated OP), so when parameters were present your example stopped working, but i was able to modify it to: ^\s*(Function|Method|Macro) (\w+)\( and it's working fine! It may be nice if the regexp also captured the Function|Method|Macro as well as the pParam names as all that is showing in my function list is the Foo3 etc, but this is a good prototype. Thank you! would probably give 50 reputation for this request.. lol <3 SO
@SuperVeetz Forgot to tag you in the previous comment. You may have never seen it --- (1) Lol, before I saw your comment I noticed the typo (honest, typo) of the \s+ in the first example. (2) It does capture the (Method|Function|Macro) type as the first capture group, \1, already. I hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.