parseGrepResult :: Text -> Maybe (FilePath, Int, Text)
parseGrepResult line
  | length ss < 2 = if ("Binary file" `isPrefixOf` (unpack line)) then Nothing
                                                                    else error ("fail to parse the result of grep: " ++ unpack line)
  | otherwise     = Just (fn, num, matched_string)
    where
      ss = split (== ':') $ line
      fn = fromText $ ss !! 0
      num = read $ ss !! 1
      matched_string = ss !! 2

-- |
--
-- >>> getPrimaryWord (map pack ["<div>", "  <%= foo %>", "</div>"]) 1 (fromText $ pack "/foo/bar.html.erb")
-- (RegexpObjective (Just "foo") "render.*bar",Nothing,[])
--
-- >>> getPrimaryWord (map pack ["def foo", "  print 123", "end"]) 1 (fromText $ pack "/foo/bar.rb")
-- (WordObjective "foo",Just 0,[Corner (RbMethod (Just "foo")) "def foo",Corner CurrentLine "  print 123",Corner RbEnd "end"])
--
-- >>> getPrimaryWord (map pack ["foo: function(e){", "  console.log(e)", "}"]) 1 (fromText $ pack "/foo/bar.js")
-- (WordObjective "foo",Just 0,[Corner (JsFunc (Just "foo")) "foo: function(e){",Corner CurrentLine "  console.log(e)",Corner JsEnd "}"])
--
parseGrepResult :: Text -> Maybe (FilePath, Int, Text)
parseGrepResult line
  | length ss < 2 = if ("Binary file" `isPrefixOf` (unpack line)) then Nothing
                                                                    else error ("fail to parse the result of grep: " ++ unpack line)
  | otherwise     = Just (fn, num, matched_string)
    where
      ss = split (== ':') $ line
      fn = fromText $ ss !! 0
      num = read $ ss !! 1
      matched_string = ss !! 2

-- |
--
-- >>> getPrimaryWord (map pack ["<div>", "  <%= foo %>", "</div>"]) 1 (fromText $ pack "/foo/bar.html.erb")
-- (RegexpObjective (Just "foo") "render.*bar",Nothing,[])
--
-- >>> getPrimaryWord (map pack ["def foo", "  print 123", "end"]) 1 (fromText $ pack "/foo/bar.rb")
-- (WordObjective "foo",Just 0,[Corner (RbMethod (Just "foo")) "def foo",Corner CurrentLine "  print 123",Corner RbEnd "end"])
--
-- >>> getPrimaryWord (map pack ["foo: function(e){", "  console.log(e)", "}"]) 1 (fromText $ pack "/foo/bar.js")
-- (WordObjective "foo",Just 0,[Corner (JsFunc (Just "foo")) "foo: function(e){",Corner CurrentLine "  console.log(e)",Corner JsEnd "}"])
--
parseGrepResult line
  | length ss < 2 = if ("Binary file" `isPrefixOf` (unpack line)) then Nothing
                                                                    else error ("fail to parse the result of grep: " ++ unpack line)
  | otherwise     = Just (fn, num, matched_string)
    where
      ss = split (== ':') $ line
      fn = fromText $ ss !! 0
      num = read $ ss !! 1
      matched_string = ss !! 2

-- |
--
-- >>> getPrimaryWord (map pack ["<div>", "  <%= foo %>", "</div>"]) 1 (fromText $ pack "/foo/bar.html.erb")
-- (RegexpObjective (Just "foo") "render.*bar",Nothing,[])
--
-- >>> getPrimaryWord (map pack ["def foo", "  print 123", "end"]) 1 (fromText $ pack "/foo/bar.rb")
-- (WordObjective "foo",Just 0,[Corner (RbMethod (Just "foo")) "def foo",Corner CurrentLine "  print 123",Corner RbEnd "end"])
--
-- >>> getPrimaryWord (map pack ["foo: function(e){", "  console.log(e)", "}"]) 1 (fromText $ pack "/foo/bar.js")
-- (WordObjective "foo",Just 0,[Corner (JsFunc (Just "foo")) "foo: function(e){",Corner CurrentLine "  console.log(e)",Corner JsEnd "}"])
--