MCPcopy Create free account
hub / github.com/docopt/docopt.go / match

Method match

docopt.go:917–1037  ·  view source on GitHub ↗
(left *patternList, collected *patternList)

Source from the content-addressed store, hash-verified

915}
916
917func (p *pattern) match(left *patternList, collected *patternList) (bool, *patternList, *patternList) {
918 if collected == nil {
919 collected = &patternList{}
920 }
921 if p.t&patternRequired != 0 {
922 l := left
923 c := collected
924 for _, p := range p.children {
925 var matched bool
926 matched, l, c = p.match(l, c)
927 if !matched {
928 return false, left, collected
929 }
930 }
931 return true, l, c
932 } else if p.t&patternOptionAL != 0 || p.t&patternOptionSSHORTCUT != 0 {
933 for _, p := range p.children {
934 _, left, collected = p.match(left, collected)
935 }
936 return true, left, collected
937 } else if p.t&patternOneOrMore != 0 {
938 if len(p.children) != 1 {
939 panic("OneOrMore.match(): assert len(p.children) == 1")
940 }
941 l := left
942 c := collected
943 var lAlt *patternList
944 matched := true
945 times := 0
946 for matched {
947 // could it be that something didn't match but changed l or c?
948 matched, l, c = p.children[0].match(l, c)
949 if matched {
950 times++
951 }
952 if lAlt == l {
953 break
954 }
955 lAlt = l
956 }
957 if times >= 1 {
958 return true, l, c
959 }
960 return false, left, collected
961 } else if p.t&patternEither != 0 {
962 type outcomeStruct struct {
963 matched bool
964 left *patternList
965 collected *patternList
966 length int
967 }
968 outcomes := []outcomeStruct{}
969 for _, p := range p.children {
970 matched, l, c := p.match(left, collected)
971 outcome := outcomeStruct{matched, l, c, len(*l)}
972 if matched {
973 outcomes = append(outcomes, outcome)
974 }

Callers

nothing calls this directly

Calls 2

singleMatchMethod · 0.95
matchMethod · 0.45

Tested by

no test coverage detected