Skip to content

Commit

Permalink
Add a function to escape STRING for output
Browse files Browse the repository at this point in the history
  • Loading branch information
mingodad committed Sep 6, 2024
1 parent 18fc689 commit f0515c4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions json2ebnf-lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ function parseJsonGrammar(fname, rule_sep, choice_sep, rule_terminator, isEbnfRR
return isOptional;
};

let escapeString = function(str) {
let str2 = str.replaceAll("\\", "\\\\");
str2 = str2.replaceAll("\t", "\\t");
str2 = str2.replaceAll("\r", "\\r");
str2 = str2.replaceAll("\n", "\\n");
str2 = str2.replaceAll("\f", "\\f");
return str2;
};

let manageRule = function (name, rule, depth) {
//print(name, rule.type); //, typeof rule);
switch(rule.type)
Expand Down Expand Up @@ -437,7 +446,7 @@ function parseJsonGrammar(fname, rule_sep, choice_sep, rule_terminator, isEbnfRR
fd.printf("\n\t/%s/", elm.value);
break;
case "STRING":
fd.printf("\n\t'%s'", elm.value);
fd.printf("\n\t'%s'", escapeString(elm.value));
break;
case "SYMBOL":
fd.printf("\n\t%s", elm.name);
Expand All @@ -461,6 +470,13 @@ function parseJsonGrammar(fname, rule_sep, choice_sep, rule_terminator, isEbnfRR
case "SYMBOL":
fd.printf("\n\t%s", elm.name);
break;
case "STRING":
fd.printf("\n\t'%s'", escapeString(elm.value));
break;
case "ALIAS":
fd.printf("\n\t");
manageRule(elm.type, elm, 0);
break;
default:
fd.printf("\n\t??:%s", elm.type);
}
Expand All @@ -478,7 +494,7 @@ function parseJsonGrammar(fname, rule_sep, choice_sep, rule_terminator, isEbnfRR
let elm2 = elm[idx2];
switch(elm2.type) {
case "STRING":
fd.printf(" '%s'", elm2.value);
fd.printf(" '%s'", escapeString(elm2.value));
break;
case "SYMBOL":
fd.printf(" %s", elm2.name);
Expand Down

0 comments on commit f0515c4

Please sign in to comment.