From 72374c92124e53df7275c1e6c52f9891355a378f Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sat, 22 Mar 2014 16:44:55 -0400 Subject: [PATCH 1/3] fixed error handling for dangling else --- parse.js | 5 +++++ tokenize.js | 16 ++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/parse.js b/parse.js index 301515c..b11b1b7 100755 --- a/parse.js +++ b/parse.js @@ -411,6 +411,11 @@ function parseIf(tokens) { if (fst(tokens) && fst(tokens)[0] === "elsexp") { tokens.pop(); + if (_.size(tokens) < 1) { + throw error.JSyntaxError(linenum, + charnum, + "Unexpected end of source"); + } var elseC = parse(tokens); return new typ.If(ifC, thenC, elseC); diff --git a/tokenize.js b/tokenize.js index 47ffcc4..99b2be3 100755 --- a/tokenize.js +++ b/tokenize.js @@ -215,7 +215,7 @@ function tokenize(tokstream, matchop) { case 45: // '-' lambda = peek(tokstream, "arrow", "->"); if (lambda) { - tokens.push(lambda); + tokens.push($.extend(lambda, [charnum, linenum])); tokstream = tokstream.substr(2); break; } @@ -242,7 +242,7 @@ function tokenize(tokstream, matchop) { case 116: // 't' result = tokenizeT(tokstream); if (result) { - tokens.push(result); + tokens.push($.extend(result, [charnum, linenum])); tokstream = tokstream.substr(4); // 4 = length of either token break; } @@ -250,13 +250,13 @@ function tokenize(tokstream, matchop) { case 105: // 'i' var ifexp = peek(tokstream, "ifexp", "if"); if (ifexp) { - tokens.push(ifexp); + tokens.push($.extend(ifexp, [linenum, charnum])); tokstream = tokstream.substr(2); break; } var inkeyword = peek(tokstream, "in", "in "); if (inkeyword) { - tokens.push(inkeyword); + tokens.push($.extend(inkeyword, [charnum, linenum])); tokstream = tokstream.substr(3); break; } @@ -279,7 +279,7 @@ function tokenize(tokstream, matchop) { case 101: // e result = peek(tokstream, "elsexp", "else"); if (result) { - tokens.push(result); + tokens.push($.extend(result, [charnum, linenum])); tokstream = tokstream.substr(4); break; } @@ -287,7 +287,7 @@ function tokenize(tokstream, matchop) { case 102: // f result = peek(tokstream, "falselit", "false"); if (result) { - tokens.push(result); + tokens.push($.extend(result, [charnum, linenum])); tokstream = tokstream.substr(5); break; } @@ -295,13 +295,13 @@ function tokenize(tokstream, matchop) { case 108: // l lambda = peek(tokstream, "lambda", "lambda"); if (lambda) { - tokens.push(lambda); + tokens.push($.extend(lambda, [linenum, charnum])); tokstream = tokstream.substr(6); break; } var letexp = peek(tokstream, "let", "let"); if (letexp) { - tokens.push(letexp); + tokens.push($.extend(letexp, [charnum, linenum])); tokstream = tokstream.substr(3); break; } -- 2.30.2 From 3b464b81cfe6cff150f90cd114d45452e86ba2e3 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sat, 22 Mar 2014 17:02:16 -0400 Subject: [PATCH 2/3] should error if an if-then-else expression has no else variant (must be that way due to the type system) --- parse.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/parse.js b/parse.js index b11b1b7..795ec7b 100755 --- a/parse.js +++ b/parse.js @@ -1,5 +1,4 @@ #! /usr/bin/node - var fs = require("fs"); var typ = require("./representation.js"); var $ = require("./tools.js"); @@ -401,10 +400,11 @@ function parseIf(tokens) { } else { var ifC = parse(tokens); - if (!fst(tokens) || fst(tokens)[0] !== "thenexp") + if (!fst(tokens) || fst(tokens)[0] !== "thenexp") { throw error.JSyntaxError(fst(tokens)[3], fst(tokens)[2], "if ``exp'' must be folowed by ``then'' exp, not "+snd(tokens)[0]); + } else { tokens.pop(); var thenC = parse(tokens); @@ -416,15 +416,18 @@ function parseIf(tokens) { charnum, "Unexpected end of source"); } + else { var elseC = parse(tokens); return new typ.If(ifC, thenC, elseC); - - } - else { - return new typ.If(ifC, thenC); - } - } - } + } + } + else { + throw error.JSyntaxError(linenum, + charnum, + "If expression must include an else variant"); + } + } + } } -- 2.30.2 From 65b2717d5ba2139ff07cc760461a86103fefce2c Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sat, 22 Mar 2014 17:05:46 -0400 Subject: [PATCH 3/3] removed spurious check on else variants --- representation.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/representation.js b/representation.js index ea4ba57..646429b 100644 --- a/representation.js +++ b/representation.js @@ -157,8 +157,7 @@ function DefFunc(ident, params, body) { function If(condition, thenexp, elseexp) { this.condition = condition; this.thenexp = thenexp; - if (elseexp) - this.elseexp = elseexp; + this.elseexp = elseexp; this.exprType = "If"; return this; } -- 2.30.2