feat: bru lang - keys can support any char except whitespace, values can have any char except newline

This commit is contained in:
Anoop M D 2023-02-03 21:44:07 +05:30
parent 2ee2e270b0
commit a21615a5fb
2 changed files with 27 additions and 25 deletions

View File

@ -6,20 +6,22 @@ const grammar = ohm.grammar(`Bru {
nl = "\\r"? "\\n" nl = "\\r"? "\\n"
st = " " | "\\t" st = " " | "\\t"
tagend = nl "}" tagend = nl "}"
validkey = ~(st | ":") any
validvalue = ~nl any
headers = "headers" st* "{" pairlist? tagend headers = "headers" st* "{" pairlist? tagend
pairlist = nl* pair (~tagend nl pair)* (~tagend space)* pairlist = nl* pair (~tagend nl pair)* (~tagend space)*
pair = st* key st* ":" st* val st* pair = st* key st* ":" st* value st*
key = ~tagend alnum* key = ~tagend validkey*
val = ~tagend letter* value = ~tagend validvalue*
script = "script" st* "{" nl* codeblock tagend script = "script" st* "{" nl* textblock tagend
test = "test" st* "{" codeblock tagend test = "test" st* "{" textblock tagend
codeblock = codeline (~tagend nl codeline)* textblock = textline (~tagend nl textline)*
codeline = codechar* textline = textchar*
codechar = ~nl any textchar = ~nl any
}`); }`);
const mapPairListToKeyValPairs = (pairList = [], enabled = true) => { const mapPairListToKeyValPairs = (pairList = [], enabled = true) => {
@ -54,34 +56,34 @@ const sem = grammar.createSemantics().addAttribute('ast', {
pairlist(_1, pair, _2, rest, _3) { pairlist(_1, pair, _2, rest, _3) {
return [pair.ast, ...rest.ast]; return [pair.ast, ...rest.ast];
}, },
pair(_1, key, _2, _3, _4, val, _5) { pair(_1, key, _2, _3, _4, value, _5) {
let res = {}; let res = {};
res[key.ast] = val.ast; res[key.ast] = value.ast;
return res; return res;
}, },
key(chars) { key(chars) {
return chars.sourceString; return chars.sourceString;
}, },
val(chars) { value(chars) {
return chars.sourceString; return chars.sourceString ? chars.sourceString.trim() : '';
}, },
script(_1, _2, _3, _4, codeblock, _5) { script(_1, _2, _3, _4, textblock, _5) {
return { return {
script: codeblock.sourceString script: textblock.sourceString
}; };
}, },
test(_1, _2, _3, codeblock, _4) { test(_1, _2, _3, textblock, _4) {
return { return {
test: codeblock.sourceString test: textblock.sourceString
};; };;
}, },
codeblock(line, _1, rest) { textblock(line, _1, rest) {
return [line.ast, ...rest.ast].join('\n'); return [line.ast, ...rest.ast].join('\n');
}, },
codeline(chars) { textline(chars) {
return chars.sourceString; return chars.sourceString;
}, },
codechar(char) { textchar(char) {
return char.sourceString; return char.sourceString;
}, },
nl(_1, _2) { nl(_1, _2) {

View File

@ -59,19 +59,19 @@ headers {
it("should parse multi headers", () => { it("should parse multi headers", () => {
const input = ` const input = `
headers { headers {
hello: world content-type: application/json
foo: bar Authorization: JWT secret
}`; }`;
const output = bruToJsonV2(input); const output = bruToJsonV2(input);
const expected = { const expected = {
"headers": [{ "headers": [{
"name": "hello", "name": "content-type",
"value": "world", "value": "application/json",
"enabled": true "enabled": true
}, { }, {
"name": "foo", "name": "Authorization",
"value": "bar", "value": "JWT secret",
"enabled": true "enabled": true
}] }]
}; };