1- import std/ re
1+ import std/ options
2+ import pkg/ pyre
23
34import ../ Objects / [
45 pyobject,
@@ -16,23 +17,69 @@ import ./private/gen
1617import ../ Python / getargs/ dispatch
1718
1819genModule re
20+ declarePyError Pattern , Base :
21+ msg{.member .}: PyStrObject
22+ pattern{.member , nil2none .}: PyObject
23+ pos{.member , nil2none .}: PyObject
24+ lineno{.member , nil2none .}: PyObject
25+ colno{.member , nil2none .}: PyObject
26+ genProperty ReModule , " PatternError" , PatternError , pyPatternErrorObjectType
1927
2028declarePyType RePattern ():
21- regex: Regex
29+ regex: pyre. Regex
2230
2331declarePyType ReMatch ():
2432 text: PyStrObject
25- first: int
26- last: int
27- groups: seq [string ]
33+ value: pyre.Match
2834
35+ proc setPatternErrorLocation (error: PyPatternErrorObject ; pattern, pos: PyObject ): PyObject =
36+ if pattern.isPyNone or pos.isPyNone:
37+ return pyNone
38+ if not pattern.ofPyStrObject or not pos.ofPyIntObject:
39+ return newTypeError (newPyAscii (" pattern must be a string and pos must be an integer" ))
40+ let patternText = $ PyStrObject (pattern).str
41+ let position = PyIntObject (pos).toSomeSignedIntUnsafe[:int ]
42+ var lineno = 1
43+ var lineStart = - 1
44+ for index in 0 ..< min (position, patternText.len):
45+ if patternText[index] == '\n ' :
46+ inc lineno
47+ lineStart = index
48+ error.lineno = newPyInt lineno
49+ error.colno = newPyInt (position - lineStart)
50+ pyNone
51+
52+ implPatternErrorMagic init (msg: PyStrObject , pattern= pyNoneObj, pos= pyNoneObj):
53+ self.msg = msg
54+ if not pattern.isPyNone:
55+ self.pattern = pattern
56+ if not pos.isPyNone:
57+ self.pos = pos
58+ retIfExc setPatternErrorLocation (self, pattern, pos)
59+ self.args = newPyTuple [msg]
60+ if not pattern.isPyNone and not pos.isPyNone:
61+ let position = PyIntObject (pos).toSomeSignedIntUnsafe[:int ]
62+ self.args = newPyTuple [newPyStr ($ msg.str &
63+ " at position " & $ position)]
64+ pyNone
2965
30- proc toRegexFlags (flags: int ): set [RegexFlag ] =
31- result = {reStudy}
32- if (flags and 2 ) != 0 : result .incl reIgnoreCase
33- if (flags and 8 ) != 0 : result .incl reMultiLine
34- if (flags and 16 ) != 0 : result .incl reDotAll
35- if (flags and 64 ) != 0 : result .incl reExtended
66+ proc newPatternError * (error: pyre.PatternError ): PyPatternErrorObject =
67+ let result = newPatternError (newPyStr error.msg)
68+ result .msg = newPyStr error.msg
69+ if error.pattern.len != 0 :
70+ result .pattern = newPyStr error.pattern
71+ if error.pos >= 0 :
72+ result .pos = newPyInt error.pos
73+ var lineno = 1
74+ var lineStart = - 1
75+ for index in 0 ..< error.pos:
76+ if error.pattern[index] == '\n ' :
77+ inc lineno
78+ lineStart = index
79+ result .lineno = newPyInt lineno
80+ result .colno = newPyInt (error.pos - lineStart)
81+ result .args = newPyTuple [newPyStr (error.msg & " at position " & $ error.pos)]
82+ result
3683
3784proc compilePattern (pattern: PyObject ; flags = 0 ): PyObject =
3885 if pattern.ofPyRePatternObject:
@@ -41,10 +88,10 @@ proc compilePattern(pattern: PyObject; flags = 0): PyObject =
4188 return newTypeError (newPyAscii (" first argument must be string or compiled pattern" ))
4289 try :
4390 let result = newPyRePatternSimple ()
44- result .regex = re ($ PyStrObject (pattern).str, toRegexFlags ( flags) )
91+ result .regex = pyre. compile ($ PyStrObject (pattern).str, flags)
4592 return result
46- except RegexError as e:
47- return newValueError ( newPyStr e.msg )
93+ except PatternError as e:
94+ return newPatternError (e )
4895
4996proc regexText (textObj: PyObject ; text: var string ; matchText: var PyStrObject ): PyObject =
5097 if textObj.ofPyStrObject:
@@ -60,23 +107,22 @@ proc regexText(textObj: PyObject; text: var string; matchText: var PyStrObject):
60107 return newTypeError (newPyAscii (" expected string or bytes-like object" ))
61108 pyNone
62109
63- proc makeMatch (pattern: PyRePatternObject ; text: string ; matchText: PyStrObject ; start = 0 ): PyObject =
64- var groups = newSeq [string ](20 )
65- let bounds = findBounds (text, pattern.regex, groups, start)
66- if bounds.first < 0 :
67- return pyNone
68- let result = newPyReMatchSimple ()
69- result .text = matchText
70- result .first = bounds.first
71- result .last = bounds.last
72- result .groups = groups
73- result
110+ proc makeMatch (pattern: PyRePatternObject ; text: string ; matchText: PyStrObject ;
111+ anchored = false ): PyObject =
112+ try :
113+ let found = if anchored: pyre.match (pattern.regex, text)
114+ else : pyre.search (pattern.regex, text)
115+ if found.isNone:
116+ return pyNone
117+ let result = newPyReMatchSimple ()
118+ result .text = matchText
119+ result .value = found.get
120+ result
121+ except PatternError as e:
122+ newPatternError (e)
74123
75124proc implMatch (pattern: PyRePatternObject ; text: string ; matchText: PyStrObject ): PyObject =
76- let result = makeMatch (pattern, text, matchText)
77- if result .isPyNone: return result
78- if PyReMatchObject (result ).first != 0 : return pyNone
79- result
125+ makeMatch (pattern, text, matchText, anchored = true )
80126
81127proc implSearch (pattern: PyRePatternObject ; text: string ; matchText: PyStrObject ): PyObject =
82128 makeMatch (pattern, text, matchText)
@@ -94,11 +140,12 @@ implRePatternMethod match(textObj: PyObject):
94140 implMatch (self, text, matchText)
95141
96142implReMatchMethod group (index: int ):
97- if index == 0 :
98- return newPyStr (($ self.text.str)[self.first.. self.last])
99- if index < 1 or index > self.groups.len:
143+ var value: Option [string ]
144+ try :
145+ value = self.value.group (index)
146+ except IndexDefect :
100147 return newIndexError (newPyAscii (" no such group" ))
101- newPyStr self.groups[index - 1 ]
148+ if value.isNone: pyNone else : newPyStr value.get
102149
103150implReModuleMethod compile (pattern: PyObject , flags = 0 ): compilePattern (pattern, flags)
104151
@@ -126,16 +173,24 @@ implReModuleMethod findall(patternObj: PyObject, textObj: PyObject):
126173 var text: string
127174 var matchText: PyStrObject
128175 retIfExc regexText (textObj, text, matchText)
129- let regex = PyRePatternObject (pattern).regex
130176 var result = newPyList ()
131- var start = 0
132- while start <= text.len:
133- let found = find (text, regex, start)
134- if found < 0 : break
135- var groups = newSeq [string ](20 )
136- let bounds = findBounds (text, regex, groups, found)
137- result .add newPyStr (text[bounds.first.. bounds.last])
138- start = if bounds.last < found: found + 1 else : bounds.last + 1
177+ try :
178+ let regex = PyRePatternObject (pattern).regex
179+ let captureCount = regex.captureCount
180+ if captureCount == 0 :
181+ for found in pyre.finditer (regex, text):
182+ result .add newPyStr found.group ().get
183+ elif captureCount == 1 :
184+ for captures in pyre.findallCaptures (regex, text):
185+ result .add newPyStr captures[0 ]
186+ else :
187+ for captures in pyre.findallCaptures (regex, text):
188+ var groups: seq [PyObject ]
189+ for capture in captures:
190+ groups.add newPyStr capture
191+ result .add newPyTuple groups
192+ except PatternError as e:
193+ return newPatternError (e)
139194 result
140195
141196implReModuleMethod sub (patternObj: PyObject , replacement: PyObject , textObj: PyObject ):
@@ -146,8 +201,34 @@ implReModuleMethod sub(patternObj: PyObject, replacement: PyObject, textObj: PyO
146201 var text: string
147202 var matchText: PyStrObject
148203 retIfExc regexText (textObj, text, matchText)
149- newPyStr replace (text, PyRePatternObject (pattern).regex,
150- $ PyStrObject (replacement).str)
204+ try :
205+ newPyStr pyre.sub (PyRePatternObject (pattern).regex,
206+ $ PyStrObject (replacement).str, text)
207+ except PatternError as e:
208+ newPatternError (e)
209+
210+ implReModuleMethod subn (patternObj: PyObject , replacement: PyObject , textObj: PyObject ):
211+ let pattern = compilePattern (patternObj)
212+ retIfExc pattern
213+ if not replacement.ofPyStrObject:
214+ return newTypeError (newPyAscii (" subn() arguments must be strings" ))
215+ var text: string
216+ var matchText: PyStrObject
217+ retIfExc regexText (textObj, text, matchText)
218+ try :
219+ let replaced = pyre.subn (PyRePatternObject (pattern).regex,
220+ $ PyStrObject (replacement).str, text)
221+ newPyTuple ([newPyStr replaced.value, newPyInt replaced.count])
222+ except PatternError as e:
223+ newPatternError (e)
224+
225+ implReModuleMethod escape (textObj: PyObject ):
226+ if not textObj.ofPyStrObject:
227+ return newTypeError (newPyAscii (" escape() argument must be str" ))
228+ try :
229+ newPyStr pyre.escape ($ PyStrObject (textObj).str)
230+ except PatternError as e:
231+ newPatternError (e)
151232
152233implReModuleMethod split (patternObj: PyObject , textObj: PyObject ):
153234 let pattern = compilePattern (patternObj)
@@ -156,6 +237,9 @@ implReModuleMethod split(patternObj: PyObject, textObj: PyObject):
156237 var matchText: PyStrObject
157238 retIfExc regexText (textObj, text, matchText)
158239 var result = newPyList ()
159- for part in split (text, PyRePatternObject (pattern).regex):
160- result .add newPyStr part
240+ try :
241+ for part in pyre.split (PyRePatternObject (pattern).regex, text):
242+ result .add newPyStr part
243+ except PatternError as e:
244+ return newPatternError (e)
161245 result
0 commit comments