typst icon indicating copy to clipboard operation
typst copied to clipboard

Arithmetic operator parsing inconsistency

Open Mc-Zen opened this issue 2 years ago • 4 comments

Just a very minor bug/inconsistency but it may be noteworthy.

The - operator apparently needs a space before it when it is preceded by an identifier while the + operator does not. E.g.

#{
  let a = 2;
  a = a+1;   // fine
  a = 1-a;   // fine
  a = 2-3;   // fine
  a = a -1;  // finer
  a = a-1;   // error
}

Mc-Zen avatar Apr 18 '23 12:04 Mc-Zen

This is because that currently a- will be treated as an identifier.

See https://github.com/typst/typst/blob/bce83d330f0efc0f8cdd6fb01d8244cb9f01ac27/src/syntax/lexer.rs#L727

I think that - should stop being accepted as identifier continuations, but I'm interested in knowing whether this came from a request or was just there from the beginning.

(I noticed that the project is currently using unicode-xid, perhaps typst should use dtolnay's crate for better performance? https://github.com/dtolnay/unicode-ident)

fee1-dead avatar Apr 18 '23 12:04 fee1-dead

Oh I see. Well, it is not really a problem but at first it is surprising and maybe a dedicated error message could be shown suggesting the solution in such a case.

Mc-Zen avatar Apr 18 '23 13:04 Mc-Zen

Wait, I thought it was considered idiomatic to name variables, just as in Closure, with dashes:

let a-b = 7

Is this not the case? (This likely ties into #872).

leo848 avatar Apr 19 '23 14:04 leo848

I thought it was considered idiomatic to name variables, just as in Closure, with dashes

It is. It would still be nice to have a helpful hint if the variable does not exist. I'm leaving this issue open to track that hint.

laurmaedje avatar Apr 20 '23 07:04 laurmaedje