lib/code/parser/string.rb in template-ruby-0.4.0 vs lib/code/parser/string.rb in template-ruby-0.5.0

- old
+ new

@@ -1,87 +1,78 @@ class Code class Parser - class String < Parslet::Parser - rule(:number) { ::Code::Parser::Number.new } - rule(:name) { ::Code::Parser::Name.new.name } - rule(:code) { ::Code::Parser::Code.new } + class String < Language + def code + ::Code::Parser::Code + end - rule(:single_quote) { str("'") } - rule(:double_quote) { str('"') } - rule(:backslash) { str("\\") } - rule(:colon) { str(":") } - rule(:opening_curly_bracket) { str("{") } - rule(:closing_curly_bracket) { str("}") } + def name + ::Code::Parser::Name + end - rule(:zero) { str("0") } - rule(:one) { str("1") } - rule(:two) { str("2") } - rule(:three) { str("3") } - rule(:four) { str("4") } - rule(:five) { str("5") } - rule(:six) { str("6") } - rule(:seven) { str("7") } - rule(:eight) { str("8") } - rule(:nine) { str("9") } - rule(:a) { str("a") | str("A") } - rule(:b) { str("b") | str("B") } - rule(:b) { str("b") | str("B") } - rule(:c) { str("c") | str("C") } - rule(:d) { str("d") | str("D") } - rule(:e) { str("e") | str("E") } - rule(:f) { str("f") | str("F") } - rule(:n) { str("n") | str("N") } - rule(:r) { str("r") | str("R") } - rule(:t) { str("t") | str("T") } - rule(:u) { str("u") | str("U") } + def single_quote + str("'") + end - rule(:interpolation) do - opening_curly_bracket >> code >> closing_curly_bracket + def double_quote + str('"') end - rule(:base_16_digit) do - zero | one | two | three | four | five | six | seven | eight | nine | - a | b | c | d | e | f + def backslash + str("\\") end - rule(:escaped_character) do - (backslash >> u >> base_16_digit.repeat(4, 4)) | - (backslash >> (b | f | n | r | t)) | (backslash.ignore >> any) + def opening_curly_bracket + str("{") end - rule(:single_quoted_character) do - escaped_character | - (opening_curly_bracket.absent? >> single_quote.absent? >> any) + def closing_curly_bracket + str("}") end - rule(:double_quoted_character) do - escaped_character | - (opening_curly_bracket.absent? >> double_quote.absent? >> any) + def colon + str(":") end - rule(:single_quoted_string) do - single_quote.ignore >> - ( - interpolation.as(:interpolation) | - single_quoted_character.repeat(1).as(:characters) - ).repeat >> single_quote.ignore + def code_part + opening_curly_bracket << code << closing_curly_bracket.maybe end - rule(:double_quoted_string) do - double_quote.ignore >> - ( - interpolation.as(:interpolation) | - double_quoted_character.repeat(1).as(:characters) - ).repeat >> double_quote.ignore + def single_quoted_text_part + ( + backslash.ignore << opening_curly_bracket | + backslash.ignore << single_quote | + single_quote.absent << opening_curly_bracket.absent << any + ).repeat(1) end - rule(:symbol) { colon.ignore >> name } + def double_quoted_text_part + ( + backslash.ignore << opening_curly_bracket | + backslash.ignore << double_quote | + double_quote.absent << opening_curly_bracket.absent << any + ).repeat(1) + end - rule(:string) do - (single_quoted_string | double_quoted_string | symbol).as(:string) | - number + def single_quoted_string + single_quote.ignore << + (code_part.aka(:code) | single_quoted_text_part.aka(:text)).repeat << + single_quote.ignore.maybe end - root(:string) + def double_quoted_string + double_quote.ignore << + (code_part.aka(:code) | double_quoted_text_part.aka(:text)).repeat << + double_quote.ignore.maybe + end + + def symbol + (colon.ignore << name).aka(:text).repeat(1, 1) + end + + def root + (single_quoted_string | double_quoted_string | symbol).aka(:string) | + ::Code::Parser::Number + end end end end