Backreferences from PCRE

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
void
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2011 9:50 pm
Location: Washington, USA

Backreferences from PCRE

Post 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)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Backreferences from PCRE

Post by Kukulkan »

void
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2011 9:50 pm
Location: Washington, USA

Re: Backreferences from PCRE

Post 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.
Post Reply