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;
52 (* CHARACTER, CHARACTER VARYING, BIT, BIT VARYING, NUMERIC, DECIMAL,
53 INTEGER, SMALLINT, FLOAT, REAL, DOUBLE PRECISION, DATE, TIME,
54 TIMESTAMP, and INTERVAL.*)
79 let all token l = k := !k @ List.map (fun x -> x,token) l in
80 all (FUNCTION (Some T.Int)) ["max"; "min"; "length"; "random";];
81 all (FUNCTION (Some T.Text)) ["concat";];
82 all CONFLICT_ALGO ["ignore"; "replace"; "abort"; "fail"; "rollback";];
83 all JOIN_TYPE1 ["left";"right";"full"];
84 all JOIN_TYPE2 ["inner";"outer";"cross"];
85 all LIKE_OP ["like";"glob";"regexp";"match"];
88 let keywords = List.map (fun (k,v) -> (String.lowercase k, v)) keywords
91 let str = String.lowercase str in
92 try List.assoc str keywords with Not_found -> IDENT str
96 let alpha = ['a'-'z' 'A'-'Z']
97 let ident = (alpha) (alpha | digit | '_' )*
98 let wsp = [' ' '\r' '\t']
100 rule ruleStatement props = parse
101 | ['\n' ' ' '\r' '\t']+ { ruleStatement props lexbuf }
103 | "--" wsp* "[sqlgg]" wsp+ (ident+ as n) wsp* "=" wsp* ([^'\n']* as v) '\n'
105 ruleStatement (Props.set props n v) lexbuf
107 | "--" { store ""; ignore (ruleComment lexbuf); ruleStatement props lexbuf }
108 | alpha [^ ';']+ as stmt ';' { Some (stmt,props) }
112 | wsp { ruleMain lexbuf }
113 (* update line number *)
114 | '\n' { advance_line lexbuf; ruleMain lexbuf}
121 | "--" { store ""; ignore (ruleComment lexbuf); ruleMain lexbuf }
122 (* | '"' { store ""; ruleInQuotes lexbuf } *)
124 | "UNION" (wsp+ "ALL")? | "EXCEPT" | "INTERSECT" { COMPOUND_OP }
134 | "/" | "%" | ">" | ">=" | "<=" | "<" | "&" | "|" { NUM_BINARY_OP }
136 | "?" { PARAM Stmt.Next }
137 | "?" (digit+ as str) { PARAM (Stmt.Numbered (int_of_string str)) }
138 | [':' '@'] (ident as str) { PARAM (Stmt.Named str) }
140 | "'" { TEXT (ruleInSingleQuotes "" lexbuf) }
141 | ['x' 'X'] "'" { BLOB (ruleInSingleQuotes "" lexbuf) }
143 | ident as str { get_ident str }
144 | digit+ as str { INTEGER (int_of_string str) }
146 | _ { error lexbuf "ruleMain" }
148 ruleInSingleQuotes acc = parse
150 | eof { error lexbuf "no terminating quote" }
151 | '\n' { advance_line lexbuf; error lexbuf "EOL before terminating quote" }
152 | "''" { ruleInSingleQuotes (acc ^ "'") lexbuf }
153 | [^'\'' '\n']+ { ruleInSingleQuotes (acc ^ Lexing.lexeme lexbuf) lexbuf }
154 | _ { error lexbuf "ruleInSingleQuotes" }
157 | '\n' { advance_line lexbuf; !curStr }
159 | [^'\n']+ { store (Lexing.lexeme lexbuf); ruleComment lexbuf; }
160 | _ { error lexbuf "ruleComment"; }
164 let parse_rule lexbuf = ruleMain lexbuf