2023-09-21 21:12:48 +02:00
|
|
|
const ohm = require('ohm-js');
|
2023-02-05 14:36:48 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const grammar = ohm.grammar(`Bru {
|
2023-09-21 21:38:35 +02:00
|
|
|
BruEnvFile = (vars | secretvars)*
|
2023-02-05 14:36:48 +01:00
|
|
|
|
|
|
|
nl = "\\r"? "\\n"
|
|
|
|
st = " " | "\\t"
|
2023-02-17 08:26:48 +01:00
|
|
|
stnl = st | nl
|
2023-02-05 14:36:48 +01:00
|
|
|
tagend = nl "}"
|
2023-02-17 08:26:48 +01:00
|
|
|
optionalnl = ~tagend nl
|
|
|
|
keychar = ~(tagend | st | nl | ":") any
|
|
|
|
valuechar = ~(nl | tagend) any
|
2023-02-05 14:36:48 +01:00
|
|
|
|
|
|
|
// Dictionary Blocks
|
|
|
|
dictionary = st* "{" pairlist? tagend
|
2023-02-17 08:26:48 +01:00
|
|
|
pairlist = optionalnl* pair (~tagend stnl* pair)* (~tagend space)*
|
|
|
|
pair = st* key st* ":" st* value st*
|
|
|
|
key = keychar*
|
|
|
|
value = valuechar*
|
2023-02-05 14:36:48 +01:00
|
|
|
|
2023-09-21 21:38:35 +02:00
|
|
|
// Array Blocks
|
|
|
|
array = st* "[" stnl* valuelist stnl* "]"
|
|
|
|
valuelist = stnl* arrayvalue stnl* ("," stnl* arrayvalue)*
|
|
|
|
arrayvalue = arrayvaluechar*
|
|
|
|
arrayvaluechar = ~(nl | st | "[" | "]" | ",") any
|
|
|
|
|
|
|
|
secretvars = "vars:secret" array
|
2023-02-05 14:36:48 +01:00
|
|
|
vars = "vars" dictionary
|
|
|
|
}`);
|
|
|
|
|
|
|
|
const mapPairListToKeyValPairs = (pairList = []) => {
|
2023-09-21 21:12:48 +02:00
|
|
|
if (!pairList.length) {
|
2023-02-05 14:36:48 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
return _.map(pairList[0], (pair) => {
|
2023-02-05 14:36:48 +01:00
|
|
|
let name = _.keys(pair)[0];
|
|
|
|
let value = pair[name];
|
|
|
|
let enabled = true;
|
2023-09-21 21:12:48 +02:00
|
|
|
if (name && name.length && name.charAt(0) === '~') {
|
2023-02-05 14:36:48 +01:00
|
|
|
name = name.slice(1);
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
enabled
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-09-21 21:38:35 +02:00
|
|
|
const mapArrayListToKeyValPairs = (arrayList = []) => {
|
|
|
|
arrayList = arrayList.filter((v) => v && v.length);
|
|
|
|
|
|
|
|
if (!arrayList.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.map(arrayList, (value) => {
|
|
|
|
let name = value;
|
|
|
|
let enabled = true;
|
|
|
|
if (name && name.length && name.charAt(0) === '~') {
|
|
|
|
name = name.slice(1);
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
value: null,
|
|
|
|
enabled
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-05 14:36:48 +01:00
|
|
|
const concatArrays = (objValue, srcValue) => {
|
|
|
|
if (_.isArray(objValue) && _.isArray(srcValue)) {
|
|
|
|
return objValue.concat(srcValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const sem = grammar.createSemantics().addAttribute('ast', {
|
|
|
|
BruEnvFile(tags) {
|
2023-09-21 21:12:48 +02:00
|
|
|
if (!tags || !tags.ast || !tags.ast.length) {
|
2023-02-06 18:52:48 +01:00
|
|
|
return {
|
|
|
|
variables: []
|
|
|
|
};
|
2023-02-05 14:36:48 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
return _.reduce(
|
|
|
|
tags.ast,
|
|
|
|
(result, item) => {
|
|
|
|
return _.mergeWith(result, item, concatArrays);
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2023-02-05 14:36:48 +01:00
|
|
|
},
|
2023-09-21 21:38:35 +02:00
|
|
|
array(_1, _2, _3, valuelist, _4, _5) {
|
|
|
|
return valuelist.ast;
|
|
|
|
},
|
|
|
|
arrayvalue(chars) {
|
|
|
|
return chars.sourceString ? chars.sourceString.trim() : '';
|
|
|
|
},
|
|
|
|
valuelist(_1, value, _2, _3, _4, rest) {
|
|
|
|
return [value.ast, ...rest.ast];
|
|
|
|
},
|
2023-02-05 14:36:48 +01:00
|
|
|
dictionary(_1, _2, pairlist, _3) {
|
|
|
|
return pairlist.ast;
|
|
|
|
},
|
|
|
|
pairlist(_1, pair, _2, rest, _3) {
|
|
|
|
return [pair.ast, ...rest.ast];
|
|
|
|
},
|
|
|
|
pair(_1, key, _2, _3, _4, value, _5) {
|
|
|
|
let res = {};
|
2023-02-17 08:26:48 +01:00
|
|
|
res[key.ast] = value.ast ? value.ast.trim() : '';
|
2023-02-05 14:36:48 +01:00
|
|
|
return res;
|
|
|
|
},
|
|
|
|
key(chars) {
|
|
|
|
return chars.sourceString ? chars.sourceString.trim() : '';
|
|
|
|
},
|
|
|
|
value(chars) {
|
|
|
|
return chars.sourceString ? chars.sourceString.trim() : '';
|
|
|
|
},
|
|
|
|
nl(_1, _2) {
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
st(_) {
|
|
|
|
return '';
|
|
|
|
},
|
2023-09-21 21:12:48 +02:00
|
|
|
tagend(_1, _2) {
|
2023-02-05 14:36:48 +01:00
|
|
|
return '';
|
|
|
|
},
|
|
|
|
_iter(...elements) {
|
2023-09-21 21:12:48 +02:00
|
|
|
return elements.map((e) => e.ast);
|
2023-02-05 14:36:48 +01:00
|
|
|
},
|
|
|
|
vars(_1, dictionary) {
|
|
|
|
const vars = mapPairListToKeyValPairs(dictionary.ast);
|
2023-09-21 21:38:35 +02:00
|
|
|
_.each(vars, (v) => {
|
|
|
|
v.secret = false;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
variables: vars
|
|
|
|
};
|
|
|
|
},
|
|
|
|
secretvars: (_1, array) => {
|
|
|
|
const vars = mapArrayListToKeyValPairs(array.ast);
|
|
|
|
_.each(vars, (v) => {
|
|
|
|
v.secret = true;
|
|
|
|
});
|
2023-02-05 14:36:48 +01:00
|
|
|
return {
|
|
|
|
variables: vars
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const parser = (input) => {
|
|
|
|
const match = grammar.match(input);
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (match.succeeded()) {
|
2023-02-05 14:36:48 +01:00
|
|
|
return sem(match).ast;
|
|
|
|
} else {
|
|
|
|
throw new Error(match.message);
|
|
|
|
}
|
2023-09-21 21:12:48 +02:00
|
|
|
};
|
2023-02-05 14:36:48 +01:00
|
|
|
|
|
|
|
module.exports = parser;
|