10 let store str = curStr := str
12 let error buf callerID =
13 Error.report "Lexer error : %s" callerID;
15 raise Parsing.Parse_error
17 let advance_line_pos pos =
18 let module L = Lexing in
19 {L.pos_fname=pos.L.pos_fname;
20 pos_lnum = pos.L.pos_lnum + 1;
22 pos_cnum = pos.L.pos_cnum}
24 let advance_line lexbuf =
25 lexbuf.Lexing.lex_curr_p <- advance_line_pos lexbuf.Lexing.lex_curr_p
27 (* use Map or Hashtbl ? *)
46 "autoincrement",AUTOINCREMENT;
76 let all token l = k := !k @ List.map (fun x -> x,token) l in
77 all (FUNCTION (Some T.Int)) ["max"; "min"; "length"; "random";];
78 all (FUNCTION (Some T.Text)) ["concat";];
79 all CONFLICT_ALGO ["ignore"; "replace"; "abort"; "fail"; "rollback";];
80 all JOIN_TYPE1 ["left";"right";"full"];
81 all JOIN_TYPE2 ["inner";"outer";"cross"];
82 all LIKE_OP ["like";"glob";"regexp";"match"];
85 let keywords = List.map (fun (k,v) -> (String.lowercase k, v)) keywords
88 let str = String.lowercase str in
89 try List.assoc str keywords with Not_found -> IDENT str
93 let alpha = ['a'-'z' 'A'-'Z']
94 let ident = (alpha) (alpha | digit | '_' )*
95 let wsp = [' ' '\r' '\t']
97 rule ruleStatement props = parse
98 | ['\n' ' ' '\r' '\t']+ { ruleStatement props lexbuf }
100 | "--" wsp* "[sql2cpp]" wsp+ (ident+ as n) wsp* "=" wsp* ([^'\n']* as v) '\n'
102 ruleStatement (Props.set props n v) lexbuf
104 | "--" { store ""; ignore (ruleComment lexbuf); ruleStatement props lexbuf }
105 | alpha [^ ';']+ as stmt ';' { Some (stmt,props) }
109 | wsp { ruleMain lexbuf }
110 (* update line number *)
111 | '\n' { advance_line lexbuf; ruleMain lexbuf}
118 | "--" { store ""; ignore (ruleComment lexbuf); ruleMain lexbuf }
119 (* | '"' { store ""; ruleInQuotes lexbuf } *)
121 | "UNION" (wsp+ "ALL")? | "EXCEPT" | "INTERSECT" { COMPOUND_OP }
131 | "/" | "%" | ">" | ">=" | "<=" | "<" | "&" | "|" { NUM_BINARY_OP }
133 | "?" { PARAM Stmt.Next }
134 | "?" (digit+ as str) { PARAM (Stmt.Numbered (int_of_string str)) }
135 | [':' '@'] (ident as str) { PARAM (Stmt.Named str) }
137 | "'" { TEXT (ruleInSingleQuotes "" lexbuf) }
138 | ['x' 'X'] "'" { BLOB (ruleInSingleQuotes "" lexbuf) }
140 | ident as str { get_ident str }
141 | digit+ as str { INTEGER (int_of_string str) }
143 | _ { error lexbuf "ruleMain" }
145 ruleInSingleQuotes acc = parse
147 | eof { error lexbuf "no terminating quote" }
148 | '\n' { advance_line lexbuf; error lexbuf "EOL before terminating quote" }
149 | "''" { ruleInSingleQuotes (acc ^ "'") lexbuf }
150 | [^'\'' '\n']+ { ruleInSingleQuotes (acc ^ Lexing.lexeme lexbuf) lexbuf }
151 | _ { error lexbuf "ruleInSingleQuotes" }
154 | '\n' { advance_line lexbuf; !curStr }
156 | [^'\n']+ { store (Lexing.lexeme lexbuf); ruleComment lexbuf; }
157 | _ { error lexbuf "ruleComment"; }
161 let parse_rule lexbuf = ruleMain lexbuf