Skip to content

Commit

Permalink
Remove regex and skip ansi codes via the parser
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed Jun 29, 2019
1 parent f6a94ff commit 9402a69
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 40 deletions.
11 changes: 6 additions & 5 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{ mkDerivation, ansi-terminal, base, bytestring, github, hpack
, hslogger, lens, megaparsec, optparse-applicative, persistent
, persistent-sqlite, persistent-template, regex-compat, stdenv
, tasty, tasty-hspec, tasty-quickcheck, text, time, vector
, hslogger, lens, megaparsec, optparse-applicative
, parser-combinators, persistent, persistent-sqlite
, persistent-template, stdenv, tasty, tasty-hspec, tasty-quickcheck
, text, time, vector
}:
mkDerivation {
pname = "performabot";
Expand All @@ -11,8 +12,8 @@ mkDerivation {
isExecutable = true;
libraryHaskellDepends = [
ansi-terminal base bytestring github hslogger lens megaparsec
persistent persistent-sqlite persistent-template regex-compat text
time vector
parser-combinators persistent persistent-sqlite persistent-template
text time vector
];
libraryToolDepends = [ hpack ];
executableHaskellDepends = [
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ library:
- hslogger
- lens
- megaparsec
- parser-combinators
- persistent
- persistent-sqlite
- persistent-template
- regex-compat
- text
- time
- vector
Expand Down
4 changes: 2 additions & 2 deletions performabot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: e66baad00a9ee7a23314f98b28839f9bbcb511da9795672b6bf1c4cecb9eb427
-- hash: a475f7d00ba30f4d6c0ae33a24988e83878600b3f468fd64bba9f2488281705d

name: performabot
version: 0.1.0
Expand Down Expand Up @@ -52,10 +52,10 @@ library
, hslogger
, lens
, megaparsec
, parser-combinators
, persistent
, persistent-sqlite
, persistent-template
, regex-compat
, text
, time
, vector
Expand Down
51 changes: 23 additions & 28 deletions src/ParserGo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
-- @since 0.1.0
module ParserGo ( parse ) where

import Control.Lens ( (.~), (^.) )
import Control.Lens ( (.~), (^.) )
import Control.Monad.Combinators ( skipMany )

import Data.Text ( pack )
import Data.Text ( pack )

import Model
( Benchmark(Benchmark), benchmarkSamples, emptyBenchmark )

import Parser ( Parser, State(Ok, Failure, NeedMore)
, StringParser, double, integer )
import Parser
( Parser, State(Ok, Failure, NeedMore), StringParser, double
, integer )

import Text.Megaparsec ( Token, anySingle, eof, errorBundlePretty
, manyTill, runParser )
import Text.Megaparsec.Char ( char, space1, spaceChar, string )
import Text.Regex ( mkRegex, subRegex )
import Text.Megaparsec
( anySingle, eof, errorBundlePretty, manyTill, runParser )
import Text.Megaparsec.Char
( char, numberChar, space, space1, spaceChar, string )

-- | Parses golang (`ginkgo --succinct`) benchmarks based on the given input
-- state, for example:
Expand All @@ -30,41 +32,34 @@ parse _ i = runStep (step0 emptyBenchmark) i

-- | Run a single step abstraction
runStep :: Parser -> String -> State
runStep a i = case runParser a "" (ansiFilter i) of
runStep a i = case runParser a "" i of
Left e -> Failure $ errorBundlePretty e
Right r -> r

-- | Strip all colors from the string
ansiFilter :: String -> String
ansiFilter line = subRegex (mkRegex "\\[[0-9]+m") stripped ""
where
stripped = filter (/= '\ESC') line

-- | The initial parse step
step0 :: Benchmark -> Parser
step0 b = do
space1
_ <- space1 <* ansi
s <- integer
_ <- string "samples" <* colon <* eof
_ <- ansi <* space <* string "samples:" <* eof
return . NeedMore $ benchmarkSamples .~ s $ b

-- | The last parse step
step1 :: Benchmark -> Parser
step1 b = do
space1
n <- manyTill anySingle $ string " - "
_ <- string "Fastest" <* spaceChar <* string "Time" <* colon
_ <- spaceChar <* double <* s <* char ',' <* spaceChar
_ <- string "Average" <* spaceChar <* string "Time" <* colon <* spaceChar
_ <- space1 <* ansi
n <- manyTill anySingle $ ansi <* string " - Fastest Time: "
_ <- ansi <* double <* ansi <* s <* char ',' <* spaceChar
<* string "Average Time: " <* ansi
a <- double
_ <- s <* spaceChar <* char '' <* spaceChar
_ <- ansi <* s <* spaceChar <* char '' <* spaceChar <* ansi
d <- double
_ <- s <* char ',' <* spaceChar <* string "Slowest" <* spaceChar
_ <- string "Time" <* colon <* spaceChar <* double <* s <* eof
_ <- ansi <* s <* string ", Slowest Time: " <* ansi <* double <* ansi <* s
<* eof
return . Ok $ Benchmark a d (pack n) (b ^. benchmarkSamples) "s"
where
s = char 's'

-- | Parses a single colon
colon :: StringParser (Token String)
colon = char ':'
-- | Parses ansi escape sequences
ansi :: StringParser ()
ansi = skipMany $ char '\ESC' <* char '[' <* manyTill numberChar (char 'm')
8 changes: 4 additions & 4 deletions test/ParserGoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ parserGoSpec = parallel $ do
^. benchmarkSamples `shouldBe` 192835128754

it "should succeed to parse with ansi colors" $
benchmark (parse Init " [1m20[0m samples:")
benchmark (parse Init " \ESC[1m20\ESC[0m samples:")
^. benchmarkSamples `shouldBe` 20

it "should fail to parse empty input" $ failure (parse Init "")
Expand All @@ -66,13 +66,13 @@ parserGoSpec = parallel $ do
`shouldContain` "expecting white space"

it "should fail to parse without integer" $ failure (parse Init " wrong")
`shouldContain` "expecting integer"
`shouldContain` "expecting escape, integer, or white space"

it "should fail to parse without 'samples'" $ failure (parse Init " 10 ")
`shouldContain` "expecting \"samples\""
`shouldContain` "expecting \"samples:\""

it "should fail to parse without colon" $ failure (parse Init " 10 samples")
`shouldContain` "expecting ':'"
`shouldContain` "expecting \"samples:\""

it "should fail to parse without eof" $
failure (parse Init " 10 samples: wrong")
Expand Down

0 comments on commit 9402a69

Please sign in to comment.