Quest for Beauty in iCal Records

The Internet Calendaring and Scheduling Core Object Specification is a media type which allows users to store and exchange this information. Robert suggested these files could be useful in wiki. wikipedia

We resumed this exploration with the challenge to make sense of the lines we read from two examples with me, Ward, at the keyboard. Noticing nested BEGIN and END lines I suggested a recursive parse. I had trouble.

Martin suggested I give up on the recursive parse and make something that looked more like a state machine. I was struggling to segment the input lines at the recursive call so a single function seemed attractive.

There are four lines to understand for BEGIN, two lines for END, and only one in other cases. github

We maintain a stack of suspended blocks while working on a nested block. We pop this stack when we encounter the END of the current block. I added line numbers to each line so that you can study how lines end up in the right block in the right order.

We were going to write a compiler once. Compiling functions and function calls isn't much different than this. Notice that BEGIN shows up in two places, the first line of the block and in the enclosing block where we were reading when we encountered it.

Robert asks, > When you push an array onto another array, you are able to change the array that you push on after this has been done?

Languages vary in what they consider permissible. Javascript arrays are flexible and retain their identity when flexed. So, yes, we can keep adding lines to the block already pushed to the result.

When we first coded the result we added the block to the result when the END was encountered but then the result came out in a scrambled order. I added the line numbers so that I could be sure the order of results were the most sensible.