Skip to content

Instantly share code, notes, and snippets.

@ThePhD
Created October 22, 2016 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThePhD/8d516eeadb3ca6629b1a12435555788c to your computer and use it in GitHub Desktop.
Save ThePhD/8d516eeadb3ca6629b1a12435555788c to your computer and use it in GitHub Desktop.
{ open Parser }
(* handle windows newlines *)
let eol = '\r'? '\n'
(* state variables: line number, etc. *)
let line_num = ref 1
(* the basic text eater *)
(* we save all the characters that we see,
we encounter the preprocessor starter *)
rule token = parse
| '#' { preprocessor_token lexbuf }
| eol { incr line_num; token lexbuf }
| eof { EOF }
| _ as c { TEXT(c) }
(* The actual preprocessor tokens *)
(* Here, we see that we just capture
string expressions, dot-delimeted ident names,
macro continuation lines and more *)
and preprocessor_token = parse
| "import" { IMPORT }
| "strings" { STRINGS }
| '"'(_* as s)'"' { STRING(s) }
| '.' { DOT }
| ident { IDENTIFIER(s) }
| eol '#' { incr line_num; CONTINUE }
| eol | eof { EOF; token lexbuf }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment