parser

package
v0.0.0-...-f4e3d25 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package parser tokenizes and parses DOS-style command lines and batch constructs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalArith

func EvalArith(expr string, e *env.Env) (int, error)

EvalArith evaluates a DOS-style SET /A expression string. Any assignments in the expression update e. Returns the value of the rightmost subexpression.

func IsComment

func IsComment(line string) bool

IsComment reports whether line is a REM or :: comment.

func ParseLabel

func ParseLabel(line string) (string, bool)

ParseLabel returns the label name if line is a batch label (:name), else "". Leading whitespace is ignored.

func StripEchoSuppress

func StripEchoSuppress(line string) (string, bool)

StripEchoSuppress returns the line without a leading @ and whether @ was present. Only a @ at the very start (after optional whitespace) is stripped.

Types

type Command

type Command struct {
	Name      string
	Args      []string
	Redirects []Redirect
	AtPrefix  bool // @ was present on this line
}

Command is a parsed single command ready for dispatch.

type Pipeline

type Pipeline struct {
	Commands []*Command
	Next     *Pipeline // next pipeline in a && or || chain; nil if last
	NextOp   TokenType // TokenAnd or TokenOr; meaningful only when Next != nil
}

Pipeline is one or more Commands connected by pipes, optionally chained to another Pipeline via && or ||.

func Parse

func Parse(line string) (*Pipeline, error)

Parse builds a Pipeline from a command line. The line should already have had variable expansion applied. Call StripEchoSuppress before Parse if you need the @ flag.

type Redirect

type Redirect struct {
	Type TokenType
	Path string // empty for 2>&1
}

Redirect describes a single I/O redirection.

type Token

type Token struct {
	Type  TokenType
	Value string
}

Token is a single lexical unit.

func Tokenize

func Tokenize(line string) ([]Token, error)

Tokenize splits a command line into tokens. The caller is responsible for stripping the @ prefix before calling. %VAR% references are left as-is; variable expansion is the caller's job.

type TokenType

type TokenType int

TokenType classifies a lexical token.

const (
	TokenWord           TokenType = iota // bare word or quoted string
	TokenRedirectOut                     // >
	TokenRedirectApp                     // >>
	TokenRedirectIn                      // <
	TokenRedirectErr                     // 2>
	TokenRedirectErrApp                  // 2>>
	TokenRedirectErrOut                  // 2>&1
	TokenPipe                            // |
	TokenAnd                             // &&
	TokenOr                              // ||
	TokenAtPrefix                        // @ (echo suppression; produced by StripEchoSuppress, not Tokenize)
)

Jump to

Keyboard shortcuts

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