Base pattern for matching a repeated pattern. The pattern can't match an empty list.
class repeat pattern[readonly value element type]
extends base pattern[element type]
pattern[element type] the pattern
boolean do match empty
repeat pattern(pattern[element type] the pattern, boolean do match empty)
this • the pattern = the pattern
this • do match empty = do match empty
implement void validate()
implement implicit boolean call(readonly list[element type] the list)
var nonnegative index : 0
while index < the listsize
match : the patternmatch prefix(the listskip(index))
if match is null
return false
assert match > 0
index += match
return index > 0 || do match empty
Check whether the given list can be a start of the sequence that matches this pattern.
implement boolean is viable prefix(readonly list[element type] the list)
if the listis empty
return true
var nonnegative index : 0
while index < the listsize
match : the patternmatch prefix(the listskip(index))
if match is null
break
assert match > 0
index += match
return index == the listsize || the patternis viable prefix(the listskip(index))
Returns the maximum number of the elements of a given list that matches the pattern, or null if there is no prefix match. This is a greedy match: it mtaches the longest prefix.
implement nonnegative or null match prefix(readonly list[element type] the list)
var nonnegative index : 0
while index < the listsize
match : the patternmatch prefix(the listskip(index))
if match is null
break
assert match > 0
index += match
if index == 0 && !do match empty
return missinginstance
else
return index
Gets the first non-empty match for this pattern.
implement range or null find first(readonly list[element type] the list, nonnegative start index)
assert start index <= the listsize
if do match empty
if start index == the listsize || the patternmatch prefix(the listskip(start index)) is null
return base range • new(start index, start index)
for var nonnegative i : start index; i < the listsize; i += 1
match : the patternmatch prefix(the listskip(i))
if match is_not null
assert match > 0
i += match
while i < the listsize
next match : the patternmatch prefix(the listskip(i))
if next match is null
break
assert next match > 0
i += next match
return base range • new(start range, i)
return missinginstance