Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added error coloring
- Loading branch information
1 parent
985d9ee
commit 6c2a8c2
Showing
3 changed files
with
127 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,126 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Pygmy</title> | ||
<style> | ||
pre#input | ||
{ | ||
color: Green; | ||
} | ||
.operator | ||
{ | ||
color: Black; | ||
} | ||
.identifier | ||
{ | ||
color: #277; | ||
} | ||
.string | ||
{ | ||
color: Red; | ||
} | ||
.number | ||
{ | ||
color: #54F; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<pre id="input" contenteditable="true"> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Pygmy</title> | ||
<style> | ||
pre#input | ||
{ | ||
color: Green; | ||
} | ||
.operator | ||
{ | ||
color: Black; | ||
} | ||
.identifier | ||
{ | ||
color: #277; | ||
} | ||
.string | ||
{ | ||
color: Red; | ||
} | ||
.number | ||
{ | ||
color: #54F; | ||
} | ||
.error | ||
{ | ||
border-bottom: 1px solid red; | ||
border-bottom-style: dashed; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<pre id="input" contenteditable="true"> | ||
a: b: c: d: 5 | ||
|
||
f:: (){ | ||
} | ||
|
||
;m!: 4 | ||
</pre><br /> | ||
<button id="tokenizer">Tokenize</button> | ||
<button id="parser">Parse</button> | ||
<button id="compiler">Compile</button><br /> | ||
Output:<br /> | ||
<pre id="output"> | ||
</pre> | ||
</body> | ||
|
||
<script src="error.js"></script> | ||
<script src="tokenizer.js"></script> | ||
<script src="parser.js"></script> | ||
<script src="compiler.js"></script> | ||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | ||
<!--<script src="http://jquery-debounce.googlecode.com/files/jquery.debounce-1.0.5.js"></script>--> | ||
<script> | ||
$.fn.getPreText = function () { | ||
var ce = $("<pre />").html(this.html()); | ||
if ($.browser.webkit) | ||
ce.find("div").replaceWith(function() { return "\n" + this.innerHTML; }); | ||
if ($.browser.msie) | ||
ce.find("p").replaceWith(function() { return this.innerHTML + "<br>"; }); | ||
if ($.browser.mozilla || $.browser.opera || $.browser.msie) | ||
ce.find("br").replaceWith("\n"); | ||
|
||
return ce.text(); | ||
}; | ||
|
||
$(function() { | ||
|
||
var getTokens = function() { | ||
var t, text = $("#input").getPreText(), | ||
error; | ||
try { | ||
t = tokenize(text); | ||
} | ||
catch (e) { | ||
t = e; | ||
error = true; | ||
} | ||
if (!error) { | ||
text = text.split(""); | ||
for (var i = 0; i < t.length; i++) { | ||
var token = t[i]; | ||
text[token.from] = "<span class=\"" + token.type + "\">" + text[token.from]; | ||
text[token.to - 1] += "</span>"; | ||
} | ||
$("#input").html(text.join("")); | ||
} | ||
return t; | ||
}; | ||
|
||
var getStatements = function(){ | ||
var tokens = getTokens(), statements; | ||
try { | ||
statements = parse(tokens); | ||
} | ||
catch (e) { | ||
statements = e; | ||
} | ||
return statements; | ||
}; | ||
|
||
$("#tokenizer").click(function() { | ||
var t = getTokens(); | ||
$("#output").html(JSON.stringify(t, ["type", "value", "from", "to", "line", "literal", "message"], 4)); | ||
}); | ||
|
||
$("#parser").click(function() { | ||
var statements = getStatements (); | ||
$("#output").html(JSON.stringify(statements, ["id", "type", "value", "arguments", "arity", "first", "second", "args", "from", "to", "message"], 4)); | ||
</pre> | ||
<br /> | ||
<button id="tokenizer">Tokenize</button> | ||
<button id="parser">Parse</button> | ||
<button id="compiler">Compile</button><br /> | ||
Output:<br /> | ||
<pre id="output"> | ||
</pre> | ||
</body> | ||
<script src="error.js"></script> | ||
<script src="tokenizer.js"></script> | ||
<script src="parser.js"></script> | ||
<script src="compiler.js"></script> | ||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | ||
<!--<script src="http://jquery-debounce.googlecode.com/files/jquery.debounce-1.0.5.js"></script>--> | ||
<script> | ||
$.fn.getPreText = function () { | ||
var ce = $("<pre />").html(this.html()); | ||
if ($.browser.webkit) | ||
ce.find("div").replaceWith(function () { return "\n" + this.innerHTML; }); | ||
if ($.browser.msie) | ||
ce.find("p").replaceWith(function () { return this.innerHTML + "<br>"; }); | ||
if ($.browser.mozilla || $.browser.opera || $.browser.msie) | ||
ce.find("br").replaceWith("\n"); | ||
|
||
return ce.text(); | ||
}; | ||
|
||
$(function () { | ||
|
||
var getTokens = function () { | ||
var t, text = $("#input").getPreText(), | ||
error; | ||
try { | ||
t = tokenize(text); | ||
} | ||
catch (e) { | ||
t = e; | ||
error = true; | ||
} | ||
text = text.split(""); | ||
for (var i = 0; i < t.length; i++) { | ||
var token = t[i]; | ||
text[token.from] = "<span class=\"" + token.type + "\">" + text[token.from]; | ||
text[token.to - 1] += "</span>"; | ||
} | ||
$("#input").html(text.join("")); | ||
return t; | ||
}; | ||
|
||
var getStatements = function () { | ||
var tokens = getTokens(), statements; | ||
try { | ||
statements = parse(tokens); | ||
} | ||
catch (e) { | ||
statements = e; | ||
var text = text = $("#input").getPreText().split(""); | ||
for (var i = 0; i < e.length; i++) { | ||
var token = e[i]; | ||
text[token.from] = "<span class=\"" + token.type + "\">" + text[token.from]; | ||
text[token.to - 1] += "</span>"; | ||
} | ||
$("#input").html(text.join("")); | ||
} | ||
return statements; | ||
}; | ||
|
||
$("#tokenizer").click(function () { | ||
var t = getTokens(); | ||
$("#output").html(JSON.stringify(t, ["type", "value", "from", "to", "line", "message"], 4)); | ||
}); | ||
|
||
$("#parser").click(function () { | ||
var statements = getStatements(); | ||
$("#output").html(JSON.stringify(statements, ["id", "type", "value", "arguments", "arity", "first", "second", "args", "from", "to", "message"], 4)); | ||
}); | ||
|
||
$("#compiler").click(function () { | ||
$("#output").html(compile(getStatements(), "javascript")); | ||
}); | ||
|
||
getTokens(); | ||
}); | ||
|
||
$("#compiler").click(function() { | ||
$("#output").html(compile(getStatements(), "javascript")); | ||
}); | ||
|
||
getTokens(); | ||
}); | ||
</script> | ||
</html> | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters