[TxMt] Re: Regexp generator for language grammars wanted

Alex Ross tm-alex at rosiba.com
Mon Sep 22 10:11:11 UTC 2008


On Sep 22, 2008, at 11:59 AM, Hans-Jörg Bibiko wrote:

> On 22.09.2008, at 10:19, Hans-Jörg Bibiko wrote:
>> Does someone have a function which does the reverse thing, i.e.
>>
>> to get from
>> NS(Mutable(String|Array)|C(oder|ell(Item)?)|Array)
>>
>> this:
>> NSArray
>> NSMutableString
>> NSMutableArray
>> NSCell
>> NSCellItem
>> NSCoder
>
>
> I believe I found a way to do the reverse.
>
> Install the attached command, edit a tmLanguage in TM, select a  
> given optimized regexp and invoke that command. It will open a new  
> doc with a sorted list of - hopefully - all matched keywords. I  
> tested it for some of these regexps BUT PLEASE check it whether it  
> works for everything ;)
>
> If someone has a better way to do this let it me know.
>
> Here the Ruby script:
>
> def decompileRe (re)
>  # handle foo(bar|boo|bou)? => foo|foobar|fooboo|foobou
>  while m = re.match(/\b(\w+)\(([^(]+?)\)\?/) do
>    re.sub!(/\b(\w+)\(([^(]+?)\)\?/, "#{m[1]}|#{m[2].split('|').map {| 
> x| m[1] + x }.join('|')}")
>  end
>  # handle foo(bar|boo|bou) => foobar|fooboo|foobou recursively
>  while m = re.match(/\b(\w+)\(([a-zA-Z\|]+?)\)/)  do
>    re.sub!(/\b(\w+)\(([^(]+?)\)/, "#{m[2].split('|').map {|x| m[1] +  
> x }.join('|')}")
>  end
>  # return sorted array
>  return re.split('|').sort
> end
>
>
> decompileRe(STDIN.read.chomp).each {|r| puts r}

Darn it Hans!  I spent an hour writing a recursive descent parser to  
do this… and you pop out this 6 line ruby regex!  Oh, well, it was a  
good exercise.

—Alex


More information about the textmate mailing list