<commit_before>package virt

import (
	"context"
	"fmt"
	"path/filepath"
	"strings"

	"github.com/jimmyfrasche/etlite/internal/device/file"
	"github.com/jimmyfrasche/etlite/internal/device/std"
	"github.com/jimmyfrasche/etlite/internal/format"
	"github.com/jimmyfrasche/etlite/internal/internal/errint"
	"github.com/jimmyfrasche/etlite/internal/internal/errusr"
	"github.com/jimmyfrasche/etlite/internal/internal/escape"
	"github.com/jimmyfrasche/etlite/internal/token"
)

//An Instruction is a single instruction in the VM.
type Instruction func(context.Context, *Machine) error

//Run executes all is instructions in execution context m.
func (m *Machine) Run(ctx context.Context, is []Instruction) error {
	var err error
loop:
	for _, i := range is {
		if err = i(ctx, m); err != nil {
			break
		}
		select {
		case <-ctx.Done():
			err = ctx.Err()
			break loop
		default:
		}
	}
	if err != nil {
		if m.stack.Open() {
			_ = m.drain(true)
			//TODO handle SQLITE_BUSY somewhere
			_ = m.exec("ROLLBACK;")
		}
		return errusr.Wrap(m.pos, err)
	}
	return m.drain(false)
}

type assertionError struct {
	pos token.Position
	msg string
}

func (a assertionError) Error() string {
	return fmt.Sprintf("%s: assertion failure: %s", a.pos, a.msg)
}

//Assert returns an assertion.
func Assert(pos token.Position, msg, query string) Instruction {
	return func(ctx context.Context, m *Machine) error {
		ret, err := m.conn.Assert(query)
		if err != nil {
			return err
		}
		if !ret {
			return assertionError{
				pos: pos,
				msg: msg,
			}
		}
		return nil
	}
}<commit_msg>remove duplicate lno info on assert<commit_after>package virt

import (
	"context"
	"fmt"
	"path/filepath"
	"strings"

	"github.com/jimmyfrasche/etlite/internal/device/file"
	"github.com/jimmyfrasche/etlite/internal/device/std"
	"github.com/jimmyfrasche/etlite/internal/format"
	"github.com/jimmyfrasche/etlite/internal/internal/errint"
	"github.com/jimmyfrasche/etlite/internal/internal/errusr"
	"github.com/jimmyfrasche/etlite/internal/internal/escape"
	"github.com/jimmyfrasche/etlite/internal/token"
)

//An Instruction is a single instruction in the VM.
type Instruction func(context.Context, *Machine) error

//Run executes all is instructions in execution context m.
func (m *Machine) Run(ctx context.Context, is []Instruction) error {
	var err error
loop:
	for _, i := range is {
		if err = i(ctx, m); err != nil {
			break
		}
		select {
		case <-ctx.Done():
			err = ctx.Err()
			break loop
		default:
		}
	}
	if err != nil {
		if m.stack.Open() {
			_ = m.drain(true)
			//TODO handle SQLITE_BUSY somewhere
			_ = m.exec("ROLLBACK;")
		}
		return errusr.Wrap(m.pos, err)
	}
	return m.drain(false)
}

//Assert returns an assertion.
func Assert(pos token.Position, msg, query string) Instruction {
	return func(ctx context.Context, m *Machine) error {
		ret, err := m.conn.Assert(query)
		if err != nil {
			return err
		}
		if !ret {
			return fmt.Errorf("assertion failure: %s", msg)
		}
		return nil
	}
}
{'fraction_non_alphanumeric': 0.132485781197725, 'fraction_numerical': 0.0, 'mean_word_length': 3.628482972136223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4864723', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1100, 'n_words': 261}