ยปCore Development>Code coverage>Lib/sre_constants.py

Python code coverage for Lib/sre_constants.py

#countcontent
1n/a#
2n/a# Secret Labs' Regular Expression Engine
3n/a#
4n/a# various symbols used by the regular expression engine.
5n/a# run this script to update the _sre include files!
6n/a#
7n/a# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
8n/a#
9n/a# See the sre.py file for information on usage and redistribution.
10n/a#
11n/a
12n/a"""Internal support module for sre"""
13n/a
14n/a# update when constants are added or removed
15n/a
16n/aMAGIC = 20140917
17n/a
18n/afrom _sre import MAXREPEAT, MAXGROUPS
19n/a
20n/a# SRE standard exception (access as sre.error)
21n/a# should this really be here?
22n/a
23n/aclass error(Exception):
24n/a def __init__(self, msg, pattern=None, pos=None):
25n/a self.msg = msg
26n/a self.pattern = pattern
27n/a self.pos = pos
28n/a if pattern is not None and pos is not None:
29n/a msg = '%s at position %d' % (msg, pos)
30n/a if isinstance(pattern, str):
31n/a newline = '\n'
32n/a else:
33n/a newline = b'\n'
34n/a self.lineno = pattern.count(newline, 0, pos) + 1
35n/a self.colno = pos - pattern.rfind(newline, 0, pos)
36n/a if newline in pattern:
37n/a msg = '%s (line %d, column %d)' % (msg, self.lineno, self.colno)
38n/a else:
39n/a self.lineno = self.colno = None
40n/a super().__init__(msg)
41n/a
42n/a
43n/aclass _NamedIntConstant(int):
44n/a def __new__(cls, value, name):
45n/a self = super(_NamedIntConstant, cls).__new__(cls, value)
46n/a self.name = name
47n/a return self
48n/a
49n/a def __str__(self):
50n/a return self.name
51n/a
52n/a __repr__ = __str__
53n/a
54n/aMAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
55n/a
56n/adef _makecodes(names):
57n/a names = names.strip().split()
58n/a items = [_NamedIntConstant(i, name) for i, name in enumerate(names)]
59n/a globals().update({item.name: item for item in items})
60n/a return items
61n/a
62n/a# operators
63n/a# failure=0 success=1 (just because it looks better that way :-)
64n/aOPCODES = _makecodes("""
65n/a FAILURE SUCCESS
66n/a
67n/a ANY ANY_ALL
68n/a ASSERT ASSERT_NOT
69n/a AT
70n/a BRANCH
71n/a CALL
72n/a CATEGORY
73n/a CHARSET BIGCHARSET
74n/a GROUPREF GROUPREF_EXISTS GROUPREF_IGNORE
75n/a IN IN_IGNORE
76n/a INFO
77n/a JUMP
78n/a LITERAL LITERAL_IGNORE
79n/a MARK
80n/a MAX_UNTIL
81n/a MIN_UNTIL
82n/a NOT_LITERAL NOT_LITERAL_IGNORE
83n/a NEGATE
84n/a RANGE
85n/a REPEAT
86n/a REPEAT_ONE
87n/a SUBPATTERN
88n/a MIN_REPEAT_ONE
89n/a RANGE_IGNORE
90n/a
91n/a MIN_REPEAT MAX_REPEAT
92n/a""")
93n/adel OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT
94n/a
95n/a# positions
96n/aATCODES = _makecodes("""
97n/a AT_BEGINNING AT_BEGINNING_LINE AT_BEGINNING_STRING
98n/a AT_BOUNDARY AT_NON_BOUNDARY
99n/a AT_END AT_END_LINE AT_END_STRING
100n/a AT_LOC_BOUNDARY AT_LOC_NON_BOUNDARY
101n/a AT_UNI_BOUNDARY AT_UNI_NON_BOUNDARY
102n/a""")
103n/a
104n/a# categories
105n/aCHCODES = _makecodes("""
106n/a CATEGORY_DIGIT CATEGORY_NOT_DIGIT
107n/a CATEGORY_SPACE CATEGORY_NOT_SPACE
108n/a CATEGORY_WORD CATEGORY_NOT_WORD
109n/a CATEGORY_LINEBREAK CATEGORY_NOT_LINEBREAK
110n/a CATEGORY_LOC_WORD CATEGORY_LOC_NOT_WORD
111n/a CATEGORY_UNI_DIGIT CATEGORY_UNI_NOT_DIGIT
112n/a CATEGORY_UNI_SPACE CATEGORY_UNI_NOT_SPACE
113n/a CATEGORY_UNI_WORD CATEGORY_UNI_NOT_WORD
114n/a CATEGORY_UNI_LINEBREAK CATEGORY_UNI_NOT_LINEBREAK
115n/a""")
116n/a
117n/a
118n/a# replacement operations for "ignore case" mode
119n/aOP_IGNORE = {
120n/a GROUPREF: GROUPREF_IGNORE,
121n/a IN: IN_IGNORE,
122n/a LITERAL: LITERAL_IGNORE,
123n/a NOT_LITERAL: NOT_LITERAL_IGNORE,
124n/a RANGE: RANGE_IGNORE,
125n/a}
126n/a
127n/aAT_MULTILINE = {
128n/a AT_BEGINNING: AT_BEGINNING_LINE,
129n/a AT_END: AT_END_LINE
130n/a}
131n/a
132n/aAT_LOCALE = {
133n/a AT_BOUNDARY: AT_LOC_BOUNDARY,
134n/a AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
135n/a}
136n/a
137n/aAT_UNICODE = {
138n/a AT_BOUNDARY: AT_UNI_BOUNDARY,
139n/a AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
140n/a}
141n/a
142n/aCH_LOCALE = {
143n/a CATEGORY_DIGIT: CATEGORY_DIGIT,
144n/a CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
145n/a CATEGORY_SPACE: CATEGORY_SPACE,
146n/a CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
147n/a CATEGORY_WORD: CATEGORY_LOC_WORD,
148n/a CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
149n/a CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
150n/a CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
151n/a}
152n/a
153n/aCH_UNICODE = {
154n/a CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
155n/a CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
156n/a CATEGORY_SPACE: CATEGORY_UNI_SPACE,
157n/a CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
158n/a CATEGORY_WORD: CATEGORY_UNI_WORD,
159n/a CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
160n/a CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
161n/a CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
162n/a}
163n/a
164n/a# flags
165n/aSRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
166n/aSRE_FLAG_IGNORECASE = 2 # case insensitive
167n/aSRE_FLAG_LOCALE = 4 # honour system locale
168n/aSRE_FLAG_MULTILINE = 8 # treat target as multiline string
169n/aSRE_FLAG_DOTALL = 16 # treat target as a single string
170n/aSRE_FLAG_UNICODE = 32 # use unicode "locale"
171n/aSRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
172n/aSRE_FLAG_DEBUG = 128 # debugging
173n/aSRE_FLAG_ASCII = 256 # use ascii "locale"
174n/a
175n/a# flags for INFO primitive
176n/aSRE_INFO_PREFIX = 1 # has prefix
177n/aSRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
178n/aSRE_INFO_CHARSET = 4 # pattern starts with character from given set
179n/a
180n/aif __name__ == "__main__":
181n/a def dump(f, d, prefix):
182n/a items = sorted(d)
183n/a for item in items:
184n/a f.write("#define %s_%s %d\n" % (prefix, item, item))
185n/a with open("sre_constants.h", "w") as f:
186n/a f.write("""\
187n/a/*
188n/a * Secret Labs' Regular Expression Engine
189n/a *
190n/a * regular expression matching engine
191n/a *
192n/a * NOTE: This file is generated by sre_constants.py. If you need
193n/a * to change anything in here, edit sre_constants.py and run it.
194n/a *
195n/a * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
196n/a *
197n/a * See the _sre.c file for information on usage and redistribution.
198n/a */
199n/a
200n/a""")
201n/a
202n/a f.write("#define SRE_MAGIC %d\n" % MAGIC)
203n/a
204n/a dump(f, OPCODES, "SRE_OP")
205n/a dump(f, ATCODES, "SRE")
206n/a dump(f, CHCODES, "SRE")
207n/a
208n/a f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
209n/a f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
210n/a f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
211n/a f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
212n/a f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
213n/a f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
214n/a f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
215n/a f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
216n/a f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
217n/a
218n/a f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
219n/a f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
220n/a f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
221n/a
222n/a print("done")