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;
69 let all token l = k := !k @ List.map (fun x -> x,token) l in
70 all (FUNCTION (Some T.Int)) ["max"; "min"; "length"; "random";];
71 all (FUNCTION (Some T.Text)) ["concat";];
72 all CONFLICT_ALGO ["ignore"; "replace"; "abort"; "fail"; "rollback";];
73 all JOIN_TYPE1 ["left";"right";"full"];
74 all JOIN_TYPE2 ["inner";"outer";"cross"];
75 all LIKE_OP ["like";"glob";"regexp";"match"];
78 let keywords = List.map (fun (k,v) -> (String.lowercase k, v)) keywords
81 let str = String.lowercase str in
82 try List.assoc str keywords with Not_found -> IDENT str
86 let alpha = ['a'-'z' 'A'-'Z']
87 let ident = (alpha) (alpha | digit | '_' )*
88 let wsp = [' ' '\r' '\t']
90 rule ruleStatement props = parse
91 | ['\n' ' ' '\r' '\t']+ { ruleStatement props lexbuf }
93 | "--" wsp* "[sql2cpp]" wsp+ (ident+ as n) wsp* "=" wsp* ([^'\n']* as v) '\n'
95 ruleStatement (Props.set props n v) lexbuf
97 | "--" { store ""; ignore (ruleComment lexbuf); ruleStatement props lexbuf }
98 | alpha [^ ';']+ as stmt ';' { Some (stmt,props) }
102 | wsp { ruleMain lexbuf }
103 (* update line number *)
104 | '\n' { advance_line lexbuf; ruleMain lexbuf}
111 | "--" { store ""; ignore (ruleComment lexbuf); ruleMain lexbuf }
112 (* | '"' { store ""; ruleInQuotes lexbuf } *)
116 | "VALUES" { VALUES }
122 | "UNION" (wsp+ "ALL")? | "EXCEPT" | "INTERSECT" { COMPOUND_OP }
132 | "/" | "%" | ">" | ">=" | "<=" | "<" | "&" | "|" { NUM_BINARY_OP }
134 | "?" { PARAM Stmt.Next }
135 | "?" (digit+ as str) { PARAM (Stmt.Numbered (int_of_string str)) }
136 | [':' '@'] (ident as str) { PARAM (Stmt.Named str) }
138 | "'" { TEXT (ruleInSingleQuotes "" lexbuf) }
139 | ['x' 'X'] "'" { BLOB (ruleInSingleQuotes "" lexbuf) }
141 | ident as str { get_ident str }
142 | digit+ as str { INTEGER (int_of_string str) }
144 | _ { error lexbuf "ruleMain" }
146 ruleInSingleQuotes acc = parse
148 | eof { error lexbuf "no terminating quote" }
149 | '\n' { advance_line lexbuf; error lexbuf "EOL before terminating quote" }
150 | "''" { ruleInSingleQuotes (acc ^ "'") lexbuf }
151 | [^'\'' '\n']+ { ruleInSingleQuotes (acc ^ Lexing.lexeme lexbuf) lexbuf }
152 | _ { error lexbuf "ruleInSingleQuotes" }
155 | '\n' { advance_line lexbuf; !curStr }
157 | [^'\n']+ { store (Lexing.lexeme lexbuf); ruleComment lexbuf; }
158 | _ { error lexbuf "ruleComment"; }
162 let parse_rule lexbuf = ruleMain lexbuf