ast

package
v0.0.0-...-b177c2e Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2021 License: MIT Imports: 13 Imported by: 0

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 Expr

type Expr struct {
	Op string
	Loc
	X, Y Node
}

Expr represents an expression of form X Op Y. For unary expressions, X will be nil.

func (*Expr) NodeInfo

func (x *Expr) NodeInfo() (value string, loc Loc)

NodeInfo returns the operator string and the location.

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 Ident

type Ident struct {
	Val string
	Loc
}

Ident represents an identifier

func (Ident) NodeInfo

func (i Ident) NodeInfo() (value string, loc Loc)

NodeInfo returns the raw identifier string (including if has a quoted string) and its location in the source code.

type JSON

type JSON struct {
	LocMap
	Node
}

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

func (j *JSON) MarshalJSON() ([]byte, error)

MarshalJSON marshals a node into JSON

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(data []byte) error

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.

func (Loc) Offset

func (l Loc) Offset(lm LocMap) (source string, start, end uint32)

Offset returns the location for a token location

func (Loc) Token

func (l Loc) Token(lm LocMap, sources SourceReader) (string, error)

Token returns the actual token given a source reader

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

func NewLocMap

func NewLocMap() LocMap

NewLocMap returns a simple loc map implementation.

type Node

type Node interface {
	NodeInfo() (value string, loc Loc)
}

Node is the main interface implemented by all nodes in the AST

func ParseString

func ParseString(s string) (Node, error)

ParseString parses a string and returns an AST.

type Number

type Number struct {
	Val string
	Loc
}

Number represents a numeric literal

func (Number) NodeInfo

func (n Number) NodeInfo() (value string, loc Loc)

NodeInfo returns the raw numeric string and its location in the source code.

type Paren

type Paren struct {
	StartOp, EndOp   string
	StartLoc, EndLoc Loc
	X, Y             Node
}

Paren represents a parenthesized expression.

func (*Paren) NodeInfo

func (p *Paren) NodeInfo() (value string, loc Loc)

NodeInfo returns the start operator and the start location.

type ParseError

type ParseError struct {
	Reason string
	Source string
	Offset int
}

ParseError is returned for all errors

func (*ParseError) Error

func (p *ParseError) Error() string

Error implements the error interface

type Quote

type Quote struct {
	Val string
	Loc
}

Quote represents a quoted string

func (Quote) NodeInfo

func (q Quote) NodeInfo() (value string, loc Loc)

NodeInfo returns the raw string (including the open/close quote and any embedded slashes) and its location in the source code.

type Seq

type Seq struct {
	StartOp, EndOp   string
	StartLoc, EndLoc Loc
	X, Y             Node
}

Seq represents a Seq expression of the form X [ Y ]

func (*Seq) NodeInfo

func (s *Seq) NodeInfo() (value string, loc Loc)

NodeInfo returns the start operator and the start location.

type Set

type Set struct {
	StartOp, EndOp   string
	StartLoc, EndLoc Loc
	X, Y             Node
}

Set represents a Seq expression of the form X { Y }

func (*Set) NodeInfo

func (s *Set) NodeInfo() (value string, loc Loc)

NodeInfo returns the start operator and the start location.

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

func (s *Sources) AddFileSource(location, filePath string)

AddFileSource adds the file contents as a source.

func (*Sources) AddPublicURLSource

func (s *Sources) AddPublicURLSource(location, url string)

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

func (s *Sources) AddStringSource(location, source string)

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.

Jump to

Keyboard shortcuts

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