Permalink
Browse files

Add a hook in the lexer for an internal preprocessor

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14631 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
  • Loading branch information...
1 parent 75fd56b commit e3aaa68105095e3dbcb2c08dd2b8203a62f8f1cc @lefessan lefessan committed Apr 17, 2014
Showing with 41 additions and 3 deletions.
  1. BIN boot/ocamlc
  2. BIN boot/ocamldep
  3. BIN boot/ocamllex
  4. +13 −0 parsing/lexer.mli
  5. +26 −3 parsing/lexer.mll
  6. +2 −0 parsing/parser.mly
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
@@ -40,3 +40,16 @@ val in_string : unit -> bool;;
val print_warnings : bool ref
val comments : unit -> (string * Location.t) list
val token_with_comments : Lexing.lexbuf -> Parser.token
+
+(*
+ [set_preprocessor init preprocessor] registers [init] as the function
+to call to initialize the preprocessor when the lexer is initialized,
+and [preprocessor] a function that is called when a new token is needed
+by the parser, as [preprocessor lexer lexbuf] where [lexer] is the
+lexing function.
+*)
+
+val set_preprocessor :
+ (unit -> unit) ->
+ ((Lexing.lexbuf -> Parser.token) -> Lexing.lexbuf -> Parser.token) ->
+ unit
View
@@ -215,6 +215,8 @@ let update_loc lexbuf file line absolute chars =
}
;;
+let preprocessor = ref None
+
(* Warn about Latin-1 characters used in idents *)
let warn_latin1 lexbuf =
@@ -283,9 +285,19 @@ let float_literal =
(['e' 'E'] ['+' '-']? ['0'-'9'] ['0'-'9' '_']*)?
rule token = parse
+ | "\\" newline {
+ match !preprocessor with
+ | None ->
+ raise (Error(Illegal_character (Lexing.lexeme_char lexbuf 0),
+ Location.curr lexbuf))
+ | Some _ ->
+ update_loc lexbuf None 1 false 0;
+ token lexbuf }
| newline
{ update_loc lexbuf None 1 false 0;
- token lexbuf
+ match !preprocessor with
+ | None -> token lexbuf
+ | Some _ -> EOL
}
| blank +
{ token lexbuf }
@@ -634,7 +646,11 @@ and skip_sharp_bang = parse
| "" { () }
{
- let token_with_comments = token
+
+ let token_with_comments lexbuf =
+ match !preprocessor with
+ | None -> token lexbuf
+ | Some (_init, preprocess) -> preprocess token lexbuf
let last_comments = ref []
let rec token lexbuf =
@@ -644,9 +660,16 @@ and skip_sharp_bang = parse
token lexbuf
| tok -> tok
let comments () = List.rev !last_comments
+
let init () =
is_in_string := false;
last_comments := [];
- comment_start_loc := []
+ comment_start_loc := [];
+ match !preprocessor with
+ | None -> ()
+ | Some (init, _preprocess) -> init ()
+
+ let set_preprocessor init preprocess =
+ preprocessor := Some (init, preprocess)
}
View
@@ -404,6 +404,8 @@ let mkexp_attrs d attrs =
%token WITH
%token <string * Location.t> COMMENT
+%token EOL
+
/* Precedences and associativities.
Tokens and rules have precedences. A reduce/reduce conflict is resolved

0 comments on commit e3aaa68

Please sign in to comment.