Parser-based implementation doesn't work because it's greedy (not possible to do lazy matching like regex).

This commit is contained in:
Dillon Kearns 2021-04-25 15:17:45 -07:00
parent 8791747ad7
commit 810010bd1a
2 changed files with 92 additions and 5 deletions

View File

@ -30,6 +30,7 @@
"elm/html": "1.0.0 <= v < 2.0.0",
"elm/http": "2.0.0 <= v < 3.0.0",
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/parser": "1.1.0 <= v < 2.0.0",
"elm/regex": "1.0.0 <= v < 2.0.0",
"elm/url": "1.0.0 <= v < 2.0.0",
"elm-community/dict-extra": "2.4.0 <= v < 3.0.0",

View File

@ -1,18 +1,82 @@
module GlobTestsNew exposing (all)
import Expect
import Parser exposing ((|.), (|=), Parser)
import Test exposing (Test, describe, test)
{-| -}
type Glob a
= Glob { pattern : String, regexPattern : String, capture : String -> ( a, String ) }
= Glob
{ pattern : String
, regexPattern : String
, capture : Parser a
}
{-| -}
literal : String -> Glob String
literal string =
Glob { pattern = string, regexPattern = string, capture = \filePath -> ( string, filePath ) }
Glob
{ pattern = string
, regexPattern = string
, capture =
Parser.succeed string
|. Parser.symbol string
}
{-| -}
succeed : match -> Glob match
succeed string =
Glob
{ pattern = ""
, regexPattern = ""
, capture =
Parser.succeed string
}
wildcard : Glob String
wildcard =
Glob
{ pattern = "*"
, regexPattern = ".*"
, capture =
--Parser.
Parser.getChompedString
(Parser.chompUntilEndOr "/")
--\filePath ->
-- ( string, filePath )
}
capture : Glob a -> Glob (a -> value) -> Glob value
capture (Glob next) (Glob previous) =
Glob
{ pattern = previous.pattern ++ next.pattern
, regexPattern = ""
, capture =
Parser.backtrackable
(previous.capture
|= next.capture
)
}
ignore : Glob a -> Glob b -> Glob b
ignore (Glob next) (Glob previous) =
Glob
{ pattern = previous.pattern ++ next.pattern
, regexPattern = ""
, capture =
Parser.backtrackable
(Parser.succeed identity
|= previous.capture
|. next.capture
)
}
all : Test
@ -25,6 +89,24 @@ all =
{ expectedMatch = "hello"
, expectedPattern = "hello"
}
, test "capture 1" <|
\() ->
succeed identity
|> capture wildcard
|> ignore (literal "/hello.txt")
|> expect "folder/hello.txt"
{ expectedMatch = "folder"
, expectedPattern = "*/hello.txt"
}
, test "capture" <|
\() ->
succeed identity
|> capture wildcard
|> ignore (literal ".txt")
|> expect "my-file.txt"
{ expectedMatch = "my-file"
, expectedPattern = "*.txt"
}
]
@ -34,9 +116,13 @@ type DataExtension
run : String -> Glob match -> match
run filePath (Glob { pattern, regexPattern, capture }) =
capture filePath
|> Tuple.first
run filePath (Glob details) =
case Parser.run details.capture filePath of
Err errors ->
Debug.todo <| Debug.toString errors
Ok ok ->
ok
expect :