parser

package
v1.26.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 9 Imported by: 15

Documentation

Overview

Package parser is an AWK parser and abstract syntax tree.

Use the ParseProgram function to parse an AWK program, and then give the result to interp.Exec, interp.ExecProgram, or interp.New to execute it.

Example (Error)
package main

import (
	"fmt"

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

func main() {
	prog, err := parser.ParseProgram([]byte("{ for if }"), nil)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(prog)
	}
}
Output:

parse error at 1:7: expected ( instead of if
Example (Valid)
package main

import (
	"fmt"

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

func main() {
	prog, err := parser.ParseProgram([]byte("$0 { print $1 }"), nil)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(prog)
	}
}
Output:

$0 {
    print $1
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParseError

type ParseError struct {
	// Source line/column position where the error occurred.
	Position Position
	// Error message.
	Message string
}

ParseError (actually *ParseError) is the type of error returned by ParseProgram.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns a formatted version of the error, including the line and column numbers.

type ParserConfig

type ParserConfig struct {
	// Enable printing of type information
	DebugTypes bool

	// io.Writer to print type information on (for example, os.Stderr)
	DebugWriter io.Writer

	// Map of named Go functions to allow calling from AWK. See docs
	// on interp.Config.Funcs for details.
	Funcs map[string]interface{}
}

ParserConfig lets you specify configuration for the parsing process (for example printing type information for debugging).

type Program

type Program struct {
	// These fields aren't intended to be used or modified directly,
	// but are exported for the interpreter (Program itself needs to
	// be exported in package "parser", otherwise these could live in
	// "internal/ast".)
	resolver.ResolvedProgram
	Compiled *compiler.Program
}

Program is the parsed and compiled representation of an entire AWK program.

func ParseProgram

func ParseProgram(src []byte, config *ParserConfig) (prog *Program, err error)

ParseProgram parses an entire AWK program, returning the *Program abstract syntax tree or a *ParseError on error. "config" describes the parser configuration (and is allowed to be nil).

func (*Program) Disassemble added in v1.15.0

func (p *Program) Disassemble(writer io.Writer) error

Disassemble writes a human-readable form of the program's virtual machine instructions to writer.

func (*Program) String

func (p *Program) String() string

String returns an indented, pretty-printed version of the parsed program.

Jump to

Keyboard shortcuts

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