Last active
March 27, 2019 11:07
-
-
Save ni-ko-o-kin/68793b1b9bd76f454028a56f2e6264cc to your computer and use it in GitHub Desktop.
This file contains 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 Main exposing (main) | |
import Browser | |
import Debug exposing (log) | |
import Html exposing (Html, button, div, text) | |
import Html.Events exposing (onClick) | |
import Parser exposing (..) | |
import Tuple exposing (pair) | |
type alias Model = | |
Maybe () | |
view : Model -> Html msg | |
view model = | |
div [] | |
[ div [] [ text funWithParsers ] | |
] | |
main : Program () Model msg | |
main = | |
Browser.sandbox | |
{ init = Nothing | |
, view = view | |
, update = \msg model -> model | |
} | |
groupSame : List String -> Parser (Step (List String) (List String)) | |
groupSame revStrs = | |
oneOf | |
[ succeed (\str -> Loop (log "loop" str :: revStrs)) | |
|= getChompedString chompWhileSame | |
, succeed () | |
|> map (\_ -> Done (log "done" List.reverse revStrs)) | |
] | |
funWithParsers : String | |
funWithParsers = | |
run (loop [] groupSame) "aAAABBBCC " | |
|> log "log" | |
|> Result.toMaybe | |
|> Maybe.withDefault [] | |
|> String.concat | |
-- expected output: ["a", "AAA", "BBB", "CC", " "] | |
-- actual output: infinite loop | |
-- run (getChompedString chompWhileSame) "aaabb" | |
-- |> log "log" | |
-- |> Result.toMaybe | |
-- |> Maybe.withDefault "" | |
-- ------------------------ | |
chompWhileSame : Parser () | |
chompWhileSame = | |
loop Nothing chompWhileSameLoop | |
chompWhileSameLoop : Maybe Char -> Parser (Step (Maybe Char) ()) | |
chompWhileSameLoop maybeChar = | |
oneOf | |
[ case maybeChar of | |
Just char -> | |
getChompedString (chompIf (\c -> char == c)) | |
|> andThen chompWhileSameNextChar | |
Nothing -> | |
getChompedString (chompIf (\_ -> True)) | |
|> andThen chompWhileSameNextChar | |
, succeed (Done ()) | |
] | |
chompWhileSameNextChar : String -> Parser (Step (Maybe Char) ()) | |
chompWhileSameNextChar str = | |
case String.uncons str of | |
Nothing -> | |
succeed (Done ()) | |
Just ( c, _ ) -> | |
succeed (Loop (Just c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment