Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lexer rule to handle operator of format like operator + comment #628

Open
wants to merge 2 commits into
base: sail2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/lib/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ let tyvar_start = '\''
(* Ensure an operator cannot start with comment openings *)
let oper_char = ['!''%''&''*''+''-''.''/'':''<''=''>''@''^''|']
let oper_char_no_slash = ['!''%''&''*''+''-''.'':''<''=''>''@''^''|']
let oper_char_no_star = ['!''%''&''+''-''.''/'':''<''=''>''@''^''|']
let oper_char_no_slash_star = ['!''%''&''+''-''.'':''<''=''>''@''^''|']
let operator1 = oper_char
let operator2 = oper_char oper_char_no_slash_star | oper_char_no_slash oper_char
let operatorn = oper_char oper_char_no_slash_star (oper_char* ('_' ident)?) | oper_char_no_slash oper_char (oper_char* ('_' ident)?) | oper_char ('_' ident)?
let operator = operator1 | operator2 | operatorn

let operator = ((oper_char_no_slash_star* ['/']? oper_char_no_slash_star*) | oper_char_no_slash*) ('_' ident)?
let escape_sequence = ('\\' ['\\''\"''\'''n''t''b''r']) | ('\\' digit digit digit) | ('\\' 'x' hexdigit hexdigit)
let lchar = [^'\n']

Expand All @@ -188,7 +187,7 @@ rule token comments = parse
token comments lexbuf }
| "@" { At }
| "2" ws "^" { TwoCaret }
| "^" { Caret }
| "^" { Caret }
| "::" { ColonColon }
| ":" { Colon ":" }
| "," { Comma }
Expand Down
96 changes: 96 additions & 0 deletions test/format/default/operator.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
default Order dec
$include <prelude.sail>

// comment
//comment
/// line_comment with more than two slash
///line_comment with more than two slash
//// line_comment with more than two slash
////line_comment with more than two slash
// /*/*/
/*/ block_comment with slash near and
*/
//* line_block_comment */
infix 4 ===_u
infix 4 ===/_u

infix 4 ===*_u
val operator ===/_u : forall 'n. (int('n), int('n)) -> bool
function operator ===/_u(x, y) = x == y

infix 4 =/
val operator =/ : forall 'n. (int('n), int('n)) -> bool
function operator =/(x, y) = x == y

infix 4 =

// comment
infix 4 </>

// comment
infix 4 =/

// comment
infix 4 ==/
infix 4 -/-

//comment
infix 4 /--

//comment
infixl 4 =

// comment
infixl 4 =/

// comment
infixl 4 ==/
infixl 4 -/-

//comment
infixl 4 -/-

//comment
infixl 4 /--

//comment
infixr 4 =

// comment
infixr 4 =/

// comment
infixr 4 ==/
infixr 4 -/-

//comment
infixr 4 -/-

//comment
infixr 4 /--

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

if op_eq_slash =/ 1 then { 1 };
if op_eq_slash_with_block_comment =/ 1 then /**/ { 1 };

let op_eq_with_line_comment =
//
1;

let op_eq_with_line_comment =
///
1;

if op_eq_with_line_comment =
// comment
if eq_slash = /**/ 1 then { 1 } else { 2 } then { 1 };

if eq_with_blcok_comment = /**/ 1 then { 1 };

if eq3_slash ===/_u 1 then { 1 };

0
}
72 changes: 72 additions & 0 deletions test/format/operator.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
default Order dec
$include <prelude.sail>

// comment
//comment
/// line_comment with more than two slash
///line_comment with more than two slash
//// line_comment with more than two slash
////line_comment with more than two slash

// /*/*/

/*/ block_comment with slash near /* and */
*/


//* line_block_comment */

infix 4 ===_u
infix 4 ===/_u

infix 4 ===*_u
val operator ===/_u : forall 'n. (int('n), int('n)) -> bool
function operator ===/_u(x, y) = x == y

infix 4 =/
val operator =/ : forall 'n. (int('n), int('n)) -> bool
function operator =/(x, y) = x == y

infix 4 = // comment
infix 4 </> // comment
infix 4 =/ // comment
infix 4 ==/
infix 4 -/- //comment
infix 4 /-- //comment

infixl 4 = // comment
infixl 4 =/ // comment
infixl 4 ==/
infixl 4 -/- //comment
infixl 4 -/- //comment
infixl 4 /-- //comment

infixr 4 = // comment
infixr 4 =/ // comment
infixr 4 ==/
infixr 4 -/- //comment
infixr 4 -/- //comment
infixr 4 /-- //comment

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

if op_eq_slash =/1 then { 1 };
if op_eq_slash_with_block_comment =/1/**/ then { 1 };

let op_eq_with_line_comment = //
1;

let op_eq_with_line_comment = ///
1;

if op_eq_with_line_comment = // comment
(if eq_slash = /**/1 then { 1 } else {2}) then {1};

if eq_with_blcok_comment = /**/1 then { 1 };


if eq3_slash ===/_u 1 then { 1 };

0
}
Loading