<p>I've been playing with an exciting feature of HTML5 that I hadn't heard of: the <codeclass="language-html">contentEditable</code> attribute. It's magical! It just makes anything suddenly be editable in-browser! It also finally provides me with the thing I've been wanting ever since growing dissatisfied with text editors: A way to build a <em>new</em> text editor, <em>without</em> having to actually code up the guts of text movement, manipulation, and entry. Oh, happy day!</p>
number = contents:[0-9]+ { return parseInt(contents.join(""), 10); }
</code></pre>
<p>Perhaps the answer is to break up the editor's coding space by lines, as webkit is wont to do. Specifically, this would be good because it'd make it easier to track the cursor position, because it would never change relative to the line element it's contained in!</p>
<styletype="text/css">
#editor div {
margin-left: 1em;
}
.block>code.line {
display: block;
margin-left: 1em;
}
.if>.branch {
display: inline-block;
}
</style>
<divid="editor">
<divclass="block">
<codeclass="line">// a comment!</code>
<codeclass="line">var a = 5;</code>
<codeclass="line">var b = foo(a); </code>
<divclass="if block">
<divclass="if branch">
<divclass="condition">
if (<codeclass="line">b > 100 && b / 7 == 3</code>)
</div>
<divclass="block">
{
<codeclass="line">// Some results should take place</code>
<p>So, it nearly killed me, but I managed to figure out how to make a parser (using pegjs) that <em>actually parses indentation-based grammars!</em> The basic way it works is that as it matches a line, it reads in the number of spaces before it. Using that, it figures out what indentation level it's at - then, as it's returning the line's payload, it puts the payload into the right code block. It's hacky and awful but I think I understand how it works. I'd rather move the logic into the <code>block</code> rule rather than the <code>line</code> rule, but that's pretty simple in theory. Here you go:
<pre><code>
{ var code = [];
var indentations = [0];
var current_head = [code]; }
b = line* { return code; }
line =
(spaces:" "* &
{ var level = spaces.length;
if (level > indentations[indentations.length-1]) {