Show
Ignore:
Timestamp:
06/18/06 15:02:55 (2 years ago)
Author:
audreyt
Message:

* Support for rule/regex/token distinction in the ->PGE bridge.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim/Match.hs

    r10113 r10736  
    1414import qualified Data.Array as Array 
    1515 
     16-- XXX - kluge: before we figure out the parrot calling convention, 
     17--       we'll simply inline the adverbs into the regex. 
     18ruleWithAdverbs :: VRule -> Eval VStr 
     19ruleWithAdverbs MkRulePGE{ rxRule = re, rxAdverbs = advs } = do 
     20    when (null re) $ 
     21        fail "Null patterns are invalid; use <?null> or an empty string instead" 
     22    hv      <- join $ doHash advs hash_fetch 
     23    advs    <- forM (Map.assocs hv) $ \(k, v) -> do 
     24        str <- case v of 
     25            VBool True  -> return "1" 
     26            VBool False -> return "0" 
     27            _           -> fromVal v 
     28        return $ \x -> ":" ++ k ++ "(" ++ str ++ ")[" ++ x ++ "]" 
     29    return $ combine advs re 
     30ruleWithAdverbs _ = fail "PCRE regexes can't be compiled to PGE regexes" 
     31 
    1632doMatch :: String -> VRule -> Eval VMatch 
    17 -- Work around PGE bug on Parrot 0.3.1 -- empty rules are errors 
    18 doMatch _ MkRulePGE{ rxRule = "" } = fail "Null patterns are invalid; use <?null> or an empty string instead" 
    19 doMatch cs MkRulePGE{ rxRule = re } = do 
     33doMatch cs rule@MkRulePGE{ rxRule = ruleStr } = do 
    2034    let pwd1 = getConfig "installarchlib" ++ "/CORE/pugs/pge" 
    2135        pwd2 = getConfig "sourcedir" ++ "/src/pge" 
     
    2539    let syms = [ (name, tvar) | (('<':'*':name), [(_, tvar)]) <- padToList glob ] 
    2640    subrules <- forM syms $ \(name, tvar) -> do 
    27         ref  <- liftSTM $ readTVar tvar 
    28         (VRule rule) <- fromVal =<< readRef ref 
    29         return (name, rxRule rule) 
    30     pge <- liftIO $ evalPGE pwd (encodeUTF8 cs) (encodeUTF8 re) subrules 
     41        ref         <- liftSTM $ readTVar tvar 
     42        VRule rule  <- fromVal =<< readRef ref 
     43        text        <- ruleWithAdverbs rule 
     44        return (name, text) 
     45    text <- ruleWithAdverbs rule 
     46    pge  <- liftIO $ evalPGE pwd (encodeUTF8 cs) (encodeUTF8 text) subrules 
    3147            `catch` (\e -> return $ ioeGetErrorString e) 
    3248    rv  <- tryIO Nothing $ fmap Just (readIO $ decodeUTF8 pge) 
     
    4359        Just m  -> fromVal (matchToVal m) 
    4460        Nothing -> do 
    45             liftIO $ putStrLn ("*** Cannot parse PGE: " ++ re ++ "\n*** Error: " ++ pge) 
     61            liftIO $ putStrLn ("*** Cannot parse PGE: " ++ ruleStr ++ "\n*** Error: " ++ pge) 
    4662            return mkMatchFail 
    4763