Text syntax parser/loader

Added string quites around text containing spaces
Added support for colon in text
This commit is contained in:
Olof hagsand 2022-05-19 14:35:18 +02:00
parent 2ece0b8f51
commit 820ed5686b
5 changed files with 116 additions and 14 deletions

View file

@ -118,6 +118,7 @@ xml2txt(cxobj *xn,
yang_stmt *ymod;
yang_stmt *ypmod;
char *prefix = NULL;
char *value;
if (xn == NULL || fn == NULL){
clicon_err(OE_XML, EINVAL, "xn or fn is NULL");
@ -148,7 +149,12 @@ xml2txt(cxobj *xn,
if (!children){ /* If no children print line */
switch (xml_type(xn)){
case CX_BODY:
(*fn)(f, "%s;\n", xml_value(xn));
value = xml_value(xn);
/* Add quotes if string contains spaces */
if (index(value, ' ') != NULL)
(*fn)(f, "\"%s\";\n", xml_value(xn));
else
(*fn)(f, "%s;\n", xml_value(xn));
break;
case CX_ELMNT:
(*fn)(f, "%*s%s;\n", 4*level, "", xml_name(xn));

View file

@ -75,7 +75,8 @@ int clixon_text_syntax_parsewrap(void)
%}
%s COMMENT
%x COMMENT
%x STRING
%%
@ -89,8 +90,8 @@ int clixon_text_syntax_parsewrap(void)
<INITIAL>\[ { return *yytext; }
<INITIAL>\] { return *yytext; }
<INITIAL>\; { return *yytext; }
<INITIAL>\: { return *yytext; }
<INITIAL>[^\n\r \t\[\]\{\}\;\:]+ {
<INITIAL>\" { _TS->ts_lex_state =INITIAL; BEGIN(STRING); return *yytext; }
<INITIAL>[^\n\r \t\[\]\{\}\;\"]+ {
clixon_text_syntax_parselval.string = strdup(yytext);
return TOKEN; }
<INITIAL>. { return -1; }
@ -99,6 +100,11 @@ int clixon_text_syntax_parsewrap(void)
<COMMENT><<EOF>> { return MY_EOF; }
<COMMENT>[^\n]+
<STRING>\" { BEGIN(_TS->ts_lex_state); return *yytext; }
<STRING>[^\n\r\"]+ { clixon_text_syntax_parselval.string = strdup(yytext);
return TOKEN; }
<STRING>. { return -1; }
%%
/*! Initialize XML scanner.

View file

@ -119,18 +119,21 @@ text_add_value(cxobj *xn,
static cxobj*
text_create_node(clixon_text_syntax_yacc *ts,
char *module,
char *name)
{
cxobj *xn;
yang_stmt *ymod;
char *ns;
char *prefix = NULL;
char *id = NULL;
if ((xn = xml_new(name, NULL, CX_ELMNT)) == NULL)
if (nodeid_split(name, &prefix, &id) < 0)
goto done;
if (module && ts->ts_yspec){
if ((xn = xml_new(id, NULL, CX_ELMNT)) == NULL)
goto done;
if (prefix && ts->ts_yspec){
/* Silently ignore if module name not found */
if ((ymod = yang_find(ts->ts_yspec, Y_MODULE, NULL)) != NULL){
if ((ymod = yang_find(ts->ts_yspec, Y_MODULE, prefix)) != NULL){
if ((ns = yang_find_mynamespace(ymod)) == NULL){
clicon_err(OE_YANG, 0, "No namespace");
goto done;
@ -141,6 +144,10 @@ text_create_node(clixon_text_syntax_yacc *ts,
}
}
done:
if (prefix)
free(prefix);
if (id)
free(id);
return xn;
}
@ -166,6 +173,10 @@ stmt : id value ';' { _PARSE_DEBUG("stmt-> id value ;");
if (text_add_value($1, $2) < 0) YYERROR;
$$ = $1;
}
| id '"' value '"' ';' { _PARSE_DEBUG("stmt-> id \" value \" ;");
if (text_add_value($1, $3) < 0) YYERROR;
$$ = $1;
}
| id '{' stmts '}' { _PARSE_DEBUG("stmt-> id { stmts }");
if (clixon_child_xvec_append($1, $3) < 0) YYERROR;
clixon_xvec_free($3);
@ -176,11 +187,8 @@ stmt : id value ';' { _PARSE_DEBUG("stmt-> id value ;");
;
id : TOKEN { _PARSE_DEBUG("id->TOKEN");
if (($$ = text_create_node(_TS, NULL, $1)) == NULL) YYERROR;;
if (($$ = text_create_node(_TS, $1)) == NULL) YYERROR;;
}
| TOKEN ':' TOKEN { _PARSE_DEBUG("id->TOKEN : TOKEN");
if (($$ = text_create_node(_TS, $1, $3)) == NULL) YYERROR;;
}
;
values : values TOKEN { _PARSE_DEBUG("values->values TOKEN"); }