Sublime Forum

OCaml syntax highlighter hangs on ML-like strings

#1

Reproduction steps

  1. Paste the following text into a new document:

let X = “X ([X “X”;X “X”;X “X”;X “X”;X “X”;X ([X ([X “X”;X “X”;X “X”;X ([X 20170428;X 20170801],1,2)],2,2);X ([X “X”;X “X”;X “X”;X ([X 20180201],1,1)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 20171101],1,1)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 20180501;X 20180801],1,2)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 20181101;X 20190131],1,2)],2,2)],1,5);X “X([])”;X ([X 20170503;X 20180205;X 20171103;X 20180503;X 20181105],1,5);X “X([])”;X ([X 20171103;X 20180503;X 20180205;X 20181105;X 20190503],1,5);X “X([])”;X ([X 20171103;X 20180503;X 20180505;X 20181105;X 20190503],1,5);X “X([])”;X ([X ([X “X”;X “X([])”;X “X([])”;X ([X 0.255555555556;X 0.255555555556],1,2)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 0.241666666667],1,1)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 0.261111111111],1,1)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 0.255555555556;X 0.261111111111],1,2)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 0.252777777778;X 0.244444444444],1,2)],2,2)],1,5);X “X([])”;X ([X “X”;X “X([])”;X “Uuid”;X “X”,2,2);X “X([])”;X ([X 10000000.;X 10000000.;X 10000000.;X 10000000.;X 10000000.],1,5);X “X([])”;X ([X ([X “X”;X “X([])”;X “X([])”;X ([X 1.;X 1.],1,2)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 1.],1,1)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 1.],1,1)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 1.;X 1.],1,2)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 1.;X 1.],1,2)],2,2)],1,5);X “X([])”;X ([X ([X “X”;X “X([])”;X “X([])”;X ([X 0.;X 0.],1,2)],2,2);X ([X “X”;X “X([])”;X “X([])”;X ([X 0.],1,1)],2,2);X ([X “X”;X “X([])”;X “X”;X ([X 0.],1,1)],2,2);X ([X “X”;X “X”;X “X”;X ([X 0.;X 0.],1,2)],2,2);X ([X “X”;X “X”;X “X”;X ([X 0.;X 0.],1,2)],2,2)],1,5);X “X([])”;X “X([])”;X “X([])”;X “X”,13,2)”

  1. Set the syntax to OCaml

Expected behavior
Syntax coloring is applied to the text

Observed behavior
The application hangs

Other information
This text is pared-down from some production code, with X’s for obfuscation. I discovered that reducing the size of the text considerably causes highlighting to succeed after some wait. This seems to indicate exponential asymptotic performance rather than an indefinite hang.

0 Likes

#2

it’s not surprising, OCaml is one of the syntaxes that needs to be rewritten to not use the backtracking regex engine

1 Like

#3

Fair enough. It prevents me from using my favorite text editor to open and edit some files though. It would be nice to have it fail gracefully, maybe indicate in the status bar that syntax highlighting failed after some timeout.

0 Likes

#4

related:

0 Likes

#5

As I understand it, the new regexp engine runs in guaranteed linear time. This should hopefully forestall the need for such a timeout.

I glanced at the OCaml syntax definition and couldn’t immediately see what was causing the problem, but there are definitely some lookbehinds in there, and overall it’s definitely written like an old tmLanguage. Someone more familiar with the language could probably spruce it up in an afternoon.

0 Likes

#6

it’d certainly be useful to isolate which expressions are causing the catastrophic backtracking - probably just adding a \b somewhere could make a massive difference in the short term before the syntax is rewritten

0 Likes