daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Feb 20th, 2018 54 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. class Lexer
  2.   Token = Struct.new(:name, :pattern)
  3.  
  4.   def initialize
  5.     @tokens = Array.new
  6.   end
  7.  
  8.   def add_token(*args)
  9.     @tokens << Token.new(*args)
  10.   end
  11.  
  12.   def lex(data)
  13.     copy, result = data.dup, Array.new
  14.     until copy.empty?
  15.       @tokens.find { |t|
  16.         copy.sub!(t.pattern, "") && (t.name.nil? || result << [t.name, $&])
  17.       } || raise(ArgumentError, "Malformed data could not be lexed correctly: #{data}. Copy is: #{copy[0..3].inspect}.  Parsed is:  #{result.inspect}")
  18.     end
  19.     result
  20.   end
  21. end
  22. class CommandParser
  23.   def self.snippet(str)
  24.     self.parse(self.lex(str))
  25.   end
  26.   def self.lex(str)
  27.     lexer = Lexer.new
  28.     [ [ :fun_begin,      /\A(?:\w|\.)+\(/                 ],
  29.       [ :assignment,     /\A[\w.]+\s*<?=\s*/              ],
  30.       [ :term,           /\A[$\w.]+(?=,|)/                ],
  31.       [ :term,           /\A\.\.\./                       ],
  32.       [ :comma,          /\A,\s*/                         ],
  33.       [ :fun_end,        /\A\)/                           ],
  34.       [ :quoted,         /\A".*?(?!\\)"/                  ],
  35.       [ :operator,       /\A\s*([\+\-\*\/!^]|&&|\|\||<=?|>=?|~)\s*/ ],
  36.       [ :number,         /\A\d+(\.\d+)?/                  ],
  37.       [ :brace_begin,    /\A\{/                           ],
  38.       [ :brace_end,      /\A\}/                           ],
  39.       [ :bracket_begin,  /\A\[/                           ],
  40.       [ :list_separator, /\A:/                            ],
  41.       [ nil,             /\A\s+/                            ] ].each do |name, regex|
  42.       lexer.add_token(name, regex)
  43.     end
  44.     lexer.lex(str)
  45.   end
  46.   def self.parse(data)
  47.     snippet = ""
  48.     snippet_counter = -1
  49.     stack = []
  50.     until data.empty?
  51.       type,match = data.shift
  52.       if !stack.empty? && stack.last == :assignment && type.to_s =~ /comma|end$/ then
  53.         snippet << "}"
  54.         stack.pop
  55.       end
  56.       case type
  57.       when :fun_begin, :brace_begin, :bracket_begin
  58.         stack << :group
  59.         snippet << "${#{snippet_counter+=1}:#{match}${#{snippet_counter+=1}:"
  60.       when :assignment
  61.         stack << :assignment
  62.         snippet << "#{match}${#{snippet_counter+=1}:"
  63.       when :comma
  64.         snippet << ("}${#{snippet_counter+=1}:" + match)
  65.       when :brace_end, :bracket_end, :fun_end
  66.         snippet << "}#{match}}"
  67.         stack.pop
  68.       when :quoted
  69.         snippet << "${#{snippet_counter+=1}:\"${#{snippet_counter+=1}:#{match[1..-2].gsub("}","\\}")}}\"}"
  70.       else
  71.         snippet << match
  72.       end
  73.     end
  74.     raise "Too many levels: #{snippet}" unless stack.length == 0
  75.     snippet[4..-2]
  76.   end
  77. end
  78. require 'pp'
  79. data=DATA.read.split("\n")
  80. # 100.times
  81.   # d = data[rand(2000)]
  82. data.each do |d|
  83.   CommandParser.snippet(d)
  84. end
  85.  
  86. __END__
  87. read.table(file, header = FALSE, sep = "", quote = "\"'", dec = ".", row.names, col.names, as.is = FALSE, na.strings = "NA", colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, comment.char = "#", allowEscapes = FALSE)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top