Skip to content

Commit

Permalink
fix templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Oct 6, 2024
1 parent 96bfd53 commit c08d732
Show file tree
Hide file tree
Showing 33 changed files with 315 additions and 197 deletions.
35 changes: 17 additions & 18 deletions builtin/next.npl
Original file line number Diff line number Diff line change
Expand Up @@ -300,40 +300,39 @@ false
{{- end}}
{{- end}}

{{/* next/node:snake.case.name: Generates a snake_case name for a node */}}
{{- define "next/node:snake.case.name"}}
{{- with .Annotations.next.snake_case}}
{{- .}}
{{- with .Annotations.next.tokens}}
{{- . | split " " | join "_" | lower}}
{{- else}}
{{- $.Name | snakeCase}}
{{- end}}
{{- end}}

{{- define "next/node:pascal.case.name"}}
{{- with .Annotations.next.pascal_case}}
{{- .}}
{{- else with .Annotations.next.snake_case}}
{{- . | snakeCase | split "_" | map capitalize | join ""}}
{{/* next/node:kebab.case.name: Generates a kebab-case name for a node */}}
{{- define "next/node:kebab.case.name"}}
{{- with .Annotations.next.tokens}}
{{- . | split " " | join "-" | lower}}
{{- else}}
{{- $.Name | pascalCase}}
{{- $.Name | kebabCase}}
{{- end}}
{{- end}}

{{/* next/node:camel.case.name: Generates a camelCase name for a node */}}
{{- define "next/node:camel.case.name"}}
{{- with .Annotations.next.camel_case}}
{{- .}}
{{- else with .Annotations.next.snake_case}}
{{- . | snakeCase | split "_" | map capitalize | join "" | uncapitalize}}
{{- with .Annotations.next.tokens}}
{{- $tokens := . | split " "}}
{{- index $tokens 0 | lower}}{{slice $tokens 1 | map capitalize | join ""}}
{{- else}}
{{- $.Name | camelCase}}
{{- end}}
{{- end}}

{{- define "next/node:kebab.case.name"}}
{{- with .Annotations.next.kebab_case}}
{{- .}}
{{- else with .Annotations.next.snake_case}}
{{- . | snakeCase | replace "_" "-"}}
{{/* next/node:pascal.case.name: Generates a PascalCase name for a node */}}
{{- define "next/node:pascal.case.name"}}
{{- with .Annotations.next.tokens}}
{{- . | split " " | map capitalize | join ""}}
{{- else}}
{{- $.Name | kebabCase}}
{{- $.Name | pascalCase}}
{{- end}}
{{- end}}
15 changes: 13 additions & 2 deletions src/compile/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,25 @@ func formatComments(list []string, appendNewline bool, indent string, beginAndEn
if len(begins) > 1 {
lines = append(begins[1:], lines...)
}

trimmedIndent := strings.TrimRight(indent, " \t")
if end == "" {
for i, line := range lines {
lines[i] = indent + begin + line
line = begin + line
if line == "" {
lines[i] = trimmedIndent
} else {
lines[i] = indent + line
}
}
} else {
lines = append([]string{begin + lines[0]}, lines[1:]...)
for i := 1; i < len(lines); i++ {
lines[i] = indent + lines[i]
if lines[i] == "" {
lines[i] = trimmedIndent
} else {
lines[i] = indent + lines[i]
}
}
lines = append(lines, end)
}
Expand Down
63 changes: 19 additions & 44 deletions src/grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,10 @@ func next(node string, parameters ...Options[AnnotationParameter]) Options[Annot
})
}

func base_next(node string, parameters ...Options[AnnotationParameter]) Options[Annotation] {
return next(node, append(parameters, available(), deprecated(), tokens())...)
}

func deprecated() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: "deprecated",
Expand All @@ -1484,39 +1488,10 @@ func default_() Options[AnnotationParameter] {
})
}

func withCases(a Options[Annotation]) Options[Annotation] {
a.value.Parameters = append(a.value.Parameters, snake_case(), camel_case(), pascal_case(), kebab_case())
return a
}

func snake_case() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: "snake_case",
Description: "Sets the snake_case name for the declaration.",
Types: types(String),
})
}

func camel_case() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: "camel_case",
Description: "Sets the camelCase name for the declaration.",
Types: types(String),
})
}

func pascal_case() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: "pascal_case",
Description: "Sets the PascalCase name for the declaration.",
Types: types(String),
})
}

func kebab_case() Options[AnnotationParameter] {
func tokens() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: "kebab_case",
Description: "Sets the kebab-case name for the declaration.",
Name: "tokens",
Description: "Sets the space separated tokens for the declaration.",
Types: types(String),
})
}
Expand Down Expand Up @@ -1553,7 +1528,7 @@ func type_() Options[AnnotationParameter] {
})
}

func LANG_package() Options[AnnotationParameter] {
func lang_package() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: ".+_package",
Description: "Sets the package name for target languages.",
Expand All @@ -1568,15 +1543,15 @@ func LANG_package() Options[AnnotationParameter] {
})
}

func LANG_imports() Options[AnnotationParameter] {
func lang_imports() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: ".+_imports",
Description: "Sets the import declarations for target languages.",
Types: types(String),
})
}

func LANG_alias() Options[AnnotationParameter] {
func lang_alias() Options[AnnotationParameter] {
return opt(AnnotationParameter{
Name: ".+_alias",
Description: "Sets the alias name for target languages.",
Expand All @@ -1591,31 +1566,31 @@ func appendTo[S ~[]T, T any](s *S, x ...T) {
var Builtin = Grammar{
builtin: true,
Package: Package{
Annotations: Annotations{withCases(next("package", available(), deprecated(), LANG_package(), LANG_imports()))},
Annotations: Annotations{base_next("package", lang_package(), lang_imports())},
},
Const: Const{
Annotations: Annotations{withCases(next("const", available(), deprecated()))},
Annotations: Annotations{base_next("const")},
Types: validConstTypes,
},
Enum: Enum{
Annotations: Annotations{next("enum", available(), deprecated(), type_())},
Annotations: Annotations{base_next("enum", type_())},
Member: EnumMember{
Annotations: Annotations{withCases(next("enum.member", available(), deprecated()))},
Annotations: Annotations{base_next("enum.member")},
Types: validEnumMemberTypes,
},
},
Struct: Struct{
Annotations: Annotations{next("struct", available(), deprecated(), LANG_alias())},
Annotations: Annotations{base_next("struct", lang_alias())},
Field: StructField{
Annotations: Annotations{withCases(next("struct.field", available(), deprecated(), optional(), default_(), LANG_alias()))},
Annotations: Annotations{base_next("struct.field", optional(), default_(), lang_alias())},
},
},
Interface: Interface{
Annotations: Annotations{next("interface", available(), deprecated(), LANG_alias())},
Annotations: Annotations{base_next("interface", lang_alias())},
Method: InterfaceMethod{
Annotations: Annotations{withCases(next("interface.method", available(), deprecated(), mut(), error_()))},
Annotations: Annotations{base_next("interface.method", mut(), error_())},
Parameter: InterfaceMethodParameter{
Annotations: Annotations{withCases(next("interface.method.parameter", mut(), LANG_alias()))},
Annotations: Annotations{next("interface.method.parameter", deprecated(), tokens(), mut(), lang_alias())},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions website/example/gen/c/a/a.next.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ typedef enum DEMO_A_IotatestEnum {
* Struct types
*/
typedef struct DEMO_A_Point2D {
double x;
double y;
double x; /* x-coordinate */
double y; /* y-coordinate */
} DEMO_A_Point2D;

typedef struct DEMO_A_Point3D {
Expand Down
18 changes: 13 additions & 5 deletions website/example/gen/c/demo/demo.next.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ typedef struct DEMO_User {
int32_t matrix[3][2];
char* email;
DEMO_Color favorite_color;
/**
* @next(tokens) applies to the node name:
* - For snake_case: "last_login_ip"
* - For camelCase: "lastLoginIP"
* - For PascalCase: "LastLoginIP"
* - For kebab-case: "last-login-ip"
*/
char* last_login_ip;
void* extra;
} DEMO_User;

Expand Down Expand Up @@ -154,11 +162,11 @@ struct DEMO_Reader {
* @next(error) applies to the method:
* - For Go: The method may return an error
* - For C++/Java: The method throws an exception
*
*
* @next(mut) applies to the method:
* - For C++: The method is non-const
* - For other languages: This annotation may not have a direct effect
*
*
* @next(mut) applies to the parameter buffer:
* - For C++: The parameter is non-const, allowing modification
* - For other languages: This annotation may not have a direct effect,
Expand All @@ -176,7 +184,7 @@ typedef struct DEMO_HTTPClient DEMO_HTTPClient;
struct DEMO_HTTPClient {
void* context;
char* (*request)(DEMO_HTTPClient* self, char* url, char* method, char* body);
char* (*request_2)(DEMO_HTTPClient* self, char* url, char* method, char* body);
char* (*request2)(DEMO_HTTPClient* self, char* url, char* method, char* body);
};

/**
Expand All @@ -186,8 +194,8 @@ static inline char* DEMO_HTTPClient_request(DEMO_HTTPClient* self, char* url, ch
return self->request(self, url, method, body);
}

static inline char* DEMO_HTTPClient_request_2(DEMO_HTTPClient* self, char* url, char* method, char* body) {
return self->request_2(self, url, method, body);
static inline char* DEMO_HTTPClient_request2(DEMO_HTTPClient* self, char* url, char* method, char* body) {
return self->request2(self, url, method, body);
}

#endif /* DEMO_DEMO_H */
4 changes: 2 additions & 2 deletions website/example/gen/cpp/a.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ enum class IotatestEnum : int32_t {
// Struct types
class Point2D {
public:
double x = {0.0};
double y = {0.0};
double x = {0.0}; // x-coordinate
double y = {0.0}; // y-coordinate

public:
Point2D() = default;
Expand Down
12 changes: 9 additions & 3 deletions website/example/gen/cpp/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class User {
std::array<std::array<int, 2>, 3> matrix;
std::string email = {""};
Color favorite_color = {Color(0)};
// @next(tokens) applies to the node name:
// - For snake_case: "last_login_ip"
// - For camelCase: "lastLoginIP"
// - For PascalCase: "LastLoginIP"
// - For kebab-case: "last-login-ip"
std::string last_login_ip = {""};
std::any extra;

public:
Expand Down Expand Up @@ -157,11 +163,11 @@ class Reader {
// @next(error) applies to the method:
// - For Go: The method may return an error
// - For C++/Java: The method throws an exception
//
//
// @next(mut) applies to the method:
// - For C++: The method is non-const
// - For other languages: This annotation may not have a direct effect
//
//
// @next(mut) applies to the parameter buffer:
// - For C++: The parameter is non-const, allowing modification
// - For other languages: This annotation may not have a direct effect,
Expand All @@ -175,7 +181,7 @@ class HTTPClient {
virtual ~HTTPClient() = default;
// Available for all languages
virtual std::string request(const std::string& url, const std::string& method, const std::string& body) const = 0;
virtual std::string request_2(const std::string& url, const std::string& method, const std::string& body) const = 0;
virtual std::string request2(const std::string& url, const std::string& method, const std::string& body) const = 0;
// Available for C++
virtual std::string post(const std::string& url, const std::string& body) const = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions website/example/gen/csharp/a/a.next.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public enum IotatestEnum
// Struct types
public class Point2D
{
public double x { get; set; }
public double y { get; set; }
public double x { get; set; } // x-coordinate
public double y { get; set; } // y-coordinate
}

public class Point3D
Expand Down
10 changes: 8 additions & 2 deletions website/example/gen/csharp/demo/demo.next.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public class User
public int[][] matrix { get; set; }
public string email { get; set; }
public Color favoriteColor { get; set; }
// @next(tokens) applies to the node name:
// - For snake_case: "last_login_ip"
// - For camelCase: "lastLoginIP"
// - For PascalCase: "LastLoginIP"
// - For kebab-case: "last-login-ip"
public string lastLoginIP { get; set; }
public object extra { get; set; }
}

Expand Down Expand Up @@ -95,11 +101,11 @@ public interface Reader
// @next(error) applies to the method:
// - For Go: The method may return an error
// - For C++/Java: The method throws an exception
//
//
// @next(mut) applies to the method:
// - For C++: The method is non-const
// - For other languages: This annotation may not have a direct effect
//
//
// @next(mut) applies to the parameter buffer:
// - For C++: The parameter is non-const, allowing modification
// - For other languages: This annotation may not have a direct effect,
Expand Down
Loading

0 comments on commit c08d732

Please sign in to comment.