Ticket #205: 0001-Haskell-support-take-two.patch

File 0001-Haskell-support-take-two.patch, 13.4 kB (added by tux_rocker, 2 years ago)

New patch against a fresh checkout

  • a/ext/ohcount_native/generator.rb

    old new  
    3434      java = CMonoglot.new("java",               '//',             [e('/*'), e('*/')], true,  false) 
    3535      javascript = CMonoglot.new("javascript",   '//',             [e('/*'), e('*/')], true,  true) 
    3636      emacslisp = LineCommentMonoglot.new("emacslisp", ";") 
     37      haskell = CMonoglot.new("haskell",         '--',             [e('{-'), e('-}')], true, false) 
    3738      lisp = LineCommentMonoglot.new("lisp", ";") 
    3839      lua = CMonoglot.new("lua",                 '--',             nil,                true,  true) 
    3940      matlab = CMonoglot.new("matlab",           '#|%',            ['{%', '%}'], false,true) 
     
    7576        java , 
    7677        javascript , 
    7778        emacslisp , 
     79        haskell, 
    7880        lisp , 
    7981        lua , 
    8082        matlab, 
  • a/lib/ohcount/detector.rb

    old new  
    120120    '.h'    => :disambiguate_h_header, 
    121121    '.hpp'  => "cncpp", 
    122122    '.h++'  => "cncpp", 
     123    '.hs'   => "haskell", 
    123124    '.hxx'  => "cncpp", 
    124125    '.hh'   => "cncpp", 
    125126    '.hrl'  => "erlang", 
  • /dev/null

    old new  
  • /dev/null

    old new  
     1module Gnutella.Misc where 
     2import Data.ByteString(ByteString) 
     3import qualified Data.ByteString as BS 
     4import Data.Bits 
     5import Data.Word 
     6import Text.Read 
     7import Data.Char(isNumber) 
     8import Data.List(intersperse) 
     9import Network 
     10import Network.BSD(getHostByName, HostEntry(..)) 
     11import Network.Socket(HostAddress(..)) 
     12import Debug.Trace 
     13composeWord32 :: ByteString -> Word32 
     14composeWord32 s = shiftL byte4 24 + shiftL byte3 16 + shiftL byte2 8 + byte1 
     15where byte1, byte2, byte3, byte4 :: Word32 
     16[byte1, byte2, byte3, byte4] = map fromIntegral $ BS.unpack (BS.take 4 s) 
     17word32ToWord8s :: Word32 -> (Word8, Word8, Word8, Word8) 
     18word32ToWord8s w = (fromIntegral (w .&. 0x000000ff) 
     19,fromIntegral (shiftR w 8 .&. 0x000000ff) 
     20,fromIntegral (shiftR w 16 .&. 0x000000ff) 
     21,fromIntegral (shiftR w 24 .&. 0x000000ff) 
     22) 
     23parseHostnameWithPort :: String -> IO (Maybe ((Word8, Word8, Word8, Word8) 
     24,PortNumber)) 
     25parseHostnameWithPort str = do maybeHostName <- stringToIP hostNameStr 
     26return $ (do portNum <- maybePortNum 
     27hostName <- maybeHostName 
     28return (hostName, portNum) 
     29) 
     30where hostNameStr = takeWhile (/=':') str 
     31maybePortNum  = case tail (dropWhile (/=':') str) of 
     32[] -> Just $ 6346 
     33s  -> case reads s of 
     34[]     -> Nothing 
     35(x:xs) -> Just $ fromIntegral $ fst x 
     36ipStringToBytes :: String -> Maybe (Word8, Word8, Word8, Word8) 
     37ipStringToBytes s = 
     38let ipBytesStrings = splitAtDots s 
     39in if all (all isNumber) ipBytesStrings 
     40then let bytesList = map (fst . head . reads) ipBytesStrings 
     41in Just (bytesList!!0 
     42,bytesList!!1 
     43,bytesList!!2 
     44,bytesList!!3 
     45) 
     46else Nothing 
     47where splitAtDots s = foldr (\c (n:nums) -> if c == '.' 
     48then [] : n : nums 
     49else (c:n) : nums 
     50) [[]] s 
     51ipBytesToString :: (Word8, Word8, Word8, Word8) -> String 
     52ipBytesToString (b1, b2, b3, b4) =  
     53concat $ intersperse "." $ map show [b1, b2, b3, b4] 
     54stringToIP :: String -> IO (Maybe (Word8, Word8, Word8, Word8)) 
     55stringToIP hostName = case ipStringToBytes hostName of 
     56Just a  -> return (Just a) 
     57Nothing -> do hostent <- getHostByName hostName 
     58let ipWord32 = head (hostAddresses hostent) 
     59ipWord8s = word32ToWord8s ipWord32 
     60return (Just ipWord8s) 
     61instance Read PortNumber where 
     62readsPrec i = map (\(a, b) -> (fromIntegral a, b)) . (readsPrec i :: ReadS Word16) 
  • /dev/null

    old new  
     1{-| 
     2This module contains some functions that are useful in several places in the 
     3program and don't belong to one specific other module. 
     4-} 
     5{-| 
     6Maakt van vier bytes een Word32. Gaat ervan uit dat die vier bytes little-endian achter elkaar 
     7staan. Als de gegeven string korter is dan 4 bytes, termineert de functie. Als de string langer 
     8is, worden alle bytes voorbij de vierde genegeerd. 
     9-} 
     10{-|  
     11Turns a Word32 into a tuple of Word8s. The tuple is little-endian: the least 
     12significant octet comes first. 
     13-} 
     14{-| 
     15Parses a host specification in the "name:12345"-style notation into a hostname 
     16and a port number. 
     17As a rather special feature, it returns 6346 as the port number when there is 
     18no port specified. When there is a port specified, but it is unparseable, it 
     19returns Nothing. 
     20-} 
     21{-| 
     22Translates a string, representing an IP address, to a list of bytes. 
     23Returns Nothing when the string does not represent an IP address in xxx.xxx.xxx.xxx format 
     24-} 
     25-- Again, hugs won't let us use regexes where they would be damn convenient 
     26{-| 
     27Translates a list of bytes representing an IP address (big endian) to a string 
     28in the xxx.xxx.xxx.xxx format. 
     29-} 
     30{-|  
     31Takes a String that's either an IP address or a hostname, and returns you the 
     32IP address as a list of 4 bytes (in big-endian byte order). It returns Nothing 
     33if there is no parse for the string as IP address and the hostname can't be 
     34found. 
     35-} 
     36-- used in reading the hostcache 
  • /dev/null

    old new  
  • /dev/null

    old new  
     1{-| 
     2-} 
  • /dev/null

    old new  
     1{-| 
     2  This module contains some functions that are useful in several places in the 
     3  program and don't belong to one specific other module. 
     4-} 
     5module Gnutella.Misc where 
     6 
     7import Data.ByteString(ByteString) 
     8import qualified Data.ByteString as BS 
     9import Data.Bits 
     10import Data.Word 
     11import Text.Read 
     12import Data.Char(isNumber) 
     13import Data.List(intersperse) 
     14import Network 
     15import Network.BSD(getHostByName, HostEntry(..)) 
     16import Network.Socket(HostAddress(..)) 
     17import Debug.Trace 
     18 
     19{-| 
     20  Maakt van vier bytes een Word32. Gaat ervan uit dat die vier bytes little-endian achter elkaar 
     21  staan. Als de gegeven string korter is dan 4 bytes, termineert de functie. Als de string langer 
     22  is, worden alle bytes voorbij de vierde genegeerd. 
     23-} 
     24composeWord32 :: ByteString -> Word32 
     25composeWord32 s = shiftL byte4 24 + shiftL byte3 16 + shiftL byte2 8 + byte1 
     26  where byte1, byte2, byte3, byte4 :: Word32 
     27        [byte1, byte2, byte3, byte4] = map fromIntegral $ BS.unpack (BS.take 4 s) 
     28 
     29{-|  
     30  Turns a Word32 into a tuple of Word8s. The tuple is little-endian: the least 
     31  significant octet comes first. 
     32-} 
     33word32ToWord8s :: Word32 -> (Word8, Word8, Word8, Word8) 
     34word32ToWord8s w = (fromIntegral (w .&. 0x000000ff) 
     35                   ,fromIntegral (shiftR w 8 .&. 0x000000ff) 
     36                   ,fromIntegral (shiftR w 16 .&. 0x000000ff) 
     37                   ,fromIntegral (shiftR w 24 .&. 0x000000ff) 
     38                   ) 
     39 
     40{-| 
     41  Parses a host specification in the "name:12345"-style notation into a hostname 
     42  and a port number. 
     43 
     44  As a rather special feature, it returns 6346 as the port number when there is 
     45  no port specified. When there is a port specified, but it is unparseable, it 
     46  returns Nothing. 
     47-} 
     48parseHostnameWithPort :: String -> IO (Maybe ((Word8, Word8, Word8, Word8) 
     49                                             ,PortNumber)) 
     50parseHostnameWithPort str = do maybeHostName <- stringToIP hostNameStr 
     51                               return $ (do portNum <- maybePortNum 
     52                                            hostName <- maybeHostName 
     53                                            return (hostName, portNum) 
     54                                        ) 
     55  where hostNameStr = takeWhile (/=':') str 
     56        maybePortNum  = case tail (dropWhile (/=':') str) of 
     57                          [] -> Just $ 6346 
     58                          s  -> case reads s of 
     59                                  []     -> Nothing 
     60                                  (x:xs) -> Just $ fromIntegral $ fst x 
     61 
     62{-| 
     63  Translates a string, representing an IP address, to a list of bytes. 
     64  Returns Nothing when the string does not represent an IP address in xxx.xxx.xxx.xxx format 
     65-} 
     66ipStringToBytes :: String -> Maybe (Word8, Word8, Word8, Word8) 
     67-- Again, hugs won't let us use regexes where they would be damn convenient 
     68ipStringToBytes s = 
     69    let ipBytesStrings = splitAtDots s 
     70    in if all (all isNumber) ipBytesStrings 
     71         then let bytesList = map (fst . head . reads) ipBytesStrings 
     72              in Just (bytesList!!0 
     73                      ,bytesList!!1 
     74                      ,bytesList!!2 
     75                      ,bytesList!!3 
     76                      ) 
     77         else Nothing 
     78  where splitAtDots s = foldr (\c (n:nums) -> if c == '.' 
     79                                              then [] : n : nums 
     80                                              else (c:n) : nums 
     81                              ) [[]] s 
     82 
     83{-| 
     84  Translates a list of bytes representing an IP address (big endian) to a string 
     85  in the xxx.xxx.xxx.xxx format. 
     86-} 
     87ipBytesToString :: (Word8, Word8, Word8, Word8) -> String 
     88ipBytesToString (b1, b2, b3, b4) =  
     89    concat $ intersperse "." $ map show [b1, b2, b3, b4] 
     90 
     91{-|  
     92  Takes a String that's either an IP address or a hostname, and returns you the 
     93  IP address as a list of 4 bytes (in big-endian byte order). It returns Nothing 
     94  if there is no parse for the string as IP address and the hostname can't be 
     95  found. 
     96-} 
     97stringToIP :: String -> IO (Maybe (Word8, Word8, Word8, Word8)) 
     98stringToIP hostName = case ipStringToBytes hostName of 
     99                        Just a  -> return (Just a) 
     100                        Nothing -> do hostent <- getHostByName hostName 
     101                                      let ipWord32 = head (hostAddresses hostent) 
     102                                          ipWord8s = word32ToWord8s ipWord32 
     103                                      return (Just ipWord8s) 
     104 
     105-- used in reading the hostcache 
     106instance Read PortNumber where 
     107    readsPrec i = map (\(a, b) -> (fromIntegral a, b)) . (readsPrec i :: ReadS Word16) 
     108 
  • /dev/null

    old new  
     1    {-| 
     2 
     3    -} 
     4 
  • /dev/null

    old new  
     1# This has been shamelessly copied from java_test.rb because my Ruby is not so 
     2# good and I don't really know what people expect from a Ohcount language test 
     3#  -- Reinier Lamers 2008-01-19 
     4require File.dirname(__FILE__) + '/../test_helper' 
     5 
     6class Ohcount::HaskellTest < Ohcount::Test 
     7        def test_comments 
     8                lb = [Ohcount::LanguageBreakdown.new("haskell", "", "--comment", 0)] 
     9                assert_equal lb, Ohcount::parse(" --comment", "haskell") 
     10        end 
     11 
     12        def test_comprehensive 
     13                verify_parse("haskell1.hs") 
     14        end 
     15 
     16        def test_comprehensive_with_carriage_returns 
     17                verify_parse("haskell2.hs") 
     18        end 
     19end 
     20 
     21                 
     22