Skip to content

Commit

Permalink
renamed format to build and added lineNum to the lineInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Apr 9, 2024
1 parent d6f5db0 commit 8b945bb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand All @@ -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)"
//}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}

Expand Down
36 changes: 19 additions & 17 deletions parsley/shared/src/main/scala/parsley/errors/ErrorBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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:
* {{{
Expand Down Expand Up @@ -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
Expand All @@ -132,27 +132,27 @@ 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.
*
* @groupprio vanilla 16
* @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
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))
}
}
4 changes: 2 additions & 2 deletions parsley/shared/src/test/scala/parsley/ParsleyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8b945bb

Please sign in to comment.