Skip to content

Commit aca1fea

Browse files
authored
Disallow unescaped newlines in strings in ocamltests (ocaml-flambda#3506)
1 parent 53fd024 commit aca1fea

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

ocamltest/tsl_lexer.mll

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ let has_comments = ref false
2424

2525
let lexer_error message =
2626
failwith (Printf.sprintf "Tsl lexer: %s" message)
27+
28+
let lexer_error_at pos message =
29+
let file = pos.Lexing.pos_fname in
30+
let line = pos.Lexing.pos_lnum in
31+
let column = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
32+
let message = Printf.sprintf "%s:%d:%d: %s" file line column message in
33+
lexer_error message
2734
}
2835

2936
let newline = ('\013'* '\010')
@@ -86,12 +93,7 @@ and token = parse
8693
| _
8794
{
8895
let pos = Lexing.lexeme_start_p lexbuf in
89-
let file = pos.Lexing.pos_fname in
90-
let line = pos.Lexing.pos_lnum in
91-
let column = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
92-
let message = Printf.sprintf "%s:%d:%d: unexpected character %s"
93-
file line column (Lexing.lexeme lexbuf) in
94-
lexer_error message
96+
lexer_error_at pos ("unexpected character %s" ^ Lexing.lexeme lexbuf)
9597
}
9698
| eof
9799
{ lexer_error "unexpected eof" }
@@ -117,6 +119,8 @@ and string acc = parse
117119
{string (acc ^ "\\") lexbuf}
118120
| '"'
119121
{acc}
122+
| newline
123+
{lexer_error_at (Lexing.lexeme_start_p lexbuf) "unescaped newline in string"}
120124
and comment = parse
121125
| "(*"
122126
{
@@ -132,12 +136,7 @@ and comment = parse
132136
| eof
133137
{
134138
let pos = List.hd !comment_start_pos in
135-
let file = pos.Lexing.pos_fname in
136-
let line = pos.Lexing.pos_lnum in
137-
let column = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
138-
let message = Printf.sprintf "%s:%d:%d: unterminated comment"
139-
file line column in
140-
lexer_error message
139+
lexer_error_at pos "unterminated comment"
141140
}
142141
| _
143142
{

0 commit comments

Comments
 (0)