Re-implement collectNamesFromPattern in a TCO way

This commit is contained in:
Jeroen Engels 2021-10-17 17:08:28 +02:00
parent ba0013793d
commit 2b386ca80b

View File

@ -5460,33 +5460,47 @@ parameters patterns =
collectNamesFromPattern : Node Pattern -> List (Node String)
collectNamesFromPattern pattern =
collectNamesFromPatternHelp [ pattern ] []
collectNamesFromPatternHelp : List (Node Pattern) -> List (Node String) -> List (Node String)
collectNamesFromPatternHelp patternsToVisit acc =
case patternsToVisit of
pattern :: restOfPatternsToVisit ->
case Node.value pattern of
Pattern.TuplePattern subPatterns ->
List.concatMap collectNamesFromPattern subPatterns
Pattern.RecordPattern names ->
names
Pattern.UnConsPattern left right ->
List.concatMap collectNamesFromPattern [ left, right ]
Pattern.ListPattern subPatterns ->
List.concatMap collectNamesFromPattern subPatterns
Pattern.VarPattern name ->
[ Node (Node.range pattern) name ]
collectNamesFromPatternHelp
restOfPatternsToVisit
(Node (Node.range pattern) name :: acc)
Pattern.NamedPattern _ subPatterns ->
List.concatMap collectNamesFromPattern subPatterns
collectNamesFromPatternHelp (subPatterns ++ restOfPatternsToVisit) acc
Pattern.AsPattern subPattern alias ->
alias :: collectNamesFromPattern subPattern
Pattern.RecordPattern names ->
collectNamesFromPatternHelp
restOfPatternsToVisit
(names ++ acc)
Pattern.ParenthesizedPattern subPattern ->
collectNamesFromPattern subPattern
collectNamesFromPatternHelp (subPattern :: restOfPatternsToVisit) acc
Pattern.AsPattern subPattern alias ->
collectNamesFromPatternHelp (subPattern :: restOfPatternsToVisit) (alias :: acc)
Pattern.TuplePattern subPatterns ->
collectNamesFromPatternHelp (subPatterns ++ restOfPatternsToVisit) acc
Pattern.UnConsPattern left right ->
collectNamesFromPatternHelp (left :: right :: restOfPatternsToVisit) acc
Pattern.ListPattern subPatterns ->
collectNamesFromPatternHelp (subPatterns ++ restOfPatternsToVisit) acc
_ ->
[]
collectNamesFromPatternHelp restOfPatternsToVisit acc
[] ->
acc
collectModuleNamesFromPattern : ScopeModuleContext -> Node Pattern -> List ( Range, ModuleName )