From b526debbf87c29613bfe802d5ec6e0e653f4b5d1 Mon Sep 17 00:00:00 2001 From: Jamie Willis Date: Sun, 28 Apr 2024 16:57:31 +0100 Subject: [PATCH] improved symbol naming --- parsley/shared/src/main/scala/parsley/combinator.scala | 6 +++++- .../shared/src/main/scala/parsley/syntax/character.scala | 4 ++-- .../main/scala/parsley/token/symbol/ConcreteSymbol.scala | 8 ++++---- .../src/main/scala/parsley/token/symbol/Symbol.scala | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/parsley/shared/src/main/scala/parsley/combinator.scala b/parsley/shared/src/main/scala/parsley/combinator.scala index a196b74c7..36bcbe628 100644 --- a/parsley/shared/src/main/scala/parsley/combinator.scala +++ b/parsley/shared/src/main/scala/parsley/combinator.scala @@ -99,7 +99,11 @@ private [parsley] trait combinator { * @group multi * @see [[parsley.Parsley.<|> `<|>`]] */ - final def choice[A](ps: Parsley[A]*): Parsley[A] = ps.reduceRightOption(_ |: _).getOrElse(empty(0)).uo("choice") + final def choice[A](ps: Parsley[A]*): Parsley[A] = ps match { + case ps if ps.isEmpty => empty + case Seq(p) => p + case ps => ps.reduceRight(_ |: _).uo("choice") + } // This combinator is still used in internal testing, but is a trap for new users // it will not be exposed in the API again. diff --git a/parsley/shared/src/main/scala/parsley/syntax/character.scala b/parsley/shared/src/main/scala/parsley/syntax/character.scala index f0acf0b51..7ba969cc0 100644 --- a/parsley/shared/src/main/scala/parsley/syntax/character.scala +++ b/parsley/shared/src/main/scala/parsley/syntax/character.scala @@ -26,7 +26,7 @@ object character { * * @see [[parsley.character.string `character.string`]] */ - @inline implicit def stringLift(str: String): Parsley[String] = string(str) + @inline implicit def stringLift(str: String): Parsley[String] = string(str).uo(s""""$str"""") /** Converts a character literal into a parser that reads that character. * * Allows for the implicit application of the `char` combinator to a @@ -34,6 +34,6 @@ object character { * * @see [[parsley.character.char `character.char`]] */ - @inline implicit def charLift(c: Char): Parsley[Char] = char(c) + @inline implicit def charLift(c: Char): Parsley[Char] = char(c).uo(s"\'$c\'") // $COVERAGE-ON$ } 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 b9a67f593..4d178c655 100644 --- a/parsley/shared/src/main/scala/parsley/token/symbol/ConcreteSymbol.scala +++ b/parsley/shared/src/main/scala/parsley/token/symbol/ConcreteSymbol.scala @@ -23,12 +23,12 @@ private [token] class ConcreteSymbol(nameDesc: NameDesc, symbolDesc: SymbolDesc, override def apply(name: String): Parsley[Unit] = { require(name.nonEmpty, "Symbols may not be empty strings") lazy val symbolLabel = err.labelSymbol.getOrElse(name, NotConfigured).orElse(err.defaultSymbolPunctuation.config(name)) - if (symbolDesc.hardKeywords(name)) softKeyword(name) - else if (symbolDesc.hardOperators(name)) softOperator(name) - else symbolLabel(atomic(string(name)).void) + if (symbolDesc.hardKeywords(name)) softKeyword(name).uo(s"symbol($name)") + else if (symbolDesc.hardOperators(name)) softOperator(name).uo(s"symbol($name)") + else symbolLabel(atomic(string(name).ut()).ut().void.uo(s"symbol($name)")) } - override def apply(name: Char): Parsley[Unit] = err.labelSymbol.getOrElse(name.toString, NotConfigured)(char(name).void) + override def apply(name: Char): Parsley[Unit] = err.labelSymbol.getOrElse(name.toString, NotConfigured)(char(name).ut().void.uo(s"symbol($name)")) override def softKeyword(name: String): Parsley[Unit] = { require(name.nonEmpty, "Keywords may not be empty strings") 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 f4c2e98fc..c29545b7d 100644 --- a/parsley/shared/src/main/scala/parsley/token/symbol/Symbol.scala +++ b/parsley/shared/src/main/scala/parsley/token/symbol/Symbol.scala @@ -130,7 +130,7 @@ abstract class Symbol private[symbol] { */ final val implicits: ImplicitSymbol = new ImplicitSymbol { /** @inheritdoc */ - implicit def implicitSymbol(s: String): Parsley[Unit] = apply(s) + implicit def implicitSymbol(s: String): Parsley[Unit] = apply(s).uo(s""""$s"""") } // $COVERAGE-OFF$