Created
March 15, 2013 00:11
-
-
Save cmatheson/5166452 to your computer and use it in GitHub Desktop.
treetop styleguide example
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
grammar Css | |
rule body | |
(styleguide / comment)* { | |
def val | |
Hash[ | |
elements.map(&:val).compact | |
] | |
end | |
} | |
end | |
rule styleguide | |
header selector definition | |
{ | |
def val | |
header, selector, _ = elements | |
[selector.val, header.val] | |
end | |
} | |
end | |
rule header | |
(whitespace)* | |
'/*' (whitespace)* '@styleguide' | |
( | |
!'*/' | |
(. / "\n") | |
)* | |
'*/' | |
(whitespace)* | |
{ | |
def val | |
elements[4].text_value.strip | |
end | |
} | |
end | |
rule selector | |
("." / "#") [\w]+ { | |
def val | |
text_value | |
end | |
} | |
end | |
rule definition | |
(whitespace)* | |
"{" | |
( | |
!"}" | |
(. / "\n") | |
)* | |
"}" | |
(whitespace)* | |
end | |
rule comment | |
(whitespace)* | |
"/*" | |
( | |
!"*/" | |
(. / "\n") | |
)* | |
"*/" | |
(whitespace)* | |
{ | |
def val | |
end | |
} | |
end | |
rule whitespace | |
[\s]+ | |
end | |
end |
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
#!/usr/bin/env ruby | |
require 'treetop' | |
require 'pp' | |
require_relative 'css' | |
parser = CssParser.new | |
if styleguide = parser.parse(' | |
/* @styleguide | |
this is a test | |
*/ | |
.foo { color: red; } | |
/* @styleguide test 2 */ | |
#bar { | |
} | |
/* this is a regular comment */ | |
/*@styleguide test 3 */ | |
.foo2 { | |
color: black; | |
} | |
/* | |
@styleguide | |
test 4*/ | |
.bar4 {} | |
') | |
pp styleguide.val | |
else | |
fail 'dang' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment