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 BoolLiteral
- type Conditional
- type Declaration
- type Discard
- type Expression
- type Field
- type FieldIdentifier
- type FloatLiteral
- type FunctionCall
- type FunctionDeclaration
- type Identifier
- type IntLiteral
- type ParseError
- type Parser
- func (p *Parser) ParseArgumentBlock() ([]Expression, error)
- func (p *Parser) ParseExpression() (Expression, error)
- func (p *Parser) ParseFieldGroup(opening, closing string) ([]Field, error)
- func (p *Parser) ParseFunction() (*FunctionDeclaration, error)
- func (p *Parser) ParseShader() (*Shader, error)
- func (p *Parser) ParseStatement() (Statement, error)
- func (p *Parser) ParseStatementList() (StatementList, error)
- func (p *Parser) ParseTextureBlock() (*TextureBlockDeclaration, error)
- func (p *Parser) ParseTypeDeclaration() (Declaration, error)
- func (p *Parser) ParseUniformBlock() (*UniformBlockDeclaration, error)
- func (p *Parser) ParseVaryingBlock() (*VaryingBlockDeclaration, error)
- type Position
- type ResolvedFunctionType
- type ResolvedType
- type Return
- type Schema
- type Shader
- type Statement
- type StatementList
- type StructTypeDeclaration
- 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 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 ( // BlockStart is the character that starts a block of code. BlockStart = "{" // BlockEnd is the character that ends a block of code. BlockEnd = "}" )
const ( // GroupStart is the character that starts an expression group or a parameter // list. GroupStart = "(" // GroupEnd is the character that ends an expression group or a parameter // list. GroupEnd = ")" )
const ( // KeywordDiscard is the keyword "discard". It is used to stop the execution // of a fragment shader. KeywordDiscard = "discard" // KeywordTexture is the keyword "texture". It is used to declare a block of // textures in a shader. KeywordTexture = "texture" // KeywordUniform is the keyword "uniform". It is used to declare a block of // uniforms in a shader. KeywordUniform = "uniform" // KeywordVarying is the keyword "varying". It is used to declare a block of // variables in a shader that can cross from the vertex shader stage to the // fragment shader stage. KeywordVarying = "varying" // KeywordTrue is the keyword "true". It is used to represent the boolean // literal true. KeywordTrue = "true" // KeywordFalse is the keyword "false". It is used to represent the boolean // literal false. KeywordFalse = "false" // KeywordType is the keyword "type". It is used to declare a new type. KeywordType = "type" // KeywordStruct is the keyword "struct". It is used to declare a new struct // type. KeywordStruct = "struct" // KeywordFunc is the keyword "func". It is used to declare a new function. KeywordFunc = "func" // KeywordVar is the keyword "var". It is used to declare a variable. KeywordVar = "var" // KeywordIf is the keyword "if". It is used to start a conditional statement. KeywordIf = "if" // KeywordElse is the keyword "else". It is used to start an alternative // branch of a conditional statement. KeywordElse = "else" // KeywordReturn is the keyword "return". It is used to return a value from a // function. KeywordReturn = "return" )
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 = "||" )
const ( // AccessOperatorDot is the character ".". It is used to access a member of a // struct variable. AccessOperatorDot = "." )
const ( // SeparatorComma is the character ",". It is used to separate items in a // list. SeparatorComma = "," )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Assignment ¶
type Assignment struct {
Target Expression
Expression Expression
Operator string
}
Assignment represents an assignment statement.
func (*Assignment) GetPos ¶ added in v0.23.0
func (a *Assignment) GetPos() Position
type BinaryExpression ¶
type BinaryExpression struct {
Operator string
Left Expression
Right Expression
}
BinaryExpression represents a binary operation.
func (*BinaryExpression) GetPos ¶ added in v0.23.0
func (b *BinaryExpression) GetPos() Position
type BoolLiteral ¶ added in v0.23.0
BoolLiteral represents a boolean literal.
func (*BoolLiteral) GetPos ¶ added in v0.23.0
func (b *BoolLiteral) GetPos() Position
type Conditional ¶
type Conditional struct {
Pos Position
Condition Expression
Then StatementList
Else Statement
}
Conditional represents a conditional statement.
func (*Conditional) GetPos ¶ added in v0.23.0
func (c *Conditional) GetPos() Position
type Declaration ¶
type Declaration interface {
// GetPos returns the position of the declaration in the source code.
GetPos() Position
// contains filtered or unexported methods
}
Declaration represents a top-level construct in a shader.
Example:
func hello() {
}
type Discard ¶
type Discard struct {
Pos Position
}
Discard represents a statement that does nothing.
type Expression ¶
type Expression interface {
// GetPos returns the position of the expression in the source code.
GetPos() Position
// contains filtered or unexported methods
}
Expression represents a sequence of tokens that can be evaluated to a value.
Example:
1 + sin(10)
type FieldIdentifier ¶
type FieldIdentifier struct {
Owner Expression
Field Identifier
}
FieldIdentifier represents a reference to a field of a structure.
func (*FieldIdentifier) GetPos ¶ added in v0.23.0
func (f *FieldIdentifier) GetPos() Position
type FloatLiteral ¶
FloatLiteral represents a floating point literal.
func (*FloatLiteral) GetPos ¶ added in v0.23.0
func (f *FloatLiteral) GetPos() Position
type FunctionCall ¶
type FunctionCall struct {
Owner Expression
Arguments []Expression
}
FunctionCall represents a function call.
func (*FunctionCall) GetPos ¶ added in v0.23.0
func (f *FunctionCall) GetPos() Position
type FunctionDeclaration ¶
type FunctionDeclaration struct {
Pos Position
Name string
Inputs []Field
OutputType string
Body StatementList
}
FunctionDeclaration represents a function declaration.
func (*FunctionDeclaration) GetPos ¶ added in v0.23.0
func (f *FunctionDeclaration) GetPos() Position
type Identifier ¶
Identifier represents a reference to a variable or a function.
func (*Identifier) GetPos ¶ added in v0.23.0
func (i *Identifier) GetPos() Position
type IntLiteral ¶
IntLiteral represents an integer literal.
func (*IntLiteral) GetPos ¶ added in v0.23.0
func (i *IntLiteral) GetPos() Position
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) ParseArgumentBlock ¶ added in v0.23.0
func (p *Parser) ParseArgumentBlock() ([]Expression, error)
ParseArgumentBlock parses a block containing argument declarations such as one used in function invocations.
Example:
(10.5, ^66, 2+3)
func (*Parser) ParseExpression ¶
func (p *Parser) ParseExpression() (Expression, error)
ParseExpression uses the Shunting Yard algorithm to parse an expression and return its AST form.
Example:
1 + 2 * 3
func (*Parser) ParseFieldGroup ¶ added in v0.23.0
ParseFieldGroup parses a block containing field declarations such as one used in textures/uniforms/varyings blocks or struct declarations.
Example:
( color vec3 )
func (*Parser) ParseFunction ¶
func (p *Parser) ParseFunction() (*FunctionDeclaration, error)
ParseFunction parses a function declaration.
Example:
func myFunction(a int, b float) {
// function body
}
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 ¶
ParseStatement parses a single imperative statement from the source code.
func (*Parser) ParseStatementList ¶
func (p *Parser) ParseStatementList() (StatementList, error)
ParseStatementList parses a list of statements from the source code.
func (*Parser) ParseTextureBlock ¶
func (p *Parser) ParseTextureBlock() (*TextureBlockDeclaration, error)
ParseTextureBlock parses a block containing texture fields.
Example:
texture ( color sampler2D )
func (*Parser) ParseTypeDeclaration ¶ added in v0.23.0
func (p *Parser) ParseTypeDeclaration() (Declaration, error)
ParseTypeDeclaration parses a type declaration.
Example:
type MyType struct {
color vec3
}
func (*Parser) ParseUniformBlock ¶
func (p *Parser) ParseUniformBlock() (*UniformBlockDeclaration, error)
ParseUniformBlock parses a block containing uniform fields.
Example:
uniform ( color vec4 )
func (*Parser) ParseVaryingBlock ¶
func (p *Parser) ParseVaryingBlock() (*VaryingBlockDeclaration, error)
ParseVaryingBlock parses a block containing varying fields.
Example:
varying ( color vec3 )
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 ResolvedFunctionType ¶ added in v0.23.0
type ResolvedFunctionType struct {
Name string
Scope UsageScope
}
type ResolvedType ¶ added in v0.23.0
type ResolvedType struct {
Name string
Scope UsageScope
ReadOnly bool
NonCopy bool
}
func BuiltInResolvedType ¶ added in v0.23.0
func BuiltInResolvedType(name string, scope UsageScope) ResolvedType
type Return ¶ added in v0.23.0
type Return struct {
Pos Position
Expression Expression
}
Return represents a return statement.
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
}
Shader represents a complete shader program.
func (*Shader) FindFunction ¶
func (s *Shader) FindFunction(name string) (*FunctionDeclaration, bool)
func (*Shader) Functions ¶
func (s *Shader) Functions() []*FunctionDeclaration
type Statement ¶
type Statement interface {
// GetPos returns the position of the statement in the source code.
GetPos() Position
// contains filtered or unexported methods
}
Statement represents a code line in a function.
Example:
a := 5.0
type StatementList ¶ added in v0.23.0
type StatementList []Statement
StatementList represents a list of statements.
func (StatementList) GetPos ¶ added in v0.23.0
func (l StatementList) GetPos() Position
type StructTypeDeclaration ¶ added in v0.23.0
StructTypeDeclaration represents a structure type declaration.
func (*StructTypeDeclaration) GetPos ¶ added in v0.23.0
func (s *StructTypeDeclaration) GetPos() Position
type TextureBlockDeclaration ¶
TextureBlockDeclaration represents a texture block declaration.
func (*TextureBlockDeclaration) GetPos ¶ added in v0.23.0
func (t *TextureBlockDeclaration) GetPos() Position
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 {
Pos Position
Operator string
Operand Expression
}
UnaryExpression represents a unary operation.
func (*UnaryExpression) GetPos ¶ added in v0.23.0
func (u *UnaryExpression) GetPos() Position
type UniformBlockDeclaration ¶
UniformBlockDeclaration represents a uniform block declaration.
func (*UniformBlockDeclaration) GetPos ¶ added in v0.23.0
func (u *UniformBlockDeclaration) GetPos() Position
type UsageScope ¶
type UsageScope uint8
const ( UsageScopeVertexShader UsageScope = 1 << iota UsageScopeFragmentShader UsageScopeNone UsageScope = 0 UsageScopeAll = UsageScopeVertexShader | UsageScopeFragmentShader )
type VariableDeclaration ¶
type VariableDeclaration struct {
Pos Position
Name string
Type string
Assignment Expression
}
VariableDeclaration represents a variable declaration.
func (*VariableDeclaration) GetPos ¶ added in v0.23.0
func (v *VariableDeclaration) GetPos() Position
type VaryingBlockDeclaration ¶
VaryingBlockDeclaration represents a varying block declaration.
func (*VaryingBlockDeclaration) GetPos ¶ added in v0.23.0
func (v *VaryingBlockDeclaration) GetPos() Position