package main type StringSegmentPathFilterAST struct { index string } func (ast StringSegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState { return StringSegmentPathFilter { index: ast.index, next: next, } } type IntegerSegmentPathFilterAST struct { index int } func (ast IntegerSegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState { return IntegerSegmentPathFilter { index: ast.index, next: next, } } type RepeatPathFilterAST struct { content PathFilterAST } func (ast RepeatPathFilterAST) compileWith(next PathFilterState) PathFilterState { nextGroup := &GroupPathFilter{} repeatStart := ast.content.compileWith(nextGroup) nextGroup.filters = []PathFilterState{next, repeatStart} return nextGroup } type SequencePathFilterAST struct { sequence []PathFilterAST } func (ast SequencePathFilterAST) compileWith(next PathFilterState) PathFilterState { for i := len(ast.sequence) - 1; i >= 0; i -= 1 { next = ast.sequence[i].compileWith(next) } return next } type AnySegmentPathFilterAST struct {} func (ast AnySegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState { return AnySegmentPathFilter{next: next} } type PathFilterAST interface { compileWith(PathFilterState) PathFilterState } func compilePathFilterAST(ast PathFilterAST) PathFilter { return PathFilter{ initial: ast.compileWith(NonePathFilter{}), } }