parser

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	LOWEST      int
	ASSIGN      // =
	OR          // ||
	AND         // &&
	EQUALS      // == or !=
	LESSGREATER // > or <
	BITWISE     // | &
	SUM         // + or -
	PRODUCT     // * / %
	PREFIX      // -x or !x
	CALL        // func()
)

Parser precedence levels

Variables

View Source
var WellKnownFunctions = map[string]FunctionSignature{

	"AimPrimary": {
		Name:       "AimPrimary",
		ParamNames: []string{"heading", "pitch"},
	},
	"AimSecondary": {
		Name:       "AimSecondary",
		ParamNames: []string{"heading", "pitch"},
	},
	"AimTertiary": {
		Name:       "AimTertiary",
		ParamNames: []string{"heading", "pitch"},
	},
	"AimFromPrimary": {
		Name:       "AimFromPrimary",
		ParamNames: []string{"piecenum"},
	},
	"AimFromSecondary": {
		Name:       "AimFromSecondary",
		ParamNames: []string{"piecenum"},
	},
	"AimFromTertiary": {
		Name:       "AimFromTertiary",
		ParamNames: []string{"piecenum"},
	},

	"QueryPrimary": {
		Name:       "QueryPrimary",
		ParamNames: []string{"piecenum"},
	},
	"QuerySecondary": {
		Name:       "QuerySecondary",
		ParamNames: []string{"piecenum"},
	},
	"QueryTertiary": {
		Name:       "QueryTertiary",
		ParamNames: []string{"piecenum"},
	},

	"SweetSpot": {
		Name:       "SweetSpot",
		ParamNames: []string{"piecenum"},
	},
	"Killed": {

		Name:       "Killed",
		ParamNames: []string{"severity", "corpsetype", "damagetype"},
	},
	"HitByWeapon": {
		Name:       "HitByWeapon",
		ParamNames: []string{"anglex", "anglez"},
	},
	"HitByWeaponId": {
		Name:       "HitByWeaponId",
		ParamNames: []string{"anglex", "anglez", "weaponid", "dmg"},
	},

	"StartMoving": {
		Name:       "StartMoving",
		ParamNames: []string{"reversing"},
	},
	"MoveRate": {
		Name:       "MoveRate",
		ParamNames: []string{"rate"},
	},
	"SetSpeed": {
		Name:       "SetSpeed",
		ParamNames: []string{"speed"},
	},
	"SetDirection": {
		Name:       "SetDirection",
		ParamNames: []string{"heading"},
	},

	"StartBuilding": {
		Name:       "StartBuilding",
		ParamNames: []string{"heading", "pitch"},
	},
	"QueryBuildInfo": {
		Name:       "QueryBuildInfo",
		ParamNames: []string{"piecenum"},
	},
	"QueryNanoPiece": {
		Name:       "QueryNanoPiece",
		ParamNames: []string{"piecenum"},
	},

	"QueryTransport": {
		Name:       "QueryTransport",
		ParamNames: []string{"piecenum"},
	},
	"BeginTransport": {
		Name:       "BeginTransport",
		ParamNames: []string{"unitid"},
	},
	"TransportPickup": {
		Name:       "TransportPickup",
		ParamNames: []string{"unitid"},
	},
	"TransportDrop": {
		Name:       "TransportDrop",
		ParamNames: []string{"unitid", "position"},
	},

	"smokeunit": {
		Name:       "smokeunit",
		ParamNames: []string{"healthpercent", "sleeptime", "smoketype"},
	},
	"smokecontrol": {

		Name:       "smokecontrol",
		ParamNames: []string{"healthpercent", "smoketype", "sleeptime"},
	},
	"rockunit": {
		Name:       "rockunit",
		ParamNames: []string{"anglex", "anglez"},
	},
	"motioncontrol": {

		Name:       "motioncontrol",
		ParamNames: []string{"moving", "aiming", "justmoved"},
	},
	"attack1": {
		Name:       "attack1",
		ParamNames: []string{"weapon"},
	},
	"attack2": {
		Name:       "attack2",
		ParamNames: []string{"weapon"},
	},
	"attack3": {
		Name:       "attack3",
		ParamNames: []string{"weapon"},
	},
	"attack4": {
		Name:       "attack4",
		ParamNames: []string{"weapon"},
	},
	"turndirection": {
		Name:       "turndirection",
		ParamNames: []string{"dir"},
	},
	"setmaxreloadtime": {
		Name:       "setmaxreloadtime",
		ParamNames: []string{"time"},
	},
	"aimweapon": {
		Name:       "aimweapon",
		ParamNames: []string{"heading", "pitch", "weaponnum"},
	},
	"fireweapon": {
		Name:       "fireweapon",
		ParamNames: []string{"weaponnum"},
	},
	"targetcleared": {
		Name:       "targetcleared",
		ParamNames: []string{"weaponnum"},
	},
	"queryweapon": {
		Name:       "queryweapon",
		ParamNames: []string{"piecenum", "weaponnum"},
	},
	"queryblood": {
		Name:       "queryblood",
		ParamNames: []string{"piecenum"},
	},
	"dying": {
		Name:       "dying",
		ParamNames: []string{"damagetype"},
	},
	"setSFXoccupy": {
		Name:       "setSFXoccupy",
		ParamNames: []string{"state"},
	},
	"windchange": {
		Name:       "windchange",
		ParamNames: []string{"windspeed", "winddirection"},
	},
	"hitbyweapon": {
		Name:       "hitbyweapon",
		ParamNames: []string{"attacker", "pitch", "roll", "severity"},
	},
	"moverate": {
		Name:       "moverate",
		ParamNames: []string{"rate"},
	},
	"RequestState": {
		Name:       "RequestState",
		ParamNames: []string{"requestedstate", "currentstate"},
	},
	"TriggerHit": {
		Name:       "TriggerHit",
		ParamNames: []string{"Trigger_ID", "unitID", "Var_3"},
	},
	"UnitDestroyed": {
		Name:       "UnitDestroyed",
		ParamNames: []string{"unitID"},
	},
}

WellKnownFunctions contains signatures for standard TA BOS functions plus TA: Kingdoms additions taken from Scriptor's [COMMON_FUNC] table. Entries register the widest known signature; the decompiler caps the rendered parameter count by the script's STACK_ALLOC count, so TA scripts using a narrower variant of a shared function (e.g. 2-arg Killed) still render correctly.

This map is easily extensible - just add new entries for additional functions

Functions

func GetParameterName

func GetParameterName(functionName string, paramIndex int) string

GetParameterName returns the proper name for a parameter at the given index, or returns the default "local_N" name if no signature is defined

func ParseNumber

func ParseNumber(s string) (float64, error)

ParseNumber attempts to parse a number from a string

Types

type AxisExpression

type AxisExpression struct {
	Token Token
	Axis  string
}

AxisExpression represents axis keywords: x-axis, y-axis, z-axis

func (*AxisExpression) String

func (ae *AxisExpression) String() string

func (*AxisExpression) TokenLiteral

func (ae *AxisExpression) TokenLiteral() string

type BlockStatement

type BlockStatement struct {
	Token      Token // TOKEN_LBRACE
	Statements []Statement
}

BlockStatement represents { ... }

func (*BlockStatement) String

func (bs *BlockStatement) String() string

func (*BlockStatement) TokenLiteral

func (bs *BlockStatement) TokenLiteral() string

type BracketExpression

type BracketExpression struct {
	Token       Token // [ or <
	Expression  Expression
	BracketType string // "[]" or "<>"
}

BracketExpression represents [expr] or <expr>

func (*BracketExpression) String

func (be *BracketExpression) String() string

func (*BracketExpression) TokenLiteral

func (be *BracketExpression) TokenLiteral() string

type CallExpression

type CallExpression struct {
	Token     Token // function name or (
	Function  Expression
	Arguments []Expression
}

CallExpression represents function calls: func(arg1, arg2)

func (*CallExpression) String

func (ce *CallExpression) String() string

func (*CallExpression) TokenLiteral

func (ce *CallExpression) TokenLiteral() string

type CommandStatement

type CommandStatement struct {
	Token   Token // command token
	Command string
	Args    []Expression
}

CommandStatement represents BOS commands (move, turn, show, etc.)

func (*CommandStatement) String

func (cs *CommandStatement) String() string

func (*CommandStatement) TokenLiteral

func (cs *CommandStatement) TokenLiteral() string

type Expression

type Expression interface {
	Node
	// contains filtered or unexported methods
}

Expression represents an expression node

type ExpressionStatement

type ExpressionStatement struct {
	Token      Token
	Expression Expression
}

ExpressionStatement wraps an expression as a statement

func (*ExpressionStatement) String

func (es *ExpressionStatement) String() string

func (*ExpressionStatement) TokenLiteral

func (es *ExpressionStatement) TokenLiteral() string

type FunctionDeclaration

type FunctionDeclaration struct {
	Token      Token // function name token
	Name       string
	Parameters []string
	Body       *BlockStatement
}

FunctionDeclaration represents a function definition

func (*FunctionDeclaration) String

func (fd *FunctionDeclaration) String() string

func (*FunctionDeclaration) TokenLiteral

func (fd *FunctionDeclaration) TokenLiteral() string

type FunctionSignature

type FunctionSignature struct {
	Name       string   // Function name (e.g., "AimPrimary")
	ParamNames []string // Parameter names in order (e.g., ["heading", "pitch"])
}

FunctionSignature defines the expected parameters for a well-known BOS function

func GetFunctionSignature

func GetFunctionSignature(name string) *FunctionSignature

GetFunctionSignature returns the signature for a well-known function, or nil if not found

type Identifier

type Identifier struct {
	Token Token
	Value string
}

Identifier represents a variable or piece name

func (*Identifier) String

func (i *Identifier) String() string

func (*Identifier) TokenLiteral

func (i *Identifier) TokenLiteral() string

type IfStatement

type IfStatement struct {
	Token       Token // TOKEN_IF
	Condition   Expression
	Consequence *BlockStatement
	Alternative *BlockStatement
}

IfStatement represents: if (condition) { ... } else { ... }

func (*IfStatement) String

func (is *IfStatement) String() string

func (*IfStatement) TokenLiteral

func (is *IfStatement) TokenLiteral() string

type InfixExpression

type InfixExpression struct {
	Token    Token
	Left     Expression
	Operator string
	Right    Expression
}

InfixExpression represents binary operations: x + y, x == y, etc.

func (*InfixExpression) String

func (ie *InfixExpression) String() string

func (*InfixExpression) TokenLiteral

func (ie *InfixExpression) TokenLiteral() string

type KeywordExpression

type KeywordExpression struct {
	Token   Token
	Keyword string
}

KeywordExpression represents special keywords as expressions

func (*KeywordExpression) String

func (ke *KeywordExpression) String() string

func (*KeywordExpression) TokenLiteral

func (ke *KeywordExpression) TokenLiteral() string

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes BOS source code

func NewLexer

func NewLexer(input string, preserveWS bool) *Lexer

NewLexer creates a new lexer for the input

func (*Lexer) AllTokens

func (l *Lexer) AllTokens() []Token

AllTokens returns all tokens from the lexer

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token

type Node

type Node interface {
	String() string
	TokenLiteral() string
}

Node is the base interface for all AST nodes

type NumberLiteral

type NumberLiteral struct {
	Token Token
	Value string
}

NumberLiteral represents a numeric value

func (*NumberLiteral) String

func (nl *NumberLiteral) String() string

func (*NumberLiteral) TokenLiteral

func (nl *NumberLiteral) TokenLiteral() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses BOS source into an AST

func NewParser

func NewParser(input string) *Parser

NewParser creates a new parser

func (*Parser) Errors

func (p *Parser) Errors() []string

Errors returns parser errors

func (*Parser) ParseProgram

func (p *Parser) ParseProgram() *Program

ParseProgram parses the entire program

type PieceDeclaration

type PieceDeclaration struct {
	Token Token // TOKEN_PIECE
	Names []string
}

PieceDeclaration represents: piece base, turret, barrel;

func (*PieceDeclaration) String

func (pd *PieceDeclaration) String() string

func (*PieceDeclaration) TokenLiteral

func (pd *PieceDeclaration) TokenLiteral() string

type PrefixExpression

type PrefixExpression struct {
	Token    Token
	Operator string
	Right    Expression
}

PrefixExpression represents unary operations: -x, !x

func (*PrefixExpression) String

func (pe *PrefixExpression) String() string

func (*PrefixExpression) TokenLiteral

func (pe *PrefixExpression) TokenLiteral() string

type Preprocessor

type Preprocessor struct {
	// contains filtered or unexported fields
}

Preprocessor handles BOS file preprocessing (#include, #define, #ifdef, etc.)

func NewPreprocessor

func NewPreprocessor(fs filesystem.FileSystem, includePaths ...string) *Preprocessor

NewPreprocessor creates a new preprocessor with a filesystem

func (*Preprocessor) Process

func (p *Preprocessor) Process(path string) (string, error)

Process processes a BOS file and returns the expanded source

func (*Preprocessor) ProcessContent

func (p *Preprocessor) ProcessContent(content, baseDir string) (string, error)

ProcessContent preprocesses raw source content (as opposed to Process which reads from a file).

type Program

type Program struct {
	Statements []Statement
}

Program is the root node of the AST

func (*Program) String

func (p *Program) String() string

func (*Program) TokenLiteral

func (p *Program) TokenLiteral() string

type ReturnStatement

type ReturnStatement struct {
	Token       Token // TOKEN_RETURN
	ReturnValue Expression
}

ReturnStatement represents: return (expr);

func (*ReturnStatement) String

func (rs *ReturnStatement) String() string

func (*ReturnStatement) TokenLiteral

func (rs *ReturnStatement) TokenLiteral() string

type Statement

type Statement interface {
	Node
	// contains filtered or unexported methods
}

Statement represents a statement node

type StaticVarDeclaration

type StaticVarDeclaration struct {
	Token Token // TOKEN_STATIC_VAR
	Names []string
}

StaticVarDeclaration represents: static-var x, y, z;

func (*StaticVarDeclaration) String

func (sv *StaticVarDeclaration) String() string

func (*StaticVarDeclaration) TokenLiteral

func (sv *StaticVarDeclaration) TokenLiteral() string

type StringLiteral

type StringLiteral struct {
	Token Token
	Value string
}

StringLiteral represents a string value

func (*StringLiteral) String

func (sl *StringLiteral) String() string

func (*StringLiteral) TokenLiteral

func (sl *StringLiteral) TokenLiteral() string

type Token

type Token struct {
	Type    TokenType
	Literal string
	Line    int
	Column  int
}

Token represents a lexical token

func TokensNoWS

func TokensNoWS(input string) []Token

TokensNoWS returns all non-whitespace tokens

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int

TokenType represents the type of a token

const (
	// Special tokens
	TOKEN_EOF TokenType = iota
	TOKEN_ILLEGAL
	TOKEN_COMMENT
	TOKEN_WHITESPACE
	TOKEN_NEWLINE

	// Literals
	TOKEN_IDENT  // identifier, piece name, variable
	TOKEN_NUMBER // 123, -456
	TOKEN_STRING // "string"

	// Keywords
	TOKEN_PIECE
	TOKEN_STATIC_VAR
	TOKEN_WHILE
	TOKEN_IF
	TOKEN_ELSE
	TOKEN_RETURN
	TOKEN_VAR

	// Commands (case-insensitive in BOS)
	TOKEN_MOVE
	TOKEN_TURN
	TOKEN_SPIN
	TOKEN_STOP_SPIN
	TOKEN_SHOW
	TOKEN_HIDE
	TOKEN_CACHE
	TOKEN_DONT_CACHE
	TOKEN_SHADE
	TOKEN_DONT_SHADE
	TOKEN_EMIT_SFX
	TOKEN_EXPLODE
	TOKEN_SLEEP
	TOKEN_WAIT_FOR_TURN
	TOKEN_WAIT_FOR_MOVE
	TOKEN_CALL_SCRIPT
	TOKEN_START_SCRIPT
	TOKEN_SIGNAL
	TOKEN_SET_SIGNAL_MASK
	TOKEN_GET
	TOKEN_SET
	TOKEN_RAND
	TOKEN_ATTACH_UNIT
	TOKEN_DROP_UNIT

	// Keywords for commands
	TOKEN_TO
	TOKEN_ALONG
	TOKEN_AROUND
	TOKEN_SPEED
	TOKEN_NOW
	TOKEN_ACCELERATE
	TOKEN_DECELERATE
	TOKEN_FROM
	TOKEN_TYPE

	// Axes
	TOKEN_X_AXIS
	TOKEN_Y_AXIS
	TOKEN_Z_AXIS

	// Operators
	TOKEN_ASSIGN  // =
	TOKEN_PLUS    // +
	TOKEN_MINUS   // -
	TOKEN_STAR    // *
	TOKEN_SLASH   // /
	TOKEN_PERCENT // %
	TOKEN_PIPE    // |
	TOKEN_AMP     // &
	TOKEN_LT      // <
	TOKEN_GT      // >
	TOKEN_LE      // <=
	TOKEN_GE      // >=
	TOKEN_EQ      // ==
	TOKEN_NE      // !=
	TOKEN_NOT     // !
	TOKEN_AND     // &&
	TOKEN_OR      // ||

	// Delimiters
	TOKEN_LPAREN    // (
	TOKEN_RPAREN    // )
	TOKEN_LBRACE    // {
	TOKEN_RBRACE    // }
	TOKEN_LBRACKET  // [
	TOKEN_RBRACKET  // ]
	TOKEN_COMMA     // ,
	TOKEN_SEMICOLON // ;
)

func LookupIdent

func LookupIdent(ident string) TokenType

LookupIdent checks if an identifier is a keyword

func (TokenType) String

func (t TokenType) String() string

type WhileStatement

type WhileStatement struct {
	Token     Token // TOKEN_WHILE
	Condition Expression
	Body      *BlockStatement
}

WhileStatement represents: while (condition) { ... }

func (*WhileStatement) String

func (ws *WhileStatement) String() string

func (*WhileStatement) TokenLiteral

func (ws *WhileStatement) TokenLiteral() string

Jump to

Keyboard shortcuts

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