Skip to content

Commit 33e2dc3

Browse files
authored
Merge pull request #28 from kcl-lang/feat-kcl-java-ast
feat: kcl java parser API and AST definitions
2 parents 65ed514 + c2e7688 commit 33e2dc3

File tree

90 files changed

+10371
-6757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+10371
-6757
lines changed

java/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
<artifactId>slf4j-api</artifactId>
2626
<version>1.7.30</version>
2727
</dependency>
28+
<!-- Jackson Core -->
29+
<dependency>
30+
<groupId>com.fasterxml.jackson.core</groupId>
31+
<artifactId>jackson-core</artifactId>
32+
<version>2.13.2</version>
33+
</dependency>
34+
<!-- Jackson Annotations -->
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.core</groupId>
37+
<artifactId>jackson-annotations</artifactId>
38+
<version>2.13.2</version>
39+
</dependency>
40+
<!-- Jackson Databind -->
41+
<dependency>
42+
<groupId>com.fasterxml.jackson.core</groupId>
43+
<artifactId>jackson-databind</artifactId>
44+
<version>2.13.2</version>
45+
</dependency>
2846
<dependency>
2947
<groupId>com.alibaba</groupId>
3048
<artifactId>fastjson</artifactId>

java/src/main/java/com/kcl/api/API.java

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,57 @@
33
import com.kcl.api.Spec.*;
44

55
public class API implements Service {
6-
static {
7-
// Load the dynamic library (the .dll, .so, or .dylib file)
8-
System.loadLibrary("kcl_lib_jni");
9-
}
6+
static {
7+
// Load the dynamic library (the .dll, .so, or .dylib file)
8+
System.loadLibrary("kcl_lib_jni");
9+
}
10+
11+
private native byte[] callNative(byte[] call, byte[] args);
1012

11-
private native byte[] callNative(byte[] call, byte[] args);
13+
public API() {
14+
}
1215

13-
// Example method that calls a native function
14-
public ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception {
15-
return ExecProgram_Result.parseFrom(call("KclvmService.ExecProgram", args.toByteArray()));
16-
}
16+
/**
17+
* Parses a program given a set of file paths and returns the result.
18+
* *
19+
* <p>
20+
* Example usage:
21+
*
22+
* <pre>{@code
23+
*
24+
* import com.kcl.api.*;
25+
* import com.kcl.ast.*;
26+
* import com.kcl.util.JsonUtil;
27+
*
28+
* // Create an instance of the API class
29+
* API api = new API();
30+
* // Parse the program by providing the file paths to the API
31+
* ParseProgram_Result result = api.parseProgram(
32+
* ParseProgram_Args.newBuilder().addPaths("a.k").build()
33+
* );
34+
* // Print the JSON representation of the AST (Abstract Syntax Tree)
35+
* System.out.println(result.getAstJson());
36+
* // Deserialize the JSON string into a Program object
37+
* Program program = JsonUtil.deserializeProgram(result.getAstJson());
38+
* }</pre>
39+
*
40+
* @param args the arguments specifying the file paths to be parsed.
41+
* @return the result of parsing the program and parse errors, including the AST in JSON format.
42+
* @throws Exception if an error occurs during the remote procedure call.
43+
*/
44+
@Override
45+
public ParseProgram_Result parseProgram(ParseProgram_Args args) throws Exception {
46+
return ParseProgram_Result.parseFrom(call("KclvmService.ParseProgram", args.toByteArray()));
47+
}
48+
49+
@Override
50+
public ParseFile_Result parseFile(ParseFile_Args args) throws Exception {
51+
return ParseFile_Result.parseFrom(call("KclvmService.ParseFile", args.toByteArray()));
52+
}
53+
54+
public ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception {
55+
return ExecProgram_Result.parseFrom(call("KclvmService.ExecProgram", args.toByteArray()));
56+
}
1757

1858
@Override
1959
public OverrideFile_Result overrideFile(OverrideFile_Args args) throws Exception {
@@ -65,26 +105,26 @@ public Test_Result test(Test_Args args) throws Exception {
65105
return Test_Result.parseFrom(call("KclvmService.Test", args.toByteArray()));
66106
}
67107

68-
private byte[] call(String name, byte[] args) throws Exception {
69-
byte[] result = callNative(name.getBytes(), args);
70-
if (result != null && startsWith(result, "ERROR")) {
71-
throw new Error(result.toString());
72-
}
73-
return result;
74-
}
75-
76-
static boolean startsWith(byte[] array, String prefix) {
77-
byte[] prefixBytes = prefix.getBytes();
78-
if (array.length < prefixBytes.length) {
79-
return false;
80-
}
81-
82-
for (int i = 0; i < prefixBytes.length; i++) {
83-
if (array[i] != prefixBytes[i]) {
84-
return false;
85-
}
86-
}
87-
88-
return true;
89-
}
108+
private byte[] call(String name, byte[] args) throws Exception {
109+
byte[] result = callNative(name.getBytes(), args);
110+
if (result != null && startsWith(result, "ERROR")) {
111+
throw new java.lang.Error(result.toString());
112+
}
113+
return result;
114+
}
115+
116+
static boolean startsWith(byte[] array, String prefix) {
117+
byte[] prefixBytes = prefix.getBytes();
118+
if (array.length < prefixBytes.length) {
119+
return false;
120+
}
121+
122+
for (int i = 0; i < prefixBytes.length; i++) {
123+
if (array[i] != prefixBytes[i]) {
124+
return false;
125+
}
126+
}
127+
128+
return true;
129+
}
90130
}

java/src/main/java/com/kcl/api/Service.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import com.kcl.api.Spec.*;
44

55
public interface Service {
6+
// Parse KCL single file AST JSON string
7+
ParseFile_Result parseFile(ParseFile_Args args) throws Exception;
8+
9+
// Parse KCL program AST JSON string
10+
ParseProgram_Result parseProgram(ParseProgram_Args args) throws Exception;
11+
612
// Execute KCL file with args
713
ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception;
814

0 commit comments

Comments
 (0)