Tapatalk

Function String for non-forward declarations only

Function String for non-forward declarations only

3
NewbieNewbie
3

    Sep 09, 2010#1

    I'm having some trouble working out a function string that only finds the actual function definitions, and not the forward declarations as well.

    E.g the scripts are like this

    Code: Select all

    // Forward declarations
    function Minister_Attack_Bases returns boolean
    params
      ship_id:                   long
    end
    
    function Minister_Attack_Ships_Normal returns boolean
    params
      ship_id:                   long
    end
    
    
    // Actual functions
    function Minister_Attack_Bases returns boolean
    params
      ship_id:                   long
    vars
      orders_given:              boolean := FALSE
    begin
      code...
    end
    
    function Minister_Attack_Ships_Normal returns boolean
    params
      ship_id:                   long
    vars
      orders_given:              boolean := FALSE
    begin
      code...
    end
    
    My problem is, in the function list Minister_Attack_Bases shows twice.
    What I'd like is to just have the actual functions in this list, but I can't work out a regex to do this.

    Can anyone give me a hand coming up with a regex to do that please?

    I believe the vars part is compulsory for actual functions and doesn't exist for forward declarations.
    Perhaps, if the regex could look for the function line, then look for a line starting with vars or end and if it found end without finding vars first it would not be a match? That's way beyond my regex skills though - I can't even get it to search across multiple lines.

    So far I got ^[[:blank:]]*function[[:blank:]]+([^[:s:]]+)[[:blank:]]+returns
    using the perl regex, but this causes the described problem.

    I'm using UES 9 BTW.

    6,685587
    Grand MasterGrand Master
    6,685587

      Sep 09, 2010#2

      Try following:

      /Regexp Type = Perl
      /Function String = "^[ \t]*function[ \t]+(\S+).+\r*\n[^vb]+?(?:vars|begin)"


      If the language for your code is case sensitive, use following:

      /Regexp Type = Perl
      /Function String = "
      (?-i)^[ \t]*function[ \t]+(\S+).+\r*\n[^vb]+?(?:vars|begin)"

      Both worked with UltraEdit v16.20.0.1009 for your example. If that function string works fine for your source files, it would be good to post in which language your code is written. That would make it easier for other users coding in this language to find this topic.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Sep 10, 2010#3

        Thanks Mofi!

        When I tried that on one of the real files I found there were a few functions which it didn't match however. I should have just posted the source at the start I think.

        The 1st case was when there was no params section e.g.

        Code: Select all

        function AI_Orders_Ships returns boolean
        vars
          minister_controlled:       boolean
        begin
          set gbl_lng_surveyors_sent_out := Sys_Get_Number_Of_Ships_With_Ord
        end
        This seemed to be matched ok when I took out the \r*\n

        Code: Select all

        ^[ \t]*function[ \t]+(\S+).+[^vb]+?(?:vars|begin)
        but I'm not sure what the implications of doing that are.

        The 2nd unmatched case that I noticed using the regex above was when there was a 'v' or 'b' anywhere in the params before vars e.g.

        Code: Select all

        function Set_Custom_Ship_Names_List_Items_Count returns boolean
        params
          value:                     long
        vars
        begin
          call Set_AI_Storage_Misc_Long(AI_STORE_LONG_MISC_CUSTOM_NAMES_COUNT, value)
        end
        I attempted to replace the [^vb]+ with something to make it search for anything except <newline>vars|<newline>begin, but I don't think I even got close :)

        Here's the source file containing the functions above, which by my count has 68 forward declarations and 79 actual functions.
        BTW It's for the game Space Empires V, and the language is called SE5 Script or SEV Script I (for anyone searching). I'll probably post the wordfile on the site http://spaceempires.net/ when it's working ok. I'm not sure if it's case sensitive or not - there's not a lot of documentation for it.

        6,685587
        Grand MasterGrand Master
        6,685587

          Sep 10, 2010#4

          Okay, I found a much better regular expression which finds the 79 functions in your file.

          /Regexp Type = Perl
          /Function String = "
          (?-i)^[ \t]*function[ \t]+(\S+)(?:.+\r*\n)+(?:vars|begin)"

          This time I explain the regular expression.

          (?-i) ... is not really necessary, but makes the search faster because it turns off case insensitivity. Case sensitive searches are faster.

          ^ ... lets the search begin at start of a line which makes also the search faster.

          [ \t]* ... matches zero or more occurrences of spaces or tabs.

          function ... the word function must be at start of the line which can have preceding whitespaces.

          [ \t]+ ... matches one or more spaces or tabs which must follow the word function.

          (\S+) ... matches one or more non-whitespace characters and mark (tag) that string. This part of the found string is the function name displayed in the function list view.

          (?:.+\r*\n)+ ... matches non-blank lines with DOS or UNIX line termination one or more times, plus the rest of the function definition line after the function name. ?: turns off capturing the found string by the expression in the round brackets.

          (?:vars|begin) ... an OR expression for the words vars or begin, again as non-capturing group. Because the expression part before matches always entire non-blank lines, the search string finds only blocks with a line starting with word function followed by zero or more non-blank lines before a line is found with either word vars or word begin at start of the line. This is the secret why the expression works now.

          Of course if there is just a line with whitespaces between a forward declaration block and a following function definition block instead of a really blank line, this expression would match also the forward declaration. But simply using Format - Trim Trailing Spaces solves that problem and your source files does not contain lines with just whitespace characters.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Sep 10, 2010#5

            Thank you very much Mofi, for the regex, and for the great explanation too. It cleared up a whole bunch of stuff and now I know a way to search across multiple lines with perl too.