Documentation ¶
Index ¶
- Constants
- Variables
- func Actor(p *ast.Parser) (*ast.Node, error)
- func ActorType(p *ast.Parser) (*ast.Node, error)
- func ArgType(p *ast.Parser) (*ast.Node, error)
- func Ascii(p *parser.Parser) (*parser.Cursor, bool)
- func Blob(p *ast.Parser) (*ast.Node, error)
- func Char(p *ast.Parser) (*ast.Node, error)
- func Comment(p *ast.Parser) (*ast.Node, error)
- func CommentText(p *ast.Parser) (*ast.Node, error)
- func ConsType(p *ast.Parser) (*ast.Node, error)
- func DataType(p *ast.Parser) (*ast.Node, error)
- func Def(p *ast.Parser) (*ast.Node, error)
- func Digit(p *parser.Parser) (*parser.Cursor, bool)
- func Escape(p *parser.Parser) (*parser.Cursor, bool)
- func FieldType(p *ast.Parser) (*ast.Node, error)
- func Fields(p *ast.Parser) (*ast.Node, error)
- func Func(p *ast.Parser) (*ast.Node, error)
- func FuncAnn(p *ast.Parser) (*ast.Node, error)
- func FuncType(p *ast.Parser) (*ast.Node, error)
- func Hex(p *parser.Parser) (*parser.Cursor, bool)
- func HexNum(p *ast.Parser) (*ast.Node, error)
- func Id(p *ast.Parser) (*ast.Node, error)
- func Import(p *ast.Parser) (*ast.Node, error)
- func Letter(p *parser.Parser) (*parser.Cursor, bool)
- func MethType(p *ast.Parser) (*ast.Node, error)
- func Name(p *ast.Parser) (*ast.Node, error)
- func Nat(p *ast.Parser) (*ast.Node, error)
- func Nl(p *ast.Parser) (*ast.Node, error)
- func Num(p *ast.Parser) (*ast.Node, error)
- func NumType(p *ast.Parser) (*ast.Node, error)
- func Opt(p *ast.Parser) (*ast.Node, error)
- func PrimType(p *ast.Parser) (*ast.Node, error)
- func Principal(p *ast.Parser) (*ast.Node, error)
- func Prog(p *ast.Parser) (*ast.Node, error)
- func Record(p *ast.Parser) (*ast.Node, error)
- func RefType(p *ast.Parser) (*ast.Node, error)
- func Service(p *ast.Parser) (*ast.Node, error)
- func Sp(p *ast.Parser) (*ast.Node, error)
- func Text(p *ast.Parser) (*ast.Node, error)
- func TupType(p *ast.Parser) (*ast.Node, error)
- func Type(p *ast.Parser) (*ast.Node, error)
- func Utf(p *ast.Parser) (*ast.Node, error)
- func UtfEnc(p *ast.Parser) (*ast.Node, error)
- func Utfcont(p *parser.Parser) (*parser.Cursor, bool)
- func Variant(p *ast.Parser) (*ast.Node, error)
- func Vec(p *ast.Parser) (*ast.Node, error)
- func Ws(p *ast.Parser) (*ast.Node, error)
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 ActorType ¶
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 ¶
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 Comment ¶
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 ConsType ¶
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 Def ¶
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 FuncType ¶
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 MethType ¶
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 TupType ¶
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>
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.