Skip to content

Commit

Permalink
feat: parser API (#208)
Browse files Browse the repository at this point in the history
* feat: parser API

Signed-off-by: peefy <[email protected]>

* chore: bump kcl-go to v0.7.5

Signed-off-by: peefy <[email protected]>

---------

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Jan 14, 2024
1 parent e5c4ee1 commit de70a20
Show file tree
Hide file tree
Showing 59 changed files with 1,256 additions and 1,128 deletions.
2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm

Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_test

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
google.golang.org/protobuf v1.30.0
gopkg.in/yaml.v3 v3.0.1
kcl-lang.io/kpm v0.4.6
kcl-lang.io/lib v0.7.4
kcl-lang.io/lib v0.7.5
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
kcl-lang.io/kpm v0.4.6 h1:yaQdRQMqByaxyUb3uEp27zI7xxcsnp1iIcgqU9hFVqI=
kcl-lang.io/kpm v0.4.6/go.mod h1:Ba2ijUHQR/sMdB4S7982vOrajly4LdgshrqXd2SXBog=
kcl-lang.io/lib v0.7.4 h1:D1mSGI6Ggb9B3lQqoscQuSOnaBpnkvucZA0WWWniLO0=
kcl-lang.io/lib v0.7.4/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
kcl-lang.io/lib v0.7.5 h1:Gr4a6RZ9DmdNmJ8+VqT3IpkOjgySzGWYXzWZhQX+UK0=
kcl-lang.io/lib v0.7.5/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
oras.land/oras-go v1.2.3/go.mod h1:M/uaPdYklze0Vf3AakfarnpoEckvw0ESbRdN8Z1vdJg=
oras.land/oras-go/v2 v2.3.0 h1:lqX1aXdN+DAmDTKjiDyvq85cIaI4RkIKp/PghWlAGIU=
Expand Down
6 changes: 3 additions & 3 deletions kclvm_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_test

Expand Down Expand Up @@ -520,9 +520,9 @@ func TestTestAPI(t *testing.T) {
}

func TestWithExternalpkg(t *testing.T) {
absPath1, err := filepath.Abs("./testdata_external/external_1/")
absPath1, err := filepath.Abs("./testdata/external/external_1/")
assert2.Equal(t, nil, err)
absPath2, err := filepath.Abs("./testdata_external/external_2/")
absPath2, err := filepath.Abs("./testdata/external/external_2/")
assert2.Equal(t, nil, err)
opt := kcl.WithExternalPkgs("external_1="+absPath1, "external_2="+absPath2)
result, err := kcl.Run("./testdata/import-external/main.k", opt)
Expand Down
2 changes: 1 addition & 1 deletion kclvm_version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm

Expand Down
2 changes: 1 addition & 1 deletion pkg/kcl/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

// Package kcl defines the top-level interface for KCL.
package kcl
Expand Down
2 changes: 1 addition & 1 deletion pkg/kcl/api_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kcl

Expand Down
2 changes: 1 addition & 1 deletion pkg/kcl/opt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kcl

Expand Down
2 changes: 1 addition & 1 deletion pkg/kcl/opt_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kcl

Expand Down
2 changes: 1 addition & 1 deletion pkg/kcl/testing_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kcl

Expand Down
2 changes: 1 addition & 1 deletion pkg/kcl/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kcl

Expand Down
2 changes: 1 addition & 1 deletion pkg/kclvm_runtime/builtin_service_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_runtime

Expand Down
2 changes: 1 addition & 1 deletion pkg/kclvm_runtime/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_runtime

Expand Down
2 changes: 1 addition & 1 deletion pkg/kclvm_runtime/kclvm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_runtime

Expand Down
2 changes: 1 addition & 1 deletion pkg/kclvm_runtime/proc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_runtime

Expand Down
2 changes: 1 addition & 1 deletion pkg/kclvm_runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package kclvm_runtime

Expand Down
2 changes: 1 addition & 1 deletion pkg/logger/example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package logger_test

Expand Down
2 changes: 1 addition & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package logger

Expand Down
43 changes: 43 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package parser

import (
"fmt"
"io"

"kcl-lang.io/kcl-go/pkg/service"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
)

// ParseFileASTJson parses the source code from the specified file or Reader
// and returns the JSON representation of the Abstract Syntax Tree (AST).
// The source code can be provided directly as a string or []byte,
// or indirectly via a filename or an io.Reader.
// If src is nil, the function reads the content from the provided filename.
func ParseFileASTJson(filename string, src interface{}) (result string, err error) {
var code string
if src != nil {
switch src := src.(type) {
case []byte:
code = string(src)
case string:
code = string(src)
case io.Reader:
d, err := io.ReadAll(src)
if err != nil {
return "", err
}
code = string(d)
default:
return "", fmt.Errorf("unsupported src type: %T", src)
}
}
client := service.NewKclvmServiceClient()
resp, err := client.ParseFile(&gpyrpc.ParseFile_Args{
Path: filename,
Source: code,
})
if err != nil {
return "", err
}
return resp.AstJson, nil
}
52 changes: 52 additions & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package parser

import (
"strings"
"testing"
"time"
)

// TestParseFileASTJson tests the ParseFileASTJson function with various input sources.
func TestParseFileASTJson(t *testing.T) {
// Example: Test with string source
src := `schema Name:
name: str
n = Name {name = "name"}` // Sample KCL source code
astJson, err := ParseFileASTJson("", src)
if err != nil {
t.Errorf("ParseFileASTJson failed with string source: %v", err)
}
if astJson == "" {
t.Errorf("Expected non-empty AST JSON with string source")
}

// Example: Test with byte slice source
srcBytes := []byte(src)
astJson, err = ParseFileASTJson("", srcBytes)
if err != nil {
t.Errorf("ParseFileASTJson failed with byte slice source: %v", err)
}
if astJson == "" {
t.Errorf("Expected non-empty AST JSON with byte slice source")
}

startTime := time.Now()
// Example: Test with io.Reader source
srcReader := strings.NewReader(src)
astJson, err = ParseFileASTJson("", srcReader)
if err != nil {
t.Errorf("ParseFileASTJson failed with io.Reader source: %v", err)
}
if astJson == "" {
t.Errorf("Expected non-empty AST JSON with io.Reader source")
}
elapsed := time.Since(startTime)
t.Logf("ParseFileASTJson took %s", elapsed)
if !strings.Contains(astJson, "Schema") {
t.Errorf("Expected Schema Node AST JSON with io.Reader source")
}
if !strings.Contains(astJson, "Assign") {
t.Errorf("Expected Assign Node AST JSON with io.Reader source")
}
}
2 changes: 1 addition & 1 deletion pkg/service/client_builtin_service.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down
18 changes: 17 additions & 1 deletion pkg/service/client_kclvm_service.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down Expand Up @@ -53,6 +53,22 @@ func (p *KclvmServiceClient) ExecProgram(args *gpyrpc.ExecProgram_Args) (resp *g
return
}

func (p *KclvmServiceClient) ParseFile(args *gpyrpc.ParseFile_Args) (resp *gpyrpc.ParseFile_Result, err error) {
p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) {
resp, err = p.getClient(c).ParseFile(args)
err = p.wrapErr(err, stderr)
})
return
}

func (p *KclvmServiceClient) ParseProgram(args *gpyrpc.ParseProgram_Args) (resp *gpyrpc.ParseProgram_Result, err error) {
p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) {
resp, err = p.getClient(c).ParseProgram(args)
err = p.wrapErr(err, stderr)
})
return
}

func (p *KclvmServiceClient) FormatCode(args *gpyrpc.FormatCode_Args) (resp *gpyrpc.FormatCode_Result, err error) {
p.Runtime.DoTask(func(c *rpc.Client, stderr io.Reader) {
resp, err = p.getClient(c).FormatCode(args)
Expand Down
8 changes: 7 additions & 1 deletion pkg/service/grpc_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down Expand Up @@ -43,6 +43,12 @@ func (p *_KclvmServiceImpl) Ping(ctx context.Context, args *gpyrpc.Ping_Args) (*
func (p *_KclvmServiceImpl) ExecProgram(ctx context.Context, args *gpyrpc.ExecProgram_Args) (*gpyrpc.ExecProgram_Result, error) {
return p.c.ExecProgram(args)
}
func (p *_KclvmServiceImpl) ParseFile(ctx context.Context, args *gpyrpc.ParseFile_Args) (*gpyrpc.ParseFile_Result, error) {
return p.c.ParseFile(args)
}
func (p *_KclvmServiceImpl) ParseProgram(ctx context.Context, args *gpyrpc.ParseProgram_Args) (*gpyrpc.ParseProgram_Result, error) {
return p.c.ParseProgram(args)
}
func (p *_KclvmServiceImpl) FormatCode(ctx context.Context, args *gpyrpc.FormatCode_Args) (*gpyrpc.FormatCode_Result, error) {
return p.c.FormatCode(args)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/http_post.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/kcl_error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down
2 changes: 2 additions & 0 deletions pkg/service/kclvm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
type KclvmService interface {
Ping(in *gpyrpc.Ping_Args) (out *gpyrpc.Ping_Result, err error)
ExecProgram(in *gpyrpc.ExecProgram_Args) (out *gpyrpc.ExecProgram_Result, err error)
ParseFile(in *gpyrpc.ParseFile_Args) (out *gpyrpc.ParseFile_Result, err error)
ParseProgram(in *gpyrpc.ParseProgram_Args) (out *gpyrpc.ParseProgram_Result, err error)
FormatCode(in *gpyrpc.FormatCode_Args) (out *gpyrpc.FormatCode_Result, err error)
FormatPath(in *gpyrpc.FormatPath_Args) (out *gpyrpc.FormatPath_Result, err error)
LintPath(in *gpyrpc.LintPath_Args) (out *gpyrpc.LintPath_Result, err error)
Expand Down
20 changes: 19 additions & 1 deletion pkg/service/rest_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down Expand Up @@ -60,6 +60,8 @@ func (p *restServer) initHttpRrouter() {
p.router.GET("/api:protorpc/BuiltinService.ListMethod", p.handle_ListMethod)

p.router.GET("/api:protorpc/KclvmService.ExecProgram", p.handle_ExecProgram)
p.router.GET("/api:protorpc/KclvmService.ParseFile", p.handle_ParseFile)
p.router.GET("/api:protorpc/KclvmService.ParseProgram", p.handle_ParseProgram)
p.router.GET("/api:protorpc/KclvmService.FormatCode", p.handle_FormatCode)
p.router.GET("/api:protorpc/KclvmService.FormatPath", p.handle_FormatPath)
p.router.GET("/api:protorpc/KclvmService.LintPath", p.handle_LintPath)
Expand All @@ -71,6 +73,8 @@ func (p *restServer) initHttpRrouter() {
p.router.POST("/api:protorpc/BuiltinService.ListMethod", p.handle_ListMethod)

p.router.POST("/api:protorpc/KclvmService.ExecProgram", p.handle_ExecProgram)
p.router.POST("/api:protorpc/KclvmService.ParseFile", p.handle_ParseFile)
p.router.POST("/api:protorpc/KclvmService.ParseProgram", p.handle_ParseProgram)
p.router.POST("/api:protorpc/KclvmService.FormatCode", p.handle_FormatCode)
p.router.POST("/api:protorpc/KclvmService.FormatPath", p.handle_FormatPath)
p.router.POST("/api:protorpc/KclvmService.LintPath", p.handle_LintPath)
Expand Down Expand Up @@ -140,6 +144,20 @@ func (p *restServer) handle_ExecProgram(w http.ResponseWriter, r *http.Request,
})
}

func (p *restServer) handle_ParseFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var args = new(gpyrpc.ParseFile_Args)
p.handle(w, r, args, func() (proto.Message, error) {
return p.c.ParseFile(args)
})
}

func (p *restServer) handle_ParseProgram(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var args = new(gpyrpc.ParseProgram_Args)
p.handle(w, r, args, func() (proto.Message, error) {
return p.c.ParseProgram(args)
})
}

func (p *restServer) handle_FormatCode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var args = new(gpyrpc.FormatCode_Args)
p.handle(w, r, args, func() (proto.Message, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/rest_server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/testmain_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package service

Expand Down
2 changes: 1 addition & 1 deletion pkg/settings/a_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package settings

Expand Down
2 changes: 1 addition & 1 deletion pkg/settings/utils_settings_yaml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package settings

Expand Down
Loading

0 comments on commit de70a20

Please sign in to comment.