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 := &OrPathFilter{} repeatStart := ast.content.compileWith(nextGroup) nextGroup.filters = [2]PathFilterState{next, repeatStart} return nextGroup } type SequencePathFilterAST struct { first PathFilterAST second PathFilterAST } func (ast SequencePathFilterAST) compileWith(next PathFilterState) PathFilterState { next = ast.second.compileWith(next) next = ast.first.compileWith(next) return next } type AnySegmentPathFilterAST struct {} func (ast AnySegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState { return AnySegmentPathFilter{next: next} } type OrPathFilterAST struct { first PathFilterAST second PathFilterAST } func (ast OrPathFilterAST) compileWith(next PathFilterState) PathFilterState { return OrPathFilter { filters: [2]PathFilterState{ ast.first.compileWith(next), ast.second.compileWith(next), }, } } type NonePathFilterAST struct {} func (ast NonePathFilterAST) compileWith(next PathFilterState) PathFilterState { return next } type PathFilterAST interface { compileWith(PathFilterState) PathFilterState } func compilePathFilterAST(ast PathFilterAST) PathFilter { return PathFilter{ initial: ast.compileWith(NonePathFilter{}), } }