Class range regex in function list



  • I am attempting to define a parser in functionList.xml. My file is split up as such:

    ------------------
    {{header}}
    ------------------ {
    
    [subheader]
    
    random text
    
    }
    

    And I’m pretending that the headers are classes and the subheaders are functions so that they will show up in a nice hierarchy in the function list, like so:

    header 1
    	subheader 1
    	subheader 2
    header 2
    	subheader 1
    

    This is the XML I’m using:

    <parser id="test_function" displayName="Test Syntax">
    	<classRange
    		mainExpr="\{\{.+?\}\}[\n\r]-+?\s\{"
    		openSymbole="\{"
    		closeSymbole="\}"
    		displayMode="node">
    		<className>
    			<nameExpr expr="[^\{].+[^\}]"/>
    		</className>
    		<function
    			mainExpr="[.+]"
    			displayMode="$functionName">
    			<functionName>
    				<nameExpr expr="[^[].+[^\]]"/>
    			</functionName>
    		</function>
    	</classRange>
    </parser>
    

    The regex works fine when I verify it on RegexPal, but does not work in NP++ no matter how I try to rewrite it. I actually managed to get something somewhat working once, but it stopped working after I updated the program. Overall I’m just confused on exactly how this thing really parses and why such a simple(-looking) format isn’t getting properly recognized despite the regex being fine.

    (I had actually posted this question on SO two years ago…but am still struggling today so, updated some regex and am now asking here. Thanks for any help!)



  • NOTE I’ve never messed with the function list parser myself, but can check out some of the regexs. Some look a bit off.

    mainExpr="\{\{.+?\}\}[\n\r]-+?\s\{"
    

    The [\n\r] looks weird because it will not match Windows line endings which use \r\n (meaning two characters). This regex can only match one \r or \n. So that is something that probably needs fixed. Also

    expr="[^\{].+[^\}]"
    

    within className/nameExpr also looks off. I’m not sure what this section of the XML does for the function list parser but assuming it is trying to find the text header then you’d want to use something like:

    expr="(?<=\{\{)[^\}]+"
    

    Also for the functionName/nameExpr again assuming you want to match subheader use something like:

    expr="(?<=[)[^\]]+"


  • Thanks, completely forgot about the \r\n thing for two years apparently! I’ve made the changes and verified all the expressions in NP++'s search by regex tool (honestly don’t know why I’ve been using external tools before…). This is my current parser:

    <classRange
    	mainExpr="^\{\{.+?\}\}\r\n-+?\s\{"
    	openSymbole="\{"
    	closeSymbole="\}"
    	displayMode="node">
    	<className>
    		<nameExpr expr="(?<=\{\{)[^\}]+"/>
    	</className>
    	<function
    		mainExpr="^[.+?]"
    		displayMode="$functionName">
    		<functionName>
    			<nameExpr expr="(?<=[)[^\]]+"/>
    		</functionName>
    	</function>
    </classRange>
    

    Unfortunately, this still doesn’t work–the functionlist is still empty. Hoping someone who’s worked with the file before can point out what’s wrong.



  • (Couldn’t find any rules about double posting here but I can no longer edit my previous posts:)

    Not sure if this information changes anything but, what’s really strange is that if I make a plain function parser using the exact same code (eg. just deleting every node relating to classRange), each header is sensed perfectly fine as a function. Subheaders work too. I just can’t get anything to work when parsing them as part of a class.



  • Hello @Matt-Klein,

    can you show the complete parser xml as well as the association part?

    Cheers
    Claudia



  • @Claudia-Frank Here’s all the relevant XML:

    <association ext=".testsyntax" userDefinedLangName="Test" id="test_function"/>
    
    <parser id="test_function" displayName="Test Syntax">
    	<classRange
    		mainExpr="^\{\{.+?\}\}\r\n-+?\s\{"
    		openSymbole="\{"
    		closeSymbole="\}"
    		displayMode="node">
    		<className>
    			<nameExpr expr="(?<=\{\{)[^\}]+"/>
    		</className>
    		<function
    			mainExpr="[.+?]"
    			displayMode="$functionName">
    			<functionName>
    				<nameExpr expr="(?<=[)[^\]]+"/>
    			</functionName>
    		</function>
    	</classRange>
    </parser>


  • @Matt-Klein

    I hope this does it

            <parser id="test_function" displayName="Test Syntax">
                <classRange 
                    mainExpr="^\{{2}.+?\}{2}\R-+?\s\{"
                    openSymbole = "\{"
    				closeSymbole = "\}">
                    <className>
                        <nameExpr expr="\w+"/>
                    </className>
                    <function mainExpr="[.+?]">
                        <functionName>
                            <funcNameExpr expr="\w+"/>
                        </functionName>
                    </function>
                </classRange>
                <function mainExpr="[.+?]">
                    <functionName>
                        <nameExpr expr="\w+"/>
                    </functionName>
                </function>
            </parser>
    

    Cheers
    Claudia



  • @Claudia-Frank Ahhhh thank you so much! After replacing a bunch of regex and checking what went wrong, it seems that writing nameExpr instead of funcNameExpr under classRange -> functionName was what had caused everything to not work. Hard to believe that took two years, but I’ll be remembering this fix for a long while…



  • @Matt-Klein to be honest, I didn’t even notice this.
    I always copy a working one and then start modifying it.

    Cheers
    Claudia



  • Hello Matt,

    Indeed, the way Claudia wrote your parser, as you noticed, is the right syntax. Just refer to the sections Class parser or Mix parser, of the article below :

    https://notepad-plus-plus.org/features/function-list.html

    Best Regards

    guy038


Log in to reply