Created
September 9, 2014 21:52
-
-
Save madmo/7900b1a2a636ca0db4c2 to your computer and use it in GitHub Desktop.
Lexing c-comments using camlp4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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