-
-
Save shelling/6667929 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| $:.unshift(".") | |
| require "treetop" | |
| require "huml" | |
| require "pp" | |
| class Huml::MultipleID < Exception; end | |
| class Huml::MultipleName < Exception; end | |
| pp HumlParser.new.parse("%html").cons | |
| pp HumlParser.new.parse("%html.foo").cons | |
| pp HumlParser.new.parse("%html#foo").cons | |
| pp HumlParser.new.parse("%html.foo.bar#head").cons | |
| pp HumlParser.new.parse(".foo").cons | |
| pp HumlParser.new.parse("#foo").cons | |
| pp HumlParser.new.parse("#foo#bar").cons # raise Exception | |
| pp HumlParser.new.parse("%foo%bar").cons # raise Exception |
This file contains hidden or 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 Huml | |
| rule top | |
| ( tag ) { | |
| def cons | |
| elements.cons | |
| end | |
| } | |
| end | |
| rule tag | |
| name classes id { | |
| def cons | |
| name.cons + classes.cons + id.cons | |
| end | |
| } | |
| end | |
| rule name | |
| ("%" [a-zA-Z]+)* { | |
| def cons | |
| raise Huml::MultipleName if elements.length > 1 | |
| [:name, (text_value.empty? ? "div" : elements.first.text_value)] | |
| end | |
| } | |
| end | |
| rule classes | |
| ("." item:[a-z_-]+)* { | |
| def cons | |
| elements.length > 0 ? [:class, elements.map(&:item).map(&:text_value)] : [] | |
| end | |
| } | |
| end | |
| rule id | |
| ("#" [a-z_-]+)* { | |
| def cons | |
| raise Huml::MultipleID if elements.length > 1 | |
| elements.first ? [:id, elements.first.text_value] : [] | |
| end | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment