interp

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2018 License: MIT Imports: 19 Imported by: 20

Documentation

Overview

Package interp is the GoAWK interpreter (a simple tree-walker).

For basic usage, use the Exec function. For more complicated use cases and configuration options, first use the parser package to parse the AWK source, and then use ExecProgram to execute it with a specific configuration.

Example (Fieldsep)
package main

import (
	"bytes"
	"fmt"

	"github.com/benhoyt/goawk/interp"
)

func main() {
	// Use ',' as the field separator
	input := bytes.NewReader([]byte("1,2\n3,4"))
	err := interp.Exec("{ print $1, $2 }", ",", input, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
}
Output:

1 2
3 4
Example (Program)
package main

import (
	"bytes"
	"fmt"

	"github.com/benhoyt/goawk/interp"
	"github.com/benhoyt/goawk/parser"
)

func main() {
	src := "{ print NR, tolower($0) }"
	input := "A\naB\nAbC"

	prog, err := parser.ParseProgram([]byte(src), nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	config := &interp.Config{
		Stdin: bytes.NewReader([]byte(input)),
		Vars:  []string{"OFS", ":"},
	}
	_, err = interp.ExecProgram(prog, config)
	if err != nil {
		fmt.Println(err)
		return
	}
}
Output:

1:a
2:ab
3:abc
Example (Simple)
package main

import (
	"bytes"
	"fmt"

	"github.com/benhoyt/goawk/interp"
)

func main() {
	input := bytes.NewReader([]byte("foo bar\n\nbaz buz"))
	err := interp.Exec("$0 { print $1 }", " ", input, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
}
Output:

foo
baz

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(source, fieldSep string, input io.Reader, output io.Writer) error

Exec provides a simple way to parse and execute an AWK program with the given field separator. Exec reads input from the given reader (nil means use os.Stdin) and writes output to stdout (nil means use a buffered version of os.Stdout).

func ExecProgram

func ExecProgram(program *Program, config *Config) (int, error)

ExecProgram executes the parsed program using the given interpreter config, returning the exit status code of the program. Error is nil on successful execution of the program, even if the program returns a non-zero status code.

Types

type Config

type Config struct {
	// Standard input reader (defaults to os.Stdin)
	Stdin io.Reader

	// Writer for normal output (defaults to a buffered version of
	// os.Stdout)
	Output io.Writer

	// Writer for non-fatal error messages (defaults to a buffered
	// version of os.Stderr)
	Error io.Writer

	// The name of the executable (accessible via ARGV[0])
	Argv0 string

	// Input arguments (usually filenames): empty slice means read
	// only from Stdin, and a filename of "-" means read from Stdin
	// instead of a real file.
	Args []string

	// List of name-value pairs for variables to set before executing
	// the program (useful for setting FS and other built-in
	// variables, for example []string{"FS", ",", "OFS", ","}).
	Vars []string
}

Config defines the interpreter configuration for ExecProgram.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error (actually *Error) is returned by Exec and Eval functions on interpreter error, for example a negative field index.

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL