Page 1 of 1

Backreferences from PCRE

Posted: Sun Oct 28, 2012 2:29 am
by void
It'd be nice if PB had a mechanism for accessing backreferences/captures/substrings from regular expressions.

There's ways of getting this information using the pcre API directly, but they're rather cumbersome.

Incomplete fragment follows, but it should be enough to see the pattern in use.

Code: Select all

ImportC ""
  pb_pcre_exec(*pcre,*extra,subject.s,length.i,startoffset.i,options.i,*ovector,ovecsize.i)
  pb_pcre_get_substring(subject.s, *ovector, stringcount.i, stringnumber.i, stringptr)
  pb_pcre_free_substring(stringptr)
EndImport

Procedure.s get_substring(string.s, *ovector, resultcount, stringnumber)
  Define outputstring.s, stringptr.i
  pb_pcre_get_substring(string, *ovector, resultcount, stringnumber, @stringptr)
  outputstring= PeekS(stringptr, -1, #PB_UTF8)
  pb_pcre_free_substring(stringptr)
  ProcedureReturn outputstring
EndProcedure

regexp_handle.i = CreateRegularExpression(#PB_Any, ".*?((static inline )?(\w*\*?)\s(cp\w*)\((.*?)\))", #PB_RegularExpression_AnyNewLine)

resultcount.i = pb_pcre_exec(PeekL(regexp_handle),0,string,Len(string),0,0,@pcre_results(),18) 

    If resultcount > 0
      fullstring.s = get_substring(string, @pcre_results(), resultcount, 0)
      returntype.s = get_substring(string, @pcre_results(), resultcount, 3)
      functionname.s = get_substring(string, @pcre_results(), resultcount, 4)
      arguments.s = get_substring(string, @pcre_results(), resultcount, 5)
    endif
It would be nice if we had something closer to how ExtractRegularExpression worked for this task.

(for any who are curious, this is part of a larger project where I am creating a tool to automate wrapper creation for a C library ... doing ~300 C functions by hand, along with all of their .desc entries for each platform, is not my idea of a good time. When my work is closer to done I'll be releasing the results in case they're useful to anyone else)

Re: Backreferences from PCRE

Posted: Mon Oct 29, 2012 8:31 am
by Kukulkan

Re: Backreferences from PCRE

Posted: Tue Oct 30, 2012 2:07 am
by void
Whoops! I tried searching, but 'grouping' wasn't one of the terms I looked for.

There are so many different terms for the same concept. It's very confusing.