Skip to content

Commit

Permalink
Rewrite grammars in ABNF.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Jan 22, 2025
1 parent f42fbb0 commit 6ca34c9
Show file tree
Hide file tree
Showing 48 changed files with 1,711 additions and 5,543 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ test-cover:
go tool cover -html=coverage.out

gen:
cd candid && go generate
cd pocketic && go generate
cd candid/internal && go generate
cd certification/http/certexp && go generate
cd clients/ledger && go generate
cd clients/registry && go generate

Expand Down
4 changes: 2 additions & 2 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/aviate-labs/agent-go/principal"
)

var _ = new(testLogger)

var (
LEDGER_PRINCIPAL = principal.MustDecode("ryjl3-tyaaa-aaaaa-aaaba-cai")
REGISTRY_PRINCIPAL = principal.MustDecode("rwlgt-iiaaa-aaaaa-aaaaa-cai")
)

var _ = new(testLogger)

func Example_anonymous_query() {
a, _ := agent.New(agent.DefaultConfig)
type Account struct {
Expand Down
18 changes: 5 additions & 13 deletions candid/candid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/aviate-labs/agent-go/candid/internal/candid"
"github.com/aviate-labs/agent-go/candid/internal/candidvalue"
"github.com/aviate-labs/agent-go/principal"
"github.com/di-wu/parser"
"github.com/di-wu/parser/ast"
)

// DecodeValueString decodes the given value into a candid string.
Expand Down Expand Up @@ -47,17 +45,14 @@ func DecodeValuesString(types []idl.Type, values []any) (string, error) {

// EncodeValueString encodes the given candid string into a byte slice.
func EncodeValueString(value string) ([]byte, error) {
p, err := ast.New([]byte(value))
p, err := candidvalue.NewParser([]rune(value))
if err != nil {
return nil, err
}
n, err := candidvalue.Values(p)
n, err := p.ParseEOF(candidvalue.Values)
if err != nil {
return nil, err
}
if _, err := p.Expect(parser.EOD); err != nil {
return nil, err
}
types, args, err := did.ConvertValues(n)
if err != nil {
return nil, err
Expand All @@ -66,18 +61,15 @@ func EncodeValueString(value string) ([]byte, error) {
}

// ParseDID parses the given raw .did files and returns the Program that is defined in it.
func ParseDID(raw []byte) (did.Description, error) {
p, err := ast.New(raw)
func ParseDID(raw []rune) (did.Description, error) {
p, err := candid.NewParser(raw)
if err != nil {
return did.Description{}, err
}
n, err := candid.Prog(p)
n, err := p.ParseEOF(candid.Prog)
if err != nil {
return did.Description{}, err
}
if _, err := p.Expect(parser.EOD); err != nil {
return did.Description{}, err
}
return did.ConvertDescription(n), nil
}

Expand Down
4 changes: 2 additions & 2 deletions candid/candid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExampleEncodeValueString_blob() {

func ExampleParseDID() {
raw, _ := os.ReadFile("testdata/counter.did")
p, _ := candid.ParseDID(raw)
p, _ := candid.ParseDID([]rune(string(raw)))
fmt.Println(p)
// Output:
// service : {
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestEncodeValue(t *testing.T) {

func TestParseDID(t *testing.T) {
raw, _ := os.ReadFile("internal/candid/testdata/ic.did")
if _, err := candid.ParseDID(raw); err != nil {
if _, err := candid.ParseDID([]rune(string(raw))); err != nil {
t.Error(err)
}
}
Expand Down
68 changes: 35 additions & 33 deletions candid/did/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"strconv"
"strings"

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

func convertNat(n *ast.Node) *big.Int {
switch n := strings.ReplaceAll(n.Value, "_", ""); {
func convertNat(n *parser.Node) *big.Int {
switch n := strings.ReplaceAll(n.Value(), "_", ""); {
case strings.HasPrefix(n, "0x"):
n = strings.TrimPrefix(n, "0x")
i, _ := strconv.ParseInt(n, 16, 64)
Expand All @@ -36,22 +36,22 @@ type Data interface {
fmt.Stringer
}

func convertData(n *ast.Node) Data {
switch n.Type {
case candid.BlobT:
func convertData(n *parser.Node) Data {
switch n.Name {
case candid.Blob.Name:
return Blob{}
case candid.OptT:
case candid.Opt.Name:
return Optional{
Data: convertData(n.FirstChild),
Data: convertData(n.Children()[0]),
}
case candid.VecT:
case candid.Vec.Name:
return Vector{
Data: convertData(n.FirstChild),
Data: convertData(n.Children()[0]),
}
case candid.RecordT:
case candid.Record.Name:
var record Record
for _, n := range n.Children() {
if n.Type == candid.CommentTextT {
if n.Name == candid.CommentText.Name {
continue
}
record = append(
Expand All @@ -60,10 +60,10 @@ func convertData(n *ast.Node) Data {
)
}
return record
case candid.VariantT:
case candid.Variant.Name:
var variant Variant
for _, n := range n.Children() {
if n.Type == candid.CommentTextT {
if n.Name == candid.CommentText.Name {
continue
}
variant = append(
Expand All @@ -72,18 +72,18 @@ func convertData(n *ast.Node) Data {
)
}
return variant
case candid.FuncT:
return convertFunc(n.FirstChild)
case candid.ServiceT:
return convertService(n.FirstChild)
case candid.PrincipalT:
case candid.Func.Name:
return convertFunc(n.Children()[0])
case candid.Service.Name:
return convertService(n.Children()[0])
case candid.Principal.Name:
return Principal{}
case candid.PrimTypeT:
return Primitive(n.Value)
case candid.IdT:
return DataId(n.Value)
case candid.PrimType.Name:
return Primitive(n.Value())
case candid.Id.Name:
return DataId(n.Value())
default:
panic(n)
panic(n.Name)
}
}

Expand Down Expand Up @@ -113,23 +113,25 @@ type Field struct {
NameData *string
}

func convertField(n *ast.Node) Field {
func convertField(n *parser.Node) Field {
var field Field
if len(n.Children()) != 1 {
switch n := n.FirstChild; n.Type {
case candid.NatT:
switch n := n.Children()[0]; n.Name {
case candid.Nat.Name:
field.Nat = convertNat(n)
case candid.TextT, candid.IdT:
field.Name = &n.Value
case candid.Text.Name, candid.Id.Name:
v := n.Value()
field.Name = &v
default:
panic(n)
}
}
switch n := n.LastChild; n.Type {
case candid.NatT:
switch n := n.Children()[0]; n.Name {
case candid.Nat.Name:
field.NatData = convertNat(n)
case candid.IdT:
field.NameData = &n.Value
case candid.Id.Name:
v := n.Value()
field.NameData = &v
default:
data := convertData(n)
field.Data = &data
Expand Down
11 changes: 6 additions & 5 deletions candid/did/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package did
import (
"fmt"

"github.com/di-wu/parser/ast"
"github.com/0x51-dev/upeg/parser"
)

// Definition represents an imports or type definition.
Expand All @@ -29,13 +29,14 @@ type Type struct {
Data Data
}

func convertType(n *ast.Node) Type {
func convertType(n *parser.Node) Type {
cs := n.Children()
var (
id = n.FirstChild
data = n.LastChild
id = cs[0]
data = cs[len(cs)-1]
)
return Type{
Id: id.Value,
Id: id.Value(),
Data: convertData(data),
}
}
Expand Down
14 changes: 7 additions & 7 deletions candid/did/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package did
import (
"strings"

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

// Description represents the interface description of a program. An interface description consists of a sequence of
Expand All @@ -16,28 +16,28 @@ type Description struct {
Services []Service
}

func ConvertDescription(n *ast.Node) Description {
func ConvertDescription(n *parser.Node) Description {
var desc Description
for _, n := range n.Children() {
switch n.Type {
case candid.TypeT:
switch n.Name {
case candid.Type.Name:
desc.Definitions = append(
desc.Definitions,
convertType(n),
)
case candid.ImportT:
case candid.Import.Name:
desc.Definitions = append(
desc.Definitions,
Import{
Text: "",
},
)
case candid.ActorT:
case candid.Actor.Name:
desc.Services = append(
desc.Services,
convertService(n),
)
case candid.CommentTextT:
case candid.CommentText.Name:
// Ignore comments.
default:
panic(n)
Expand Down
16 changes: 9 additions & 7 deletions candid/did/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/di-wu/parser/ast"
"github.com/0x51-dev/upeg/parser"
)

// Argument describes the argument types of a Field.
Expand All @@ -15,15 +15,17 @@ type Argument struct {
Data Data
}

func convertArgument(n *ast.Node) Argument {
data := convertData(n.LastChild)
func convertArgument(n *parser.Node) Argument {
cs := n.Children()
data := convertData(cs[len(cs)-1])
if len(n.Children()) == 1 {
return Argument{
Data: data,
}
}
name := cs[0].Value()
return Argument{
Name: &n.FirstChild.Value,
Name: &name,
Data: data,
}
}
Expand All @@ -47,7 +49,7 @@ type Func struct {
Annotation *FuncAnnotation
}

func convertFunc(n *ast.Node) Func {
func convertFunc(n *parser.Node) Func {
var f Func
for i, n := range n.Children() {
switch i {
Expand All @@ -56,7 +58,7 @@ func convertFunc(n *ast.Node) Func {
case 1:
f.ResTypes = convertTuple(n)
case 2:
ann := FuncAnnotation(n.Value)
ann := FuncAnnotation(n.Value())
f.Annotation = &ann
default:
panic(n)
Expand Down Expand Up @@ -89,7 +91,7 @@ const (
// Tuple represents one or more arguments.
type Tuple []Argument

func convertTuple(n *ast.Node) Tuple {
func convertTuple(n *parser.Node) Tuple {
var tuple Tuple
for _, n := range n.Children() {
tuple = append(tuple, convertArgument(n))
Expand Down
Loading

0 comments on commit 6ca34c9

Please sign in to comment.