yang printer CHANGE some tunning of printer to unify output with pyang
diff --git a/src/printer_yang.c b/src/printer_yang.c
index 29df721..b46c957 100644
--- a/src/printer_yang.c
+++ b/src/printer_yang.c
@@ -45,6 +45,17 @@
LYS_YPR_COMPILED /**< YANG printer of the compiled schema */
};
+#define YPR_CTX_FLAG_EXTRA_LINE 0x01 /**< Flag for ::ypr_ctx::flags to print extra line in schema */
+
+#define YPR_EXTRA_LINE(COND, CTX) if (COND) { (CTX)->flags |= YPR_CTX_FLAG_EXTRA_LINE; }
+#define YPR_EXTRA_LINE_PRINT(CTX) \
+ if ((CTX)->flags & YPR_CTX_FLAG_EXTRA_LINE) { \
+ (CTX)->flags &= ~YPR_CTX_FLAG_EXTRA_LINE; \
+ if (DO_FORMAT) { \
+ ly_print_((CTX)->out, "\n"); \
+ } \
+ }
+
/**
* @brief Compiled YANG printer context
*
@@ -55,6 +66,7 @@
struct {
struct ly_out *out; /**< output specification */
uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
+ uint16_t flags; /**< internal flags for use by printer */
uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
const struct lys_module *module; /**< schema to print */
};
@@ -492,7 +504,7 @@
{
ly_bool flag = 0;
- ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
+ ly_print_(ctx->out, "%*sfeature %s", INDENT, feat->name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_FEATURE, 0, feat->exts, &flag, 0);
yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
@@ -509,7 +521,7 @@
ly_bool flag = 0;
LY_ARRAY_COUNT_TYPE u;
- ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
+ ly_print_(ctx->out, "%*sidentity %s", INDENT, ident->name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_IDENTITY, 0, ident->exts, &flag, 0);
@@ -534,7 +546,7 @@
ly_bool flag = 0;
LY_ARRAY_COUNT_TYPE u;
- ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
+ ly_print_(ctx->out, "%*sidentity %s", INDENT, ident->name);
LEVEL++;
yprc_extension_instances(ctx, LY_STMT_IDENTITY, 0, ident->exts, &flag, 0);
@@ -954,7 +966,7 @@
static void
yprp_typedef(struct lys_ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
{
- ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
+ ly_print_(ctx->out, "%*stypedef %s {\n", INDENT, tpdf->name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL, 0);
@@ -991,7 +1003,7 @@
struct lysp_node_notif *notif;
struct lysp_node_grp *subgrp;
- ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
+ ly_print_(ctx->out, "%*sgrouping %s", INDENT, grp->name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_GROUPING, 0, grp->exts, &flag, 0);
@@ -1040,8 +1052,9 @@
return;
}
ypr_open(ctx->out, flag);
+ YPR_EXTRA_LINE_PRINT(ctx);
- ly_print_(ctx->out, "\n%*s%s {\n", INDENT, inout->name);
+ ly_print_(ctx->out, "%*s%s {\n", INDENT, inout->name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_MUST, 0, inout->exts, NULL, 0);
@@ -1179,16 +1192,24 @@
ypr_description(ctx, action->dsc, action->exts, &flag);
ypr_reference(ctx, action->ref, action->exts, &flag);
+ YPR_EXTRA_LINE(flag, ctx);
+
LY_ARRAY_FOR(action->typedefs, u) {
ypr_open(ctx->out, &flag);
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_typedef(ctx, &action->typedefs[u]);
}
+ YPR_EXTRA_LINE(action->typedefs, ctx);
+
LY_LIST_FOR(action->groupings, grp) {
ypr_open(ctx->out, &flag);
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_grouping(ctx, grp);
}
+ YPR_EXTRA_LINE(action->groupings, ctx);
+
yprp_inout(ctx, &action->input, &flag);
yprp_inout(ctx, &action->output, &flag);
@@ -2040,7 +2061,8 @@
continue;
}
- ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
+ YPR_EXTRA_LINE_PRINT(ctx);
+ ly_print_(ctx->out, "%*simport %s {\n", INDENT, modp->imports[u].name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL, 0);
ypr_substmt(ctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
@@ -2052,13 +2074,16 @@
LEVEL--;
ly_print_(ctx->out, "%*s}\n", INDENT);
}
+ YPR_EXTRA_LINE(modp->imports, ctx);
+
LY_ARRAY_FOR(modp->includes, u) {
if (modp->includes[u].injected) {
/* do not print the includes injected from submodules */
continue;
}
+ YPR_EXTRA_LINE_PRINT(ctx);
if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
- ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
+ ly_print_(ctx->out, "%*sinclude %s {\n", INDENT, modp->includes[u].name);
LEVEL++;
yprp_extension_instances(ctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL, 0);
if (modp->includes[u].rev[0]) {
@@ -2072,6 +2097,7 @@
ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
}
}
+ YPR_EXTRA_LINE(modp->includes, ctx);
}
static void
@@ -2085,49 +2111,85 @@
struct lysp_node_augment *aug;
LY_ARRAY_FOR(modp->extensions, u) {
- ly_print_(ctx->out, "\n");
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_extension(ctx, &modp->extensions[u]);
}
+
+ YPR_EXTRA_LINE(modp->extensions, ctx);
+
if (modp->exts) {
- ly_print_(ctx->out, "\n");
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_extension_instances(ctx, LY_STMT_MODULE, 0, modp->exts, NULL, 0);
}
+ YPR_EXTRA_LINE(modp->exts, ctx);
+
LY_ARRAY_FOR(modp->features, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_feature(ctx, &modp->features[u]);
+ YPR_EXTRA_LINE(1, ctx);
}
+ YPR_EXTRA_LINE(modp->features, ctx);
+
LY_ARRAY_FOR(modp->identities, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_identity(ctx, &modp->identities[u]);
+ YPR_EXTRA_LINE(1, ctx);
}
+ YPR_EXTRA_LINE(modp->identities, ctx);
+
LY_ARRAY_FOR(modp->typedefs, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_typedef(ctx, &modp->typedefs[u]);
+ YPR_EXTRA_LINE(1, ctx);
}
+ YPR_EXTRA_LINE(modp->typedefs, ctx);
+
LY_LIST_FOR(modp->groupings, grp) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_grouping(ctx, grp);
+ YPR_EXTRA_LINE(1, ctx);
}
+ YPR_EXTRA_LINE(modp->groupings, ctx);
+
LY_LIST_FOR(modp->data, data) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_node(ctx, data);
}
+ YPR_EXTRA_LINE(modp->data, ctx);
+
LY_LIST_FOR(modp->augments, aug) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_augment(ctx, aug);
}
+ YPR_EXTRA_LINE(modp->augments, ctx);
+
LY_LIST_FOR(modp->rpcs, action) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_action(ctx, action);
}
+ YPR_EXTRA_LINE(modp->rpcs, ctx);
+
LY_LIST_FOR(modp->notifs, notif) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_notification(ctx, notif);
}
+ YPR_EXTRA_LINE(modp->notifs, ctx);
+
LY_ARRAY_FOR(modp->deviations, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_deviation(ctx, &modp->deviations[u]);
}
+
+ YPR_EXTRA_LINE(modp->deviations, ctx);
}
LY_ERR
@@ -2144,29 +2206,33 @@
if (modp->version) {
ypr_substmt(ctx, LY_STMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
}
-
ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
+ YPR_EXTRA_LINE(1, ctx);
+
/* linkage-stmts (import/include) */
yang_print_parsed_linkage(ctx, modp);
/* meta-stmts */
if (module->org || module->contact || module->dsc || module->ref) {
- ly_print_(out, "\n");
+ YPR_EXTRA_LINE_PRINT(ctx);
}
ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
+ YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, ctx);
+
/* revision-stmts */
- if (modp->revs) {
- ly_print_(out, "\n");
- }
LY_ARRAY_FOR(modp->revs, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_revision(ctx, &modp->revs[u]);
}
+
+ YPR_EXTRA_LINE(modp->revs, ctx);
+
/* body-stmts */
yang_print_parsed_body(ctx, modp);
@@ -2207,25 +2273,30 @@
yprp_belongsto(ctx, submodp);
+ YPR_EXTRA_LINE(1, ctx);
+
/* linkage-stmts (import/include) */
yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
/* meta-stmts */
if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
- ly_print_(out, "\n");
+ YPR_EXTRA_LINE_PRINT(ctx);
}
ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
ypr_substmt(ctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
ypr_substmt(ctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
+ YPR_EXTRA_LINE(submodp->org || submodp->contact || submodp->dsc || submodp->ref, ctx);
+
/* revision-stmts */
- if (submodp->revs) {
- ly_print_(out, "\n");
- }
LY_ARRAY_FOR(submodp->revs, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprp_revision(ctx, &submodp->revs[u]);
}
+
+ YPR_EXTRA_LINE(submodp->revs, ctx);
+
/* body-stmts */
yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
@@ -2261,30 +2332,39 @@
ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modc->exts);
ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modc->exts);
+ YPR_EXTRA_LINE(1, ctx);
+
/* no linkage-stmts */
/* meta-stmts */
if (module->org || module->contact || module->dsc || module->ref) {
- ly_print_(out, "\n");
+ YPR_EXTRA_LINE_PRINT(ctx);
}
ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modc->exts);
ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modc->exts);
ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modc->exts);
ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modc->exts);
+ YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, ctx);
+
/* revision-stmts */
if (module->revision) {
- ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
+ YPR_EXTRA_LINE_PRINT(ctx);
+ ly_print_(ctx->out, "%*srevision %s;\n", INDENT, module->revision);
+ YPR_EXTRA_LINE(1, ctx);
}
/* body-stmts */
if (modc->exts) {
- ly_print_(out, "\n");
+ YPR_EXTRA_LINE_PRINT(ctx);
yprc_extension_instances(ctx, LY_STMT_MODULE, 0, module->compiled->exts, NULL, 0);
+ YPR_EXTRA_LINE(1, ctx);
}
LY_ARRAY_FOR(module->identities, u) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprc_identity(ctx, &module->identities[u]);
+ YPR_EXTRA_LINE(1, ctx);
}
if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
@@ -2293,16 +2373,25 @@
struct lysc_node_notif *notif;
LY_LIST_FOR(modc->data, data) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprc_node(ctx, data);
}
+ YPR_EXTRA_LINE(modc->data, ctx);
+
LY_LIST_FOR(modc->rpcs, rpc) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprc_action(ctx, rpc);
}
+ YPR_EXTRA_LINE(modc->rpcs, ctx);
+
LY_LIST_FOR(modc->notifs, notif) {
+ YPR_EXTRA_LINE_PRINT(ctx);
yprc_notification(ctx, notif);
}
+
+ YPR_EXTRA_LINE(modc->notifs, ctx);
}
LEVEL--;