View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Matt Lilley
    4    E-mail:        matt.s.lilley@gmail.com
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2014, Mike Elston, Matt Lilley
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35/*  PostgreSQL is a trademark of the PostgreSQL Global Development Group.
   36    Microsoft, SQL Server, and Windows are either registered trademarks or
   37    trademarks of Microsoft Corporation in the United States and/or other
   38    countries. SQLite is a registered trademark of Hipp, Wyrick & Company,
   39    Inc in the United States. All other trademarks or registered trademarks
   40    are the property of their respective owners.
   41*/
   42
   43:-module(sql_tokenizer,
   44         [sql_tokens//1]).   45
   46:-use_module(library(cql/cql), [sql_gripe/3]).   47
   48% No codes -> no tokens
   49sql_tokens([], [], []):- !.
   50sql_tokens([Token|Tokens])-->
   51        optional_whitespace,
   52        sql_token(Token),
   53        optional_whitespace,
   54        sql_tokens(Tokens).
   55
   56optional_whitespace-->
   57	char_in(_, " \t\n\r"),
   58        !,
   59        optional_whitespace.
   60optional_whitespace--> [].
   61
   62%:- meta_predicate read_until(//,*,?,?).
   63
   64:- if(current_prolog_flag(double_quotes,string)).   65:- multifile check:valid_string_goal/1.   66check:valid_string_goal(sql_tokenizer:read_until(S,_,_,_)) :- string(S).
   67
   68read_until(Terminator, Codes) -->
   69	{ string_codes(Terminator, List)
   70	},
   71	read_until_(List, Codes).
   72:- else.   73read_until(Terminator, Codes) -->
   74	read_until_(Terminator, Codes).
   75:- endif.   76
   77read_until_(_, [], [], []).
   78read_until_(Terminator, [])-->
   79        Terminator, !.
   80read_until_(Terminator, [Code|Codes])-->
   81        [Code],
   82        read_until_(Terminator, Codes).
   83
   84
   85numeric_codes([Code|Codes])-->
   86	char_in(Code, "0123456789."),
   87        !,
   88        numeric_codes(Codes).
   89numeric_codes([])--> [], !.
 char_in(+Code, +Set) is semidet
True when Code appears in Set. Set is a double-quoted string. Calls code_in_set/2 to deal with the SWI-6/7 compatibility.
   97char_in(Code, Set) -->
   98	[Code],
   99	{ code_in_set(Code, Set) }.
  100
  101:- if(current_prolog_flag(double_quotes,string)).  102:- multifile check:valid_string_goal/1.  103
  104check:valid_string_goal(sql_tokenizer:char_in(_,S,_,_)) :- string(S).
  105check:valid_string_goal(sql_tokenizer:code_in_set(_,S)) :- string(S).
  106
  107code_in_set(Code, Set) :-
  108	string_code(_, Set, Code), !.
  109
  110:- else.  111
  112code_in_set(Code, Set) :-
  113	memberchk(Code, Set).
  114
  115:- endif.  116
  117quoted_literal([39|Codes], [39, 39|In], Out):-
  118        !,
  119        quoted_literal(Codes, In, Out).
  120quoted_literal([], [39|In], In):-!.
  121quoted_literal([Code|Codes])-->
  122        [Code],
  123        quoted_literal(Codes).
  124
  125
  126sql_token(comment(long, Codes))-->
  127        "/*", !, read_until("*/", Codes).
  128
  129sql_token(comment(short, Codes))-->
  130        "--", !, read_until("\n", Codes).
  131
  132% All of these are a token of their own.
  133sql_token('=')--> "=", !.
  134sql_token('<>')--> "!=", !, {sql_gripe(1,'The not-equals operator in SQL is <> and not !=', [])}.
  135sql_token('<>')--> "! =", !, {sql_gripe(1,'The not-equals operator in SQL is <> and not ! =', [])}.
  136% There are some WEIRD things that people put in views...
  137sql_token('>=')--> "! <", !, {sql_gripe(1,'The greater-than-or-equal-to operator in SQL is >= and not ! <', [])}.
  138sql_token('>=')--> "!<", !, {sql_gripe(1,'The greater-than-or-equal-to operator in SQL is >= and not !<', [])}.
  139sql_token('<=')--> "!>", !, {sql_gripe(1,'The less-than-or-equal-to operator in SQL is <= and not !>', [])}.
  140sql_token('<=')--> "! >", !, {sql_gripe(1,'The less-than-or-equal-to operator in SQL is <= and not ! >', [])}.
  141sql_token('<>')--> "<>", !.
  142sql_token('>=')--> ">=", !.
  143sql_token('<=')--> "<=", !.
  144sql_token('<')--> "<", !.
  145sql_token('>')--> ">", !.
  146sql_token('.')--> ".", !.
  147sql_token(',')--> ",", !.
  148sql_token('(')--> "(", !.
  149sql_token(')')--> ")", !.
  150sql_token('/')--> "/", !.
  151sql_token('+')--> "+", !.
  152sql_token('*')--> "*", !.
  153sql_token('-')--> "-", !.
  154sql_token('{')--> "{", !.
  155sql_token('}')--> "}", !.
  156
  157
  158sql_token(literal(Literal, string))-->
  159        "'",
  160        !,
  161        quoted_literal(Codes),
  162        {atom_codes(Literal, Codes)}.
  163
  164
  165sql_token(literal(Literal, identifier))-->
  166        "\"",
  167        !,
  168        read_until("\"", Codes),
  169        {atom_codes(Literal, Codes)}.
  170
  171% This should return numeric/2 instead of decimal/2, according to http://msdn.microsoft.com/en-us/library/ms187746
  172% But it also says they are functionally equivalent
  173sql_token(literal(Literal, Type))-->
  174	char_in(Code, "0123456789"),
  175        !,
  176        numeric_codes(Codes),
  177        {number_codes(Literal, [Code|Codes])},
  178        {(integer(Literal)->
  179            length(Codes, L),
  180            LL is L+1,
  181            ( LL > 10->
  182                Type = decimal(LL, 0)
  183            ; otherwise->
  184                Type = int(LL)
  185            )
  186         ; Code == 0'0, Codes = [0'.|_]->
  187            % 0.00 is numeric(2,2) not (3,2). I suppose the leading 0 is just a placeholder?
  188            length(Codes, L),
  189            P is L - 1,
  190            Type = decimal(P, P)
  191         ; otherwise->
  192            nth1(P, Codes, 0'.), %'
  193
  194            length(Codes, L),
  195            S is L - P,
  196            PP is P + S,
  197            Type = decimal(PP, S)
  198         )}.
  199
  200sql_token(literal(Literal, identifier))-->
  201        "[",
  202        !,
  203        read_until("]", Codes),
  204        {atom_codes(Literal, Codes)}.
  205
  206sql_token(Token)-->
  207        !,
  208        sql_token_1(Codes),
  209        {atom_codes(Token, Codes)}.
  210
  211sql_token_1([], [], []):- !.
  212% Any of these codes should end the current token. This allows us to correctly
  213% tokens 3+4 as the sum of two values rather than a single token
  214sql_token_1([], [Terminator|Codes], [Terminator|Codes]):-
  215        code_in_set(Terminator, ".,()*+-/<=> \t\n\r"), !.
  216
  217% Everything else goes into the token
  218sql_token_1([Code|Codes])-->
  219        [Code],
  220        sql_token_1(Codes)