Skip to content

Instantly share code, notes, and snippets.

@cmatheson
Created March 15, 2013 00:11
Show Gist options
  • Save cmatheson/5166452 to your computer and use it in GitHub Desktop.
Save cmatheson/5166452 to your computer and use it in GitHub Desktop.
treetop styleguide example
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
#!/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