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