Skip to content

Instantly share code, notes, and snippets.

@madmo
Created September 9, 2014 21:52
Show Gist options
  • Select an option

  • Save madmo/7900b1a2a636ca0db4c2 to your computer and use it in GitHub Desktop.

Select an option

Save madmo/7900b1a2a636ca0db4c2 to your computer and use it in GitHub Desktop.
Lexing c-comments using camlp4
type token =
| Text of char
| Comment of string
let rec lex = parser
| [< ''/'; p = parser (* left factoring... *)
| [< ''/'; stream >] ->
let buffer = Buffer.create 1 in
lex_comment buffer stream
| [< ''*'; stream >] ->
let buffer = Buffer.create 1 in
lex_multi_comment buffer stream
>] -> p
| [< 'c; stream >] ->
[< 'Text c; lex stream >]
| [< >] -> [< >]
and lex_comment buffer = parser
| [< ' ('\n'); stream=lex >] ->
[< 'Comment (Buffer.contents buffer); stream >]
| [< 'c; stream >] ->
Buffer.add_char buffer c;
lex_comment buffer stream
and lex_multi_comment buffer = parser
| [< ''*'; ''/'; stream=lex >] ->
[< 'Comment (Buffer.contents buffer); stream >]
| [< 'c; stream >] ->
Buffer.add_char buffer c;
lex_multi_comment buffer stream
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment