Changeset 6109

Show
Ignore:
Timestamp:
08/07/05 20:12:10 (3 years ago)
Author:
putter
Message:

PCRE :global rules, without a subpattern, now return the correct match array. my @a = 'aaa' rx:perl5:g/a/; +@a #=> 3, not 0. PGE rules are not fixed, awaiting a way to determine if they contain a subpattern. Match .from and .to still incorrect. The patch is a bit messy. This would all be better handled by p6-side analysis and implementation of :global.

Location:
src
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/AST/Internals.hs

    r6067 r6109  
    16511651                                --     'Regex' object) 
    16521652        , rxGlobal    :: !Bool  -- ^ Flag indicating \'global\' (match-all) 
     1653        , rxNumSubs   :: !Int   -- ^ The number of subpatterns present. 
    16531654            , rxStringify :: !Bool 
    16541655        , rxRuleStr   :: !String -- ^ The rule string, for user reference. 
  • src/Pugs/Eval.hs

    r6061 r6109  
    4040import Pugs.External 
    4141import Pugs.Eval.Var 
     42 
     43import RRegex.PCRE as PCRE 
    4244 
    4345{-| 
     
    633635    flag_s  <- fromAdverb hv ["stringify"] -- XXX hack 
    634636    adverbHash <- reduce adverbs 
    635     let rx | p5 = MkRulePCRE p5re g flag_s str adverbHash 
     637    let p5re = mkRegexWithPCRE (encodeUTF8 str) $ 
     638                    [ pcreUtf8 
     639                    , ('i' `elem` p5flags || flag_i) `implies` pcreCaseless 
     640                    , ('m' `elem` p5flags) `implies` pcreMultiline 
     641                    , ('s' `elem` p5flags) `implies` pcreDotall 
     642                    , ('x' `elem` p5flags) `implies` pcreExtended 
     643                    ] 
     644    ns <- liftIO $ PCRE.numSubs p5re 
     645    let rx | p5 = MkRulePCRE p5re g ns flag_s str adverbHash 
    636646           | otherwise = MkRulePGE p6re g flag_s adverbHash 
    637647        g = ('g' `elem` p5flags || flag_g) 
     
    640650                      ':':_ -> ":w"   ++ str 
    641651                      _     -> ":w::" ++ str 
    642         p5re = mkRegexWithPCRE (encodeUTF8 str) $ 
    643                     [ pcreUtf8 
    644                     , ('i' `elem` p5flags || flag_i) `implies` pcreCaseless 
    645                     , ('m' `elem` p5flags) `implies` pcreMultiline 
    646                     , ('s' `elem` p5flags) `implies` pcreDotall 
    647                     , ('x' `elem` p5flags) `implies` pcreExtended 
    648                     ] 
    649652    retVal $ VRule rx 
    650653    where 
  • src/Pugs/Prim/Match.hs

    r6010 r6109  
    144144            else return (VList rv) 
    145145    where 
     146    hasSubpatterns = case rx of 
     147        MkRulePGE{}             -> True -- bogus 
     148        MkRulePCRE{rxNumSubs=n} -> not (n == 0) 
    146149    matchOnce :: String -> Eval [Val] 
    147150    matchOnce str = do 
     
    149152        if not (matchOk match) then return [] else do 
    150153        rest <- matchOnce (genericDrop (matchTo match) str) 
    151         return $ matchSubPos match ++ rest 
     154        return $ if hasSubpatterns 
     155                 then (matchSubPos match) ++ rest 
     156                 else (VMatch match):rest 
    152157 
    153158op2Match x (VRule rx) = do 
  • src/RRegex/PCRE.hs

    r3372 r6109  
    2323    pcreUtf8,       --  UTF-8 semantics 
    2424     
     25    numSubs, 
     26 
    2527    getVersion 
    2628    ) where 
     
    5961            return $ Left (fi eo,es) 
    6062          else fmap (Right . Regex) (newForeignPtr_ v) 
     63 
     64numSubs :: Regex -> IO Int 
     65numSubs (Regex pcre_fptr) = withForeignPtr pcre_fptr $ \pcre_ptr -> do 
     66    nsub <- getNumSubs pcre_ptr  
     67    return $ fromIntegral nsub 
    6168 
    6269getNumSubs :: Ptr PCRE -> IO CInt