rems-project / sail

Sail architecture definition language
Other
587 stars 102 forks source link

Operator's rule makes its parsing disjointed with comment #627

Open trdthg opened 2 months ago

trdthg commented 2 months ago

I find it strange how sail currently handles operator + comment forms like ==/**/ or =/**/.

It seems the issue is caused by the operatorn rule in lexel.mll. To debug this, I added some print statements inside:

  | operator as op { 
      Printf.printf "%s\n" op;
      OpId op }

When I format the following code:

function f () = {
    if a ==/**/ 1 then { 1 };

    let b =/**/ 1
}

Here is the output:

$ ./sail -fmt ./test/format/demo.sail 
Syntax error:
./test/format/demo.sail:3.10-10:
3 |    let b =/**/ 1
  |          ^
  | current token: =/**/
==/**/
=/**/

Comments are treated as part of the operator (although they are valid operators in the documentation?).

if this behavior is valid, then we should fix the error? if not, is there a way to separate the comment? any advice?

Alasdair commented 2 months ago

Hmm, well spotted! The lexer forbids operators starting with either // or /*, but those characters are generally allowed within operators. It might be better if we were to forbid // and /* anywhere within an operator, assuming there is a nice way to do that in ocamllex