zevv / npeg

PEGs for Nim, another take
MIT License
330 stars 22 forks source link

RFC: Unicode concat operator #58

Closed dxxb closed 1 year ago

dxxb commented 1 year ago

I personally find using '' for concatenation visually "noisy". I understand an operator with the right precedence is required and so I went looking for an alternative that would not look like zero or more ''. This PR implements support for \bullet () because it is listed among Unicode operators here https://nim-lang.org/docs/manual.html#lexical-analysis-unicode-operators. I also tried adding \cdot (), which seems more appropriate because it is normally used as concatenation symbol in math script, but it currently won't work.

Not sure if this is something you would be interested in merging but it works and maybe someone else will find it useful.

zevv commented 1 year ago

Neat idea, I never considered using operators that are not on my keyboard, but it indeed is nice to reduce the noise and avoid confusion with the zero-or-more Kleene star. Not sure if the specific unicode handing is needed though, this works just fine for me?

--- a/src/npeg/parsepatt.nim
+++ b/src/npeg/parsepatt.nim
@@ -115,6 +115,7 @@ proc parsePatt*(pattName: string, nn: NimNode, grammar: Grammar, dot: Dot = nil)

       of nnkInfix:
         case n[0].strVal:
+          of "∙": result = aux(n[1]) * aux(n[2])
           of "*": result = aux(n[1]) * aux(n[2])
           of "-": result = aux(n[1]) - aux(n[2])
           of "^": result = newPattAssoc(aux(n[1]), intVal(n[2]), assocLeft)
dxxb commented 1 year ago

Not sure if the specific unicode handing is needed though, this works just fine for me?

You are correct. I just tested it and the unicode specific dance is not required. I will update the PR accordingly.

zevv commented 1 year ago

Thanks.

dxxb commented 1 year ago

Thank you!

zevv commented 1 year ago

Just added to tests and README as well. I'll spin a release shortly (Waiting for an update on the last open PR)