candid

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	Unknown = iota

	ProgT        // 001
	TypeT        // 002
	ImportT      // 003
	ActorT       // 004
	ActorTypeT   // 005
	MethTypeT    // 006
	FuncTypeT    // 007
	FuncAnnT     // 008
	TupTypeT     // 009
	ArgTypeT     // 010
	FieldTypeT   // 011
	DataTypeT    // 012
	PrimTypeT    // 013
	BlobT        // 014
	OptT         // 015
	VecT         // 016
	RecordT      // 017
	VariantT     // 018
	FuncT        // 019
	ServiceT     // 020
	PrincipalT   // 021
	IdT          // 022
	TextT        // 023
	NatT         // 024
	CommentTextT // 025
)

Node Types

View Source
const (
	ESC = 0x005C // \
)

Token Definitions

Variables

View Source
var NodeTypes = []string{
	"UNKNOWN",

	"Prog",
	"Type",
	"Import",
	"Actor",
	"ActorType",
	"MethType",
	"FuncType",
	"FuncAnn",
	"TupType",
	"ArgType",
	"FieldType",
	"DataType",
	"PrimType",
	"Blob",
	"Opt",
	"Vec",
	"Record",
	"Variant",
	"Func",
	"Service",
	"Principal",
	"Id",
	"Text",
	"Nat",
	"CommentText",
}

Functions

func Actor

func Actor(p *ast.Parser) (*ast.Node, error)

func ActorType

func ActorType(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	var example = `{
	addUser : (name : text, age : nat8) -> (id : nat64);
	userName : (id : nat64) -> (text) query;
	userAge : (id : nat64) -> (nat8) query;
	deleteUser : (id : nat64) -> () oneway;
}`
	p, _ := ast.New([]byte(example))
	actor, _ := candid.ActorType(p)
	fmt.Println(len(actor.Children()))
}
Output:

4

func ArgType

func ArgType(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	p := func(s string) *ast.Parser {
		p, _ := ast.New([]byte(s))
		return p
	}
	fmt.Println(candid.ArgType(p("name : text")))
	fmt.Println(candid.ArgType(p("age : nat8")))
	fmt.Println(candid.ArgType(p("id : nat64")))
}
Output:

["ArgType",[["Id","name"],["PrimType","text"]]] <nil>
["ArgType",[["Id","age"],["PrimType","nat8"]]] <nil>
["ArgType",[["Id","id"],["PrimType","nat64"]]] <nil>

func Ascii

func Ascii(p *parser.Parser) (*parser.Cursor, bool)

func Blob

func Blob(p *ast.Parser) (*ast.Node, error)

func Char

func Char(p *ast.Parser) (*ast.Node, error)

func Comment

func Comment(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	var example = `// This is a comment.
`
	p, _ := ast.New([]byte(example))
	comment, _ := candid.Comment(p)
	fmt.Println(comment.FirstChild.Value)
}
Output:

This is a comment.

func CommentText

func CommentText(p *ast.Parser) (*ast.Node, error)

func ConsType

func ConsType(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	for _, record := range []string{
		"record {\n  num : nat;\n}",
		"record { nat; nat }",
		"record { 0 : nat; 1 : nat }",
	} {
		p, _ := ast.New([]byte(record))
		fmt.Println(candid.ConsType(p))
	}
}
Output:

["Record",[["FieldType",[["Id","num"],["PrimType","nat"]]]]] <nil>
["Record",[["FieldType",[["PrimType","nat"]]],["FieldType",[["PrimType","nat"]]]]] <nil>
["Record",[["FieldType",[["Nat","0"],["PrimType","nat"]]],["FieldType",[["Nat","1"],["PrimType","nat"]]]]] <nil>

func DataType

func DataType(p *ast.Parser) (*ast.Node, error)

func Def

func Def(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	for _, def := range []string{
		"type list = opt node",
		"type color = variant { red; green; blue }",
		"type tree = variant {\n  leaf : int;\n  branch : record {left : tree; val : int; right : tree};\n}",
		"type stream = opt record {head : nat; next : func () -> stream}",
	} {
		p, _ := ast.New([]byte(def))
		fmt.Println(candid.Def(p))
	}
}
Output:

["Type",[["Id","list"],["Opt",[["Id","node"]]]]] <nil>
["Type",[["Id","color"],["Variant",[["FieldType",[["Id","red"]]],["FieldType",[["Id","green"]]],["FieldType",[["Id","blue"]]]]]]] <nil>
["Type",[["Id","tree"],["Variant",[["FieldType",[["Id","leaf"],["PrimType","int"]]],["FieldType",[["Id","branch"],["Record",[["FieldType",[["Id","left"],["Id","tree"]]],["FieldType",[["Id","val"],["PrimType","int"]]],["FieldType",[["Id","right"],["Id","tree"]]]]]]]]]]] <nil>
["Type",[["Id","stream"],["Opt",[["Record",[["FieldType",[["Id","head"],["PrimType","nat"]]],["FieldType",[["Id","next"],["Func",[["FuncType",[["TupType","()"],["ArgType",[["Id","stream"]]]]]]]]]]]]]]] <nil>

func Digit

func Digit(p *parser.Parser) (*parser.Cursor, bool)

func Escape

func Escape(p *parser.Parser) (*parser.Cursor, bool)

func FieldType

func FieldType(p *ast.Parser) (*ast.Node, error)

func Fields

func Fields(p *ast.Parser) (*ast.Node, error)

func Func

func Func(p *ast.Parser) (*ast.Node, error)

func FuncAnn

func FuncAnn(p *ast.Parser) (*ast.Node, error)

func FuncType

func FuncType(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	for _, function := range []string{
		"(text, text, nat16) -> (text, nat64)",
		"(name : text, address : text, nat16) -> (text, id : nat64)",
		"(name : text, address : text, nr : nat16) -> (nick : text, id : nat64)",
	} {
		p, _ := ast.New([]byte(function))
		fmt.Println(candid.FuncType(p))
	}
}
Output:

["FuncType",[["TupType",[["ArgType",[["PrimType","text"]]],["ArgType",[["PrimType","text"]]],["ArgType",[["PrimType","nat16"]]]]],["TupType",[["ArgType",[["PrimType","text"]]],["ArgType",[["PrimType","nat64"]]]]]]] <nil>
["FuncType",[["TupType",[["ArgType",[["Id","name"],["PrimType","text"]]],["ArgType",[["Id","address"],["PrimType","text"]]],["ArgType",[["PrimType","nat16"]]]]],["TupType",[["ArgType",[["PrimType","text"]]],["ArgType",[["Id","id"],["PrimType","nat64"]]]]]]] <nil>
["FuncType",[["TupType",[["ArgType",[["Id","name"],["PrimType","text"]]],["ArgType",[["Id","address"],["PrimType","text"]]],["ArgType",[["Id","nr"],["PrimType","nat16"]]]]],["TupType",[["ArgType",[["Id","nick"],["PrimType","text"]]],["ArgType",[["Id","id"],["PrimType","nat64"]]]]]]] <nil>

func Hex

func Hex(p *parser.Parser) (*parser.Cursor, bool)

func HexNum

func HexNum(p *ast.Parser) (*ast.Node, error)

func Id

func Id(p *ast.Parser) (*ast.Node, error)

func Import

func Import(p *ast.Parser) (*ast.Node, error)

func Letter

func Letter(p *parser.Parser) (*parser.Cursor, bool)

func MethType

func MethType(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	for _, method := range []string{
		"addUser : (name : text, age : nat8) -> (id : nat64)",
		"userName : (id : nat64) -> (text) query",
		"userAge : (id : nat64) -> (nat8) query",
		"deleteUser : (id : nat64) -> () oneway",
	} {
		p, _ := ast.New([]byte(method))
		fmt.Println(candid.MethType(p))
	}
}
Output:

["MethType",[["Id","addUser"],["FuncType",[["TupType",[["ArgType",[["Id","name"],["PrimType","text"]]],["ArgType",[["Id","age"],["PrimType","nat8"]]]]],["TupType",[["ArgType",[["Id","id"],["PrimType","nat64"]]]]]]]]] <nil>
["MethType",[["Id","userName"],["FuncType",[["TupType",[["ArgType",[["Id","id"],["PrimType","nat64"]]]]],["TupType",[["ArgType",[["PrimType","text"]]]]],["FuncAnn","query"]]]]] <nil>
["MethType",[["Id","userAge"],["FuncType",[["TupType",[["ArgType",[["Id","id"],["PrimType","nat64"]]]]],["TupType",[["ArgType",[["PrimType","nat8"]]]]],["FuncAnn","query"]]]]] <nil>
["MethType",[["Id","deleteUser"],["FuncType",[["TupType",[["ArgType",[["Id","id"],["PrimType","nat64"]]]]],["TupType","()"],["FuncAnn","oneway"]]]]] <nil>

func Name

func Name(p *ast.Parser) (*ast.Node, error)

func Nat

func Nat(p *ast.Parser) (*ast.Node, error)

func Nl

func Nl(p *ast.Parser) (*ast.Node, error)

func Num

func Num(p *ast.Parser) (*ast.Node, error)

func NumType

func NumType(p *ast.Parser) (*ast.Node, error)

func Opt

func Opt(p *ast.Parser) (*ast.Node, error)

func PrimType

func PrimType(p *ast.Parser) (*ast.Node, error)

func Principal

func Principal(p *ast.Parser) (*ast.Node, error)

func Prog

func Prog(p *ast.Parser) (*ast.Node, error)

func Record

func Record(p *ast.Parser) (*ast.Node, error)

func RefType

func RefType(p *ast.Parser) (*ast.Node, error)

func Service

func Service(p *ast.Parser) (*ast.Node, error)

func Sp

func Sp(p *ast.Parser) (*ast.Node, error)

func Text

func Text(p *ast.Parser) (*ast.Node, error)

func TupType

func TupType(p *ast.Parser) (*ast.Node, error)
Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/candid/internal/candid"
	"github.com/di-wu/parser/ast"
)

func main() {
	for _, tuple := range []string{
		"(name : text, age : nat8)",
		"(id : nat64)",
		"()",
	} {
		p, _ := ast.New([]byte(tuple))
		n, err := candid.TupType(p)
		fmt.Println(n, err)
	}
}
Output:

["TupType",[["ArgType",[["Id","name"],["PrimType","text"]]],["ArgType",[["Id","age"],["PrimType","nat8"]]]]] <nil>
["TupType",[["ArgType",[["Id","id"],["PrimType","nat64"]]]]] <nil>
["TupType","()"] <nil>

func Type

func Type(p *ast.Parser) (*ast.Node, error)

func Utf

func Utf(p *ast.Parser) (*ast.Node, error)

func UtfEnc

func UtfEnc(p *ast.Parser) (*ast.Node, error)

func Utfcont

func Utfcont(p *parser.Parser) (*parser.Cursor, bool)

func Variant

func Variant(p *ast.Parser) (*ast.Node, error)

func Vec

func Vec(p *ast.Parser) (*ast.Node, error)

func Ws

func Ws(p *ast.Parser) (*ast.Node, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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