parser

package
v0.0.0-...-e2dc8b5 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MarkerShift    = 8
	IntermedShift  = 16
	CommandMask    = 0xff
	HasMoreFlag    = math.MinInt32
	ParamMask      = ^HasMoreFlag
	MissingParam   = ParamMask
	MissingCommand = MissingParam
	MaxParam       = math.MaxUint16 // the maximum value a parameter can have
)

Shift and masks for sequence parameters and intermediates.

View Source
const (
	// MaxParamsSize is the maximum number of parameters a sequence can have.
	MaxParamsSize = 32

	// DefaultParamValue is the default value used for missing parameters.
	DefaultParamValue = 0
)
View Source
const (
	TransitionActionShift = 4
	TransitionStateMask   = 15
	IndexStateShift       = 8

	// DefaultTableSize is the default size of the transition table.
	DefaultTableSize = 4096
)

Table values are generated like this:

index:  currentState << IndexStateShift | charCode
value:  action << TransitionActionShift | nextState

Variables

View Source
var ActionNames = []string{
	"NoneAction",
	"ClearAction",
	"CollectAction",
	"MarkerAction",
	"DispatchAction",
	"ExecuteAction",
	"StartAction",
	"PutAction",
	"ParamAction",
	"PrintAction",
}

nolint: unused

View Source
var StateNames = []string{
	"GroundState",
	"CsiEntryState",
	"CsiIntermediateState",
	"CsiParamState",
	"DcsEntryState",
	"DcsIntermediateState",
	"DcsParamState",
	"DcsStringState",
	"EscapeState",
	"EscapeIntermediateState",
	"OscStringState",
	"SosStringState",
	"PmStringState",
	"ApcStringState",
	"Utf8State",
}

nolint: unused

Table is a DEC ANSI transition table.

Functions

func Command

func Command(cmd int) int

Command returns the command byte of the CSI sequence.

func HasMore

func HasMore(params []int, i int) bool

HasMore returns true if the parameter has more sub-parameters.

func Intermediate

func Intermediate(cmd int) int

Intermediate returns the intermediate byte of the sequence. An intermediate byte is in the range of 0x20-0x2F. This includes these characters from ' ', '!', '"', '#', '$', '%', '&', ”', '(', ')', '*', '+', ',', '-', '.', '/'. Zero is returned if the sequence does not have an intermediate byte.

func Len

func Len(params []int) int

Len returns the number of parameters in the sequence. This will return the number of parameters in the sequence, excluding any sub-parameters.

func Marker

func Marker(cmd int) int

Marker returns the marker byte of the sequence. This is always gonna be one of the following '<' '=' '>' '?' and in the range of 0x3C-0x3F. Zero is returned if the sequence does not have a marker.

func Param

func Param(params []int, i int) int

Param returns the parameter at the given index. It returns -1 if the parameter does not exist.

func Range

func Range(params []int, fn func(i int, param int, hasMore bool) bool)

Range iterates over the parameters of the sequence and calls the given function for each parameter. The function should return false to stop the iteration.

func Subparams

func Subparams(params []int, i int) []int

Subparams returns the sub-parameters of the given parameter. It returns nil if the parameter does not exist.

Types

type Action

type Action = byte

Action is a DEC ANSI parser action.

const (
	NoneAction Action = iota
	ClearAction
	CollectAction
	MarkerAction
	DispatchAction
	ExecuteAction
	StartAction // Start of a data string
	PutAction   // Put into the data string
	ParamAction
	PrintAction

	IgnoreAction = NoneAction
)

These are the actions that the parser can take.

type State

type State = byte

State is a DEC ANSI parser state.

const (
	GroundState State = iota
	CsiEntryState
	CsiIntermediateState
	CsiParamState
	DcsEntryState
	DcsIntermediateState
	DcsParamState
	DcsStringState
	EscapeState
	EscapeIntermediateState
	OscStringState
	SosStringState
	PmStringState
	ApcStringState

	// Utf8State is not part of the DEC ANSI standard. It is used to handle
	// UTF-8 sequences.
	Utf8State
)

These are the states that the parser can be in.

type TransitionTable

type TransitionTable []byte

TransitionTable is a DEC ANSI transition table. https://vt100.net/emu/dec_ansi_parser

func GenerateTransitionTable

func GenerateTransitionTable() TransitionTable

GenerateTransitionTable generates a DEC ANSI transition table compatible with the VT500-series of terminals. This implementation includes a few modifications that include:

  • A new Utf8State is introduced to handle UTF8 sequences.
  • Osc and Dcs data accept UTF8 sequences by extending the printable range to 0xFF and 0xFE respectively.
  • We don't ignore 0x3A (':') when building Csi and Dcs parameters and instead use it to denote sub-parameters.
  • Support dispatching SosPmApc sequences.

func NewTransitionTable

func NewTransitionTable(size int) TransitionTable

NewTransitionTable returns a new DEC ANSI transition table.

func (TransitionTable) AddMany

func (t TransitionTable) AddMany(codes []byte, state State, action Action, next State)

AddMany adds many transitions.

func (TransitionTable) AddOne

func (t TransitionTable) AddOne(code byte, state State, action Action, next State)

AddOne adds a transition.

func (TransitionTable) AddRange

func (t TransitionTable) AddRange(start, end byte, state State, action Action, next State)

AddRange adds a range of transitions.

func (TransitionTable) SetDefault

func (t TransitionTable) SetDefault(action Action, state State)

SetDefault sets default transition.

func (TransitionTable) Transition

func (t TransitionTable) Transition(state State, code byte) (State, Action)

Transition returns the next state and action for the given state and byte.

Jump to

Keyboard shortcuts

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