1/*  Part of SWI-Prolog
2
3    Author:        Jan Wielemaker
4    E-mail:        J.Wielemaker@vu.nl
5    WWW:           http://www.swi-prolog.org
6    Copyright (c)  2009-2013, University of Amsterdam
7                              VU University Amsterdam
8    All rights reserved.
9
10    Redistribution and use in source and binary forms, with or without
11    modification, are permitted provided that the following conditions
12    are met:
13
14    1. Redistributions of source code must retain the above copyright
15       notice, this list of conditions and the following disclaimer.
16
17    2. Redistributions in binary form must reproduce the above copyright
18       notice, this list of conditions and the following disclaimer in
19       the documentation and/or other materials provided with the
20       distribution.
21
22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33    POSSIBILITY OF SUCH DAMAGE.
34*/
35
36:- module(turtle_unicode,
37          [ mkclassify/1,
38            run/0
39          ]).
40
41/** <module> Generate turtle_chars.c
42*/
43
44run :-
45    mkclassify('turtle_chars.c', 'static ').
46
47%!  mkclassify(+File)
48%
49%   Generate the core of xml_unicode.c.
50
51mkclassify(File) :-
52    mkclassify(File, '').
53
54mkclassify(File, Decl) :-
55    tell(File),
56    call_cleanup(forall(list(List, _),
57                        mkfunc(List, Decl)),
58                 told).
59
60mkfunc(Name, Decl) :-
61    format('~wint~n', [Decl]),
62    format('wcis_~w(int c)~n', [Name]),
63    format('{ '),
64    list(Name, List),
65    mkswitch(List),
66    format('}~n~n').
67
68mkswitch(List) :-
69    mkswitch(List, 2).
70
71mkswitch([Low-High], Indent) :-
72    !,
73    indent(Indent),
74    format('return (c >= 0x~|~`0t~16r~4+ && c <= 0x~|~`0t~16r~4+);~n', [Low, High]).
75mkswitch([Value], Indent) :-
76    !,
77    indent(Indent),
78    format('return (c == 0x~|~`0t~16r~4+);', [Value]).
79mkswitch(List, Indent) :-
80    split(List, Low, High),
81    end(Low, MaxLow),
82    indent(Indent),
83    NextIndent is Indent + 2,
84    format('if ( c <= 0x~|~`0t~16r~4+ )~n', [MaxLow]),
85    indent(Indent),
86    format('{ '),
87    mkswitch(Low, NextIndent),
88    indent(Indent),
89    format('} else~n'),
90    indent(Indent),
91    format('{ '),
92    mkswitch(High, NextIndent),
93    indent(Indent),
94    format('}~n').
95
96end(List, Max) :-
97    last(List, Last),
98    (   Last = _-Max
99    ->  true
100    ;   Max = Last
101    ).
102
103split(List, Low, High) :-
104    length(List, Len),
105    Mid is Len//2,
106    length(Low, Mid),
107    append(Low, High, List).
108
109indent(N) :-
110    line_position(current_output, Pos),
111    Spaces is N - Pos,
112    format('~*c', [Spaces, 32]).
113
114
115
116list(pn_chars_base,
117     [ 0'A-0'Z,
118       0'a-0'z,
119       0x00C0-0x00D6,
120       0x00D8-0x00F6,
121       0x00F8-0x02FF,
122       0x0370-0x037D,
123       0x037F-0x1FFF,
124       0x200C-0x200D,
125       0x2070-0x218F,
126       0x2C00-0x2FEF,
127       0x3001-0xD7FF,
128       0xF900-0xFDCF,
129       0xFDF0-0xFFFD,
130       0x10000-0xEFFFF
131     ]).
132
133list(pn_chars_extra,
134     [ 0'-,
135       0'0-0'9,
136       0x00B7,
137       0x0300-0x036F,
138       0x203F-0x2040
139     ]).
140