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.charnum = charnum;
this.errormessage = message;
this.stxerror = function() {
console.log("Syntax Error\n",
"Line #", this.linenum,"\n",
"Character #", this.charnum, "\n",
this.errormessage);
};
return this;
}
function TypeError(linenum, charnum, token, message) {
function JTypeError(linenum, charnum, token, message) {
this.linenum = linenum;
this.charnum = charnum;
this.errormessage = message;
this.token = token;
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 desugarer = require("./desugar.js");
var pprint = require("./pprint.js");
var error = require("./errors.js");
var print = console.log;
@ -45,7 +46,7 @@ function parseMany(exprType, valid, tokens) {
parsed = parse(tokens);
}
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);
@ -105,7 +106,7 @@ function parseList(tokens) {
var xs = parseBetween(function (x) { return true; }, "comma", tokens);
}
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();
return new typ.ListT(xs);
@ -114,8 +115,8 @@ function parseList(tokens) {
function parseDefFunction(tokens) {
var fname = parse(tokens);
if (!fname.exprType === "identifier") {
throw "Error, expected an identifier in function definition";
if (fname.exprType != "Name") {
throw error.JSyntaxError(fst(tokens)[3], fst(tokens)[2], "Error, expected an identifier in function definition");
}
if (fst(tokens)[0] === "right_paren") {
var parameters = [];
@ -319,8 +320,7 @@ function parseFull(tokenized) {
}
return ast;
} catch (e) {
print("An exception occured, could not finish parsing");
print(e);
e.stxerror();
process.exit(1);
}
}

6
tokenize.js

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

Loading…
Cancel
Save