Last active
November 17, 2019 17:59
-
-
Save tomphp/e014f4c2adf4d9625b4b1ed41fbd30ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LogParser exposing (..) | |
import Parser exposing (..) | |
type LogPart | |
= Text String | |
| URL String | |
logLine : Parser (List LogPart) | |
logLine = | |
many logPart | |
logPart : Parser LogPart | |
logPart = | |
oneOf | |
[ logPartURL | |
, logPartText | |
] | |
logPartURL : Parser LogPart | |
logPartURL = | |
Parser.map URL url | |
logPartText : Parser LogPart | |
logPartText = | |
Parser.map Text (getChompedString <| upto "http") | |
url : Parser String | |
url = | |
getChompedString (keyword "http" |. notWhitespace) | |
notWhitespace : Parser () | |
notWhitespace = | |
chompWhile (not << isWhitespace) | |
upto : String -> Parser () | |
upto match = | |
chompIf (\_ -> True) |. chompUntilEndOr match | |
isWhitespace : Char -> Bool | |
isWhitespace c = | |
c == ' ' || c == '\n' || c == '\u{000D}' | |
many : Parser a -> Parser (List a) | |
many parser = | |
loop [] (manyLoop parser) | |
manyLoop : Parser a -> List a -> Parser (Step (List a) (List a)) | |
manyLoop parser revParts = | |
oneOf | |
[ succeed (\part -> Loop (part :: revParts)) | |
|= parser | |
, succeed () | |
|> map (\_ -> Done (List.reverse revParts)) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment