[TxMt] Re: Regexp generator for language grammars wanted

Hans-Jörg Bibiko bibiko at eva.mpg.de
Mon Sep 22 09:59:08 UTC 2008


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}

--Hans


-------------- next part --------------
A non-text attachment was scrubbed...
Name: DeOptimize Regexp Alternations.tmCommand.zip
Type: application/zip
Size: 942 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20080922/e1909b7c/attachment.zip>
-------------- next part --------------



More information about the textmate mailing list