Fixed Java language rules

MegaJar
2010-03-17
2013-05-21
  • MegaJar
    MegaJar
    2010-03-17

    Hi all,

    I've noticed for a while that the Java parsing rules don't seem to work at all in NPP 5.6.8. I got tired of just seeing a blank function list, so I decided to experiment with writing my own - and to my surprise, I succeeded!

    Here's the relevant section of FunctionListRules.xml. It's not perfect - the VARIABLE section appears above the CLASS section, and contains *all* variables, both class-level attributes and function-scoped local variables - but the list of functions itself works, so it's better than not having the list at all.

    <Language name="Java" imagelistpath="plugins\Config\C++.flb">
            <CommList param1="//" param2="" />
            <CommList param1="/\*" param2="\*/" />
            <CommList param1='[^\\]\&quot;' param2='[^\\]\&quot;' />
            <CommList param1="[^\\]&apos;" param2="[^\\]&apos;" />
            <Group name="IMPORT" subgroup="" icon="4" child="0" autoexp="0" matchcase="1" fendtobbeg="" bbegtobend="" keywords="">
                <Rules regexbeg="import\s+" regexfunc="[\w\.\*]+" regexend="\;" bodybegin="" bodyend="$" sep="" />
            </Group>
            <Group name="VARIABLE" subgroup="" icon="15" child="15" autoexp="0" matchcase="1" fendtobbeg="" bbegtobend="" keywords="\&lt;return\&gt;|\&lt;case\&gt;|\&lt;else\&gt;|\&lt;static\&gt;|\&lt;for\&gt;|\&lt;throw\&gt;">
                <Rules regexbeg="^\s*" regexfunc="[\w&lt;&gt;]+[\s\*]+\w+[^\(\)]*;" regexend="" bodybegin="" bodyend="$" sep="" />
                <Rules regexbeg="^\s*" regexfunc="\w+[\s\*]+\(\*\w+\)\s*\(.*;" regexend="" bodybegin="" bodyend="$" sep="" />
                <Rules regexbeg="^\s*" regexfunc="\w*\s*\w+[\s\*]+\w+[\s\[]*\w*[\s\]]*=[\s\w\(]*.*;" regexend="" bodybegin="" bodyend="$" sep="" />
            </Group>
            <Group name="CLASS" subgroup="FUNCTION" icon="1" child="11" autoexp="0" matchcase="1" fendtobbeg="" bbegtobend="\{" keywords="">
                <Rules regexbeg="\s*class\s+" regexfunc="\w+" regexend="" bodybegin="\{" bodyend="\}" sep="" />
            </Group>
            <Group name="FUNCTION" subgroup="VARIABLE" icon="9" child="17" autoexp="0" matchcase="1" fendtobbeg="" bbegtobend="\{" keywords="\&lt;if\&gt;|\&lt;else\&gt;|\&lt;return\&gt;|\&lt;new\&gt;">
                <Rules regexbeg="\s*\w+[\s\*]+" regexfunc="\w+" regexend="\s*\(.*\)" bodybegin="\{" bodyend="\}" sep=";" />
                <Rules regexbeg="\s*\w+[\s\*]+" regexfunc="\w+" regexend="\s*\(.*\)\s*;" bodybegin="" bodyend="$" sep="" />
            </Group>
        </Language>
    

    Let me know how it works. If anyone can think of a way to split the variables list up so that they appear under their correct respective scopes, feel free to post an improved version.

     
  • Michael Lorms
    Michael Lorms
    2010-03-24

    for me it looks perfect! please let it include in default xml!!!

     
  • MegaJar
    MegaJar
    2010-03-31

    Here's a simplified version. This one excludes the imports, variables, and the class itself; all that's left is the list of functions.

    <Language name="Java" imagelistpath="plugins\Config\C++.flb">
        <CommList param1="//" param2="" />
        <CommList param1="/\*" param2="\*/" />
        <CommList param1='[^\\]\&quot;' param2='[^\\]\&quot;' />
        <CommList param1="[^\\]&apos;" param2="[^\\]&apos;" />
        <Group name="FUNCTION" subgroup="" icon="9" child="17" autoexp="1" matchcase="1" fendtobbeg="" bbegtobend="\{" keywords="\&lt;if\&gt;|\&lt;else\&gt;|\&lt;return\&gt;|\&lt;new\&gt;">
            <Rules regexbeg="\s*\w+[\s\*]+" regexfunc="\w+" regexend="\s*\(.*\)" bodybegin="\{" bodyend="\}" sep=";" />
            <Rules regexbeg="\s*\w+[\s\*]+" regexfunc="\w+" regexend="\s*\(.*\)\s*;" bodybegin="" bodyend="$" sep="" />
        </Group>
    </Language>
    

    (Let's see if the BBCode tags stay intact this time.)

     
  • Stefan M.
    Stefan M.
    2010-06-18

    I use the ruleset from megajar. But functions with a return type contains the character < and > are not displayed.

    Examples:
    public Vector<HashMap<String, String>> getList()
    public Vector<String> getList()

    My try to fix this did not worked :(. Anyone have an idea?

     
  • MegaJar
    MegaJar
    2010-06-18

    Wow, it's been nearly 3 months since the last post. I thought this thread was long-dead; good thing I have a watch set on it! ;-)

    Ok, I whipped up a new version that accepts angle-brackets in the return type (although it's not smart enough to enforce open/close bracket pairs). And as an added bonus, I also added two extra rules to allow for package-private functions without any terms preceding the function name.

    How's this work for you?

    <Language name="Java" imagelistpath="plugins\Config\C++.flb">
            <CommList param1="//" param2="" />
            <CommList param1="/\*" param2="\*/" />
            <CommList param1='[^\\]\&quot;' param2='[^\\]\&quot;' />
            <CommList param1="[^\\]&apos;" param2="[^\\]&apos;" />
            <Group name="FUNCTION" subgroup="" icon="9" child="17" autoexp="1" matchcase="1" fendtobbeg="" bbegtobend="\{" keywords="\&lt;if\&gt;|\&lt;else\&gt;|\&lt;return\&gt;|\&lt;new\&gt;">
                <Rules regexbeg="\s*[\w\&lt;\&gt;]+[\s\*]+" regexfunc="\w+" regexend="\s*\(.*\)" bodybegin="\{" bodyend="\}" sep=";" />
                <Rules regexbeg="\s*[\w\&lt;\&gt;]+[\s\*]+" regexfunc="\w+" regexend="\s*\(.*\)\s*;" bodybegin="" bodyend="$" sep="" />
                <Rules regexbeg="\s*" regexfunc="\w+" regexend="\s*\(.*\)" bodybegin="\{" bodyend="\}" sep=";" />
                <Rules regexbeg="\s*" regexfunc="\w+" regexend="\s*\(.*\)\s*;" bodybegin="" bodyend="$" sep="" />
            </Group>
        </Language>
    
     
  • Stefan M.
    Stefan M.
    2010-06-19

    @ megajar: Thanks, it works great.  I first saw today that both rulesets are from you. But you was right, I need the short (function only) one :).

     
  • Nyugi
    Nyugi
    2010-07-06

    Hi!
    I'd like the FunctionList to show inner classes' methods separately. Has anybody a solution for this?
    It seems to me that a group can have only one subgroup, so there is no way to get a tree that would represent correctly the structure of a java class file. Am I right?

     
  • MegaJar
    MegaJar
    2010-07-06

    nyugi:

    I'm no expert, but from what I can tell, it's probably not possible. As you said, each group can only have one subgroup; you'd need some way to allow the CLASS group to contain either a FUNCTION or another CLASS, and I don't think that can be done. I experimented with making two different CLASS groups, one containing FUNCTIONs and the other containing inner CLASSes, but that didn't work; since both CLASS groups need to have exactly the same syntax, the parsing rules don't know which is which, so the inner class's functions end up listed under the outer class anyway.

     
  • YDS Roy
    YDS Roy
    2011-03-20

    private int 
         Function begin: \s*\w++\*+\s*+   

     
  • YDS Roy
    YDS Roy
    2011-03-20

    compatiable with  or <> or normal ones
    Function begin:         +[\\s*]+