-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
59 changed files
with
1,256 additions
and
1,128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.