Permalink
Browse files

Add parser.

  • Loading branch information...
1 parent 1f471f7 commit 92baac1f2bea3c751f7ec5d65542818eb7bb8c66 @LTe committed Jun 13, 2012
Showing with 769 additions and 1 deletion.
  1. +0 −1 .gitignore
  2. +769 −0 lib/machete/parser.rb
View
1 .gitignore
@@ -1,4 +1,3 @@
-lib/machete/parser.rb
doc/
.yardoc
*.rbc
View
769 lib/machete/parser.rb
@@ -0,0 +1,769 @@
+#
+# DO NOT MODIFY!!!!
+# This file is automatically generated by Racc 1.4.8
+# from Racc grammer file "".
+#
+
+require 'racc/parser.rb'
+module Machete
+ class Parser < Racc::Parser
+
+module_eval(<<'...end parser.y/module_eval...', 'parser.y', 143)
+
+include Matchers
+
+class SyntaxError < StandardError; end
+
+def parse(input)
+ @input = input
+ @pos = 0
+
+ do_parse
+end
+
+private
+
+def integer_value(value)
+ if value =~ /^0[bB]/
+ value[2..-1].to_i(2)
+ elsif value =~ /^0[oO]/
+ value[2..-1].to_i(8)
+ elsif value =~ /^0[dD]/
+ value[2..-1].to_i(10)
+ elsif value =~ /^0[xX]/
+ value[2..-1].to_i(16)
+ elsif value =~ /^0/
+ value[1..-1].to_i(8)
+ else
+ value.to_i
+ end
+end
+
+def string_value(value)
+ quote = value[0..0]
+ if quote == "'"
+ value[1..-2].gsub("\\\\", "\\").gsub("\\'", "'")
+ elsif quote == '"'
+ value[1..-2].
+ gsub("\\\\", "\\").
+ gsub('\\"', '"').
+ gsub("\\n", "\n").
+ gsub("\\t", "\t").
+ gsub("\\r", "\r").
+ gsub("\\f", "\f").
+ gsub("\\v", "\v").
+ gsub("\\a", "\a").
+ gsub("\\e", "\e").
+ gsub("\\b", "\b").
+ gsub("\\s", "\s").
+ gsub(/\\([0-7]{1,3})/) { $1.to_i(8).chr }.
+ gsub(/\\x([0-9a-fA-F]{1,2})/) { $1.to_i(16).chr }
+ else
+ raise "Unknown quote: #{quote.inspect}."
+ end
+end
+
+def symbol_value(value)
+ value.to_s[1..-1]
+end
+
+# "^" needs to be here because if it were among operators recognized by
+# METHOD_NAME, "^=" would be recognized as two tokens.
+SIMPLE_TOKENS = [
+ "|",
+ "<",
+ ">",
+ ",",
+ "=",
+ "^=",
+ "^",
+ "$=",
+ "[",
+ "]",
+ "*=",
+ "*",
+ "+",
+ "?",
+ "{",
+ "}"
+]
+
+COMPLEX_TOKENS = [
+ [:TRUE, /^true/],
+ [:FALSE, /^false/],
+ [:NIL, /^nil/],
+ # INTEGER needs to be before METHOD_NAME, otherwise e.g. "+1" would be
+ # recognized as two tokens.
+ [
+ :INTEGER,
+ /^
+ [+-]? # sign
+ (
+ 0[bB][01]+(_[01]+)* # binary (prefixed)
+ |
+ 0[oO][0-7]+(_[0-7]+)* # octal (prefixed)
+ |
+ 0[dD]\d+(_\d+)* # decimal (prefixed)
+ |
+ 0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)* # hexadecimal (prefixed)
+ |
+ 0[0-7]*(_[0-7]+)* # octal (unprefixed)
+ |
+ [1-9]\d*(_\d+)* # decimal (unprefixed)
+ )
+ /x
+ ],
+ [
+ :STRING,
+ /^
+ (
+ ' # sinqle-quoted string
+ (
+ \\[\\'] # escape
+ |
+ [^'] # regular character
+ )*
+ '
+ |
+ " # double-quoted string
+ (
+ \\ # escape
+ (
+ [\\"ntrfvaebs] # one-character escape
+ |
+ [0-7]{1,3} # octal number escape
+ |
+ x[0-9a-fA-F]{1,2} # hexadecimal number escape
+ )
+ |
+ [^"] # regular character
+ )*
+ "
+ )
+ /x
+ ],
+ # ANY, EVEN and ODD need to be before METHOD_NAME, otherwise they would be
+ # recognized as method names.
+ [:ANY, /^any/],
+ [:EVEN, /^even/],
+ [:ODD, /^odd/],
+ # We exclude "*", "+", "<", ">", "^" and "|" from method names since they are
+ # lexed as simple tokens. This is because they have also other meanings in
+ # Machette patterns beside Ruby method names.
+ [
+ :METHOD_NAME,
+ /^
+ (
+ # regular name
+ [a-z_][a-zA-Z0-9_]*[?!=]?
+ |
+ # operator (sorted by length, then alphabetically)
+ (<=>|===|\[\]=|\*\*|\+@|-@|<<|<=|==|=~|>=|>>|\[\]|[%&\-\/`~])
+ )
+ /x
+ ],
+ [:CLASS_NAME, /^[A-Z][a-zA-Z0-9_]*/],
+ [
+ :SYMBOL,
+ /^
+ :
+ (
+ # class name
+ [A-Z][a-zA-Z0-9_]*
+ |
+ # regular method name
+ [a-z_][a-zA-Z0-9_]*[?!=]?
+ |
+ # operator (sorted by length, then alphabetically)
+ (<=>|===|\[\]=|\*\*|\+@|-@|<<|<=|==|=~|>=|>>|\[\]|[%&*+\-\/<>^`|~])
+ )
+ /x
+ ]
+]
+
+def next_token
+ skip_whitespace
+
+ return false if remaining_input.empty?
+
+ # Complex tokens need to be before simple tokens, otherwise e.g. "<<" would be
+ # recognized as two tokens.
+
+ COMPLEX_TOKENS.each do |type, regexp|
+ if remaining_input =~ regexp
+ @pos += $&.length
+ return [type, $&]
+ end
+ end
+
+ SIMPLE_TOKENS.each do |token|
+ if remaining_input[0...token.length] == token
+ @pos += token.length
+ return [token, token]
+ end
+ end
+
+ raise SyntaxError, "Unexpected character: #{remaining_input[0..0].inspect}."
+end
+
+def skip_whitespace
+ if remaining_input =~ /\A^[ \t\r\n]+/
+ @pos += $&.length
+ end
+end
+
+def remaining_input
+ @input[@pos..-1]
+end
+
+def on_error(error_token_id, error_value, value_stack)
+ raise SyntaxError, "Unexpected token: #{error_value.inspect}."
+end
+...end parser.y/module_eval...
+##### State transition tables begin ###
+
+racc_action_table = [
+ 73, 17, 12, 13, 14, 10, 11, 15, 70, 42,
+ 43, 7, 9, 23, 44, 45, 12, 13, 14, 10,
+ 11, 15, 16, 72, 8, 7, 9, 12, 13, 14,
+ 10, 11, 15, 64, 66, 17, 7, 9, 8, 63,
+ 65, 12, 13, 14, 10, 11, 15, 68, 47, 8,
+ 7, 9, 12, 13, 14, 10, 11, 15, 69, 67,
+ 18, 7, 9, 8, 29, 30, 31, 62, 71, 32,
+ 33, 34, 28, 61, 8, 40, 37, 38, 54, 48,
+ 49, 56, 57, 35, 36, 39, 29, 30, 31, 55,
+ 17, 32, 33, 34, 28, 46, 74, 40, 37, 38,
+ 50, 51, 52, 53, 75, 35, 36, 39 ]
+
+racc_action_check = [
+ 68, 19, 50, 50, 50, 50, 50, 50, 56, 19,
+ 19, 50, 50, 16, 19, 19, 0, 0, 0, 0,
+ 0, 0, 1, 68, 50, 0, 0, 8, 8, 8,
+ 8, 8, 8, 52, 53, 1, 8, 8, 0, 52,
+ 53, 17, 17, 17, 17, 17, 17, 54, 21, 8,
+ 17, 17, 47, 47, 47, 47, 47, 47, 55, 54,
+ 7, 47, 47, 17, 18, 18, 18, 51, 57, 18,
+ 18, 18, 18, 51, 47, 18, 18, 18, 45, 25,
+ 25, 45, 45, 18, 18, 18, 49, 49, 49, 45,
+ 60, 49, 49, 49, 49, 20, 69, 49, 49, 49,
+ 27, 27, 27, 27, 73, 49, 49, 49 ]
+
+racc_action_pointer = [
+ 14, 22, nil, nil, nil, nil, nil, 46, 25, nil,
+ nil, nil, nil, nil, nil, nil, 13, 39, 62, -12,
+ 70, 32, nil, nil, nil, 64, nil, 83, nil, nil,
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ nil, nil, nil, nil, nil, 73, nil, 50, nil, 84,
+ 0, 61, 27, 28, 31, 53, -20, 40, nil, nil,
+ 77, nil, nil, nil, nil, nil, nil, nil, -5, 68,
+ nil, nil, nil, 76, nil, nil ]
+
+racc_action_default = [
+ -54, -54, -1, -3, -4, -5, -6, -7, -32, -47,
+ -48, -49, -50, -51, -52, -53, -54, -54, -54, -36,
+ -54, -33, -34, 76, -2, -54, -9, -54, -18, -19,
+ -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
+ -30, -37, -38, -39, -40, -54, -31, -54, -8, -54,
+ -54, -54, -54, -54, -54, -54, -54, -54, -35, -10,
+ -11, -12, -15, -13, -16, -14, -17, -41, -54, -54,
+ -45, -46, -42, -54, -43, -44 ]
+
+racc_goto_table = [
+ 1, 22, 26, 20, 25, 21, 24, 41, nil, nil,
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ nil, nil, nil, 59, nil, nil, nil, nil, nil, nil,
+ 58, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ 60 ]
+
+racc_goto_check = [
+ 1, 12, 8, 10, 7, 11, 2, 13, nil, nil,
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ nil, nil, nil, 8, nil, nil, nil, nil, nil, nil,
+ 12, nil, nil, nil, nil, nil, nil, nil, nil, nil,
+ 1 ]
+
+racc_goto_pointer = [
+ nil, 0, -11, nil, nil, nil, nil, -14, -16, nil,
+ -5, -3, -7, -12 ]
+
+racc_goto_default = [
+ nil, 19, 2, 3, 4, 5, 6, nil, nil, 27,
+ nil, nil, nil, nil ]
+
+racc_reduce_table = [
+ 0, 0, :racc_error,
+ 1, 30, :_reduce_none,
+ 3, 30, :_reduce_2,
+ 1, 31, :_reduce_none,
+ 1, 31, :_reduce_none,
+ 1, 31, :_reduce_none,
+ 1, 31, :_reduce_none,
+ 1, 32, :_reduce_7,
+ 4, 32, :_reduce_8,
+ 1, 36, :_reduce_none,
+ 3, 36, :_reduce_10,
+ 3, 37, :_reduce_11,
+ 3, 37, :_reduce_12,
+ 3, 37, :_reduce_13,
+ 3, 37, :_reduce_14,
+ 3, 37, :_reduce_15,
+ 3, 37, :_reduce_16,
+ 3, 37, :_reduce_17,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 1, 38, :_reduce_none,
+ 3, 33, :_reduce_31,
+ 0, 39, :_reduce_32,
+ 1, 39, :_reduce_none,
+ 1, 40, :_reduce_34,
+ 3, 40, :_reduce_35,
+ 1, 41, :_reduce_none,
+ 2, 41, :_reduce_37,
+ 1, 42, :_reduce_38,
+ 1, 42, :_reduce_39,
+ 1, 42, :_reduce_40,
+ 3, 42, :_reduce_41,
+ 4, 42, :_reduce_42,
+ 4, 42, :_reduce_43,
+ 5, 42, :_reduce_44,
+ 3, 42, :_reduce_45,
+ 3, 42, :_reduce_46,
+ 1, 34, :_reduce_47,
+ 1, 34, :_reduce_48,
+ 1, 34, :_reduce_49,
+ 1, 34, :_reduce_50,
+ 1, 34, :_reduce_51,
+ 1, 34, :_reduce_52,
+ 1, 35, :_reduce_53 ]
+
+racc_reduce_n = 54
+
+racc_shift_n = 76
+
+racc_token_table = {
+ false => 0,
+ :error => 1,
+ :TRUE => 2,
+ :FALSE => 3,
+ :NIL => 4,
+ :INTEGER => 5,
+ :STRING => 6,
+ :ANY => 7,
+ :EVEN => 8,
+ :ODD => 9,
+ :METHOD_NAME => 10,
+ :CLASS_NAME => 11,
+ :SYMBOL => 12,
+ "|" => 13,
+ "<" => 14,
+ ">" => 15,
+ "," => 16,
+ "=" => 17,
+ "^=" => 18,
+ "$=" => 19,
+ "*=" => 20,
+ "*" => 21,
+ "+" => 22,
+ "^" => 23,
+ "[" => 24,
+ "]" => 25,
+ "?" => 26,
+ "{" => 27,
+ "}" => 28 }
+
+racc_nt_base = 29
+
+racc_use_result_var = true
+
+Racc_arg = [
+ racc_action_table,
+ racc_action_check,
+ racc_action_default,
+ racc_action_pointer,
+ racc_goto_table,
+ racc_goto_check,
+ racc_goto_default,
+ racc_goto_pointer,
+ racc_nt_base,
+ racc_reduce_table,
+ racc_token_table,
+ racc_shift_n,
+ racc_reduce_n,
+ racc_use_result_var ]
+
+Racc_token_to_s_table = [
+ "$end",
+ "error",
+ "TRUE",
+ "FALSE",
+ "NIL",
+ "INTEGER",
+ "STRING",
+ "ANY",
+ "EVEN",
+ "ODD",
+ "METHOD_NAME",
+ "CLASS_NAME",
+ "SYMBOL",
+ "\"|\"",
+ "\"<\"",
+ "\">\"",
+ "\",\"",
+ "\"=\"",
+ "\"^=\"",
+ "\"$=\"",
+ "\"*=\"",
+ "\"*\"",
+ "\"+\"",
+ "\"^\"",
+ "\"[\"",
+ "\"]\"",
+ "\"?\"",
+ "\"{\"",
+ "\"}\"",
+ "$start",
+ "expression",
+ "primary",
+ "node",
+ "array",
+ "literal",
+ "any",
+ "attrs",
+ "attr",
+ "method_name",
+ "items_opt",
+ "items",
+ "item",
+ "quantifier" ]
+
+Racc_debug_parser = false
+
+##### State transition tables end #####
+
+# reduce 0 omitted
+
+# reduce 1 omitted
+
+module_eval(<<'.,.,', 'parser.y', 20)
+ def _reduce_2(val, _values, result)
+ result = if val[0].is_a?(ChoiceMatcher)
+ ChoiceMatcher.new(val[0].alternatives << val[2])
+ else
+ ChoiceMatcher.new([val[0], val[2]])
+ end
+
+ result
+ end
+.,.,
+
+# reduce 3 omitted
+
+# reduce 4 omitted
+
+# reduce 5 omitted
+
+# reduce 6 omitted
+
+module_eval(<<'.,.,', 'parser.y', 33)
+ def _reduce_7(val, _values, result)
+ result = NodeMatcher.new(val[0].to_sym)
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 36)
+ def _reduce_8(val, _values, result)
+ result = NodeMatcher.new(val[0].to_sym, val[2])
+
+ result
+ end
+.,.,
+
+# reduce 9 omitted
+
+module_eval(<<'.,.,', 'parser.y', 40)
+ def _reduce_10(val, _values, result)
+ result = val[0].merge(val[2])
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 42)
+ def _reduce_11(val, _values, result)
+ result = { val[0].to_sym => val[2] }
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 44)
+ def _reduce_12(val, _values, result)
+ result = {
+ val[0].to_sym => SymbolRegexpMatcher.new(
+ Regexp.new("^" + Regexp.escape(symbol_value(val[2])))
+ )
+ }
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 51)
+ def _reduce_13(val, _values, result)
+ result = {
+ val[0].to_sym => SymbolRegexpMatcher.new(
+ Regexp.new(Regexp.escape(symbol_value(val[2])) + "$")
+ )
+ }
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 58)
+ def _reduce_14(val, _values, result)
+ result = {
+ val[0].to_sym => SymbolRegexpMatcher.new(
+ Regexp.new(Regexp.escape(symbol_value(val[2])))
+ )
+ }
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 65)
+ def _reduce_15(val, _values, result)
+ result = {
+ val[0].to_sym => StringRegexpMatcher.new(
+ Regexp.new("^" + Regexp.escape(string_value(val[2])))
+ )
+ }
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 72)
+ def _reduce_16(val, _values, result)
+ result = {
+ val[0].to_sym => StringRegexpMatcher.new(
+ Regexp.new(Regexp.escape(string_value(val[2])) + "$")
+ )
+ }
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 79)
+ def _reduce_17(val, _values, result)
+ result = {
+ val[0].to_sym => StringRegexpMatcher.new(
+ Regexp.new(Regexp.escape(string_value(val[2])))
+ )
+ }
+
+ result
+ end
+.,.,
+
+# reduce 18 omitted
+
+# reduce 19 omitted
+
+# reduce 20 omitted
+
+# reduce 21 omitted
+
+# reduce 22 omitted
+
+# reduce 23 omitted
+
+# reduce 24 omitted
+
+# reduce 25 omitted
+
+# reduce 26 omitted
+
+# reduce 27 omitted
+
+# reduce 28 omitted
+
+# reduce 29 omitted
+
+# reduce 30 omitted
+
+module_eval(<<'.,.,', 'parser.y', 103)
+ def _reduce_31(val, _values, result)
+ result = ArrayMatcher.new(val[1])
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 105)
+ def _reduce_32(val, _values, result)
+ result = []
+ result
+ end
+.,.,
+
+# reduce 33 omitted
+
+module_eval(<<'.,.,', 'parser.y', 108)
+ def _reduce_34(val, _values, result)
+ result = [val[0]]
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 109)
+ def _reduce_35(val, _values, result)
+ result = val[0] << val[2]
+ result
+ end
+.,.,
+
+# reduce 36 omitted
+
+module_eval(<<'.,.,', 'parser.y', 112)
+ def _reduce_37(val, _values, result)
+ result = Quantifier.new(val[0], *val[1])
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 114)
+ def _reduce_38(val, _values, result)
+ result = [0, nil, 1]
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 115)
+ def _reduce_39(val, _values, result)
+ result = [1, nil, 1]
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 116)
+ def _reduce_40(val, _values, result)
+ result = [0, 1, 1]
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 118)
+ def _reduce_41(val, _values, result)
+ result = [integer_value(val[1]), integer_value(val[1]), 1]
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 121)
+ def _reduce_42(val, _values, result)
+ result = [integer_value(val[1]), nil, 1]
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 124)
+ def _reduce_43(val, _values, result)
+ result = [0, integer_value(val[2]), 1]
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 127)
+ def _reduce_44(val, _values, result)
+ result = [integer_value(val[1]), integer_value(val[3]), 1]
+
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 129)
+ def _reduce_45(val, _values, result)
+ result = [0, nil, 2]
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 130)
+ def _reduce_46(val, _values, result)
+ result = [1, nil, 2]
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 132)
+ def _reduce_47(val, _values, result)
+ result = LiteralMatcher.new(symbol_value(val[0]).to_sym)
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 133)
+ def _reduce_48(val, _values, result)
+ result = LiteralMatcher.new(integer_value(val[0]))
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 134)
+ def _reduce_49(val, _values, result)
+ result = LiteralMatcher.new(string_value(val[0]))
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 135)
+ def _reduce_50(val, _values, result)
+ result = LiteralMatcher.new(true)
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 136)
+ def _reduce_51(val, _values, result)
+ result = LiteralMatcher.new(false)
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 137)
+ def _reduce_52(val, _values, result)
+ result = LiteralMatcher.new(nil)
+ result
+ end
+.,.,
+
+module_eval(<<'.,.,', 'parser.y', 139)
+ def _reduce_53(val, _values, result)
+ result = AnyMatcher.new
+ result
+ end
+.,.,
+
+def _reduce_none(val, _values, result)
+ val[0]
+end
+
+ end # class Parser
+ end # module Machete

0 comments on commit 92baac1

Please sign in to comment.