walk

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: ISC Imports: 1 Imported by: 0

README

walk

CI Go Reference Go Report Card

-- import "vimagination.zapto.org/bash/walk"

Package walk provides a Bash type walker.

Highlights

  • Simple interface to allow control over walking through parsed Bash.
  • Allows modification to the tree as it's being walked.

Usage

package main

import (
	"fmt"

	"vimagination.zapto.org/bash"
	"vimagination.zapto.org/bash/walk"
	"vimagination.zapto.org/parser"
)

func main() {
	src := "a='Beep''Boop'\nprint() {\n\techo $a;\n}\n\nprint;"
	tk := parser.NewStringTokeniser(src)

	b, err := bash.Parse(&tk)
	if err != nil {
		fmt.Println(err)

		return
	}

	var walkFn walk.Handler

	walkFn = walk.HandlerFunc(func(t bash.Type) error {
		switch t := t.(type) {
		case *bash.WordPart:
			switch t.Part.Data {
			case "'Beep'":
				t.Part.Data = "'Hello'"
			case "'Boop'":
				t.Part.Data = "', world'"
			case "print":
				t.Part.Data = "do_print"
			}
		case *bash.FunctionCompound:
			t.Identifier.Data = "do_print"
		}

		return walk.Walk(t, walkFn)
	})

	walk.Walk(b, walkFn)

	fmt.Printf("%s", b)

	// Output:
	// a='Hello'', world';
	// do_print() { echo $a; }
	//
	// do_print;
}

Documentation

Full API docs can be found at:

https://pkg.go.dev/vimagination.zapto.org/bash/walk

Documentation

Overview

Package walk provides a bash type walker.

Example
package main

import (
	"fmt"

	"vimagination.zapto.org/bash"
	"vimagination.zapto.org/bash/walk"
	"vimagination.zapto.org/parser"
)

func main() {
	src := "a='Beep''Boop'\nprint() {\n\techo $a;\n}\n\nprint;"
	tk := parser.NewStringTokeniser(src)

	b, err := bash.Parse(&tk)
	if err != nil {
		fmt.Println(err)

		return
	}

	var walkFn walk.Handler

	walkFn = walk.HandlerFunc(func(t bash.Type) error {
		switch t := t.(type) {
		case *bash.WordPart:
			switch t.Part.Data {
			case "'Beep'":
				t.Part.Data = "'Hello'"
			case "'Boop'":
				t.Part.Data = "', world'"
			case "print":
				t.Part.Data = "do_print"
			}
		case *bash.FunctionCompound:
			t.Identifier.Data = "do_print"
		}

		return walk.Walk(t, walkFn)
	})

	walk.Walk(b, walkFn)

	fmt.Printf("%s", b)

}
Output:
a='Hello'', world';
do_print() { echo $a; }

do_print;

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(t bash.Type, fn Handler) error

Walk calls the Handle function on the given interface for each non-nil, non-Token field of the given bash type.

Types

type Handler

type Handler interface {
	Handle(bash.Type) error
}

Handler is used to process bash types.

type HandlerFunc

type HandlerFunc func(bash.Type) error

Handle implements the Handler interface.

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(t bash.Type) error

Jump to

Keyboard shortcuts

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