diff --git a/parse.js b/parse.js index 84c1959..f7337ef 100755 --- a/parse.js +++ b/parse.js @@ -207,7 +207,7 @@ function parseDefFunction(tokens, linenum, charnum) { fst(tokens)[2], fst(tokens)[3]); } - if ((fst(tokens)[0]) !== "right_paren") { + if (!tokens || (fst(tokens)[0]) !== "right_paren") { throw error.JSyntaxError(fst(tokens)[3], fst(tokens)[2], "Formal parameters must be followed by )"); @@ -353,6 +353,34 @@ function parseLetItem(tokens) { } } +function parseDataType(tokens, linenum, charnum) { + var typeName = parse(tokens, linenum, charnum); + var typeParams; + var typeBody; + + if (typeName.exprType !== "TypeOperator") { + throw error.JSyntaxError(typeName.linenum, + typeName.charnum, + "Expected a type operator in data type definition"); + } + parameters = parseMany(parse, + validName, + validFormPar, + tokens, + charnum, + linenum); + if (!tokens || (fst(tokens)[0]) !== "right_paren") { + throw error.JSyntaxError(fst(tokens)[3], + fst(tokens)[2], + "Data type parameters must be followed by )"); + } + tokens.pop(); + typeBody = parse(tokens); + return addSrcPos(new typ.DataType, tokens, typeBody.linenum, typeBody.charnum); +} + + + function parseDefType(tokens, linenum, charnum) { var result; var rhs; @@ -464,7 +492,7 @@ function parseDef(tokens, linenum, charnum) { charnum, "A definition cannot be the value of a binding"); } - result = addSrcPos(new typ.Def(identifier, bound), tokens, linenum, charnum); + result = addSrcPos(new typ.Def(identifier, bound), tokens, bound.linenum, bound.charnum); return result; } } @@ -562,7 +590,7 @@ function parseIf(tokens) { } else { var elseC = parse(tokens); - result = addSrcPos(new typ.If(ifC, thenC, elseC), tokens, linenum, charnum); + result = addSrcPos(new typ.If(ifC, thenC, elseC), tokens, elseC.linenum, elseC.charnum); return result; } } @@ -599,7 +627,7 @@ function parseLambda(tokens) { } tokens.pop(); var body = parse(tokens); - result = addSrcPos(new typ.FuncT(parameters, body), tokens, linenum, charnum); + result = addSrcPos(new typ.FuncT(parameters, body), tokens, body.linenum, body.charnum); return result; } @@ -687,7 +715,7 @@ function parseInfix(tokens, minPrec, lhs, linenum, charnum) { tokens.pop(); /*remove the operator token*/ var rhs = parseInfix(tokens, nextMinPrec); - lhs = addSrcPos(typ.makeApp(op, [lhs, rhs]), tokens, linenum, charnum); + lhs = addSrcPos(typ.makeApp(op, [lhs, rhs]), tokens, rhs.linenum, rhs.charnum); } return lhs; } diff --git a/representation.js b/representation.js index bea8317..ea87fec 100644 --- a/representation.js +++ b/representation.js @@ -410,5 +410,6 @@ module.exports = TypeApp: TypeApp, Closure : Closure, isTypeExpr : isTypeExprRec, - DefType : DefType + DefType : DefType, + DataType : DataType };