9 let store str = curStr := str
11 let error buf callerID =
12 Error.report "Lexer error : %s" callerID;
14 raise Parsing.Parse_error
16 let advance_line_pos pos =
17 let module L = Lexing in
18 {L.pos_fname=pos.L.pos_fname;
19 pos_lnum = pos.L.pos_lnum + 1;
21 pos_cnum = pos.L.pos_cnum}
23 let advance_line lexbuf =
24 lexbuf.Lexing.lex_curr_p <- advance_line_pos lexbuf.Lexing.lex_curr_p
26 (* use Map or Hashtbl ? *)
45 "autoincrement",AUTOINCREMENT;
68 let all token l = k := !k @ List.map (fun x -> x,token) l in
69 all FUNCTION ["max"; "min"; "concat"; "length"; "random";];
70 all CONFLICT_ALGO ["ignore"; "replace"; "abort"; "fail"; "rollback";];
71 all JOIN_TYPE1 ["left";"right";"full"];
72 all JOIN_TYPE2 ["inner";"outer";"cross"];
73 all LIKE_OP ["like";"glob";"regexp";"match"];
76 let keywords = List.map (fun (k,v) -> (String.lowercase k, v)) keywords
79 let str = String.lowercase str in
80 try List.assoc str keywords with Not_found -> IDENT str
84 let alpha = ['a'-'z' 'A'-'Z']
85 let ident = (alpha) (alpha | digit | '_' )*
88 rule ruleStatement props = parse
89 | ['\n' ' ' '\t']+ { ruleStatement props lexbuf }
91 | "--" wsp* "[sql2cpp]" wsp+ (ident+ as n) wsp* "=" wsp* ([^'\n']* as v) '\n'
93 ruleStatement (Props.set props n v) lexbuf
95 | "--" { store ""; ignore (ruleComment lexbuf); ruleStatement props lexbuf }
96 | alpha [^ ';']+ as stmt ';' { Some (stmt,props) }
100 | wsp { ruleMain lexbuf }
101 (* update line number *)
102 | '\n' { advance_line lexbuf; ruleMain lexbuf}
109 | "--" { store ""; ignore (ruleComment lexbuf); ruleMain lexbuf }
110 (* | '"' { store ""; ruleInQuotes lexbuf } *)
114 | "VALUES" { VALUES }
120 | "UNION" (wsp+ "ALL")? | "EXCEPT" | "INTERSECT" { COMPOUND_OP }
130 | "/" | "%" | ">" | ">=" | "<=" | "<" | "&" | "|" { NUM_BINARY_OP }
132 | "?" { PARAM Stmt.Next }
133 | "?" (digit+ as str) { PARAM (Stmt.Numbered (int_of_string str)) }
134 | [':' '@'] (ident as str) { PARAM (Stmt.Named str) }
136 | "'" { TEXT (ruleInSingleQuotes "" lexbuf) }
137 | ['x' 'X'] "'" { BLOB (ruleInSingleQuotes "" lexbuf) }
139 | ident as str { get_ident str }
140 | digit+ as str { INTEGER (int_of_string str) }
142 | _ { error lexbuf "ruleMain" }
144 ruleInSingleQuotes acc = parse
146 | eof { error lexbuf "no terminating quote" }
147 | '\n' { advance_line lexbuf; error lexbuf "EOL before terminating quote" }
148 | "''" { ruleInSingleQuotes (acc ^ "'") lexbuf }
149 | [^'\'' '\n']+ { ruleInSingleQuotes (acc ^ Lexing.lexeme lexbuf) lexbuf }
150 | _ { error lexbuf "ruleInSingleQuotes" }
153 | '\n' { advance_line lexbuf; !curStr }
155 | [^'\n']+ { store (Lexing.lexeme lexbuf); ruleComment lexbuf; }
156 | _ { error lexbuf "ruleComment"; }
160 let parse_rule lexbuf = ruleMain lexbuf