diff options
Diffstat (limited to 'Logstat/Parse.hs')
-rw-r--r-- | Logstat/Parse.hs | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/Logstat/Parse.hs b/Logstat/Parse.hs index 29415da..7e59258 100644 --- a/Logstat/Parse.hs +++ b/Logstat/Parse.hs @@ -18,7 +18,6 @@ import System.FilePath (takeDirectory,(</>)) import System.IO (hPutStr,stderr) import Text.Megaparsec import Text.Megaparsec.Expr -import Text.Regex.PCRE.Light import qualified Data.ByteString as B import qualified Data.Map.Strict as M import qualified Data.Text as T @@ -26,6 +25,7 @@ import qualified Data.Text.Encoding as T import qualified Text.Megaparsec.Lexer as L import Logstat.Types +import Logstat.Regex import Logstat.Value @@ -93,7 +93,14 @@ reg defopt = do <|> caseless <$ char 'i' case compileM (T.encodeUtf8 $ T.pack s) (defopt ++ opts) of Left e -> fail $ "Error in regular expression: " ++ e - Right r -> return r + -- Studying the regex at this point is somewhat premature - we may not + -- actually be using this regex in our program. However, we also should not + -- defer the study to after the main program has been assembled, because at + -- that point the func/proc expansion may have duplicated the Regex object. + -- Studying should ideally happen after assembling the main program but + -- before func/proc expansion. These actions are currently performed in the + -- wrong order. + Right r -> return (study r) expr :: Monad m => Parser m Expr expr = label "expression" $ makeExprParser term @@ -137,7 +144,29 @@ expr = label "expression" $ makeExprParser term <|> fromIntegral <$> try (char '0' >> char' 'o' >> L.octal) <|> fromIntegral <$> L.decimal - func n = do + func n + | n == "extract" = fextract + | n == "replace" = freplace + | otherwise = fdefined n + + fextract = do + e <- expr + symbol "," + r <- reg [] + when (captureCount r < 1) $ fail "Regular expression does not have any subpatterns to extract" + symbol ")" + return $ EExtract e r + + freplace = do + e <- expr + symbol "," + r <- reg [] + symbol "," + n <- str + symbol ")" + return $ EReplace e r n + + fdefined n = do f' <- M.lookup n . cfgFuncs <$> get (args, e) <- maybe (fail $ "Unknown function " ++ n) return f' a <- sepBy expr (symbol ",") |