aboutsummaryrefslogtreecommitdiffstats
path: root/src/3rdparty/masm/yarr/create_regex_tables
blob: 4c3dbbe3fbd494a23824b33418942a7a61ab78bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python

# Copyright (C) 2010, 2013-2017 Apple Inc. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

import os
import sys

types = {
    "wordchar": { "UseTable" : True, "data": ['_', ('0','9'), ('A', 'Z'), ('a','z')]},
    "wordUnicodeIgnoreCaseChar": { "UseTable" : False, "data": ['_', ('0', '9'), ('A', 'Z'), ('a', 'z'), 0x017f, 0x212a]},
    "nonwordchar": { "UseTable" : True, "Inverse": "wordchar", "data": ['`', (0, ord('0') - 1), (ord('9') + 1, ord('A') - 1), (ord('Z') + 1, ord('_') - 1), (ord('z') + 1, 0x10ffff)]},
    "nonwordUnicodeIgnoreCaseChar": { "UseTable" : False, "Inverse": "wordUnicodeIgnoreCaseChar", "data": ['`', (0, ord('0') - 1), (ord('9') + 1, ord('A') - 1), (ord('Z') + 1, ord('_') - 1), (ord('z') + 1, 0x017e), (0x0180, 0x2129), (0x212b, 0x10ffff)]},
    "newline": { "UseTable" : False, "data": ['\n', '\r', 0x2028, 0x2029]},
    "spaces": { "UseTable" : True, "data": [' ', ('\t', '\r'), 0xa0, 0x1680, 0x180e, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, (0x2000, 0x200a), 0xfeff]},
    "nonspaces": { "UseTable" : True, "Inverse": "spaces", "data": [(0, ord('\t') - 1), (ord('\r') + 1, ord(' ') - 1), (ord(' ') + 1, 0x009f), (0x00a1, 0x167f), (0x1681, 0x180d), (0x180f, 0x1fff), (0x200b, 0x2027), (0x202a, 0x202e), (0x2030, 0x205e), (0x2060, 0x2fff), (0x3001, 0xfefe), (0xff00, 0x10ffff)]},
    "digits": { "UseTable" : False, "data": [('0', '9')]},
    "nondigits": { "UseTable" : False, "Inverse": "digits", "data": [(0, ord('0') - 1), (ord('9') + 1, 0x10ffff)] }
}
entriesPerLine = 50
arrays = "";
functions = "";
emitTables = (len(sys.argv) < 2 or sys.argv[1] != "--no-tables")

for name, classes in types.items():
    ranges = [];
    size = 0;
    for _class in classes["data"]:
        if type(_class) == str:
            ranges.append((ord(_class), ord(_class)))
        elif type(_class) == int:
            ranges.append((_class, _class))
        else:
            (min, max) = _class;
            if type(min) == str:
                min = ord(min)
            if type(max) == str:
                max = ord(max)
            if max > 0x7f and min <= 0x7f:
                ranges.append((min, 0x7f))
                min = 0x80
            ranges.append((min,max))
    ranges.sort();
    
    if emitTables and classes["UseTable"] and (not "Inverse" in classes):
        array = ("static const char _%sData[65536] = {\n" % name);
        i = 0
        for (min,max) in ranges:
            while i < min:
                i = i + 1
                array += ('0,')
                if (i % entriesPerLine == 0) and (i != 0):
                    array += ('\n')
            while i <= max:
                i = i + 1
                if (i == 65536):
                    array += ("1")
                else:
                    array += ('1,')
                if (i % entriesPerLine == 0) and (i != 0):
                    array += ('\n')
        while i < 0xffff:
            array += ("0,")
            i = i + 1;
            if (i % entriesPerLine == 0) and (i != 0):
                array += ('\n')
        if i == 0xffff:
            array += ("0")
        array += ("\n};\n\n");
        arrays += array
    
    # Generate createFunction:
    function = "";
    function += ("std::unique_ptr<CharacterClass> %sCreate()\n" % name)
    function += ("{\n")
    if emitTables and classes["UseTable"]:
        if "Inverse" in classes:
            function += ("    auto characterClass = std::make_unique<CharacterClass>(_%sData, true);\n" % (classes["Inverse"]))
        else:
            function += ("    auto characterClass = std::make_unique<CharacterClass>(_%sData, false);\n" % (name))
    else:
        function += ("    auto characterClass = std::make_unique<CharacterClass>();\n")
    hasNonBMPCharacters = False
    for (min, max) in ranges:
        if (min == max):
            if (min > 127):
                function += ("    characterClass->m_matchesUnicode.append(0x%04x);\n" % min)
            else:
                function += ("    characterClass->m_matches.append(0x%02x);\n" % min)
            continue
        if (min > 127) or (max > 127):
            function += ("    characterClass->m_rangesUnicode.append(CharacterRange(0x%04x, 0x%04x));\n" % (min, max))
        else:
            function += ("    characterClass->m_ranges.append(CharacterRange(0x%02x, 0x%02x));\n" % (min, max))
        if max >= 0x10000:
            hasNonBMPCharacters = True
    function += ("    characterClass->m_hasNonBMPCharacters = %s;\n" % ("true" if hasNonBMPCharacters else "false"))
    function += ("    return characterClass;\n")
    function += ("}\n\n")
    functions += function

if (len(sys.argv) > 1):
    path = sys.argv[-1]
    dirname = os.path.dirname(path)
    if not os.path.isdir(dirname):
        os.makedirs(dirname)
    f = open(path, "w")
    f.write(arrays)
    f.write(functions)
    f.close()
else:
    print(arrays)
    print(functions)