escape comments
[sqlgg.git] / sql_lexer.mll
blobbf129492beacd42eb470172abf474c5f12743f42
1 (*
2   $Id$ 
3 *)
6   open Sql_parser
8   let curStr = ref ""
9   let store str = curStr := str
11 let error buf callerID =
12   Error.report "Lexer error : %s" callerID;
13 (*      update_pos buf;*)
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;
20    pos_bol = 0;
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
27 let digit = ['0'-'9']
28 let alpha = ['a'-'z' 'A'-'Z']
29 let ident = (alpha) (alpha | digit | '_' )*
30 let wsp = [' ' '\t']
31 let mnot = ("NOT" wsp+)?
33 rule ruleStatement props = parse
34   | ['\n' ' ' '\t']+ { ruleStatement props lexbuf }
35 (* fixme strings *)
36   | "--" wsp* "[sql2cpp]" wsp+ (ident+ as n) wsp* "=" wsp* ([^'\n']* as v) '\n' 
37       { 
38         ruleStatement (Props.set props n v) lexbuf
39       }
40   | "--" { store ""; ignore (ruleComment lexbuf); ruleStatement props lexbuf }
41   | alpha [^ ';']+ as stmt ';' { Some (stmt,props) }
42   | _ { None }
43 and
44 ruleMain = parse
45   | wsp   { ruleMain lexbuf }
46   (* update line number *)
47   | '\n'  { advance_line lexbuf; ruleMain lexbuf}
49   | '('         { LPAREN }
50   | ')'         { RPAREN }
51   | ','   { COMMA }
52   | '.'   { DOT }
54   | "--" { store ""; ignore (ruleComment lexbuf); ruleMain lexbuf }
55 (*  | '"' { store ""; ruleInQuotes lexbuf } *)
57   | "SELECT" { SELECT }
58   | "CREATE" wsp+ "TABLE" { CREATE_TABLE }
59   | "INSERT" { INSERT }
60   | "REPLACE" { REPLACE }
61   | "UPDATE" { UPDATE }
62   | "DELETE" wsp+ "FROM" { DELETE_FROM }
64   | "OR" { OR }
65   | "INTO" { INTO }
66   | "VALUES" { VALUES }
67   | "WHERE" { WHERE }
68   | "FROM" { FROM }
69   | "*" { ASTERISK }
70   | "SET" { SET }
72   | "UNION" (wsp+ "ALL")? | "EXCEPT" | "INTERSECT" { COMPOUND_OP }
74   | "=" { EQUAL }
75   | "+" { PLUS }
76   | "-" { MINUS }
77   | "/" { DIVIDE }
78   | "%" { PERCENT }
79   | "!" { EXCL }
80   | "~" { TILDE }
81   | "NOT" { NOT }
83   (* column-constraint *)
84   | "NOT" wsp+ "NULL" { NOT_NULL }
85   | "UNIQUE" { UNIQUE }
86   | "PRIMARY" wsp+ "KEY" { PRIMARY_KEY }
87   | "AUTOINCREMENT" { AUTOINCREMENT }
88   | "DEFAULT" { DEFAULT }
90   | "TEXT" { T_TEXT }
91   | "INTEGER" { T_INTEGER }
92   | "INT" { T_INTEGER }
93   | "BLOB" { T_BLOB }
95   | "DISTINCT" { DISTINCT }
96   | "ALL" { ALL }
98   | "ORDER" wsp+ "BY" { ORDER_BY }
99   | "LIMIT" { LIMIT }
100   | "DESC" { DESC }
101   | "ASC" { ASC }
102   | "OFFSET" { OFFSET }
104   | "?" { PARAM Stmt.Raw.Next }
105   | "?" (digit+ as str) { PARAM (Stmt.Raw.Numbered (int_of_string str)) }
106   | [':' '@'] (ident as str) { PARAM (Stmt.Raw.Named str) }
108   | "ON" { ON }
109   | "CONFLICT" { CONFLICT }
110   | "USING" { USING }
112   | "IGNORE" { IGNORE }
113   | "REPLACE" { REPLACE }
114   | "ABORT" { ABORT }
115   | "FAIL" { FAIL }
116   | "ROLLBACK" { ROLLBACK }
118   | ("NATURAL" wsp+)? 
119     (("LEFT"|"RIGHT"|"FULL") wsp+)? 
120     (("INNER"|"OUTER"|"CROSS") wsp+)? 
121     "JOIN" { JOIN }
123   | mnot ("LIKE" | "GLOB" | "REGEXP" | "MATCH") { LIKE_OP }
124   
125   | "AS" { AS }
127   | "MAX" | "MIN" | "CONCAT" { FUNCTION }
128   | "ISNULL" | "NOTNULL" { TEST_NULL }
129   | "BETWEEB" { BETWEEN }
130   | "AND" { AND }
131   | "ESCAPE" { ESCAPE }
133   | ident as str { IDENT (str) }
134   | digit+ as str { INTEGER (int_of_string str) }
135   | eof         { EOF }
136   | _           { error lexbuf "ruleMain" }
137 and 
138 (*ruleInQuotes = parse
139   | '"'         { TEXT (!curStr) }
140   | eof         { error lexbuf "no terminating quote"; }
141   | '\n'        { advance_line lexbuf; error lexbuf "EOL before terminating quote"; }
142   | [^'"' '\n']+  { store (Lexing.lexeme lexbuf); ruleInQuotes lexbuf; }
143   | _           { error lexbuf "ruleInQuotes"; }
144 and*)
145 ruleComment = parse
146   | '\n'              { advance_line lexbuf; !curStr }
147   | eof         { !curStr }
148   | [^'\n']+    { store (Lexing.lexeme lexbuf); ruleComment lexbuf; }
149   | _           { error lexbuf "ruleComment"; }
153   let parse_rule lexbuf = ruleMain lexbuf