diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res index 3e5abceb..1731168f 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res @@ -22,7 +22,7 @@ let expectExpressionToBe = (expr, answer, ~v="_", ()) => { } else { let a2 = rExpr - ->E.R2.errMap(e => e->SqError.Message.fromParseError->SqError.fromMessage) + ->E.R2.errMap(e => e->SqError.fromParseError) ->Result.flatMap(expr => Expression.BackCompatible.evaluate(expr)) ->Reducer_Value.toStringResultOkless (a1, a2)->expect->toEqual((answer, v)) diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_includes_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_includes_test.res index 6c6d46fe..9c21ff1d 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_includes_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_includes_test.res @@ -25,7 +25,7 @@ x=1`, let mainIncludes = Project.getIncludes(project, "main") switch mainIncludes { | Ok(includes) => expect(includes) == ["common"] - | Error(error) => fail(error->SqError.Message.toString) + | Error(error) => fail(error->SqError.toString) } }) test("past chain", () => { @@ -60,7 +60,7 @@ x=1`, let mainIncludes = Project.getIncludes(project, "main") switch mainIncludes { | Ok(includes) => expect(includes) == ["common", "myModule"] - | Error(error) => fail(error->SqError.Message.toString) + | Error(error) => fail(error->SqError.toString) } }) @@ -99,7 +99,7 @@ x=1`, let mainIncludes = Project.getIncludes(project, "main") switch mainIncludes { | Ok(includes) => expect(includes) == ["common", "common2", "myModule"] - | Error(error) => fail(error->SqError.Message.toString) + | Error(error) => fail(error->SqError.toString) } }) test("direct past chain", () => { diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_includes_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_includes_test.res index bd06c481..5692d93c 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_includes_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_includes_test.res @@ -36,7 +36,7 @@ Here we will finally proceed to a real life scenario. */ /* Parse includes has set the includes */ switch project->Project.getIncludes("main") { | Ok(includes) => includes->expect == ["common"] - | Error(err) => err->SqError.Message.toString->fail + | Error(err) => err->SqError.toString->fail } /* If the includes cannot be parsed then you get a syntax error. Otherwise you get the includes. @@ -85,7 +85,7 @@ Here we will finally proceed to a real life scenario. */ let rIncludes = project->Project.getIncludes(sourceName) switch rIncludes { /* Maybe there is an include syntax error */ - | Error(err) => err->SqError.Message.toString->Js.Exn.raiseError + | Error(err) => err->SqError.toString->Js.Exn.raiseError | Ok(includes) => includes->Belt.Array.forEach(newIncludeName => { @@ -169,7 +169,7 @@ Here we will finally proceed to a real life scenario. */ test("getIncludes", () => { switch Project.getIncludes(project, "main") { | Ok(includes) => includes->expect == ["common"] - | Error(err) => err->SqError.Message.toString->fail + | Error(err) => err->SqError.toString->fail } }) }) diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res index daa4e7a6..c8904e00 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res @@ -104,10 +104,8 @@ let cleanAllResults = (project: reducerProject): unit => project->Private.cleanA To set the includes one first has to call "parseIncludes". The parsed includes or the parser error is returned. */ @genType -let getIncludes = (project: reducerProject, sourceId: string): result< - array, - errorMessage, -> => project->Private.getIncludes(sourceId) +let getIncludes = (project: reducerProject, sourceId: string): result, error> => + project->Private.getIncludes(sourceId) /* Other sources contributing to the global namespace of this source. */ @genType diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res index a2d98f92..809ac514 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res @@ -12,5 +12,5 @@ let createContext = (stdLib: Reducer_Namespace.t, environment: Reducer_T.environ } let currentFunctionName = (t: t): string => { - t.inFunction->E.O2.fmap(Reducer_Lambda_T.name)->E.O2.default("") + t.inFunction->E.O2.fmap(Reducer_Lambda_T.name)->E.O2.default(Reducer_T.topFrameName) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res index 0036816b..4906dd54 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res @@ -149,7 +149,5 @@ module BackCompatible = { } let evaluateString = (peggyCode: string): result => - parse(peggyCode) - ->E.R2.errMap(e => e->SqError.Message.fromParseError->SqError.fromMessage) - ->Result.flatMap(evaluate) + parse(peggyCode)->E.R2.errMap(e => e->SqError.fromParseError)->Result.flatMap(evaluate) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res index e61efd5d..46640439 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res @@ -27,6 +27,9 @@ let extend = (t: t, name: string, location: option location: location, }) +let makeSingleFrameStack = (location: Reducer_Peggy_Parse.location): t => + make()->extend(Reducer_T.topFrameName, Some(location)) + let toString = (t: t) => t ->Belt.List.map(s => " " ++ s->Frame.toString ++ "\n") diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res index dcc9e4bf..36025e40 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res @@ -65,3 +65,5 @@ and context = { } and reducerFn = (expression, context) => (value, context) + +let topFrameName = "" diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ParseIncludes.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ParseIncludes.res index c0612be8..bab3fa88 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ParseIncludes.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ParseIncludes.res @@ -1,7 +1,7 @@ @module("./ReducerProject_IncludeParser.js") external parse__: string => array> = "parse" -let parseIncludes = (expr: string): result, SqError.Message.t> => +let parseIncludes = (expr: string): result, SqError.t> => try { let answer = parse__(expr) // let logEntry = answer->Js.Array2.joinWith(",") @@ -9,8 +9,9 @@ let parseIncludes = (expr: string): result, SqError.Mess Belt.Array.map(answer, item => (item[0], item[1]))->Ok } catch { | Js.Exn.Error(obj) => - RESyntaxError( - Belt.Option.getExn(Js.Exn.message(obj)), - Reducer_Peggy_Parse.syntaxErrorToLocation(obj)->Some, - )->Error + RESyntaxError(Belt.Option.getExn(Js.Exn.message(obj))) + ->SqError.fromMessageWithFrameStack( + Reducer_FrameStack.makeSingleFrameStack(Reducer_Peggy_Parse.syntaxErrorToLocation(obj)), + ) + ->Error } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res index 2211f004..3612c492 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res @@ -152,10 +152,7 @@ let parseIncludes = (this: t): t => { } } let doRawParse = (this: t): T.rawParseArgumentType => - this - ->getSource - ->Reducer_Peggy_Parse.parse(this.sourceId) - ->E.R2.errMap(SqError.Message.fromParseError) + this->getSource->Reducer_Peggy_Parse.parse(this.sourceId)->E.R2.errMap(SqError.fromParseError) let rawParse = (this: t): t => this->getRawParse->E.O2.defaultFn(() => doRawParse(this))->setRawParse(this, _) @@ -190,7 +187,7 @@ let doRun = (this: t, context: Reducer_T.context): t => } catch { | e => this->failRun(e->SqError.fromException) } - | Error(e) => this->failRun(e->SqError.fromMessage) + | Error(e) => this->failRun(e) } | None => this->failRun(RETodo("attempt to run without expression")->SqError.fromMessage) } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res index 9ef83536..bcdbfa02 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res @@ -3,9 +3,9 @@ module ExpressionT = Reducer_Expression_T type sourceArgumentType = string type sourceType = string -type rawParseArgumentType = result +type rawParseArgumentType = result type rawParseType = option -type expressionArgumentType = result +type expressionArgumentType = result type expressionType = option type continuationArgumentType = Reducer_T.namespace type continuationType = option @@ -15,7 +15,7 @@ type resultType = option type continuesArgumentType = array type continuesType = array type includesArgumentType = string -type includesType = result, SqError.Message.t> +type includesType = result, SqError.t> type importAsVariablesType = array<(string, string)> type projectItem = { diff --git a/packages/squiggle-lang/src/rescript/SqError.res b/packages/squiggle-lang/src/rescript/SqError.res index 29d98517..a9950020 100644 --- a/packages/squiggle-lang/src/rescript/SqError.res +++ b/packages/squiggle-lang/src/rescript/SqError.res @@ -19,7 +19,7 @@ module Message = { | REOperationError(Operation.operationError) | RERecordPropertyNotFound(string, string) | RESymbolNotFound(string) - | RESyntaxError(string, option) + | RESyntaxError(string) | RETodo(string) // To do | REUnitNotFound(string) | RENeedToRun @@ -27,10 +27,6 @@ module Message = { exception MessageException(t) - let fromParseError = ( - SyntaxError(message, location): Reducer_Peggy_Parse.parseError, - ) => RESyntaxError(message, location->Some) - let toString = (err: t) => switch err { | REArityError(_oFnName, arity, usedArity) => @@ -61,7 +57,7 @@ module Message = { | RENotAFunction(valueString) => `${valueString} is not a function` | RERecordPropertyNotFound(msg, index) => `${msg}: ${index}` | RESymbolNotFound(symbolName) => `${symbolName} is not defined` - | RESyntaxError(desc, _) => `Syntax Error: ${desc}` + | RESyntaxError(desc) => `Syntax Error: ${desc}` | RETodo(msg) => `TODO: ${msg}` | REExpectedType(typeName, valueString) => `Expected type: ${typeName} but got: ${valueString}` | REUnitNotFound(unitName) => `Unit not found: ${unitName}` @@ -106,6 +102,11 @@ let fromMessageWithFrameStack = (message: Message.t, frameStack: Reducer_FrameSt let fromMessage = (message: Message.t) => fromMessageWithFrameStack(message, Reducer_FrameStack.make()) +let fromParseError = (SyntaxError(message, location): Reducer_Peggy_Parse.parseError) => + RESyntaxError(message)->fromMessageWithFrameStack( + Reducer_FrameStack.makeSingleFrameStack(location), + ) + @genType let getTopFrame = (t: t): option => t.frameStack->Reducer_FrameStack.getTopFrame