xstrp4_here_lexer.mll 3.2 KB
Newer Older
gerd's avatar
gerd committed
1
(* $Id: xstrp4_here_lexer.mll,v 1.3 2002/12/28 18:41:33 gerd Exp $
gerd's avatar
gerd committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * ----------------------------------------------------------------------
 *
 *)

{
  open Xstrp4_here_types

  type val_id = LC of string | UC of string | End_of_id

  let rec parse_val_id f buf =
    let id = f buf in
      match id with
	  UC s -> s :: parse_val_id f buf 
	| LC s -> [s]
	| End_of_id -> []
}

let ucletter = [ 'A' - 'Z' ]
let lcletter = [ 'a' - 'z' '_' ] 
let acletter = ucletter | lcletter

let format = '%'
             [ '0' '-' ' ' ]*    (* no more modifiers are supported by Ocaml *)
             ['0'-'9']* 
             ( '.' ['0'-'9']* )?
27 28 29
	     ( ( ['L' 'l' 'n'] [ 'd' 'i' 'u' 'x' 'X' 'o' ])
	       | [ 'd' 'i' 'u' 'x' 'X' 's' 'c' 'f' 'e' 'E' 'g' 'G' 'b' 'a' 't' ]
	     )
gerd's avatar
gerd committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

rule token = parse
    '$' ( ucletter acletter* '.' )* lcletter acletter*
      { let s = Lexing.lexeme lexbuf in
	let buf = Lexing.from_string (String.sub s 1 (String.length s - 1)) in
	  Variable (parse_val_id value_identifier buf, 
		    "%s",
		   Lexing.lexeme_start lexbuf+1,
		   Lexing.lexeme_end lexbuf)
      }
  | '$' '{' ( ucletter acletter* '.' )* lcletter acletter*
            ( ',' format )?  
        '}'
      { let s = Lexing.lexeme lexbuf in
	let k_close = String.index s '}' in
	let k_percent = try String.index s '%' with Not_found -> (-1) in
	let buf = Lexing.from_string (String.sub s 2 (k_close - 1)) in
	let fmt = 
	  if k_percent >= 0 then
	    String.sub s k_percent (String.length s - k_percent - 1)
	  else
	    "%s"
	in
	let start = Lexing.lexeme_start lexbuf in
	Variable (parse_val_id value_identifier buf, 
		  fmt,
		  start+2,
		  start+(if k_percent >= 0 then k_percent-1 else k_close))
      }
  | '$'
      { failwith "Bad $ expander" }
  | '\\' '\n' 
      { Literal "" }
  | '\\' '$'
      { Literal "$" }
gerd's avatar
gerd committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  | '\\' [ '0'-'9' ] [ '0'-'9' ] [ '0'-'9' ]
      {  Literal (let s = Lexing.lexeme lexbuf in
		  let n = int_of_string(String.sub s 1 3) in
                  Printf.sprintf "%c" (Char.chr n)
		 ) 
      }
  | '\\' 'o' [ '0'-'9' ] [ '0'-'9' ] [ '0'-'9' ]
      {  Literal (let s = Lexing.lexeme lexbuf in
		  let n = int_of_string("0" ^ String.sub s 1 4) in
                  Printf.sprintf "%c" (Char.chr n)
		 ) 
      }
  | '\\' 'x' [ '0'-'9' 'a'-'f' 'A'-'F' ] [ '0'-'9' 'a'-'f' 'A'-'F' ]
      {  Literal (let s = Lexing.lexeme lexbuf in
		  let n = int_of_string("0" ^ String.sub s 1 3) in
                  Printf.sprintf "%c" (Char.chr n)
		 ) 
      }

gerd's avatar
gerd committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  | '\\' _
      { Literal (Lexing.lexeme lexbuf) }
  | [^ '$' '\\']+
      { Literal (Lexing.lexeme lexbuf) }
  | eof 
      { Textend }
  | _ 
      { Literal (Lexing.lexeme lexbuf) }

and value_identifier = parse
    ucletter acletter* '.'
      { let s = Lexing.lexeme lexbuf in
	  UC (String.sub s 0 (String.length s - 1))
      }
  | lcletter acletter*
      { LC(Lexing.lexeme lexbuf) }
  | eof
      { End_of_id }

(* ======================================================================
 * History:
 * 
 * $Log: xstrp4_here_lexer.mll,v $
gerd's avatar
gerd committed
107 108 109
 * Revision 1.3  2002/12/28 18:41:33  gerd
 * 	New backslash sequences
 *
110 111 112
 * Revision 1.2  2002/08/15 23:33:26  gerd
 * 	Added qualifiers for int32, int64, and nativeint.
 *
gerd's avatar
gerd committed
113 114 115 116 117
 * Revision 1.1  1999/07/18 16:42:39  gerd
 * 	Initial revision.
 *
 * 
 *)