aoc-2023/day_19/grammar.ne
2024-12-09 22:41:02 +09:00

76 lines
1.4 KiB
Plaintext

main -> rules eol materials {% (d) => {
return {
rules: d[0],
materials: d[2],
}
}
%}
rules -> (rule eol:?):+ {% d=> {
return d[0].map(x=> x[0])
}
%}
rule -> rulename _ "{" _ conditions _ "," _ rulename _ "}" {% d=>{
return {
name: d[0],
rules: d[4],
always: d[8],
}
}
%}
conditions -> (condition ","):* condition {% d=>{
return d[0].map(x=> x[0]).concat([d[1]])
}%}
condition -> cond _ ":" _ rulename {% d=>{
return {
condition: d[0],
dest:d[4]
}
}
%}
cond -> quality _ condSymbol _ int {% d=> {
return {
quality: d[0],
cond: d[2][0],
value: d[4]
}
} %}
condSymbol -> ">" | "<" {% d => d[0]%}
quality -> [xmas] {% d=> d[0]%}
rulename -> [a-zA-Z]:+ {% d=> {
return d[0].join("");
}%}
materials -> (material eol:?):+ {% d => d[0].map(x=> x[0]) %}
material -> "{" _ statement (_ "," _ statement):+ _ ",":? _ "}" {% d=>{
const ret = {};
const state = d[2];
ret[state.quality] = state.value;
d[3].map(x=> x[3]).forEach(s=>{
ret[s.quality] = s.value;
});
return ret;
}
%}
statement -> quality _ "=" _ int {% d=>{
return {
quality: d[0],
value:d[4],
}
}
%}
_ -> wschar:* {% d=> null %}
__ -> wschar:+ {% d=> null %}
wschar -> [ \t] {% id %}
eol -> "\n" | "\r\n" {% d=> null %}
int -> [0-9]:+ {% d=> parseInt(d[0].join("")) %}