[ocamllex] how to specify strings not matching a pattern

Hello,

I’m trying to implement a RestructuredText parser as a stepping stone for an OCaML implementaition of Sphinx. I’ve got a problem with implementing the inline markup especially the strong markup (eg **my strongified text**). I would like to match the strings that do not contain the ending pattern (ie "**" ( ' ' | '\n'| eof)). I know I can do that by creating an equivalent regex but as I need to replicate that for other markup constructs it would become difficult to maintain.

It might be easiest to use different lexer context for this rather than trying to cram this into a regexp: detect the ** and branch into a new context where you stay until you reach the end of it. When lexing a programming language, this is typically the way how strings and comments are recognised. You could take a look at the lever for OCaml itself.

3 Likes