Skip to content

Commit

Permalink
Added new labelSymbol configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jan 29, 2024
1 parent 987717a commit 42c0f02
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -731,91 +741,106 @@ 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.
* @since 4.1.0
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 42c0f02

Please sign in to comment.