<commit_before>package expr

import (
	"fmt"

	u "github.com/araddon/gou"

	"github.com/araddon/qlbridge/lex"
)

const (
	maxIncludeDepth = 100
)

var (

	// If we hit max depth
	ErrMaxDepth = fmt.Errorf("Recursive Evaluation Error")
)

// InlineIncludes take an expression and resolve any includes so that
// the included expression is "Inline"
func InlineIncludes(ctx Includer, n Node) (Node, error) {
	return inlineIncludesDepth(ctx, n, 0)
}
func inlineIncludesDepth(ctx Includer, arg Node, depth int) (Node, error) {
	if depth > maxIncludeDepth {
		return nil, ErrMaxDepth
	}

	switch n := arg.(type) {
	case NodeArgs:
		args := n.ChildrenArgs()
		for i, narg := range args {
			newNode, err := inlineIncludesDepth(ctx, narg, depth+1)
			if err != nil {
				return nil, err
			}
			if newNode != nil {
				args[i] = newNode
			}
		}
		return arg, nil
	case *NumberNode, *IdentityNode, *StringNode, nil,
		*ValueNode, *NullNode:
		return nil, nil
	case *IncludeNode:
		return resolveInclude(ctx, n, depth+1)
	}
	return nil, fmt.Errorf("unrecognized node type %T", arg)
}

func resolveInclude(ctx Includer, inc *IncludeNode, depth int) (Node, error) {

	if inc.inlineExpr == nil {
		n, err := ctx.Include(inc.Identity.Text)
		if err != nil {
			// ErrNoIncluder is pretty common so don't log it
			if err == ErrNoIncluder {
				return nil, err
			}
			u.Debugf("Could not find include for filter:%s err=%v", inc.String(), err)
			return nil, err
		}
		if n == nil {
			u.Debugf("Includer %T returned a nil filter statement!", inc)
			return nil, ErrIncludeNotFound
		}
		// Now inline, the inlines
		n, err = InlineIncludes(ctx, n)
		if err != nil {
			return nil, err<commit_msg>Expression inline includer bug<commit_after>package expr

import (
	"fmt"

	u "github.com/araddon/gou"

	"github.com/araddon/qlbridge/lex"
)

const (
	maxIncludeDepth = 100
)

var (

	// If we hit max depth
	ErrMaxDepth = fmt.Errorf("Recursive Evaluation Error")
)

// InlineIncludes take an expression and resolve any includes so that
// the included expression is "Inline"
func InlineIncludes(ctx Includer, n Node) (Node, error) {
	return inlineIncludesDepth(ctx, n, 0)
}
func inlineIncludesDepth(ctx Includer, arg Node, depth int) (Node, error) {
	if depth > maxIncludeDepth {
		return nil, ErrMaxDepth
	}

	switch n := arg.(type) {
	case NodeArgs:
		args := n.ChildrenArgs()
		for i, narg := range args {
			newNode, err := inlineIncludesDepth(ctx, narg, depth+1)
			if err != nil {
				return nil, err
			}
			if newNode != nil {
				args[i] = newNode
			}
		}
		return arg, nil
	case *NumberNode, *IdentityNode, *StringNode, nil,
		*ValueNode, *NullNode:
		return arg, nil
	case *IncludeNode:
		return resolveInclude(ctx, n, depth+1)
	}
	return nil, fmt.Errorf("unrecognized node type %T", arg)
}

func resolveInclude(ctx Includer, inc *IncludeNode, depth int) (Node, error) {

	if inc.inlineExpr == nil {
		n, err := ctx.Include(inc.Identity.Text)
		if err != nil {
			// ErrNoIncluder is pretty common so don't log it
			if err == ErrNoIncluder {
				return nil, err
			}
			u.Debugf("Could not find include for filter:%s err=%v", inc.String(), err)
			return nil, err
		}
		if n == nil {
			u.Debugf("Includer %T returned a nil filter statement!", inc)
			return nil, ErrIncludeNotFound
		}
		// Now inline, the inlines
		n, err = InlineIncludes(ctx, n)
		if err != nil {
			return nil, err
{'fraction_non_alphanumeric': 0.1188997338065661, 'fraction_numerical': 0.00354924578527063, 'mean_word_length': 3.497340425531915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2433069', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1174, 'n_words': 376}