goawk: github.com/benhoyt/goawk/parser Index | Examples | Files

package parser

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

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 one of the interp.Exec* functions to execute it.

Code:

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

Code:

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

Package Files

parser.go resolve.go

type ParseError Uses

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 Uses

func (e *ParseError) Error() string

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

type ParserConfig Uses

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{}
}

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

type Program Uses

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".)
    Begin     []Stmts
    Actions   []Action
    End       []Stmts
    Functions []Function
    Scalars   map[string]int
    Arrays    map[string]int
}

Program is the abstract syntax tree for an entire AWK program.

func ParseProgram Uses

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) String Uses

func (p *Program) String() string

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

Package parser imports 9 packages (graph) and is imported by 4 packages. Updated 2019-01-30. Refresh now. Tools for package owners.