Beispiel #1
0
func simplifyInterleave(refs ast.RefLookup, p1, p2 *ast.Pattern) *ast.Pattern {
	if isNotZany(p1) || isNotZany(p2) {
		return emptyset
	}
	if isEmpty(p1) {
		return p2
	}
	if isEmpty(p2) {
		return p1
	}
	if isZany(p1) && isZany(p2) {
		return p1
	}
	left := getInterleaves(p1)
	right := getInterleaves(p2)
	list := append(left, right...)
	list = ast.Set(list)
	ast.Sort(list)
	var p *ast.Pattern = list[0]
	for i := range list {
		if i == 0 {
			continue
		}
		p = ast.NewInterleave(p, list[i])
	}
	return p
}
Beispiel #2
0
func zip(patterns []*ast.Pattern) ([]*ast.Pattern, []int) {
	zipped := ast.Set(patterns)
	ast.Sort(zipped)
	indexes := make([]int, len(patterns))
	for i, pattern := range patterns {
		indexes[i] = ast.Index(zipped, pattern)
	}
	return zipped, indexes
}
Beispiel #3
0
func simplifyAnd(refs ast.RefLookup, p1, p2 *ast.Pattern, record bool) *ast.Pattern {
	if isNotZany(p1) || isNotZany(p2) {
		return emptyset
	}
	if isZany(p1) {
		return p2
	}
	if isZany(p2) {
		return p1
	}
	if isEmpty(p1) {
		if Nullable(refs, p2) {
			return ast.NewEmpty()
		} else {
			return emptyset
		}
	}
	if isEmpty(p2) {
		if Nullable(refs, p1) {
			return ast.NewEmpty()
		} else {
			return emptyset
		}
	}
	if p1.GetLeafNode() != nil && p2.GetLeafNode() != nil {
		expr1, err1 := compose.ConvertBuiltInIntoFunction(p1.GetLeafNode().GetExpr())
		expr2, err2 := compose.ConvertBuiltInIntoFunction(p2.GetLeafNode().GetExpr())
		if err1 == nil && err2 == nil {
			return ast.NewLeafNode(ast.NewFunction("and", expr1, expr2))
		}
	}
	left := getAnds(p1)
	right := getAnds(p2)
	list := append(left, right...)
	list = ast.Set(list)
	list = simplifyChildren(list, func(left, right *ast.Pattern) *ast.Pattern {
		return simplifyAnd(refs, left, right, record)
	}, record)
	ast.Sort(list)
	var p *ast.Pattern = list[0]
	for i := range list {
		if i == 0 {
			continue
		}
		p = ast.NewAnd(p, list[i])
	}
	return p
}
Beispiel #4
0
func zip(patterns []*ast.Pattern) ([]*ast.Pattern, []int) {
	set := ast.Set(patterns)
	ast.Sort(set)

	if index := ast.Index(set, zipIgnoreSet[0]); index != -1 {
		set = ast.Remove(set, index)
	}
	if index := ast.Index(set, zipIgnoreSet[1]); index != -1 {
		set = ast.Remove(set, index)
	}
	indexes := make([]int, len(patterns))
	for i, pattern := range patterns {
		index := ast.Index(set, pattern)
		if index == -1 {
			index = ast.Index(zipIgnoreSet, pattern)
			index *= -1
			index -= 1
		}
		indexes[i] = index
	}
	return set, indexes
}