move location to Reducer_Peggy_Parse, separate parse errors
This commit is contained in:
parent
41574e08c9
commit
111dd5535c
|
@ -13,7 +13,7 @@ let expectParseToBe = (expr, answer) =>
|
|||
|
||||
let testParse = (expr, answer) => test(expr, () => expectParseToBe(expr, answer))
|
||||
|
||||
let expectToExpressionToBe = (expr, answer, ~v="_", ()) => {
|
||||
let expectExpressionToBe = (expr, answer, ~v="_", ()) => {
|
||||
let rExpr = Parse.parse(expr, "test")->Result.map(ToExpression.fromNode)
|
||||
let a1 = rExpr->ExpressionT.toStringResultOkless
|
||||
|
||||
|
@ -22,7 +22,9 @@ let expectToExpressionToBe = (expr, answer, ~v="_", ()) => {
|
|||
} else {
|
||||
let a2 =
|
||||
rExpr
|
||||
->E.R2.errMap(e => e->Reducer_ErrorValue.attachEmptyStackTraceToErrorValue)
|
||||
->E.R2.errMap(e =>
|
||||
e->Reducer_ErrorValue.fromParseError->Reducer_ErrorValue.attachEmptyStackTraceToErrorValue
|
||||
)
|
||||
->Result.flatMap(expr => Expression.BackCompatible.evaluate(expr))
|
||||
->Reducer_Value.toStringResultOkless
|
||||
(a1, a2)->expect->toEqual((answer, v))
|
||||
|
@ -30,16 +32,16 @@ let expectToExpressionToBe = (expr, answer, ~v="_", ()) => {
|
|||
}
|
||||
|
||||
let testToExpression = (expr, answer, ~v="_", ()) =>
|
||||
test(expr, () => expectToExpressionToBe(expr, answer, ~v, ()))
|
||||
test(expr, () => expectExpressionToBe(expr, answer, ~v, ()))
|
||||
|
||||
module MyOnly = {
|
||||
let testParse = (expr, answer) => Only.test(expr, () => expectParseToBe(expr, answer))
|
||||
let testToExpression = (expr, answer, ~v="_", ()) =>
|
||||
Only.test(expr, () => expectToExpressionToBe(expr, answer, ~v, ()))
|
||||
Only.test(expr, () => expectExpressionToBe(expr, answer, ~v, ()))
|
||||
}
|
||||
|
||||
module MySkip = {
|
||||
let testParse = (expr, answer) => Skip.test(expr, () => expectParseToBe(expr, answer))
|
||||
let testToExpression = (expr, answer, ~v="_", ()) =>
|
||||
Skip.test(expr, () => expectToExpressionToBe(expr, answer, ~v, ()))
|
||||
Skip.test(expr, () => expectExpressionToBe(expr, answer, ~v, ()))
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
@genType type reducerError = Reducer_ErrorValue.error //alias
|
||||
@genType type reducerErrorValue = Reducer_ErrorValue.errorValue //alias
|
||||
@genType type location = Reducer_ErrorValue.location //alias
|
||||
@genType type location = Reducer_Peggy_Parse.location //alias
|
||||
|
||||
@genType
|
||||
let toString = (e: reducerError): string => Reducer_ErrorValue.errorToString(e)
|
||||
|
|
|
@ -1,16 +1,4 @@
|
|||
// Do not gentype this, use LocationRange from peggy types instead
|
||||
// TODO - rename locationPoint -> location, location -> locationRange to match peggy
|
||||
@genType
|
||||
type locationPoint = {
|
||||
line: int,
|
||||
column: int,
|
||||
}
|
||||
@genType
|
||||
type location = {
|
||||
source: string,
|
||||
start: locationPoint,
|
||||
end: locationPoint,
|
||||
}
|
||||
type location = Reducer_Peggy_Parse.location
|
||||
|
||||
@genType.opaque
|
||||
type errorValue =
|
||||
|
@ -51,6 +39,10 @@ type error = {
|
|||
|
||||
exception ExceptionWithStackTrace(error)
|
||||
|
||||
let fromParseError = (
|
||||
SyntaxError(message, location): Reducer_Peggy_Parse.parseError,
|
||||
) => RESyntaxError(message, location->Some)
|
||||
|
||||
let errorValueToString = (err: errorValue) =>
|
||||
switch err {
|
||||
| REArityError(_oFnName, arity, usedArity) =>
|
||||
|
|
|
@ -125,7 +125,7 @@ let rec evaluate: T.reducerFn = (expression, context): (T.value, T.context) => {
|
|||
module BackCompatible = {
|
||||
// Those methods are used to support the existing tests
|
||||
// If they are used outside limited testing context, error location reporting will fail
|
||||
let parse = (peggyCode: string): result<T.expression, errorValue> =>
|
||||
let parse = (peggyCode: string): result<T.expression, Reducer_Peggy_Parse.parseError> =>
|
||||
peggyCode->Reducer_Peggy_Parse.parse("main")->Result.map(Reducer_Peggy_ToExpression.fromNode)
|
||||
|
||||
let evaluate = (expression: T.expression): result<T.value, Reducer_ErrorValue.error> => {
|
||||
|
@ -144,6 +144,8 @@ module BackCompatible = {
|
|||
|
||||
let evaluateString = (peggyCode: string): result<T.value, Reducer_ErrorValue.error> =>
|
||||
parse(peggyCode)
|
||||
->E.R2.errMap(e => e->Reducer_ErrorValue.attachEmptyStackTraceToErrorValue)
|
||||
->E.R2.errMap(e =>
|
||||
e->Reducer_ErrorValue.fromParseError->Reducer_ErrorValue.attachEmptyStackTraceToErrorValue
|
||||
)
|
||||
->Result.flatMap(evaluate)
|
||||
}
|
||||
|
|
|
@ -31,30 +31,16 @@ let rec toString = (expression: t) =>
|
|||
let toStringResult = codeResult =>
|
||||
switch codeResult {
|
||||
| Ok(a) => `Ok(${toString(a)})`
|
||||
| Error(m) => `Error(${Reducer_ErrorValue.errorValueToString(m)})`
|
||||
| Error(m) => `Error(${Reducer_Peggy_Parse.toStringError(m)})`
|
||||
}
|
||||
|
||||
let toStringResultOkless = codeResult =>
|
||||
switch codeResult {
|
||||
| Ok(a) => toString(a)
|
||||
| Error(m) => `Error(${Reducer_ErrorValue.errorValueToString(m)})`
|
||||
| Error(m) => `Error(${Reducer_Peggy_Parse.toStringError(m)})`
|
||||
}
|
||||
|
||||
let inspect = (expr: t): t => {
|
||||
Js.log(toString(expr))
|
||||
expr
|
||||
}
|
||||
|
||||
let inspectResult = (r: result<t, Reducer_ErrorValue.errorValue>): result<
|
||||
t,
|
||||
Reducer_ErrorValue.errorValue,
|
||||
> => {
|
||||
Js.log(toStringResult(r))
|
||||
r
|
||||
}
|
||||
|
||||
let resultToValue = (rExpression: result<t, Reducer_ErrorValue.t>): t =>
|
||||
switch rExpression {
|
||||
| Ok(expression) => expression
|
||||
| Error(errorValue) => Reducer_ErrorValue.toException(errorValue)
|
||||
}
|
||||
|
|
|
@ -1,23 +1,39 @@
|
|||
module Extra = Reducer_Extra
|
||||
open Reducer_ErrorValue
|
||||
|
||||
type node = {"type": string, "location": Reducer_ErrorValue.location}
|
||||
// Do not gentype this, use LocationRange from peggy types instead
|
||||
// TODO - rename locationPoint -> location, location -> locationRange to match peggy
|
||||
@genType
|
||||
type locationPoint = {
|
||||
line: int,
|
||||
column: int,
|
||||
}
|
||||
@genType
|
||||
type location = {
|
||||
source: string,
|
||||
start: locationPoint,
|
||||
end: locationPoint,
|
||||
}
|
||||
|
||||
type node = {"type": string, "location": location}
|
||||
|
||||
type parseError = SyntaxError(string, location)
|
||||
|
||||
type parseResult = result<node, parseError>
|
||||
|
||||
@module("./Reducer_Peggy_GeneratedParser.js")
|
||||
external parse__: (string, {"grammarSource": string}) => node = "parse"
|
||||
|
||||
type withLocation = {"location": Reducer_ErrorValue.location}
|
||||
type withLocation = {"location": location}
|
||||
external castWithLocation: Js.Exn.t => withLocation = "%identity"
|
||||
|
||||
let syntaxErrorToLocation = (error: Js.Exn.t): Reducer_ErrorValue.location =>
|
||||
castWithLocation(error)["location"]
|
||||
let syntaxErrorToLocation = (error: Js.Exn.t): location => castWithLocation(error)["location"]
|
||||
|
||||
let parse = (expr: string, source: string): result<node, errorValue> =>
|
||||
let parse = (expr: string, source: string): parseResult =>
|
||||
try {
|
||||
Ok(parse__(expr, {"grammarSource": source}))
|
||||
} catch {
|
||||
| Js.Exn.Error(obj) =>
|
||||
RESyntaxError(Belt.Option.getExn(Js.Exn.message(obj)), syntaxErrorToLocation(obj)->Some)->Error
|
||||
SyntaxError(Belt.Option.getExn(Js.Exn.message(obj)), syntaxErrorToLocation(obj))->Error
|
||||
}
|
||||
|
||||
type nodeBlock = {...node, "statements": array<node>}
|
||||
|
@ -154,8 +170,13 @@ let rec pgToString = (ast: ast): string => {
|
|||
}
|
||||
and toString = (node: node): string => node->nodeToAST->pgToString
|
||||
|
||||
let toStringResult = (rNode: result<node, errorValue>): string =>
|
||||
let toStringError = (error: parseError): string => {
|
||||
let SyntaxError(message, _) = error
|
||||
`Syntax Error: ${message}}`
|
||||
}
|
||||
|
||||
let toStringResult = (rNode: parseResult): string =>
|
||||
switch rNode {
|
||||
| Ok(node) => toString(node)
|
||||
| Error(error) => `Error(${errorValueToString(error)})`
|
||||
| Ok(node) => node->toString
|
||||
| Error(error) => `Error(${error->toStringError})`
|
||||
}
|
||||
|
|
|
@ -152,7 +152,10 @@ let parseIncludes = (this: t): t => {
|
|||
}
|
||||
}
|
||||
let doRawParse = (this: t): T.rawParseArgumentType =>
|
||||
this->getSource->Reducer_Peggy_Parse.parse(this.sourceId)
|
||||
this
|
||||
->getSource
|
||||
->Reducer_Peggy_Parse.parse(this.sourceId)
|
||||
->E.R2.errMap(Reducer_ErrorValue.fromParseError)
|
||||
|
||||
let rawParse = (this: t): t =>
|
||||
this->getRawParse->E.O2.defaultFn(() => doRawParse(this))->setRawParse(this, _)
|
||||
|
|
Loading…
Reference in New Issue
Block a user