Skip to content

Instantly share code, notes, and snippets.

@djanowski
Created June 22, 2012 15:50
Show Gist options
  • Save djanowski/2973631 to your computer and use it in GitHub Desktop.
Save djanowski/2973631 to your computer and use it in GitHub Desktop.
grammar ERB
rule document
(whitespace / text / erb / self_closing_tag / open_tag / close_tag)* {
def content
elements.map{ |e| e.content }
end
}
end
rule whitespace
[\s]+ {
def content
[:whitespace, text_value]
end
}
end
rule text
[^<]+ {
def content
[:text, text_value]
end
}
end
rule open_erb_tag
"<%" {
def content
[:open_erb_tag]
end
}
end
rule open_tag
"<" name attribute_list ">" {
def content
[:open_tag, elements[1].content, elements[2].content]
end
}
end
rule self_closing_tag
"<" name attribute_list [\s]* "/>" {
def content
[:self_closing_tag, elements[1].content, elements[2].content]
end
}
end
rule close_erb_tag
"%>" {
def content
[:close_erb_tag]
end
}
end
rule close_tag
"</" name ">" {
def content
[:close_tag, elements[1].content]
end
}
end
rule name
[A-Za-z0-9]+ {
def content
text_value
end
def quote
nil
end
}
end
rule attribute_list
attribute* {
def content
elements.inject({}){ |hash, e| hash.merge(e.content) }
end
}
end
rule erb
open_erb_tag ruby_code close_erb_tag {
def content
[:erb, elements[1].content]
end
}
end
rule attribute
[\s]+ name "=" (quoted_value / single_quoted_value / name) {
def content
{elements[1].content => [elements[3].quote, elements[3].content]}
end
}
end
rule ruby_code
[^%]+ {
def content
text_value
end
}
end
rule single_quoted_value
"'" [^']* "'" {
def content
elements[1].text_value
end
def quote
"'"
end
}
end
rule quoted_value
'"' [^"]* '"' {
def content
elements[1].text_value
end
def quote
'"'
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment