From 8b945bbc9286199c06a2e20a91ab26e6c9ce0fd1 Mon Sep 17 00:00:00 2001 From: Jamie Willis Date: Tue, 9 Apr 2024 16:31:21 +0100 Subject: [PATCH] renamed format to build and added lineNum to the lineInfo --- .../parsley/errors/DefaultErrorBuilder.scala | 14 ++++---- .../scala/parsley/errors/ErrorBuilder.scala | 36 ++++++++++--------- .../parsley/internal/errors/ParseError.scala | 6 ++-- .../src/test/scala/parsley/ParsleyTest.scala | 4 +-- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/parsley/shared/src/main/scala/parsley/errors/DefaultErrorBuilder.scala b/parsley/shared/src/main/scala/parsley/errors/DefaultErrorBuilder.scala index 9a05118f9..07bc93093 100644 --- a/parsley/shared/src/main/scala/parsley/errors/DefaultErrorBuilder.scala +++ b/parsley/shared/src/main/scala/parsley/errors/DefaultErrorBuilder.scala @@ -5,6 +5,8 @@ */ package parsley.errors +import scala.annotation.unused + // Turn coverage off, because the tests have their own error builder // We might want to test this on its own though // $COVERAGE-OFF$ @@ -21,9 +23,9 @@ package parsley.errors */ abstract class DefaultErrorBuilder extends ErrorBuilder[String] { /** @inheritdoc */ - override def format(pos: Position, source: Source, lines: ErrorInfoLines): String = DefaultErrorBuilder.format(pos, source, lines) + override def build(pos: Position, source: Source, lines: ErrorInfoLines): String = DefaultErrorBuilder.build(pos, source, lines) - //override def format(pos: Position, source: Source, ctxs: NestedContexts, lines: ErrorInfoLines): String = { + //override def build(pos: Position, source: Source, ctxs: NestedContexts, lines: ErrorInfoLines): String = { // DefaultErrorBuilder.blockError(header = s"${DefaultErrorBuilder.mergeScopes(source, ctxs)}$pos", lines, indent = 2)" //} @@ -81,8 +83,8 @@ abstract class DefaultErrorBuilder extends ErrorBuilder[String] { /** @inheritdoc */ override val numLinesAfter = DefaultErrorBuilder.NumLinesAfter /** @inheritdoc */ - override def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], errorPointsAt: Int, errorWidth: Int): LineInfo = { - DefaultErrorBuilder.lineInfo(line, linesBefore, linesAfter, errorPointsAt, errorWidth) + override def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], lineNum: Int, errorPointsAt: Int, errorWidth: Int): LineInfo = { + DefaultErrorBuilder.lineInfo(line, linesBefore, linesAfter, lineNum, errorPointsAt, errorWidth) } /** @inheritdoc */ @@ -117,7 +119,7 @@ object DefaultErrorBuilder { * * @since 4.3.0 */ - def format(pos: String, source: Option[String], lines: Seq[String]): String = { + def build(pos: String, source: Option[String], lines: Seq[String]): String = { blockError(header = s"${source.fold("")(name => s"In $name ")}$pos", lines, indent = 2) } /** If the `sourceName` exists, wraps it in quotes and adds `file` onto the front. @@ -229,7 +231,7 @@ object DefaultErrorBuilder { * * @since 4.3.0 */ - def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], errorPointsAt: Int, errorWidth: Int): Seq[String] = { + def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], @unused lineNum: Int, errorPointsAt: Int, errorWidth: Int): Seq[String] = { Seq.concat(linesBefore.map(inputLine), Seq(inputLine(line), caretLine(errorPointsAt, errorWidth)), linesAfter.map(inputLine)) } diff --git a/parsley/shared/src/main/scala/parsley/errors/ErrorBuilder.scala b/parsley/shared/src/main/scala/parsley/errors/ErrorBuilder.scala index ebb17e507..f9eb27c25 100644 --- a/parsley/shared/src/main/scala/parsley/errors/ErrorBuilder.scala +++ b/parsley/shared/src/main/scala/parsley/errors/ErrorBuilder.scala @@ -5,7 +5,7 @@ */ package parsley.errors -/** This typeclass specifies how to format an error from a parser +/** This typeclass specifies how to construct an error from a parser * as a specified type. * * An instance of this trait is required when calling `parse` @@ -45,7 +45,7 @@ package parsley.errors * There can be at most one unexpected line, at most one expected line, and zero or more reasons. * Both of the unexpected and expected info are built up of ''error items'', which are either: * the end of input, a named token, raw input taken from the parser definition. These can all be - * formatted separately. + * constructed separately. * * The overall structure of a ''Vanilla'' error is given in the following diagram: * {{{ @@ -118,10 +118,10 @@ package parsley.errors * @group formatting * * @groupprio outer 0 - * @groupname outer Top-Level Formatting + * @groupname outer Top-Level Construction * @groupdesc outer - * These methods help assembly the final products of the error messages. The `format` method will return the desired `Err` types, - * whereas `specializedError` and `vanillaError` both assemble an `ErrorInfoLines` that the `format` method can consume. + * These methods help assembly the final products of the error messages. The `build` method will return the desired `Err` types, + * whereas `specializedError` and `vanillaError` both assemble an `ErrorInfoLines` that the `build` method can consume. * * @groupprio contextlines 10 * @groupname contextlines Contextual Input Lines @@ -132,13 +132,13 @@ package parsley.errors * @groupprio preamble 5 * @groupname preamble Error Preamble * @groupdesc preamble - * These methods control the formatting of the preamble of an error message, which is the position and source info. - * These are then consumed by `format` itself. + * These methods control the construction of the preamble of an error message, which is the position and source info. + * These are then consumed by `build` itself. * * @groupprio item 18 * @groupname item Error Items * @groupdesc item - * These methods control how error items within ''Vanilla'' errors are formatted. These are either + * These methods control how error items within ''Vanilla'' errors are constructed. These are either * the end of input, a named label generated by the [[combinator.ErrorMethods.label `label`]] combinator, * or a raw piece of input intrinsically associated with a combinator. * @@ -146,13 +146,13 @@ package parsley.errors * @groupname vanilla Vanilla-Specific Components * @groupdesc vanilla * These methods control the ''Vanilla''-specific error components, namely how expected error items - * should be combined, how to format the unexpected line, and how to format reasons generated from + * should be combined, how to represent the unexpected line, and how to represent reasons generated from * [[combinator.ErrorMethods.explain `explain`]]. * * @groupprio spec 15 * @groupname spec Specialized-Specific Components * @groupdesc spec - * These methods control the ''Specialized''-specific components, namely the formatting of a bespoke + * These methods control the ''Specialized''-specific components, namely the construction of a bespoke * error message. * * @groupprio shared 12 @@ -163,7 +163,7 @@ package parsley.errors * to form a unified block of content lines. */ trait ErrorBuilder[+Err] { - /* This is the top level function, which finally compiles all the formatted + /* This is the top level function, which finally compiles all the built * sub-parts into a finished value of type `Err`. * * @param pos this is the representation of the position of the error in the input (see the [[pos `pos`]] method). @@ -174,9 +174,9 @@ trait ErrorBuilder[+Err] { * @since 5.0.0 * @inheritdoc */ - //def format(pos: Position, source: Source, ctxs: NestedContexts, lines: ErrorInfoLines): Err + //def build(pos: Position, source: Source, ctxs: NestedContexts, lines: ErrorInfoLines): Err - /** This is the top level function, which finally compiles all the formatted + /** This is the top level function, which finally compiles all the built * sub-parts into a finished value of type `Err`. * * @param pos this is the representation of the position of the error in the input (see the [[pos `pos`]] method). @@ -186,7 +186,7 @@ trait ErrorBuilder[+Err] { * @since 3.0.0 * @group outer */ - def format(pos: Position, source: Source, lines: ErrorInfoLines): Err + def build(pos: Position, source: Source, lines: ErrorInfoLines): Err /** The representation type of position information within the generated message. * @@ -357,17 +357,19 @@ trait ErrorBuilder[+Err] { * @group spec */ def message(msg: String): Message - /** Describes how to format the information about the line that + /** Describes how to represent the information about the line that * the error occured on. * * @param line the full line of input that produced this error message. * @param linesBefore the lines of input just before the one that produced this message (up to [[numLinesBefore `numLinesBefore`]]). * @param linesAfter the lines of input just after the one that produced this message (up to [[numLinesAfter `numLinesAfter`]]). + * @param lineNum the line number of the error message. * @param errorPointsAt the offset into the line that the error points at. - * @since 3.1.0 + * @param errorWidth how wide the caret in the message should be. + * @since 5.0.0 * @group contextlines */ - def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], errorPointsAt: Int, errorWidth: Int): LineInfo + def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], lineNum: Int, errorPointsAt: Int, errorWidth: Int): LineInfo /** The number of lines of input to request before an error occured. * @since 3.1.0 diff --git a/parsley/shared/src/main/scala/parsley/internal/errors/ParseError.scala b/parsley/shared/src/main/scala/parsley/internal/errors/ParseError.scala index 329a7314d..1766a6da0 100644 --- a/parsley/shared/src/main/scala/parsley/internal/errors/ParseError.scala +++ b/parsley/shared/src/main/scala/parsley/internal/errors/ParseError.scala @@ -19,7 +19,7 @@ private [internal] sealed trait ParseError { val beforeLines = helper.getLinesBefore(offset, builder.numLinesBefore) val afterLines = helper.getLinesAfter(offset, builder.numLinesAfter) val lines = format(errLine, beforeLines, afterLines, caret) - builder.format(builder.pos(line, col), builder.source(sourceName), lines) + builder.build(builder.pos(line, col), builder.source(sourceName), lines) } } // The reasons here are lightweight, two errors can merge their messages, but messages do not get converted to hints @@ -37,13 +37,13 @@ private [internal] case class TrivialError(offset: Int, line: Int, col: Int, builder.combineMessages(reasons.map(builder.reason(_)).toSeq), // this was changed to +1 to allow EoF caret, does this need more nuance? // FIXME: this should use the number of codepoints and not length - builder.lineInfo(line, beforeLines, afterLines, caret, math.min(caretSize, line.length - caret + 1))) + builder.lineInfo(line, beforeLines, afterLines, this.line, caret, math.min(caretSize, line.length - caret + 1))) } } private [internal] case class FancyError(offset: Int, line: Int, col: Int, msgs: List[String], caretWidth: Int) extends ParseError { def format(line: String, beforeLines: List[String], afterLines: List[String], caret: Int)(implicit builder: ErrorBuilder[_]): builder.ErrorInfoLines = { builder.specializedError( builder.combineMessages(msgs.map(builder.message(_))), - builder.lineInfo(line, beforeLines, afterLines, caret, math.min(caretWidth, line.length - caret + 1))) + builder.lineInfo(line, beforeLines, afterLines, this.line, caret, math.min(caretWidth, line.length - caret + 1))) } } diff --git a/parsley/shared/src/test/scala/parsley/ParsleyTest.scala b/parsley/shared/src/test/scala/parsley/ParsleyTest.scala index 0b2e7ca28..d75d5acc2 100644 --- a/parsley/shared/src/test/scala/parsley/ParsleyTest.scala +++ b/parsley/shared/src/test/scala/parsley/ParsleyTest.scala @@ -26,7 +26,7 @@ case class Named(item: String) extends TestErrorItem case object EndOfInput extends TestErrorItem abstract class TestErrorBuilder extends ErrorBuilder[TestError] { - override def format(pos: Position, source: Source, lines: ErrorInfoLines): TestError = TestError(pos, lines) + override def build(pos: Position, source: Source, lines: ErrorInfoLines): TestError = TestError(pos, lines) type Position = (Int, Int) override def pos(line: Int, col: Int): Position = (line, col) @@ -56,7 +56,7 @@ abstract class TestErrorBuilder extends ErrorBuilder[TestError] { override def message(msg: String): Message = msg type LineInfo = Int - override def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], errorPointsAt: Int, errorWidth: Int): Int = errorWidth + override def lineInfo(line: String, linesBefore: Seq[String], linesAfter: Seq[String], lineNum: Int, errorPointsAt: Int, errorWidth: Int): Int = errorWidth override val numLinesBefore: Int = 2 override val numLinesAfter: Int = 2