Last active
August 29, 2015 14:00
-
-
Save akuhn/11198319 to your computer and use it in GitHub Desktop.
This script extracts the structure of language grammars used for syntax highlighting in Sublime. You need to convert the grammar to a JSON file before running this script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
# This script extracts the structure of Sublime language grammars ... | |
# | |
# Usage: ruby tmLanguage.rb PHP.JSON-tmLanguage | |
$root = JSON.parse(File.read(ARGV.first)) | |
$seen = [] | |
$stack = [] | |
$indent = 0 | |
def echo(string) | |
puts ' ' * $indent + string | |
end | |
def unravel_each(name,patterns) | |
return unless patterns | |
echo name + ' ...' | |
$indent += 1 | |
patterns = patterns.values if Hash === patterns | |
patterns.each{|each|unravel(each)} | |
$indent -= 1 | |
end | |
def unravel(json) | |
if $indent > 20 | |
echo '(TOO DEEP)' | |
elsif Hash === json | |
if json['include'] | |
key = json['include'] | |
echo key + ' ...' | |
return echo ' (RECURSION)' if $stack.include? key | |
return echo ' (SEE ABOVE)' if $seen.include? key | |
$seen.push key | |
$stack.push key | |
$indent += 1 | |
unravel $root['repository'][key.gsub('#','')] | |
$indent -= 1 | |
$stack.pop | |
else | |
echo json['name'] if json['name'] | |
$indent += 1 if json['name'] | |
unravel_each 'begin captures', json['beginCaptures'] | |
unravel_each 'captures', json['captures'] | |
unravel_each 'patterns', json['patterns'] | |
unravel_each 'begin captures', json['endCaptures'] | |
$indent -= 1 if json['name'] | |
end | |
elsif Array === json | |
json.each{|each|unravel each} | |
elsif nil === json | |
# pass | |
elsif | |
p json | |
raise # unexpected value | |
end | |
end | |
unravel $root |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment