parser

package
v1.1.1-0...-c57de61 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2018 License: MIT Imports: 9 Imported by: 0

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

Example (Error)
package main

import (
	"fmt"

	"github.com/ktye/awk/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/ktye/awk/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{}
}

ParseConfig 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".)
	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

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

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