scparse

package
v0.0.0-...-a3e9adc Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2021 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package scparse implements parsing and formatting of SC files. It provides node types that are used to build an abstract syntax tree (AST).

The Parse function parses SC source text and creates an AST.

The Format function formats an AST back to source text.

In general, clients should use the sc package for working with SC data in Go. This package should only be used if you need to directly maniplate AST nodes. An example use case would be to access comments in an SC file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(n *DictionaryNode) []byte

Format converts the SC AST to a textual representation and returns it as a byte slice.

An effort will be made to preserve comments near the data they describe and format the data nicely. However, the original textual representation of the source is not guaranteed to be preserved.

Types

type BoolNode

type BoolNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	True         bool // The boolean value.
}

BoolNode holds a boolean value.

func (*BoolNode) Comments

func (n *BoolNode) Comments() *CommentGroup

func (*BoolNode) Position

func (n *BoolNode) Position() Pos

func (*BoolNode) String

func (n *BoolNode) String() string

func (*BoolNode) Type

func (n *BoolNode) Type() NodeType

type Comment

type Comment struct {
	Pos     Pos    // Position of the comment in the input string.
	Text    string // The comment text with the // or /* stripped.
	IsBlock bool   // Is it a /*-style comment.
}

Comment represents a comment. It can be either a line comment or a block comment.

type CommentGroup

type CommentGroup struct {
	Head   []Comment // Comments before the node.
	Inline []Comment // Comments after the node on the same line.

	// Comments after the node on a separate line.
	// Only used for comments after the last element of a list or dictionary.
	Foot  []Comment
	Inner []Comment // Comments inside an empty list or dictionary.
}

CommentGroup contains all comments associated with a node.

type DictionaryNode

type DictionaryNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Members      []*MemberNode // The members in the order they were scanned.
}

DictionaryNode holds a dictionary that contains a list of members.

func Parse

func Parse(input []byte) (n *DictionaryNode, err error)

Parse parses the SC source and generates an AST. If err is not nil, it will contain details on the error encountered and it's location in input.

func (*DictionaryNode) Comments

func (n *DictionaryNode) Comments() *CommentGroup

func (*DictionaryNode) Position

func (n *DictionaryNode) Position() Pos

func (*DictionaryNode) String

func (n *DictionaryNode) String() string

func (*DictionaryNode) Type

func (n *DictionaryNode) Type() NodeType

type Error

type Error struct {
	// Pos is the position of the error in the source.
	Pos Pos
	// Context contains the details of the error.
	Context string
}

Error represents an error that occurred during parsing. It contains information about the context of the error.

func (*Error) Error

func (e *Error) Error() string

type IdentifierNode

type IdentifierNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Name         string // The identifier name.
}

IdentifierNode holds an identifier.

func (*IdentifierNode) Comments

func (n *IdentifierNode) Comments() *CommentGroup

func (*IdentifierNode) KeyString

func (n *IdentifierNode) KeyString() string

func (*IdentifierNode) Position

func (n *IdentifierNode) Position() Pos

func (*IdentifierNode) String

func (n *IdentifierNode) String() string

func (*IdentifierNode) Type

func (n *IdentifierNode) Type() NodeType

type InterpolatedStringNode

type InterpolatedStringNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Components   []StringContentNode // Each component is either a StringNode or VariableNode.
}

InterpolatedStringNode is a double quoted string that may have variables interpolated into it. It contains a list of component nodes which are either StringNodes or VariableNodes.

func (*InterpolatedStringNode) Comments

func (n *InterpolatedStringNode) Comments() *CommentGroup

func (*InterpolatedStringNode) Position

func (n *InterpolatedStringNode) Position() Pos

func (*InterpolatedStringNode) String

func (n *InterpolatedStringNode) String() string

func (*InterpolatedStringNode) Type

func (n *InterpolatedStringNode) Type() NodeType

type KeyNode

type KeyNode interface {
	Node
	// KeyString returns the string value of the key.
	// This is a convenient way to access the key value by avoiding
	// a type assertion when the underlying node type does not matter.
	KeyString() string
}

KeyNode is a Node that can act as a dictionary key.

type ListNode

type ListNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Elements     []ValueNode // The elements in the order they were scanned.
}

ListNode holds a list that contains a sequence of nodes.

func (*ListNode) Comments

func (n *ListNode) Comments() *CommentGroup

func (*ListNode) Position

func (n *ListNode) Position() Pos

func (*ListNode) String

func (n *ListNode) String() string

func (*ListNode) Type

func (n *ListNode) Type() NodeType

type MemberNode

type MemberNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Key          KeyNode   // The key of the member.
	Value        ValueNode // The value of the member.
}

MemberNode holds a member of a dictionary. It contains the key and the value.

func (*MemberNode) Comments

func (n *MemberNode) Comments() *CommentGroup

func (*MemberNode) Position

func (n *MemberNode) Position() Pos

func (*MemberNode) String

func (n *MemberNode) String() string

func (*MemberNode) Type

func (n *MemberNode) Type() NodeType

type Node

type Node interface {
	// Type identifies the type of the node.
	Type() NodeType
	// Position returns the position of the node in the input text.
	Position() Pos
	// Comments returns the comments attached to the node.
	Comments() *CommentGroup
	String() string
	// contains filtered or unexported methods
}

A Node is an element in the AST.

The interface contains an unexported method so that only types in this package can implement it.

type NodeType

type NodeType int

NodeType identifies the type of an AST node.

const (
	NodeNull NodeType = iota
	NodeBool
	NodeNumber
	NodeString
	NodeInterpolatedString
	NodeRawString
	NodeIdentifier
	NodeVariable
	NodeList
	NodeMember
	NodeDictionary
)

func (NodeType) String

func (nt NodeType) String() string

type NullNode

type NullNode struct {
	Pos          Pos
	CommentGroup CommentGroup
}

NullNode holds the special identifier 'null' representing the null value.

func (*NullNode) Comments

func (n *NullNode) Comments() *CommentGroup

func (*NullNode) Position

func (n *NullNode) Position() Pos

func (*NullNode) String

func (n *NullNode) String() string

func (*NullNode) Type

func (n *NullNode) Type() NodeType

type NumberNode

type NumberNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	IsUint       bool    // The number has a unsigned int value.
	IsInt        bool    // The number has an int value.
	IsFloat      bool    // The number has a float value.
	Uint64       uint64  // The unsigned int value.
	Int64        int64   // The int value.
	Float64      float64 // The float value.
	Raw          string  // The raw string value from the input.
}

NumberNode holds a number, either an int or a float. The value is parsed and stored under all types that can represent the value.

func (*NumberNode) Comments

func (n *NumberNode) Comments() *CommentGroup

func (*NumberNode) Position

func (n *NumberNode) Position() Pos

func (*NumberNode) String

func (n *NumberNode) String() string

func (*NumberNode) Type

func (n *NumberNode) Type() NodeType

type Pos

type Pos struct {
	// Line is the line in the input text that this position occurs at.
	// Lines are counted starting at 1.
	Line int
	// Column is the column in the input text that this position occurs at.
	// Columns are counted starting at 1.
	//
	// Column takes unicode characters into account and reflects where the
	// character appears visually.
	Column int
	// Byte is the byte offset in the input where the position occurs.
	// Bytes are counted starting at 0.
	//
	// Byte reflects the exact byte in the input and does not take unicode
	// characters into account.
	Byte int
}

Pos represents a source position in the original input text.

type RawStringNode

type RawStringNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Value        string // The string value, after quotes have been removed.
}

RawStringNode holds a raw string value. The quotes have been removed.

func (*RawStringNode) Comments

func (n *RawStringNode) Comments() *CommentGroup

func (*RawStringNode) KeyString

func (n *RawStringNode) KeyString() string

func (*RawStringNode) Position

func (n *RawStringNode) Position() Pos

func (*RawStringNode) String

func (n *RawStringNode) String() string

func (*RawStringNode) Type

func (n *RawStringNode) Type() NodeType

type StringContentNode

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

StringContentNode is a node that can appear inside an InterpolatedStringNode.

type StringNode

type StringNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Value        string // The string value, after quotes have been removed.
}

StringNode holds a string value. The quotes have been removed.

func (*StringNode) Comments

func (n *StringNode) Comments() *CommentGroup

func (*StringNode) KeyString

func (n *StringNode) KeyString() string

func (*StringNode) Position

func (n *StringNode) Position() Pos

func (*StringNode) String

func (n *StringNode) String() string

func (*StringNode) Type

func (n *StringNode) Type() NodeType

type ValueNode

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

ValueNode is a node that is an SC value.

type VariableNode

type VariableNode struct {
	Pos          Pos
	CommentGroup CommentGroup
	Identifier   *IdentifierNode // The variable name.
}

VariableNode holds a variable.

func (*VariableNode) Comments

func (n *VariableNode) Comments() *CommentGroup

func (*VariableNode) Position

func (n *VariableNode) Position() Pos

func (*VariableNode) String

func (n *VariableNode) String() string

func (*VariableNode) Type

func (n *VariableNode) Type() NodeType

Jump to

Keyboard shortcuts

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