lualex

package
v0.0.0-...-15c7053 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lualex provides a scanner to split a byte stream into Lua lexical elements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseInt

func ParseInt(s string) (int64, error)

ParseInt converts the given string to a 64-bit signed integer according to the lexical rules of Lua. Surrounding whitespace is permitted, and any error returned will be of type *strconv.NumError.

func ParseNumber

func ParseNumber(s string) (float64, error)

ParseNumber converts the given string to a 64-bit floating-point number according to the lexical rules of Lua. Surrounding whitespace is permitted, and any error returned will be of type *strconv.NumError.

func Quote

func Quote(s string) string

Quote returns a double-quoted Lua string literal representing s.

func Unquote

func Unquote(s string) (string, error)

Unquote interprets s as a single-quoted, double-quoted, or bracket-delimited Lua string literal, returning the string value that s quotes.

Types

type Position

type Position struct {
	// Line is the 1-based line number.
	Line int
	// Column is the 1-based column number.
	// Columns are based in bytes.
	// Zero indicates that the position only has line number information.
	Column int
}

Position represents a position in a textual source file.

func Pos

func Pos(line, col int) Position

Pos returns a new position with the given line number and column. It panics if the resulting Position would not be valid (as reported by Position.IsValid).

func (Position) IsValid

func (pos Position) IsValid() bool

IsValid reports whether pos has a positive line number and a non-negative column. (A zero column indicates line-only position information.)

func (Position) String

func (pos Position) String() string

String formats the position as "line:col".

type Scanner

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

A Scanner parses Lua tokens from a byte stream.

func NewScanner

func NewScanner(r io.ByteScanner) *Scanner

NewScanner returns a Scanner that reads from r. NewScanner does not buffer r.

func (*Scanner) Scan

func (s *Scanner) Scan() (Token, error)

Scan reads the next Token from the stream. If Scan returns an error, then the returned token will be an ErrorToken with the Position field set to the approximate position of the error. If an ErrorToken does not have a valid Position, then it indicates an error returned from the underlying reader or an otherwise unrecoverable error.

type Token

type Token struct {
	Kind     TokenKind
	Position Position
	// Value holds information for
	// an [IdentifierToken], a [StringToken], or a [NumeralToken].
	Value string
}

Token represents a single lexical element in a Lua source file.

func (Token) String

func (tok Token) String() string

String formats the token as it would appear in Lua source. String returns "<eof>" for ErrorToken.

type TokenKind

type TokenKind int

TokenKind is an enumeration of valid Token types. The zero value is ErrorToken.

const (
	// ErrorToken indicates an invalid token.
	ErrorToken TokenKind = iota
	// IdentifierToken indicates a name.
	// The Value field of [Token] will contain the identifier.
	IdentifierToken
	// StringToken indicates a literal string.
	// The Value field of [Token] will contain the parsed value of the string.
	StringToken
	// NumeralToken indicates a numeric constant.
	// The Value field of [Token] will contain the constant as written.
	NumeralToken

	AndToken      // and
	BreakToken    // break
	DoToken       // do
	ElseToken     // else
	ElseifToken   // elseif
	EndToken      // end
	FalseToken    // false
	ForToken      // for
	FunctionToken // function
	GotoToken     // goto
	IfToken       // if
	InToken       // in
	LocalToken    // local
	NilToken      // nil
	NotToken      // not
	OrToken       // or
	RepeatToken   // repeat
	ReturnToken   // return
	ThenToken     // then
	TrueToken     // true
	UntilToken    // until
	WhileToken    // while

	AddToken          // +
	SubToken          // -
	MulToken          // *
	DivToken          // /
	ModToken          // %
	PowToken          // ^
	LenToken          // #
	BitAndToken       // &
	BitXorToken       // ~
	BitOrToken        // |
	LShiftToken       // <<
	RShiftToken       // >>
	IntDivToken       // //
	EqualToken        // ==
	NotEqualToken     // ~=
	LessEqualToken    // <=
	GreaterEqualToken // >=
	LessToken         // <
	GreaterToken      // >
	AssignToken       // =
	LParenToken       // (
	RParenToken       // )
	LBraceToken       // {
	RBraceToken       // }
	LBracketToken     // [
	RBracketToken     // ]
	LabelToken        // ::
	SemiToken         // ;
	ColonToken        // :
	CommaToken        // ,
	DotToken          // .
	ConcatToken       // ..
	VarargToken       // ...
)

TokenKind values.

func (TokenKind) String

func (i TokenKind) String() string

Jump to

Keyboard shortcuts

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