Documentation
¶
Overview ¶
Package lsl provides a simple interface to the Lacking Shader Language.
Index ¶
- Constants
- func Tokenize(source string) iter.Seq[Token]
- func Validate(shader *Shader, schema Schema) error
- type Assignment
- type BinaryExpression
- type Conditional
- type Declaration
- type Discard
- type Expression
- type ExpressionGroup
- type Field
- type FieldIdentifier
- type FloatLiteral
- type FunctionCall
- type FunctionDeclaration
- type Identifier
- type IntLiteral
- type ParseError
- type Parser
- func (p *Parser) ParseBlockEnd() error
- func (p *Parser) ParseBlockStart() error
- func (p *Parser) ParseComment() error
- func (p *Parser) ParseExpression() (Expression, error)
- func (p *Parser) ParseFunction() (*FunctionDeclaration, error)
- func (p *Parser) ParseNamedParameterList() ([]Field, error)
- func (p *Parser) ParseNewLine() error
- func (p *Parser) ParseOptionalRemainder() error
- func (p *Parser) ParseShader() (*Shader, error)
- func (p *Parser) ParseStatement() (Statement, error)
- func (p *Parser) ParseStatementList() ([]Statement, error)
- func (p *Parser) ParseTextureBlock() (*TextureBlockDeclaration, error)
- func (p *Parser) ParseUniformBlock() (*UniformBlockDeclaration, error)
- func (p *Parser) ParseUnnamedParameterList() ([]Field, error)
- func (p *Parser) ParseVaryingBlock() (*VaryingBlockDeclaration, error)
- type Position
- type Schema
- type Shader
- func (s *Shader) FindFunction(name string) (*FunctionDeclaration, bool)
- func (s *Shader) FindTextureBlock() (*TextureBlockDeclaration, bool)
- func (s *Shader) FindUniformBlock() (*UniformBlockDeclaration, bool)
- func (s *Shader) FindVaryingBlock() (*VaryingBlockDeclaration, bool)
- func (s *Shader) Functions() []*FunctionDeclaration
- func (s *Shader) TextureBlocks() []*TextureBlockDeclaration
- func (s *Shader) UniformBlocks() []*UniformBlockDeclaration
- func (s *Shader) VaryingBlocks() []*VaryingBlockDeclaration
- type Statement
- type TextureBlockDeclaration
- type Token
- func (t Token) IsAssignmentOperator() bool
- func (t Token) IsBinaryOperator() bool
- func (t Token) IsComment() bool
- func (t Token) IsEOF() bool
- func (t Token) IsError() bool
- func (t Token) IsIdentifier() bool
- func (t Token) IsNewLine() bool
- func (t Token) IsNumber() bool
- func (t Token) IsOperator() bool
- func (t Token) IsSpecificIdentifier(value string) bool
- func (t Token) IsSpecificOperator(value string) bool
- func (t Token) IsTerminal() bool
- func (t Token) IsUnaryOperator() bool
- func (t Token) String() string
- type TokenType
- type Tokenizer
- type UnaryExpression
- type UniformBlockDeclaration
- type UsageScope
- type Validator
- type VariableDeclaration
- type VaryingBlockDeclaration
Constants ¶
const ( // TypeNameBool is the name of the boolean type. TypeNameBool = "bool" // TypeNameInt is the name of the signed 32bit integer type. TypeNameInt = "int" // TypeNameUint is the name of the unsigned 32bit integer type. TypeNameUint = "uint" // TypeNameFloat is the name of the 32bit floating point type. TypeNameFloat = "float" // TypeNameVec2 is the name of the 32bit floating point 2D vector type. TypeNameVec2 = "vec2" // TypeNameVec3 is the name of the 32bit floating point 3D vector type. TypeNameVec3 = "vec3" // TypeNameVec4 is the name of the 32bit floating point 4D vector type. TypeNameVec4 = "vec4" // TypeNameBVec2 is the name of the boolean 2D vector type. TypeNameBVec2 = "bvec2" // TypeNameBVec3 is the name of the boolean 3D vector type. TypeNameBVec3 = "bvec3" // TypeNameBVec4 is the name of the boolean 4D vector type. TypeNameBVec4 = "bvec4" // TypeNameIVec2 is the name of the signed 32bit integer 2D vector type. TypeNameIVec2 = "ivec2" // TypeNameIVec3 is the name of the signed 32bit integer 3D vector type. TypeNameIVec3 = "ivec3" // TypeNameIVec4 is the name of the signed 32bit integer 4D vector type. TypeNameIVec4 = "ivec4" // TypeNameUVec2 is the name of the unsigned 32bit integer 2D vector type. TypeNameUVec2 = "uvec2" // TypeNameUVec3 is the name of the unsigned 32bit integer 3D vector type. TypeNameUVec3 = "uvec3" // TypeNameUVec4 is the name of the unsigned 32bit integer 4D vector type. TypeNameUVec4 = "uvec4" // TypeNameMat2 is the name of the 2x2 matrix type. TypeNameMat2 = "mat2" // TypeNameMat3 is the name of the 3x3 matrix type. TypeNameMat3 = "mat3" // TypeNameMat4 is the name of the 4x4 matrix type. TypeNameMat4 = "mat4" // TypeNameSampler2D is the name of the 2D sampler type. TypeNameSampler2D = "sampler2D" // TypeNameSamplerCube is the name of the cube sampler type. TypeNameSamplerCube = "samplerCube" )
const ( // AssignmentOperatorEq is the assignment operator "=". It assigns the // value to the variable. AssignmentOperatorEq = "=" // AssignmentOperatorAuto is the assignment operator ":=", where the type // of the variable is inferred from the value. AssignmentOperatorAuto = ":=" // AssignmentOperatorAdd is the assignment operator "+=". It adds the value // to the variable. AssignmentOperatorAdd = "+=" // AssignmentOperatorSub is the assignment operator "-=". It subtracts the // value from the variable. AssignmentOperatorSub = "-=" // AssignmentOperatorMul is the assignment operator "*=". It multiplies the // variable by the value. AssignmentOperatorMul = "*=" // AssignmentOperatorDiv is the assignment operator "/=". It divides the // variable by the value. AssignmentOperatorDiv = "/=" // AssignmentOperatorMod is the assignment operator "%=". It takes the // modulo of the variable and the value. AssignmentOperatorMod = "%=" // AssignmentOperatorShl is the assignment operator "<<=". It shifts the // variable to the left by the value. AssignmentOperatorShl = "<<=" // AssignmentOperatorShr is the assignment operator ">>=". It shifts the // variable to the right by the value. AssignmentOperatorShr = ">>=" // AssignmentOperatorAnd is the assignment operator "&=". It performs a // bitwise AND operation on the variable and the value. AssignmentOperatorAnd = "&=" // AssignmentOperatorOr is the assignment operator "|=". It performs a // bitwise OR operation on the variable and the value. AssignmentOperatorOr = "|=" // AssignmentOperatorXor is the assignment operator "^=". It performs a // bitwise XOR operation on the variable and the value. AssignmentOperatorXor = "^=" )
const ( // UnaryOperatorNot is the unary operator "!". It inverts the value. UnaryOperatorNot = "!" // UnaryOperatorNeg is the unary operator "-". It negates the value. UnaryOperatorNeg = "-" // UnaryOperatorPos is the unary operator "+". It is a no-op. UnaryOperatorPos = "+" // UnaryOperatorBitNot is the unary operator "^". It performs a bitwise // NOT operation on the value. UnaryOperatorBitNot = "^" )
const ( // BinaryOperatorAdd is the binary operator "+". It adds two values. BinaryOperatorAdd = "+" // BinaryOperatorSub is the binary operator "-". It subtracts two values. BinaryOperatorSub = "-" // BinaryOperatorMul is the binary operator "*". It multiplies two values. BinaryOperatorMul = "*" // BinaryOperatorDiv is the binary operator "/". It divides two values. BinaryOperatorDiv = "/" // BinaryOperatorMod is the binary operator "%". It takes the modulo of two // values. BinaryOperatorMod = "%" // BinaryOperatorShl is the binary operator "<<". It shifts the first value // to the left by the second value. BinaryOperatorShl = "<<" // BinaryOperatorShr is the binary operator ">>". It shifts the first value // to the right by the second value. BinaryOperatorShr = ">>" // BinaryOperatorEq is the binary operator "==". It checks if two values are // equal. BinaryOperatorEq = "==" // BinaryOperatorNotEq is the binary operator "!=". It checks if two values // are not equal. BinaryOperatorNotEq = "!=" // BinaryOperatorLess is the binary operator "<". It checks if the first // value is less than the second value. BinaryOperatorLess = "<" // BinaryOperatorGreater is the binary operator ">". It checks if the first // value is greater than the second value. BinaryOperatorGreater = ">" // BinaryOperatorLessEq is the binary operator "<=". It checks if the first // value is less than or equal to the second value. BinaryOperatorLessEq = "<=" // BinaryOperatorGreaterEq is the binary operator ">=". It checks if the // first value is greater than or equal to the second value. BinaryOperatorGreaterEq = ">=" // BinaryOperatorBitAnd is the binary operator "&". It performs a bitwise // AND operation on two values. BinaryOperatorBitAnd = "&" // BinaryOperatorBitOr is the binary operator "|". It performs a bitwise OR // operation on two values. BinaryOperatorBitOr = "|" // BinaryOperatorBitXor is the binary operator "^". It performs a bitwise // XOR operation on two values. BinaryOperatorBitXor = "^" // BinaryOperatorAnd is the binary operator "&&". It performs a logical AND // operation on two values. BinaryOperatorAnd = "&&" // BinaryOperatorOr is the binary operator "||". It performs a logical OR // operation on two values. BinaryOperatorOr = "||" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Assignment ¶
type Assignment struct { Operator string Target Expression Expression Expression }
type BinaryExpression ¶
type BinaryExpression struct { Operator string Left Expression Right Expression }
type Conditional ¶
type Conditional struct { Condition Expression Then []Statement ElseIf *Conditional Else []Statement }
type Declaration ¶
type Declaration interface {
// contains filtered or unexported methods
}
Declaration represents a top-level construct in a shader.
Example:
func hello() {}
type Expression ¶
type Expression interface {
// contains filtered or unexported methods
}
Expression represents a sequence of tokens that can be evaluated to a value.
Example:
1 + sin(10)
type ExpressionGroup ¶
type ExpressionGroup struct {
Expression Expression
}
ExpressionGroup represents a paren enclosed expression.
type FieldIdentifier ¶
type FieldIdentifier struct { ObjName string // TODO: Identifier, or maybe even expression? FieldName string // TODO: Identifier }
FieldIdentifier represents a reference to a field of a structure.
type FloatLiteral ¶
type FloatLiteral struct {
Value float64
}
type FunctionCall ¶
type FunctionCall struct { Name string Arguments []Expression }
type FunctionDeclaration ¶
type Identifier ¶
type Identifier struct {
Name string
}
Identifier represents a reference to a variable or a function.
type IntLiteral ¶
type IntLiteral struct {
Value int64
}
type ParseError ¶ added in v0.22.0
type ParseError struct { // Pos is the position in the source code where the error occurred. Pos Position // Message is the error message. Message string }
ParseError is an error that occurs during parsing.
func (*ParseError) Error ¶ added in v0.22.0
func (e *ParseError) Error() string
Error returns the error message.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is responsible for parsing LSL source code into a shader AST object.
func (*Parser) ParseBlockEnd ¶
ParseBlockEnd parses the closing bracket of a block. It assumes that the next token to follow is a closing bracket token. If the token is not a closing bracket, an error is returned. Whitespace characters up to the closing bracket token are ignored.
func (*Parser) ParseBlockStart ¶
ParseBlockStart parses the opening bracket of a block. It assumes that the next token to follow is an opening bracket token. If the token is not an opening bracket, an error is returned. Whitespace characters up to the opening bracket token are ignored.
func (*Parser) ParseComment ¶
ParseComment assumes that the next token to follow is a comment token and consumes it. Whitespace characters up to the comment token are ignored. Anything other will result in an error. If the comment is followed by a new line token, it is also consumed.
func (*Parser) ParseExpression ¶
func (p *Parser) ParseExpression() (Expression, error)
func (*Parser) ParseFunction ¶
func (p *Parser) ParseFunction() (*FunctionDeclaration, error)
func (*Parser) ParseNamedParameterList ¶
ParseNamedParameterList parses a list of field name and type pairs.
func (*Parser) ParseNewLine ¶
ParseNewLine assumes that the next token to follow is a new line token and consumes it. Whitespace characters up to the new line token are ignored. Anything other will result in an error.
func (*Parser) ParseOptionalRemainder ¶
ParseOptionalRemainder consumes the remainder of the line, including any new line or comment tokens. It assumes that anything to follow is non-vital (comments, new lines) and can be ignored.
func (*Parser) ParseShader ¶
ParseShader parses the LSL source code and returns a shader AST object. If the source code is invalid, an error is returned.
func (*Parser) ParseStatement ¶
func (*Parser) ParseStatementList ¶
func (*Parser) ParseTextureBlock ¶
func (p *Parser) ParseTextureBlock() (*TextureBlockDeclaration, error)
ParseTextureBlock parses a block containing texture fields.
func (*Parser) ParseUniformBlock ¶
func (p *Parser) ParseUniformBlock() (*UniformBlockDeclaration, error)
ParseUniformBlock parses a block containing uniform fields.
func (*Parser) ParseUnnamedParameterList ¶
ParseUnnamedParameterList parses a list of field types.
func (*Parser) ParseVaryingBlock ¶
func (p *Parser) ParseVaryingBlock() (*VaryingBlockDeclaration, error)
ParseVaryingBlock parses a block containing varying fields.
type Position ¶ added in v0.22.0
type Position struct { // Line is the line number, starting from 1. Line uint32 // Column is the column number, starting from 1. Column uint32 }
Position represents a position of interest in the source code.
type Schema ¶
type Schema interface { // IsAllowedTextureType returns whether the provided type name is // allowed to be used in a texture block. IsAllowedTextureType(typeName string) bool // IsAllowedUniformType returns whether the provided type name is // allowed to be used in a uniform block. IsAllowedUniformType(typeName string) bool // IsAllowedVaryingType returns whether the provided type name is // allowed to be used in a varying block. IsAllowedVaryingType(typeName string) bool // IsAllowedVariableType returns whether the provided type name is // allowed to be used in a variable declaration. IsAllowedVariableType(typeName string) bool }
func DefaultSchema ¶
func DefaultSchema() Schema
DefaultSchema returns the default schema implementation.
func ForwardSchema ¶
func ForwardSchema() Schema
func GeometrySchema ¶
func GeometrySchema() Schema
func PostprocessSchema ¶
func PostprocessSchema() Schema
func ShadowSchema ¶
func ShadowSchema() Schema
type Shader ¶
type Shader struct {
Declarations []Declaration
}
func MustParse ¶
MuseParse parses the given LSL source code and returns a shader AST object. If the source code is invalid, it will panic.
func (*Shader) FindFunction ¶
func (s *Shader) FindFunction(name string) (*FunctionDeclaration, bool)
func (*Shader) FindTextureBlock ¶
func (s *Shader) FindTextureBlock() (*TextureBlockDeclaration, bool)
func (*Shader) FindUniformBlock ¶
func (s *Shader) FindUniformBlock() (*UniformBlockDeclaration, bool)
func (*Shader) FindVaryingBlock ¶
func (s *Shader) FindVaryingBlock() (*VaryingBlockDeclaration, bool)
func (*Shader) Functions ¶
func (s *Shader) Functions() []*FunctionDeclaration
func (*Shader) TextureBlocks ¶
func (s *Shader) TextureBlocks() []*TextureBlockDeclaration
func (*Shader) UniformBlocks ¶
func (s *Shader) UniformBlocks() []*UniformBlockDeclaration
func (*Shader) VaryingBlocks ¶
func (s *Shader) VaryingBlocks() []*VaryingBlockDeclaration
type Statement ¶
type Statement interface {
// contains filtered or unexported methods
}
Statement represents a code line in a function.
type TextureBlockDeclaration ¶
type TextureBlockDeclaration struct {
Fields []Field
}
type Token ¶ added in v0.22.0
type Token struct { // Type is the type of token. Type TokenType // Value is the value of the token. Value string // Pos is the position of the token in the source code. Pos Position }
Token represents a single item of interest in the LSL source code. A Tokenizer will convert a string of LSL source code into a sequence of tokens.
func (Token) IsAssignmentOperator ¶ added in v0.22.0
IsAssignmentOperator returns true if the token is an assignment operator token.
func (Token) IsBinaryOperator ¶ added in v0.22.0
IsBinaryOperator returns true if the token is a binary operator token.
func (Token) IsIdentifier ¶ added in v0.22.0
IsIdentifier returns true if the token is an identifier token.
func (Token) IsOperator ¶ added in v0.22.0
IsOperator returns true if the token is an operator token.
func (Token) IsSpecificIdentifier ¶ added in v0.22.0
IsSpecificIdentifier returns true if the token is an identifier token with the specified value.
func (Token) IsSpecificOperator ¶ added in v0.22.0
IsSpecificOperator returns true if the token is an operator token with the specified value.
func (Token) IsTerminal ¶ added in v0.22.0
IsTerminal returns true if the token is a final token and no subsequent tokens will be returned.
func (Token) IsUnaryOperator ¶ added in v0.22.0
IsUnaryOperator returns true if the token is a unary operator token.
type TokenType ¶ added in v0.22.0
type TokenType uint8
TokenType represents the type of a token.
const ( // TokenTypeEOF represents the end of the input. TokenTypeEOF TokenType = iota // TokenTypeError represents an error during tokenization. TokenTypeError // TokenTypeNewLine represents a new line. TokenTypeNewLine // TokenTypeComment represents a comment. TokenTypeComment // TokenTypeIdentifier represents an identifier (e.g. variable name, // type name, field, function name, etc). TokenTypeIdentifier // TokenTypeOperator represents an operator (e.g. assignment, braces, etc). TokenTypeOperator // TokenTypeNumber represents a numeric value. TokenTypeNumber )
type Tokenizer ¶ added in v0.22.0
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer is a mechanism to split an LSL source code into key pieces of information, called tokens. Each token represents a single element of the source code, such as an identifier, a number, an operator, etc.
One would normally use the Parser to process LSL source code, since the Tokenizer provides low-level information about the source code.
func NewTokenizer ¶ added in v0.22.0
NewTokenizer creates a new Tokenizer for the given source code. The source code is expected to be a valid UTF-8 string. If the source code is not a valid UTF-8 string, the tokenizer will return an error token.
type UnaryExpression ¶
type UnaryExpression struct { Operator string Operand Expression }
type UniformBlockDeclaration ¶
type UniformBlockDeclaration struct {
Fields []Field
}
type UsageScope ¶
type UsageScope uint8
const ( UsageScopeNone UsageScope = 0 UsageScopeVertexShader UsageScope = 1 << iota UsageScopeFragmentShader )
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func NewValidator ¶
type VariableDeclaration ¶
type VariableDeclaration struct { Name string Type string Assignment Expression }
type VaryingBlockDeclaration ¶
type VaryingBlockDeclaration struct {
Fields []Field
}