I've been playing with an exciting feature of HTML5 that I hadn't heard of: the contentEditable
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 new text editor, without having to actually code up the guts of text movement, manipulation, and entry. Oh, happy day!
More on this as the situation progresses.
/*
* This is my parser.
* It's not much, but it's mine.
*/
blocks = code:(if_block / lines)* { return code; }
if_block =
"if" ws "(" cond:string ")" ws "{" result:lines "}"
{ return {type: "if", condition: cond, body: result }; }
/*
while_block = "while" ws "(" cond:string ")" ws "{" result:lines "}"
{ return {type: "while", condition: cond, body: result }; }
*/
lines = lines:line+ ws?
{ return lines; }
line = ws? contents:string ";"
{ return contents; }
ws = (" " / "\n")+ { return ""; }
string = characters:([A-Za-z] [0-9A-Za-z "']*)
{ return characters[0] + characters[1].join(""); }
Then I started realizing I had no idea what I was doing. So I started defining the grammar for a Lisp! Sort of. More or less.
expression =
parenthetical
/ bracketed
/ identifier
/ string
/ number
parenthetical =
"(" cons: expression? cdr:(" "+ expression)* ")"
{
var output = [];
for (var i=0; i<cdr.length; i++) {
output.push(cdr[i][1]);
}
return {type: "parenthetical", first: cons, rest: output};
}
bracketed =
"[" cons: expression? cdr: (" "+ expression)* "]"
{
var output = [cons];
for (var i=0; i<cdr.length; i++) {
output.push(cdr[i][1]);
}
return output;
return {type: "list", contents: output};
}
identifier = contents:[A-Za-z]+ { return contents.join(""); }
string =
'"' contents:[^"]* '"' { return contents.join(""); }
/ "'" contents:[^']* "'" { return contents.join(""); }
number = contents:[0-9]+ { return parseInt(contents.join(""), 10); }
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!
// a comment!
var a = 5;
var b = foo(a);
b > 100 && b / 7 == 3
)
// Some results should take place
b += 294800;
}
b < 20
)
b -= 1;
}
raise UgnaughtException();
}
// So yeah