38
38
*/
39
39
package com .oracle .graal .python .builtins .modules ;
40
40
41
- import java .util .ArrayList ;
42
41
import java .util .List ;
43
42
import java .util .regex .Matcher ;
44
43
import java .util .regex .Pattern ;
62
61
public class SREModuleBuiltins extends PythonBuiltins {
63
62
@ Override
64
63
protected List <? extends NodeFactory <? extends PythonBuiltinNode >> getNodeFactories () {
65
- return new ArrayList <> ();
64
+ return SREModuleBuiltinsFactory . getFactories ();
66
65
}
67
66
68
67
/**
@@ -71,7 +70,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinNode>> getNodeFactor
71
70
@ Builtin (name = "tregex_preprocess" , fixedNumOfArguments = 1 )
72
71
@ GenerateNodeFactory
73
72
abstract static class TregexPreprocessNode extends PythonUnaryBuiltinNode {
74
- @ CompilationFinal private Pattern pattern ;
73
+ @ CompilationFinal private Pattern commentPattern ;
75
74
76
75
@ Specialization
77
76
Object run (PString str ) {
@@ -80,18 +79,31 @@ Object run(PString str) {
80
79
81
80
@ Specialization
82
81
Object run (String str ) {
83
- str .replaceAll ("[^\\ []?#[^\\ ]]*\n " , "" );
84
- if (pattern == null ) {
82
+ if (commentPattern == null ) {
85
83
CompilerDirectives .transferToInterpreterAndInvalidate ();
86
- pattern = Pattern .compile ("(?<CMT> #[^\\ ]]*\n )" );
84
+ commentPattern = Pattern .compile ("(#[^\\ ]]*\n )" );
87
85
}
88
86
return replaceAll (str );
89
87
}
90
88
91
89
@ TruffleBoundary
92
90
private String replaceAll (String r ) {
93
- Matcher matcher = pattern .matcher (r );
94
- return matcher .replaceAll ("" );
91
+ Matcher matcher = commentPattern .matcher (r );
92
+ String res = matcher .replaceAll ("" );
93
+ StringBuilder sb = new StringBuilder ();
94
+ int charclassNestingLevel = 0 ;
95
+ for (int i = 0 ; i < res .length (); i ++) {
96
+ char c = res .charAt (i );
97
+ if (c == '[' ) {
98
+ charclassNestingLevel ++;
99
+ } else if (c == ']' ) {
100
+ charclassNestingLevel --;
101
+ }
102
+ if (!Character .isWhitespace (c ) || charclassNestingLevel != 0 ) {
103
+ sb .append (res .charAt (i ));
104
+ }
105
+ }
106
+ return sb .toString ();
95
107
}
96
108
97
109
@ Fallback
0 commit comments