Browse Source

more error stuff

pull/1/head
Wesley Kerfoot 11 years ago
parent
commit
129d339396
  1. 15
      errors.js
  2. 12
      parse.js
  3. 6
      tokenize.js

15
errors.js

@ -19,17 +19,28 @@ var JLException = {
} }
} }
function SyntaxError(linenum, charnum, message) { function JSyntaxError(linenum, charnum, message) {
this.linenum = linenum; this.linenum = linenum;
this.charnum = charnum; this.charnum = charnum;
this.errormessage = message; this.errormessage = message;
this.stxerror = function() {
console.log("Syntax Error\n",
"Line #", this.linenum,"\n",
"Character #", this.charnum, "\n",
this.errormessage);
};
return this; return this;
} }
function TypeError(linenum, charnum, token, message) { function JTypeError(linenum, charnum, token, message) {
this.linenum = linenum; this.linenum = linenum;
this.charnum = charnum; this.charnum = charnum;
this.errormessage = message; this.errormessage = message;
this.token = token; this.token = token;
return this; return this;
} }
TypeError.prototype = JLException;
module.exports =
{JSyntaxError : JSyntaxError,
JTypeError : JTypeError};

12
parse.js

@ -6,6 +6,7 @@ var tool = require("./tools.js");
var tokenizer = require("./tokenize.js"); var tokenizer = require("./tokenize.js");
var desugarer = require("./desugar.js"); var desugarer = require("./desugar.js");
var pprint = require("./pprint.js"); var pprint = require("./pprint.js");
var error = require("./errors.js");
var print = console.log; var print = console.log;
@ -45,7 +46,7 @@ function parseMany(exprType, valid, tokens) {
parsed = parse(tokens); parsed = parse(tokens);
} }
else { else {
throw "Error: unexpected token "+fst(tokens)[0]+" in parseMany"; throw error.JSyntaxError(fst(tokens)[2], fst(tokens)[3], "Error: unexpected token "+fst(tokens)[0]+" in parseMany");
} }
results.push(parsed); results.push(parsed);
@ -105,7 +106,7 @@ function parseList(tokens) {
var xs = parseBetween(function (x) { return true; }, "comma", tokens); var xs = parseBetween(function (x) { return true; }, "comma", tokens);
} }
if (fst(tokens)[0] !== "right_square") { if (fst(tokens)[0] !== "right_square") {
throw "Error, list must be terminated by ]"; throw error.JSyntaxError(fst(tokens)[3], fst(tokens)[2], "Error, list must be terminated by ]");
} }
tokens.pop(); tokens.pop();
return new typ.ListT(xs); return new typ.ListT(xs);
@ -114,8 +115,8 @@ function parseList(tokens) {
function parseDefFunction(tokens) { function parseDefFunction(tokens) {
var fname = parse(tokens); var fname = parse(tokens);
if (!fname.exprType === "identifier") { if (fname.exprType != "Name") {
throw "Error, expected an identifier in function definition"; throw error.JSyntaxError(fst(tokens)[3], fst(tokens)[2], "Error, expected an identifier in function definition");
} }
if (fst(tokens)[0] === "right_paren") { if (fst(tokens)[0] === "right_paren") {
var parameters = []; var parameters = [];
@ -319,8 +320,7 @@ function parseFull(tokenized) {
} }
return ast; return ast;
} catch (e) { } catch (e) {
print("An exception occured, could not finish parsing"); e.stxerror();
print(e);
process.exit(1); process.exit(1);
} }
} }

6
tokenize.js

@ -2,6 +2,7 @@
var rep = require("./representation.js"); var rep = require("./representation.js");
var tools = require("./tools.js"); var tools = require("./tools.js");
var error = require("./errors.js");
var operators = Object.keys(rep.OPInfo); var operators = Object.keys(rep.OPInfo);
var matchop = tools.opMatch(operators); var matchop = tools.opMatch(operators);
@ -103,7 +104,7 @@ function tokenizeStr(tokstream, charnum, linenum) {
n++; n++;
charnum++; charnum++;
if (tokstream.length < 1) { if (tokstream.length < 1) {
throw "Error: missing quotation mark"; throw error.JSyntaxError(linenum, charnum, "Error: missing quotation mark");
} }
} }
n++; n++;
@ -316,8 +317,7 @@ function tokenizeFull(input) {
return x[0] !== "whitespace"; return x[0] !== "whitespace";
}); });
} catch (e) { } catch (e) {
console.log("An error occured during tokenization"); console.log(e.stxerror());
console.log(e);
process.exit(1); process.exit(1);
} }
} }

Loading…
Cancel
Save