Changeset 3388 for src/Pugs/Parser.hs

Show
Ignore:
Timestamp:
05/18/05 16:06:00 (4 years ago)
Author:
iblech
svk:copy_cache_prev:
4977
Message:

User defined infix ops! :)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Parser.hs

    r3372 r3388  
    810810tightOperators = do 
    811811  [optionary, namedUnary, preUnary, postUnary] <- currentUnaryFunctions 
     812  infixOps <- currentInfixOps 
    812813  return $ 
    813814    [ methOps  " . .+ .? .* .+ .() .[] .{} .<<>> .= "   -- Method postfix 
     
    823824               " >>*<< >>/<< >>x<< >>xx<< >>~<< " ++ 
    824825               " * / % x xx +& +< +> ~& ~< ~> "         -- Multiplicative 
     826    , leftOps $ " " ++ unwords infixOps ++ " "          -- User defined ops 
     827                                                        -- XXX: But they shouldn't 
     828                                                        -- automatically be leftOps, 
     829                                                        -- right? --iblech 
    825830    , leftOps  " »+« >>+<< + - ~ +| +^ ~| ~^ ?| "       -- Additive 
    826831    , listOps  " & "                                    -- Junctive And 
     
    925930    where 
    926931    mapPair f (x, y) = (f x, f y) 
     932 
     933-- Following code is from a Haskell newbie. 
     934-- Please check for correctness. --iblech 
     935currentInfixOps :: RuleParser [String] 
     936currentInfixOps = do 
     937    -- We retrieve the list of all current functions 
     938    funs    <- currentFunctions 
     939    -- Then we grep for the &infix:... ones. 
     940    let (infixs, _) = (`partition` funs) $ \x -> case x of 
     941            ('i':'n':'f':'i':'x':':':_, _, _) -> True 
     942            _  -> False 
     943    -- Finally, we return the names of the ops. 
     944    -- But we've to s/^infix://, as we've to return (say) "+" instead of "infix:+". 
     945    return $ map extractName infixs 
     946    where 
     947    extractName ('i':'n':'f':'i':'x':':':name, _, _) = encodeUTF8 name 
     948    -- GHC dies with "non exhaustive patterns" if I omit the following line. 
     949    -- But as we grep above for /^infix:/, we can't possible branch here. 
     950    extractName _ = error "Should never happen (Parser.hs:currentInfixOps:extractName)" 
    927951 
    928952parseOp :: RuleParser Exp