Skip to content

Commit

Permalink
Fix lexer rule to handle operator of format like operator + comment
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Jul 22, 2024
1 parent 561df10 commit 6ae0cc5
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/lib/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ let tyvar_start = '\''
let oper_char = ['!''%''&''*''+''-''.''/'':''<''=''>''@''^''|']
let oper_char_no_slash = ['!''%''&''*''+''-''.'':''<''=''>''@''^''|']
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 oper_char_no_star = ['!''%''&''+''-''.''/'':''<''=''>''@''^''|']
let operator_any_start = oper_char (oper_char_no_slash_star oper_char)*
| (oper_char oper_char_no_slash_star)*
let operator_no_slash_start = oper_char_no_slash (oper_char oper_char_no_slash_star)*
| (oper_char_no_slash_star oper_char)*
let operator = (operator_any_start | operator_no_slash_start) ('_' ident)?
let escape_sequence = ('\\' ['\\''\"''\'''n''t''b''r']) | ('\\' digit digit digit) | ('\\' 'x' hexdigit hexdigit)
let lchar = [^'\n']

Expand All @@ -188,7 +190,7 @@ rule token comments = parse
token comments lexbuf }
| "@" { At }
| "2" ws "^" { TwoCaret }
| "^" { Caret }
| "^" { Caret }
| "::" { ColonColon }
| ":" { Colon ":" }
| "," { Comma }
Expand Down Expand Up @@ -231,11 +233,44 @@ rule token comments = parse
Pragma (i, arg) }
| "infix" ws (digit as p) ws (operator as op)
{ Fixity (Infix, Big_int.of_string (Char.escaped p), op) }
| "infix" ws (digit as p) ws (operator as op) "//" {
line_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) lexbuf;
Fixity (Infix, Big_int.of_string (Char.escaped p), op)
}
| "infix" ws (digit as p) ws (operator as op) "/*" {
block_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) 0 lexbuf;
Fixity (Infix, Big_int.of_string (Char.escaped p), op)
}
| "infixl" ws (digit as p) ws (operator as op)
{ Fixity (InfixL, Big_int.of_string (Char.escaped p), op) }
| "infixl" ws (digit as p) ws (operator as op) "//" {
line_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) lexbuf;
Fixity (InfixL, Big_int.of_string (Char.escaped p), op)
}
| "infixl" ws (digit as p) ws (operator as op) "/*" {
block_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) 0 lexbuf;
Fixity (InfixL, Big_int.of_string (Char.escaped p), op)
}
| "infixr" ws (digit as p) ws (operator as op)
{ Fixity (InfixR, Big_int.of_string (Char.escaped p), op) }
| operator as op { OpId op }
| "infixr" ws (digit as p) ws (operator as op) "//" {
line_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) lexbuf;
Fixity (InfixR, Big_int.of_string (Char.escaped p), op)
}
| "infixr" ws (digit as p) ws (operator as op) "/*" {
block_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) 0 lexbuf;
Fixity (InfixR, Big_int.of_string (Char.escaped p), op)
}
| operator as op
{ OpId op }
| ((oper_char* oper_char_no_slash) as op) "/*" {
block_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) 0 lexbuf;
if op = "=" then Eq op else OpId op
}
| ((oper_char* oper_char_no_slash) as op) "//" {
line_comment comments (Lexing.lexeme_start_p lexbuf) (Buffer.create 10) lexbuf;
if op = "=" then Eq op else OpId op
}
| tyvar_start startident ident* as i { TyVar i }
| "~" { Id "~" }
| startident ident* as i { if M.mem i kw_table then
Expand Down
88 changes: 88 additions & 0 deletions test/format/default/operator.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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

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 ==/
infix 4 -/-

//comment
infix 4 /-

//comment
infix 4 /-*

//comment
infixl 4 =

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

//comment
infixl 4 /-

//comment
infixl 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
}
70 changes: 70 additions & 0 deletions test/format/operator.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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

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 ==/
infix 4 -/-//comment
infix 4 /-//comment
infix 4 /-*//comment

infixl 4 =// comment
infixl 4 ==/
infixl 4 -/-//comment
infixl 4 /-//comment
infixl 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
}

0 comments on commit 6ae0cc5

Please sign in to comment.