Documentation ¶
Overview ¶
Package ast implements a parser and formatter for slang
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FormatOptions ¶
type FormatOptions struct { // Formatter is the default formatter to use to format a // node. This is used when a Node is recursively formaatted // allowing callers to wrap a formatter with another. Formatter }
FormatOptions defines the set of format options availqble
type Formatter ¶
type Formatter interface {
Format(w io.Writer, n Node, options *FormatOptions) error
}
Formatter is the interface to format a Node
type JSON ¶
JSON provides support for marshaling and unmarshaling a Node.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/argots/slang/pkg/ast" ) func main() { n, err := ast.ParseString("x + y") if err != nil { panic(err) } data, err := json.MarshalIndent(&ast.JSON{Node: n}, "", " ") if err != nil { panic(err) } fmt.Println(string(data)) }
Output: { "type": "Expr", "op": "+", "nodes": [ { "type": "Ident", "val": "x" }, { "type": "Ident", "val": "y" } ] }
func (*JSON) MarshalJSON ¶
MarshalJSON marshals a node into JSON
func (*JSON) UnmarshalJSON ¶
UnmarshalJSON unmarshals a set of bytes into a node
type Loc ¶
type Loc uint32
Loc represents a location.
This can be resolved to an offset using a location map.
type LocMap ¶
type LocMap interface { Get(handle Loc) (location string, start, end uint32) Add(location string, start, end uint32) (handle Loc) }
LocMap implements a map of token offsets to a Loc handle
type Node ¶
Node is the main interface implemented by all nodes in the AST
func ParseString ¶
ParseString parses a string and returns an AST.
type ParseError ¶
ParseError is returned for all errors
type SourceReader ¶
type SourceReader interface {
ReadSource(location string) io.ReadCloser
}
SourceReader implements an Reader for source code.
type Sources ¶
type Sources struct { // Client allows overriding the default client if needed. *http.Client // contains filtered or unexported fields }
Sources implements a source reaader for reading for text, file or url.
func (*Sources) AddFileSource ¶
AddFileSource adds the file contents as a source.
func (*Sources) AddPublicURLSource ¶
AddPublicURLSource adds the URL contents as a source.
If the Client field of Sources is non-nil, that is used. If not, a custom client is used with timeouts filled in.
func (*Sources) AddStringSource ¶
AddStringSource adds the string contents as a source.
func (*Sources) ReadSource ¶
func (s *Sources) ReadSource(location string) io.ReadCloser
ReadSource returns an io.Reader for the source contents.
type TextFormatter ¶
type TextFormatter struct{}
TextFormatter implements a simple text formatting of a node
func (*TextFormatter) Format ¶
func (f *TextFormatter) Format(w io.Writer, n Node, options *FormatOptions) error
Format formats a node.