From 42c0f02f68186413d008027f1ba16802f443973d Mon Sep 17 00:00:00 2001 From: Jamie Willis Date: Mon, 29 Jan 2024 21:10:26 +0000 Subject: [PATCH] Added new labelSymbol configuration --- .../parsley/token/errors/ErrorConfig.scala | 25 +++++++++++++++++++ .../parsley/token/symbol/ConcreteSymbol.scala | 10 ++++---- .../scala/parsley/token/symbol/Symbol.scala | 6 +++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/parsley/shared/src/main/scala/parsley/token/errors/ErrorConfig.scala b/parsley/shared/src/main/scala/parsley/token/errors/ErrorConfig.scala index ba8fbb408..d36b61ea3 100644 --- a/parsley/shared/src/main/scala/parsley/token/errors/ErrorConfig.scala +++ b/parsley/shared/src/main/scala/parsley/token/errors/ErrorConfig.scala @@ -721,6 +721,16 @@ class ErrorConfig { */ def verifiedStringBadCharsUsedInLiteral: VerifiedBadChars = Unverified + /** Gives names and/or reasons to symbols. + * + * Symbols that do not appear in the map are assumed to be `NotConfigured`. + * + * @since 5.0.0 + * @note defaults to the empty map + * @group symbol + */ + def labelSymbol: Map[String, LabelWithExplainConfig] = Map.empty + /** Gives names to punctuation if it is otherwise unspecified. * * Symbols that do not appear in the map are unlabelled. When a symbol @@ -731,6 +741,7 @@ class ErrorConfig { * @note defaults to the empty map * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolPunctuation: Map[String, Option[String]] = Map.empty /** How to refer to a `;` symbol in an error message. @@ -738,84 +749,98 @@ class ErrorConfig { * @note defaults to "semicolon" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolSemi: LabelConfig = Label("semicolon") /** How to refer to a `,` symbol in an error message. * @since 4.1.0 * @note defaults to "comma" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolComma: LabelConfig = Label("comma") /** How to refer to a `:` symbol in an error message. * @since 4.1.0 * @note defaults to "colon" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolColon: LabelConfig = Label("colon") /** How to refer to a `.` symbol in an error message. * @since 4.1.0 * @note defaults to "dot" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolDot: LabelConfig = Label("dot") /** How to refer to a `(` symbol in an error message. * @since 4.1.0 * @note defaults to "open parenthesis" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolOpenParen: LabelConfig = Label("open parenthesis") /** How to refer to a `{` symbol in an error message. * @since 4.1.0 * @note defaults to "open brace" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolOpenBrace: LabelConfig = Label("open brace") /** How to refer to a `[` symbol in an error message. * @since 4.1.0 * @note defaults to "open square bracket" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolOpenSquare: LabelConfig = Label("open square bracket") /** How to refer to a `<` symbol in an error message. * @since 4.1.0 * @note defaults to "open angle bracket" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolOpenAngle: LabelConfig = Label("open angle bracket") /** How to refer to a `)` symbol in an error message. * @since 4.1.0 * @note defaults to "closing parenthesis" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolClosingParen: LabelConfig = Label("closing parenthesis") /** How to refer to a `}` symbol in an error message. * @since 4.1.0 * @note defaults to "closing brace" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolClosingBrace: LabelConfig = Label("closing brace") /** How to refer to a `]` symbol in an error message. * @since 4.1.0 * @note defaults to "closing square bracket" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolClosingSquare: LabelConfig = Label("closing square bracket") /** How to refer to a `>` symbol in an error message. * @since 4.1.0 * @note defaults to "closing angle bracket" * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolClosingAngle: LabelConfig = Label("closing angle bracket") /** How a given keyword should be described in an error message. * @since 4.1.0 * @note defaults to labelling with the symbol itself * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolKeyword(symbol: String): LabelWithExplainConfig = Label(symbol) /** How a given operator should be described in an error message. * @since 4.1.0 * @note defaults to labelling with the symbol itself * @group symbol */ + @deprecated("This will be removed in favour of `labelSymbol` in a future milestone", "5.0.0-M3") def labelSymbolOperator(symbol: String): LabelWithExplainConfig = Label(symbol) /** How the required end of a given keyword should be specified in an error. * @since 4.1.0 diff --git a/parsley/shared/src/main/scala/parsley/token/symbol/ConcreteSymbol.scala b/parsley/shared/src/main/scala/parsley/token/symbol/ConcreteSymbol.scala index 442960cbf..f66f71dbf 100644 --- a/parsley/shared/src/main/scala/parsley/token/symbol/ConcreteSymbol.scala +++ b/parsley/shared/src/main/scala/parsley/token/symbol/ConcreteSymbol.scala @@ -8,7 +8,7 @@ package parsley.token.symbol import parsley.Parsley, Parsley.atomic import parsley.character.{char, string} import parsley.token.descriptions.{NameDesc, SymbolDesc} -import parsley.token.errors.ErrorConfig +import parsley.token.errors.{ErrorConfig, NotConfigured} import parsley.internal.deepembedding.singletons.token @@ -24,20 +24,20 @@ private [token] class ConcreteSymbol(nameDesc: NameDesc, symbolDesc: SymbolDesc, require(name.nonEmpty, "Symbols may not be empty strings") if (symbolDesc.hardKeywords(name)) softKeyword(name) else if (symbolDesc.hardOperators(name)) softOperator(name) - else atomic(string(name)).void + else err.labelSymbol.getOrElse(name, NotConfigured)(atomic(string(name)).void) } - override def apply(name: Char): Parsley[Unit] = char(name).void + override def apply(name: Char): Parsley[Unit] = err.labelSymbol.getOrElse(name.toString, NotConfigured)(char(name).void) override def softKeyword(name: String): Parsley[Unit] = { require(name.nonEmpty, "Keywords may not be empty strings") new Parsley(new token.SoftKeyword(name, nameDesc.identifierLetter, symbolDesc.caseSensitive, - err.labelSymbolKeyword(name), err.labelSymbolEndOfKeyword(name))) + err.labelSymbol.getOrElse(name, err.labelSymbolKeyword(name)), err.labelSymbolEndOfKeyword(name))) } override def softOperator(name: String): Parsley[Unit] = { require(name.nonEmpty, "Operators may not be empty strings") new Parsley(new token.SoftOperator(name, nameDesc.operatorLetter, symbolDesc.hardOperatorsTrie, - err.labelSymbolOperator(name), err.labelSymbolEndOfOperator(name))) + err.labelSymbol.getOrElse(name, err.labelSymbolOperator(name)), err.labelSymbolEndOfOperator(name))) } } diff --git a/parsley/shared/src/main/scala/parsley/token/symbol/Symbol.scala b/parsley/shared/src/main/scala/parsley/token/symbol/Symbol.scala index 3c00c380b..9b01f929c 100644 --- a/parsley/shared/src/main/scala/parsley/token/symbol/Symbol.scala +++ b/parsley/shared/src/main/scala/parsley/token/symbol/Symbol.scala @@ -7,7 +7,7 @@ package parsley.token.symbol import parsley.Parsley import parsley.errors.combinator.ErrorMethods -import parsley.token.errors.{ErrorConfig, LabelConfig} +import parsley.token.errors.{ErrorConfig, LabelWithExplainConfig} /** This class provides implicit functionality to promote string * literals into tokens. @@ -136,7 +136,9 @@ abstract class Symbol private[symbol] (err: ErrorConfig) { // $COVERAGE-OFF$ // These really don't need testing - private final def apply(name: Char, label: LabelConfig): Parsley[Unit] = label(apply(name)) + private final def apply(name: Char, label: LabelWithExplainConfig): Parsley[Unit] = { + err.labelSymbol.getOrElse(name.toString, label)(apply(name)) //remove this later, it should be handled inside + } /** This parser parses a semicolon `;` as a symbol. * * @since 4.0.0