1 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 27 28 29 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
(***********************************************************************)
(* *)
(* CamlIDL *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1999 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the Q Public License version 1.0 *)
(* *)
(***********************************************************************)
(* $Id: lexer_midl.mll,v 1.13 2004-05-12 12:40:40 xleroy Exp $ *)
(* Lexer for IDL interface files *)
{
open Utils
open Parse_aux
open Parser_midl
let keywords = Hashtbl.create 29
let _ =
List.iter
(fun (txt, kwd) -> Hashtbl.add keywords txt kwd)
[ "boolean", BOOLEAN;
"byte", BYTE;
"case", CASE;
"char", CHAR;
"const", CONST;
"cpp_quote", CPP_QUOTE;
"default", DEFAULT;
"double", DOUBLE;
"enum", ENUM;
"false", FALSE;
"float", FLOAT;
"handle_t", HANDLE_T;
"hyper", HYPER;
"import", IMPORT;
"int", INT;
"__int64", INT64_COMPAT;
"int8", INT8;
"int16", INT16;
"int32", INT32;
"int64", INT64;
"interface", INTERFACE;
"long", LONG;
"NULL", NULL;
"quote", QUOTE;
"short", SHORT;
"signed", SIGNED;
"sizeof", SIZEOF;
"small", SMALL;
"struct", STRUCT;
"switch", SWITCH;
"true", TRUE;
"typedef", TYPEDEF;
"union", UNION;
"unsigned", UNSIGNED;
"uint8", UINT8;
"uint16", UINT16;
"uint32", UINT32;
"uint64", UINT64;
"void", VOID;
"wchar_t", WCHAR_T ]
let string_buffer = Buffer.create 80
(* To translate escape sequences *)
let char_for_backslash = function
| 'n' -> '\010'
| 'r' -> '\013'
| 'b' -> '\008'
| 't' -> '\009'
| c -> c
let char_for_code lexbuf i j =
let s = Lexing.lexeme lexbuf in
let s' = String.sub s i (String.length s - i - j) in
Char.chr(int_of_string("0o" ^ s'))
(* To report lexical errors *)
exception Lex_error of string
}
let blank = [' ' '\010' '\013' '\009' '\012']
let eol = ('\n' | '\r' | "\r\n")
let identstart = ['A'-'Z' 'a'-'z' '_']
let identchar = identstart | ['0'-'9']
let decimal_literal = ['0'-'9']+
let hex_literal = '0' ['x' 'X'] ['0'-'9' 'A'-'F' 'a'-'f']+
let octal_literal = '0' ['0'-'7']+
let hex = ['0'-'9' 'a'-'f' 'A'-'F']
let hex4 = hex hex hex hex
let hex8 = hex4 hex4
let hex12 = hex4 hex4 hex4
rule token = parse
blank +
{ token lexbuf }
| "/*"
{ comment lexbuf }
| "//" [ ^ '\n' ] * eol
{ token lexbuf }
| "#" ("line")? [' ' '\t']* ['0'-'9']+ [^ '\n' '\r'] * eol
(* # linenum "filename" flags \n *)
{ token lexbuf }
| "#" blank* "pragma" [^ '\n' '\r'] * eol
(* #pragma introduced by some C preprocessors *)
{ token lexbuf }
| identstart identchar *
{ let s = Lexing.lexeme lexbuf in
try
Hashtbl.find keywords s
with Not_found ->
if StringSet.mem s !type_names
then TYPEIDENT s
else IDENT s }
| octal_literal
{ INTEGER(Int64.of_string("0o" ^ Lexing.lexeme lexbuf)) }
| decimal_literal | hex_literal
{ INTEGER(Int64.of_string(Lexing.lexeme lexbuf)) }
| "\""
{ Buffer.reset string_buffer;
string lexbuf;
let s = Buffer.contents string_buffer in
Buffer.reset string_buffer;
STRING s }
| "'" [^ '\\' '\''] "'"
{ CHARACTER(Lexing.lexeme_char lexbuf 1) }
| "'" '\\' ['\\' '\'' 'n' 't' 'b' 'r'] "'"
{ CHARACTER(char_for_backslash (Lexing.lexeme_char lexbuf 2)) }
| "'" '\\' ['0'-'3'] ['0'-'7']? ['0'-'7']? "'"
{ CHARACTER(char_for_code lexbuf 2 1) }
| "&" { AMPER }
| "&&" { AMPERAMPER }
| "!" { BANG }
| "!=" { BANGEQUAL }
| "|" { BAR }
| "||" { BARBAR }
| "^" { CARET }
| ":" { COLON }
| "," { COMMA }
| "." { DOT }
| "=" { EQUAL }
| "==" { EQUALEQUAL }
| ">" { GREATER }
| ">=" { GREATEREQUAL }
| ">>" { GREATERGREATER }
| "{" { LBRACE }
| "[" { LBRACKET }
| "<" { LESS }
| "<=" { LESSEQUAL }
| "<<" { LESSLESS }
| "(" { LPAREN }
| "%" { PERCENT }
| "+" { PLUS }
| "}" { RBRACE }
| "]" { RBRACKET }
| ")" { RPAREN }
| ";" { SEMI }
| "/" { SLASH }
| "*" { STAR }
| "~" { TILDE }
| "-" { MINUS }
| "?" { QUESTIONMARK }
| '(' hex8 '-' hex4 '-' hex4 '-' hex4 '-' hex12 ')'
{ let s = Lexing.lexeme lexbuf in
UUID(String.sub s 1 (String.length s - 2)) }
| eof { EOF }
| _ { raise (Lex_error ("Illegal character " ^
Char.escaped(Lexing.lexeme_char lexbuf 0))) }
and comment = parse
"*/" { token lexbuf }
| _ { comment lexbuf }
| eof { raise (Lex_error "Unterminated comment") }
and string = parse
'"'
{ () }
| '\\' eol [' ' '\009'] *
{ string lexbuf }
| '\\' ['\\' '"' 'n' 't' 'b' 'r']
{ Buffer.add_char string_buffer
(char_for_backslash(Lexing.lexeme_char lexbuf 1));
string lexbuf }
| '\\' ['0'-'3'] ['0'-'7']? ['0'-'7']?
{ Buffer.add_char string_buffer (char_for_code lexbuf 1 0);
string lexbuf }
| eof
{ raise (Lex_error "Unterminated string") }
| _
{ Buffer.add_char string_buffer (Lexing.lexeme_char lexbuf 0);
string lexbuf }
|