When writing a language with braces (I can see why whitespace sensitive languages are a thing now), one vital thing you will need to know is what thing is inside what brace. I am writing a sort of subset of JavaScript in the style of es5 (more like es 5.5 because some es6 features are just good!). I need to know how many braces are within a range of line no's, between L1 and L5 for example.
Given the sample:
const input = `
{
{
// Depth 2
}
}
`;
Initially I thought line by line we step through the input, when we encounter the token { L_BRACE
we should increment a count by one, when we encounter a } R_BRACE
we should decrement.
In the sample we should have the following count.
0
1
2
2
2
1
0
That looks great doesn't it! Not only do we know block we are in we also know the depth, ideal, but an object literal is not a block scope, so how do we avoid that, what about destructuring? Okay so we could avoid this whole mess with a simple (yet incorrect assumption), what do blocked scoped things have in common?
function name(){
function (){
class Foo {
class {
for () {
try {
while {
do {
with {
if {
switch {
{
Okay that's all I could remember this time In the morning ☕. In answering my previous question Where should the brace sit? At the End Of Line (EOL), so I could check that the opening brace is preceded by a whitelist of words, or it is not an assignment AND the brace is at EOL. Great sorted yes? No, because some programmers like my brother care not for good formatting and could do this.
function anoying () { const imATroll = true;
// I laugh at your feable algorithms
}
My brother is such a pain, because now the brace is not at the end of the line, we can't use this algorithm. I'm stumped 🙄, I'm sure I will solve it and that's my goal for this week.
What other problems could I solve?
Well mismatched braces are easier to detect, nievely you could filter the lines containing the braces that fit the above unsolved criteria, then count the length, if the number is odd, we can say stop! This program is broken... But where?
Looping through line by line will not tell you where the program broke because we need the entire program to determine what looks a bit off.
This problem is so simple in theory, anyway wish me luck and add your suggestions in the comments!