Ticket #174: regexp-lit-ops3.patch
File regexp-lit-ops3.patch, 35.6 KB (added by hopscc, 14 years ago) |
---|
-
Source/Expr.cobra
2659 2659 base._bindImp 2660 2660 _type = .compiler.nilType 2661 2661 2662 class RegexpLit 2663 is partial 2664 inherits AtomicLiteral 2665 """ 2666 A Regular Expression Literal. 2662 2667 2668 Form is similar to that of a (raw) string; single or double quote delimited but with a 're' prefix. 2669 It may be either a just a pattern (delimited or not) re'<Ptn>' or re'/<Ptn>/' 2670 or a delimited pattern with option flags re'/<Ptn>/<flags>'. 2671 2672 If flags are desired the pattern must be delimited (with matching /) and any flags follow the trailing / 2673 2674 Flags can be one or more of: 2675 i - Ignorecase = case-insensitive matching. 2676 c - Compile = compile the RE. This yields faster execution but increases startup time 2677 s - Singleline = Single-line mode. Change so '.' matches every char instead of '[^\n]*' (every character except \n). 2678 m - Multiline = Multiline mode. Change so '^' and '$' match start and end of lines instead of start and end of entire string 2679 x - ExplicitCaptures - the only valid captures are explicitly named or numbered groups of the form (?<name>�). 2680 W - Ignores unescaped white space in the pattern and enables comments marked with '#'. 2681 2682 e.g 2683 re'[A-Za-z_][A-Za-z0-9_]*' 2684 re"/c'(?:\\'|\\?[^'])'/" 2685 re'/^\s*@param\s+(.*)$/m' # multiline 2686 re'/args\s+\w+/i' # Case insensitive 2687 """ 2688 2689 var _string as String # Regexp raw pattern (with no surrounding quotes) with possible delimiters and flags 2690 var _pattern as String # Regexp pattern (no delimiters) 2691 var _flags as String = '' 2692 2693 const _validFlags = 'icsmxW' 2694 2695 cue init(token as IToken) 2696 require token.which.startsWith('REGEXP') 2697 base.init(token as IToken) 2698 _string = token.value to String 2699 _pattern='' 2700 2701 get string from var 2702 get pattern from var 2703 get flags from var 2704 2705 def _bindInt is override 2706 if not _type 2707 _type = .compiler.regexpType 2708 base._bindInt 2709 2710 def _bindImp is override 2711 str = _string 2712 if str.startsWith('/') 2713 str = str[1:] 2714 idx = str.lastIndexOf('/') 2715 if idx == -1 2716 sugg = 'Did you mean "/[str]/"' 2717 .throwError('Regexp Literal has an opening "/" delimiter but no trailing delimiter. [sugg]') 2718 p = str[:idx] # p = str.before('/') 2719 flags = str[idx+1:] # flags = str.after('/') 2720 else 2721 p = str 2722 flags = '' # no flags 2723 if not p.length, .throwError('Regexp Literal has no pattern specified.') 2724 _pattern = p 2725 for fl in flags 2726 if not fl in _validFlags 2727 .throwError('Unrecognised Regexp flag "[fl]" in Regexp Literal "[.string]". Flag must be one of the characters "[_validFlags]".') 2728 _flags = flags 2729 if not _type 2730 _type = .compiler.regexpType 2731 base._bindImp 2732 2733 def toCobraSource as String is override 2734 return .token.text 2735 2663 2736 class StringLit 2664 2737 is partial 2665 2738 inherits AtomicLiteral -
Source/BackEndClr/SharpGenerator.cobra
3832 3832 return 'null' 3833 3833 3834 3834 3835 class RegexpLit 3836 is partial 3837 3838 var _flagsToOptions = { 3839 c'i' : 'RegexOptions.IgnoreCase', 3840 c'c' : 'RegexOptions.Compiled', 3841 c's' : 'RegexOptions.Singleline', 3842 c'm' : 'RegexOptions.Multiline', 3843 c'x' : 'RegexOptions.ExplicitCapture', 3844 c'W' : 'RegexOptions.IgnorePatternWhitespace', 3845 } 3846 3847 get asSharp as String is override 3848 return Utils.sharpStringLiteralFor(_string) 3849 3850 def writeSharpDef(sw as CurlyWriter, parens as bool) is override 3851 if parens, sw.write('( ') 3852 sw.write('new [_type.sharpRef]( ') 3853 sw.write('@"[.pattern]"') 3854 3855 if .flags.length 3856 assert _flagsToOptions.count == _validFlags.length, 'RegexpLit flagsMap out of sync with list of valid flags' 3857 sb = StringBuilder(',') 3858 sep = '' 3859 for fl in .flags 3860 sb.append('[sep] System.Text.RegularExpressions.[_flagsToOptions[fl]] ') 3861 sep = '|' 3862 sw.write(sb.toString) 3863 sw.write(')') 3864 if parens, sw.write(' )') 3865 3835 3866 class StringLit 3836 3867 is partial 3837 3868 3838 3869 get asSharp as String is override 3839 3870 return Utils.sharpStringLiteralFor(_string) 3840 3871 3841 3842 3872 class StringSubstLit 3843 3873 is partial 3844 3874 … … 4467 4497 itemIndex += 1 4468 4498 sw.write(')') 4469 4499 4500 class RegexpExpr 4501 is partial 4502 4503 def writeSharpDef(sw as CurlyWriter, parens as bool) is override 4504 if parens, sw.write('(') 4505 _writeSharpDef(sw) 4506 if parens, sw.write(')') 4470 4507 4508 def _writeSharpDef(sw as CurlyWriter) is override 4509 left = _left 4510 right = _right 4511 op = _op 4512 4513 if left.type.isDynamic or right.type.isDynamic 4514 call = 'CobraLangInternal.CobraImp.DynamicRegexp' 4515 branch op 4516 on 'TILDE', call += 'Match' 4517 on 'RE_HASMATCH', call += 'HasMatch' 4518 on 'RE_MATCHES', call += 'Matches' 4519 on 'RE_SPLITS', call += 'Splits' 4520 else 4521 throw FallThroughException('Unknown regexp op [op]') 4522 sw.write(call+ '(') 4523 left.writeSharpDef(sw, false) 4524 sw.write(',') 4525 right.writeSharpDef(sw, false) 4526 sw.write(')') 4527 return 4528 4529 assert op in ['TILDE', 'RE_MATCHES', 'RE_HASMATCH', 'RE_SPLITS'] # TILDE_COLON'] 4530 branch op 4531 on 'TILDE', call = 'CobraLangInternal.CobraImp.RegexpMatch(' #'.Match(' 4532 on 'RE_HASMATCH', call = '.IsMatch(' 4533 on 'RE_MATCHES', call = '.Matches(' 4534 on 'RE_SPLITS', call = 'CobraLangInternal.CobraImp.RegexpSplits(' #'.Split(' 4535 else 4536 throw FallThroughException('Unknown regexp op [op]') 4537 if op == 'TILDE' or op == 'RE_SPLITS' 4538 sw.write(call) 4539 left.writeSharpDef(sw) 4540 sw.write(', ') 4541 right.writeSharpDef(sw, false) 4542 sw.write(')') 4543 else 4544 left.writeSharpDef(sw) 4545 sw.write(call) 4546 right.writeSharpDef(sw, false) 4547 sw.write(')') 4548 4549 4471 4550 class DotExpr 4472 4551 is partial 4473 4552 -
Source/CobraTokenizer.cobra
131 131 r"SHARP_SINGLE sharp'(?:\\.?|[^'\n])*'", 132 132 r'SHARP_DOUBLE sharp"(?:\\.?|[^"\n])*"', 133 133 134 # regexp Literal strings 135 r"REGEXP_SINGLE re'(?:\\.?|[^'\n])*'", 136 r'REGEXP_DOUBLE re"(?:\\.?|[^"\n])*"', 137 134 138 # raw strings 135 139 r"STRING_RAW_SINGLE r'(?:\\.?|[^'\n])*'", 136 140 r'STRING_RAW_DOUBLE r"(?:\\.?|[^"\n])*"', … … 195 199 r'AMPERSAND \&', 196 200 r'VERTICAL_BAR \|', 197 201 r'CARET \^', 198 r'TILDE \~', 202 r'TILDE \~', # RE_MATCH 199 203 200 204 r'EQ ==', 201 205 r'NE <>', … … 220 224 221 225 r'QUESTION_EQUALS \?=', 222 226 r'BANG_EQUALS \!=', 227 r'RE_HASMATCH \~=', 228 r'RE_SPLITS \~\|', 223 229 ] 224 230 225 231 get keywords as IList<of String> is override … … 811 817 return tok 812 818 813 819 ## 820 ## Regexp Literal String 821 ## re'/<rePattern>/<flags>' or re'<rePattern>' or 822 ## re"/<rePattern>/<flags>" or re"<rePattern>" 823 ## Currently only single line 824 ## TODO: support multiline for IgnorePatternWhitespace when strings do. 825 826 def onREGEXP_SINGLE(tok as IToken) as IToken 827 require tok.text.startsWith('re') 828 #tok.value =.tokValueForString(tok.text.substring(2)) 829 tok.value = tok.text[3:-1] # contents between re' and ' 830 tok.which = 'REGEXP_LIT' 831 return tok 832 833 def onREGEXP_DOUBLE(tok as IToken) as IToken 834 require tok.text.startsWith('re') 835 #tok.value =.tokValueForString(tok.text.substring(2)) 836 tok.value = tok.text[3:-1] 837 tok.which = 'REGEXP_LIT' 838 return tok 839 840 ## 814 841 ## Simple string literals 815 842 ## 816 843 -
Source/Compiler.cobra
1178 1178 def stringType as Class 1179 1179 return _libraryClass('System.String') 1180 1180 1181 def regexpType as Class 1182 return _libraryClass('System.Text.RegularExpressions.Regex') 1183 1184 def regexpMatchType as Class 1185 return _libraryClass('System.Text.RegularExpressions.Match') 1186 1187 def regexpManyMatchType as Class 1188 return _libraryClass('System.Text.RegularExpressions.MatchCollection') 1189 1181 1190 def exceptionType as Class 1182 1191 return _libraryClass('System.Exception') 1183 1192 -
Source/Cobra.Lang/Native.cs
853 853 } 854 854 855 855 856 // Regex wrappers non dynamic 857 // Single match: Null on failure, Match on success 858 static public System.Text.RegularExpressions.Match RegexpMatch(System.Text.RegularExpressions.Regex re, string str) 859 { 860 System.Text.RegularExpressions.Match m = re.Match(str); 861 return m.Success ? m : null; 862 } 863 864 static public List<String> RegexpSplits(System.Text.RegularExpressions.Regex re, string str) 865 { 866 String[] sa = re.Split(str); 867 return new List<String>(sa); 868 } 869 856 870 // Dynamic Binding 857 871 858 872 static private readonly BindingFlags BaseDynamicFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; … … 1202 1216 return a + b; 1203 1217 } 1204 1218 1219 1220 // Handling for dynamic operands with regexp operator 1221 static public System.Text.RegularExpressions.Match DynamicRegexpMatch(Object a, Object b) { 1222 DynamicRegexp_Chk(a,b); 1223 System.Text.RegularExpressions.Regex re = (System.Text.RegularExpressions.Regex)a; 1224 string str = (string)b; 1225 return RegexpMatch(re, str); 1226 } 1227 static public bool DynamicRegexpHasMatch(Object a, Object b) { 1228 DynamicRegexp_Chk(a,b); 1229 System.Text.RegularExpressions.Regex re = (System.Text.RegularExpressions.Regex)a; 1230 string str = (string)b; 1231 return re.IsMatch(str); 1232 } 1233 static public System.Text.RegularExpressions.MatchCollection DynamicRegexpMatches(Object a, Object b) { 1234 DynamicRegexp_Chk(a,b); 1235 System.Text.RegularExpressions.Regex re = (System.Text.RegularExpressions.Regex)a; 1236 string str = (string)b; 1237 return re.Matches(str); 1238 } 1239 static public List<String> DynamicRegexpSplits(Object a, Object b) { 1240 DynamicRegexp_Chk(a,b); 1241 System.Text.RegularExpressions.Regex re = (System.Text.RegularExpressions.Regex)a; 1242 string str = (string)b; 1243 return RegexpSplits(re, str); 1244 //return new List<String(re.Split(str)); 1245 } 1246 static void DynamicRegexp_Chk(Object a, Object b) { 1247 if (a == null) throw new NullReferenceException("First operand nil, expecting a Regexp"); 1248 if (b == null) throw new NullReferenceException("Second operand nil, expecting a String"); 1249 //if (!(a.GetType() is System.Text.RegularExpressions.Regex)) { 1250 if (!(a.GetType().Name == "Regex")) { 1251 throw new ArgumentException("First operand in dynamic regexp match expression must be a Regex not "+ a.GetType().Name); 1252 } 1253 //if (!(b.GetType() is string) ) { 1254 if (!(b.GetType().Name == "String") ) { 1255 throw new ArgumentException("Second operand in dynamic regexp match expression must be a String not "+ b.GetType().Name); 1256 } 1257 } 1258 1205 1259 static public string RunAndCaptureAllOutput(object process) { 1206 1260 // CC: change to extension method on Process class 1207 1261 System.Diagnostics.Process proc = (System.Diagnostics.Process)process; -
Source/BinaryOpExpr.cobra
13 13 BinaryMathExpr 14 14 BinaryBoolExpr 15 15 CompareExpr 16 RegexpExpr 16 17 DotExpr 17 18 InExpr 18 19 InheritsExpr … … 70 71 return BinaryBitwiseExpr(opToken, op, left, right) 71 72 on 'AMPERSAND_EQUALS' or 'VERTICAL_BAR_EQUALS' or 'CARET_EQUALS' or 'DOUBLE_LT_EQUALS' or 'DOUBLE_GT_EQUALS' 72 73 return AugAssignBitwiseExpr(opToken, op, left, right) 74 on 'TILDE' or 'RE_HASMATCH' or 'RE_SPLITS' # TILDE_COLON 75 return RegexpExpr(opToken, op, left, right) 73 76 else 74 77 throw FallThroughException([opToken, op, left, right]) 75 78 … … 601 604 msg += ' Maybe you should try "inherits" instead of "is".' 602 605 .throwError(msg) 603 606 607 class RegexpExpr 608 is partial 609 inherits BinaryOpExpr 604 610 611 # TODO: TILDE_COLON = substitute 612 613 cue init(opToken as IToken, op as String, left as Expr, right as Expr) 614 base.init(opToken, op, left, right) 615 616 def _bindImp 617 base._bindImp 618 if _left.type is nil or _right.type is nil 619 assert .hasError 620 return 621 leftType = _left.type to ! 622 rightType = _right.type to ! 623 # expr is 're isMatch str' or 're match str' or 're (all)matches str' which works for me - re always lhs 624 # but 're split str' seems wrong way round ( 'str split(by) re' seems more natural) 625 # so its 're splits str' - re on lhs 626 if not (leftType inherits NilableType and leftType.isDynamic) and not leftType.isDynamicOrPassThrough and not leftType.isDescendantOf(.compiler.regexpType) 627 .throwError('The leftside of regexp op "[.token.text]" must be a RegularExpression type not a [leftType.name]') 628 if not (rightType inherits NilableType and rightType.isDynamic) and not rightType.isDynamicOrPassThrough and not rightType is .compiler.stringType 629 .throwError('The rightside of regexp op "[.token.text]" must be a String type not a [rightType.name]') 630 631 assert .op in ['TILDE', 'RE_HASMATCH', 'RE_SPLITS'] # RE_SUBST (~:) 632 if .op == 'TILDE' # regexp match 633 _type = .compiler.nilableType(.compiler.regexpMatchType) 634 # overload for 'm in re ~ str' 635 #print .superNode 636 if .superNode inherits ForEnumerableStmt 637 _op = 'RE_MATCHES' 638 _type = .compiler.regexpManyMatchType 639 else if .op == 'RE_HASMATCH' # '~=' 640 _type = .compiler.boolType 641 else if .op == 'RE_SPLITS' # '~|' 642 _type = .compiler.listOfType.constructedTypeFor([.compiler.stringType to IType]) # List<of String> 643 #_type = .compiler.arrayType(compiler.stringType) # Array of String 644 645 605 646 interface IDotRightExpr 606 647 inherits IExpr 607 648 """ -
Source/Node.cobra
1212 1212 def objectClass as Class 1213 1213 def setOfType as Class 1214 1214 def stringType as Class 1215 def regexpType as Class 1216 def regexpMatchType as Class 1217 def regexpManyMatchType as Class 1215 1218 1216 1219 1217 1220 ## Other -
Source/CobraParser.cobra
2668 2668 'INHERITS': 40, 2669 2669 'IMPLEMENTS': 40, 2670 2670 2671 'TILDE': 37, # RE_MATCH 2672 'RE_HASMATCH': 37, # TILDE_EQUALS (~=) is REGEXP_HASMATCH test not augmented assignment 2673 'RE_SPLITS': 37, # TILDE_BAR ~| 2674 2671 2675 'IN': 35, 2672 2676 'NOTIN': 35, 2673 2677 … … 2691 2695 'CARET_EQUALS': 20, 2692 2696 'DOUBLE_LT_EQUALS': 20, 2693 2697 'DOUBLE_GT_EQUALS': 20, 2698 2694 2699 } 2695 2700 2696 2701 get binaryOpPrec from var … … 2720 2725 return expr 2721 2726 finally 2722 2727 _inExpression -= 1 2723 2724 2728 def expression(precedence as int) as Expr 2725 2729 return .expression(precedence, nil) 2726 2730 … … 2878 2882 return FractionalLit(.grab) 2879 2883 on 'FLOAT_LIT' 2880 2884 return FloatLit(.grab) 2885 on 'REGEXP_LIT' 2886 return RegexpLit(.grab) 2881 2887 on 'LBRACKET' 2882 2888 return .literalList 2883 2889 on 'ARRAY_OPEN' -
Tests/320-misc-two/410-regexp/250-regexp-matches-expr.cobra
1 class REMatches 2 3 def main is shared 4 str = '@param farethee well\n @param fare thee well\n@param fare tw my darling maid' 5 re = re'/^\s*@param\s+(.*)$/m' 6 reX = re'/^\s*quack quack Quaack\s+(.*)$/m' 7 8 for m in re ~ str 9 assert m.success 10 assert m.groups[0].value.trim.startsWith('@param') 11 12 c=0 13 for m in reX ~ str 14 c += 1 15 assert c == 0 16 17 c = 0 18 for m in re'\s*well' ~ str 19 c += 1 20 assert m.value == ' well' 21 assert c == 2 -
Tests/320-misc-two/410-regexp/210-regexp-match-expr.cobra
1 use System.Text.RegularExpressions 2 3 class ReMatch 4 5 def main is shared 6 str = '@param farethee well\n @param fare thee well\n@param ftw my darling maid' 7 8 re0 = Regex(r'^\s*@param\s+(.*)$', RegexOptions.Multiline) 9 m0 = re0.match(str) 10 re = re'/^\s*@param\s+(.*)$/m' 11 reX = re'/^\s*quack quack Quaack\s+(.*)$/m' 12 13 assert re ~= str 14 m = re ~ str 15 assert m 16 assert 'Match' in m.typeOf.toString 17 assert m.success 18 .matchMatches(m, m0) 19 20 if re ~= str, assert true 21 else, assert false, 're ~= str FAIL' 22 if re ~ str, assert true 23 else, assert false, 're ~str FAIL' 24 25 # Non matching 26 assert not reX ~= str 27 assert not reX ~ str 28 m00 = reX ~ str # failure to match with ~ returns nil 29 assert not m00 30 assert m00 == nil 31 32 if not reX ~= str, assert true 33 else, assert false, 'not reX ~= str FAIL' 34 35 if not reX ~ str, assert true 36 else, assert false, 'not reX ~ str FAIL' 37 38 39 reA = re'/^\s*@param\s+(.*)\n/' # non Multiline, match EOL 40 assert reA ~= str 41 42 reB = re'^\s*@param\s+(.*)' # No flags at all 43 assert reB ~= str 44 45 assert not re'^\s*@PARAM\s+(.*)' ~= str # case sensitive 46 assert re'/^\s*@PARAM\s+(.*)/i' ~= str # case insensitive 47 48 49 re2 = Regex(r'^\s*@param\s+(.*)$', RegexOptions.Singleline) 50 assert re2 ~= str 51 assert re'/^\s*@param\s+(.*)$/s' ~= str 52 reC = re'/^\s*@param\s+(.*)$/s' 53 .matchMatches(re2 ~ str, reC ~ str) 54 55 56 def matchMatches(m as Match?, m0 as Match?) is shared 57 # assert m == m0 fails as does m.groups[0] == m0.groups[0]... 58 # but .. 59 assert m 60 assert m0 61 assert m.success == m0.success 62 assert m.value == m0.value 63 assert m.groups[0].toString == m0.groups[0].toString 64 assert m.groups[0].captures[0].toString == m0.groups[0].captures[0].toString 65 assert m.groups[1].toString == m0.groups[1].toString 66 -
Tests/320-misc-two/410-regexp/110-regexp-lit2.cobra
1 class RegexpLit 2 def main is shared 3 str = 'Diddly pom, diddley pam, diddley pumlee' 4 5 reC = re'/p.m/c' # compiled 6 assert reC.isMatch(str) 7 8 m = reC.match(str) 9 assert m.success 10 assert m.value == 'pom' 11 12 m = m.nextMatch to ! 13 assert m.success 14 assert m.success 15 assert m.value == 'pam' 16 17 m = m.nextMatch to ! 18 assert m.success 19 assert m.success 20 assert m.value == 'pum' 21 22 m = m.nextMatch to ! 23 assert not m.success 24 assert not m.value.length 25 26 27 rex = re'/[Dd]\w+\W(p.m)/' 28 m = rex.match(str) 29 assert m.success 30 assert m.value == 'Diddly pom' 31 assert m.groups[1].captures.count == 1 32 assert m.groups[1].captures[0].value == 'pom' 33 34 # Explicit capture flag 35 rex = re'/[Dd]\w+\W(p.m)/x' 36 m = rex.match(str) 37 assert m.success 38 assert m.value == 'Diddly pom' 39 assert m.groups[1].captures.count == 0 40 41 rex = re'/[Dd]\w+\W(?<v>p.m)/x' 42 m = rex.match(str) 43 assert m.success 44 assert m.value == 'Diddly pom' 45 assert m.groups[1].captures.count == 1 46 assert m.groups[1].captures[0].value == 'pom' 47 -
Tests/320-misc-two/410-regexp/120-regexp-lit-matches.cobra
1 class RegexpLit 2 def main is shared 3 str = 'Ka mate, Ka mate, Ki ora, Ki ora' 4 5 re = re'/[Kk]. \w/' 6 assert re.isMatch(str) 7 8 for m in re.matches(str) 9 assert m.value in ['Ka m', 'Ki o'] 10 11 mc = re.matches(str) 12 assert mc.count == 4 13 14 assert mc[0].value == 'Ka m' 15 assert mc[1].value == 'Ka m' 16 assert mc[2].value == 'Ki o' 17 assert mc[3].value == 'Ki o' 18 -
Tests/320-misc-two/410-regexp/200-regexp-match-min.cobra
1 # simple test of each of '~=', '~' and 'for m in re ~ str' 2 class ReMatch 3 4 def main is shared 5 str = '@param farethee well\n @param fare thee well\n@param fare tw my darling maid' 6 7 re = re'/^\s*@param\s+(.*)$/m' 8 reX = re'/^no.match.evah$/m' 9 assert 'Regex' in re.typeOf.toString 10 11 # isMatch 12 if re ~= str, assert true 13 else, assert false, 'str match re - ismatch FAIL' 14 assert re ~= str 15 assert not reX ~= str 16 17 # Match 18 if re ~ str, assert true 19 else, assert false, 'str match re - match FAIL' 20 m = re ~ str 21 assert 'Match' in m.typeOf.toString 22 assert m and m.success 23 #print m 24 25 m = reX ~ str 26 assert not m 27 assert not reX ~ str 28 29 #Matches/MatchCollection 30 for m in re ~ str 31 assert m.groups[1].value.startsWith('fare') 32 33 # split 34 reSplit = re'\n?\s?@param ' 35 split = reSplit ~| str 36 assert split.count == 4 37 assert split[0] == '' 38 for i in 1 : split.count 39 assert split[i].startsWith('fare') -
Tests/320-misc-two/410-regexp/300-regexp-splits.cobra
1 # test regexp splits operator 2 class RegexpSplits 3 def main 4 re = re'-' 5 str = 'plum|pear' 6 split = re.split(str).toList 7 assert split.count == 1 8 assert split[0] == str 9 10 split = re ~| str 11 assert split.count == 1 12 assert split[0] == str 13 14 str = 'plum--pear' 15 split = re.split(str).toList 16 .validate(split, ['plum', '', 'pear']) 17 split = re ~| str 18 .validate(split, ['plum', '', 'pear']) 19 20 re2 = re'(-)' 21 str = 'plum-pear' 22 #split = re2.split(str).toList 23 split = re2 ~| str 24 .validate(split, ['plum', '-', 'pear']) 25 26 str = r'07/14/2007' 27 re3 = re'(-)|(/)' 28 #split = re3.split(str).toList 29 split = re3 ~| str 30 #print split 31 .validate(split, ['07', '/', '14', '/', '2007']) 32 33 str = "10 blah 20 30 nonsense 40 50" 34 re4 = re'[^\d.]+' 35 #split = re4.split(str).toList 36 split = re4 ~| str 37 .validate(split, [ '10', '20', '30', '40', '50' ]) 38 str = "10 blah 20 30 nonsense 40.5 50" 39 #split = re4.split(str).toList 40 split = re4 ~| str 41 .validate(split, [ '10', '20', '30', '40.5', '50']) 42 43 str = '001ASD003DFG012SASAS210ASSA1000XXX0' 44 re5 = re'([a-zA-Z]+)' 45 #split = re5.split(str).toList 46 split = re5 ~| str 47 .validate(split, ['001', 'ASD', '003', 'DFG', '012', 'SASAS', '210', 'ASSA', '1000', 'XXX', '0' ]) 48 49 def validate(obtained as List<of String>, expected as List<of String>) 50 assert obtained.count == expected.count 51 for i, match in obtained.numbered 52 assert match == expected[i] 53 -
Tests/320-misc-two/410-regexp/292-regexp-match-dynamic-err.cobra
1 class REMatchDynamicErr 2 def main is shared 3 str = '@param any old iron\n @param Any old eyeron\n@param any auld iron at all' 4 re = re'/^\s*@param\s+(.*)$/m' 5 reX = re'/^\s*quack quack Quaack\s+(.*)$/m' 6 7 # re match ops as dynamic 8 dstr = str to dynamic 9 dre = re to dynamic 10 dreX = reX to dynamic 11 try 12 assert dstr ~= dre 13 catch exc as System.ArgumentException 14 #print exc.message 15 assert 'First operand' in exc.message 16 assert 'must be a Regex' in exc.message 17 18 try 19 assert dre ~= dreX 20 catch exc as System.ArgumentException 21 assert 'Second operand' in exc.message 22 assert 'must be a String' in exc.message 23 24 try 25 assert dstr ~ dre 26 catch exc as System.ArgumentException 27 #print exc.message 28 assert 'First operand' in exc.message 29 assert 'must be a Regex' in exc.message 30 31 try 32 assert dreX ~ dre 33 catch exc as System.ArgumentException 34 assert 'Second operand' in exc.message 35 assert 'must be a String' in exc.message 36 37 s1 as dynamic = 'hello' # perhaps forgot re prefix 38 s2 as dynamic = 'othello' 39 40 try 41 assert s1 ~= s2 42 catch exc as System.ArgumentException 43 #print exc.message 44 assert 'First operand' in exc.message 45 assert 'must be a Regex' in exc.message 46 47 nd as dynamic? = nil 48 try 49 assert nd ~ 'hello nil' 50 catch nExc as NullReferenceException 51 assert 'First operand nil' in nExc.message 52 assert 'expecting a Regexp' in nExc.message 53 54 try 55 assert dre ~= nd 56 catch nExc as NullReferenceException 57 assert 'Second operand nil' in nExc.message 58 assert 'expecting a String' in nExc.message 59 60 rs1 as dynamic = re'hello' 61 assert rs1 ~= s2 62 assert rs1 ~ s2 63 -
Tests/320-misc-two/410-regexp/165-regexp-lit-delim-err.cobra
1 class RegexpLit 2 def main is shared 3 4 re = re'/f.oo' # .error. no trailing delimiter 5 re1 = re'/' # .error. no trailing delimiter 6 re1 = re'/f.o./' 7 CobraCore.noOp(re,re1) -
Tests/320-misc-two/410-regexp/240-regexp-match-dynamic.cobra
1 class REMatchDy 2 def main is shared 3 str = '@param Quack\n @param quack well\n@param quack quack quaaaack' 4 5 re as dynamic = re'/^\s*@param\s+(.*)$/m' 6 reX as dynamic = re'/^\s*quack quack Quaack\s+(.*)$/m' 7 8 assert re ~= str # IsMatch 9 m = re ~ str 10 assert m 11 assert 'Match' in m.typeOf.toString 12 assert m.success 13 assert m.value == '@param Quack' 14 assert m.groups[1].captures[0].toString == 'Quack' 15 16 m = m.nextMatch to ! 17 assert m.success 18 assert m.value.trim == '@param quack well' 19 assert m.groups[1].captures[0].toString == 'quack well' 20 m = m.nextMatch to ! 21 assert m.success 22 m = m.nextMatch to ! 23 assert not m.success 24 25 if re ~= str, assert true 26 else, assert false, 're ~= str FAIL' 27 28 if re ~ str, assert true 29 else, assert false, 're ~str FAIL' 30 31 # Non matching 32 assert not reX ~= str 33 assert not reX ~ str 34 m00 = reX ~ str # failure to match with ~ returns nil 35 assert not m00 36 37 if not reX ~= str, assert true 38 else, assert false, 'not reX ~= str FAIL' 39 40 if not reX ~ str, assert true 41 else, assert false, 'not reX ~ str FAIL' 42 43 44 45 -
Tests/320-misc-two/410-regexp/150-regexp-lit-split.cobra
1 # Test for Regex split using a RE Literal 2 class ReSplit 3 4 def main is shared 5 str ='aaa:-:bbb:+:cc cc:=:' 6 7 re= re'/:.:/c' # compiled 8 9 args = re.split(str) # returns String[] 10 assert args.length == 4 11 assert args[0] == 'aaa' 12 assert args[1] == 'bbb' 13 assert args[2] == 'cc cc' 14 assert args[3] == '' 15 16 argsl = re.split(str).toList 17 assert argsl.count == 4 18 assert argsl[0] == 'aaa' 19 assert argsl[1] == 'bbb' 20 assert argsl[2] == 'cc cc' 21 assert argsl[3] == '' 22 23 str1 = 'xxxx-yyyy' 24 args = re.split(str1) 25 assert args.length == 1 26 assert args[0] == str1 27 28 l = re'[-_+:]'.split(str1).toList 29 assert l.count == 2 30 assert l[0] == 'xxxx' 31 assert l[1] == 'yyyy' 32 -
Tests/320-misc-two/410-regexp/100-regexp-lit.cobra
1 use System.Text.RegularExpressions 2 3 class ReMatch 4 5 def main is shared 6 str = '@param farethee well\n @param fare thee well\n@param ftw my darling maid' 7 8 re0 = Regex(r'^\s*@param\s+(.*)$', RegexOptions.Multiline) 9 m0 = re0.match(str) 10 #print 'isMatch ?', re0.isMatch(str) 11 #print 'Match ?', m0.success, 'value=', m0.value 12 13 /# for match in re0.matches(str) 14 # groups[0] is entire match to pattern 15 print '0 {[match.groups[0]]}' 16 print '1 {[match.groups[1].captures[0]]}' 17 #print match # == match.value 18 #groups = match.groups 19 #for group as Group in match.groups 20 # #print g 21 # for c in group.captures 22 # print 'capture=[c]' 23 print 24 #/ 25 re = re'/^\s*@param\s+(.*)$/m' 26 #assert re.typeOf == System.Text.RegularExpressions.Regex 27 assert 'Regex' in re.typeOf.toString 28 29 assert re.isMatch(str) 30 m = re.match(str) 31 assert m.success 32 .matchMatches(m, m0) 33 34 /# for match in re.matches(str) 35 # groups[0] is entire match to pattern 36 print '0 {[match.groups[0]]}' 37 print '1 {[match.groups[1].captures[0]]}' 38 #print match # == match.value 39 #groups = match.groups 40 #for group as Group in match.groups 41 # #print g 42 # for c in group.captures 43 # print 'capture=[c]' 44 print 45 #/ 46 47 reA = re'/^\s*@param\s+(.*)\n/' # non Multiline, match EOL 48 assert reA.isMatch(str) 49 50 reB = re'^\s*@param\s+(.*)' # No flags at all 51 assert reB.isMatch(str) 52 53 assert not re'^\s*@PARAM\s+(.*)'.isMatch(str) # case sensitive 54 assert re'/^\s*@PARAM\s+(.*)/i'.isMatch(str) # case insensitive 55 56 57 re2 = Regex(r'^\s*@param\s+(.*)$', RegexOptions.Singleline) 58 assert re2.isMatch(str) 59 assert re'/^\s*@param\s+(.*)$/s'.isMatch(str) 60 reC = re'/^\s*@param\s+(.*)$/s' 61 .matchMatches(re2.match(str), reC.match(str)) 62 63 64 def matchMatches(m as Match, m0 as Match) is shared 65 # assert m == m0 fails as does m.groups[0] == m0.groups[0]... 66 # but .. 67 assert m.success == m0.success 68 assert m.value == m0.value 69 assert m.groups[0].toString == m0.groups[0].toString 70 assert m.groups[0].captures[0].toString == m0.groups[0].captures[0].toString 71 assert m.groups[1].toString == m0.groups[1].toString 72 -
Tests/320-misc-two/410-regexp/290-regexp-match-err.cobra
1 class REMatchErr 2 def main is shared 3 4 str = '@param any old iron\n @param Any old eyeron\n@param any auld iron at all' 5 re = re'/^\s*@param\s+(.*)$/m' 6 reX = re'/^\s*quack quack Quaack\s+(.*)$/m' 7 8 assert str ~= re # .error. leftside of regexp op "~=" must be a RegularExpression type 9 assert re ~= reX # .error. rightside of regexp op "~=" must be a String type 10 11 assert str ~ re #.error. leftside of regexp op "~" must be a RegularExpression type 12 assert reX ~ re #.error. rightside of regexp op "~" must be a String type 13 14 assert 'hello' ~= 'othello' # .error. leftside of regexp op "~=" must be a RegularExpression type -
Tests/320-misc-two/410-regexp/220-regexp-match-expr2.cobra
1 class RegexpMatchE 2 def main is shared 3 str = 'Diddly pom, diddley pam, diddley pumlee' 4 5 reC = re'/p.m/c' # compiled 6 assert reC ~ str 7 8 m = reC ~ str 9 assert m.success 10 assert m.value == 'pom' 11 assert reC ~ str 12 assert (reC ~ str).success 13 assert (reC ~ str).value == 'pom' 14 15 16 m = m.nextMatch to ! 17 assert m.success 18 assert m.value == 'pam' 19 20 m = m.nextMatch to ! 21 assert m.success 22 assert m.value == 'pum' 23 24 m = m.nextMatch to ! 25 assert not m.success 26 assert not m.value.length 27 28 29 rex = re'/[Dd]\w+\W(p.m)/' 30 m = rex ~ str 31 assert m.success 32 assert m.value == 'Diddly pom' 33 assert m.groups[1].captures.count == 1 34 assert m.groups[1].captures[0].value == 'pom' 35 36 # Explicit capture flag 37 rex = re'/[Dd]\w+\W(p.m)/x' 38 m = rex ~ str 39 assert m.success 40 assert m.value == 'Diddly pom' 41 assert m.groups[1].captures.count == 0 42 43 rex = re'/[Dd]\w+\W(?<v>p.m)/x' 44 m = rex ~ str 45 assert m.success 46 assert m.value == 'Diddly pom' 47 assert m.groups[1].captures.count == 1 48 assert m.groups[1].captures[0].value == 'pom' 49 -
Tests/320-misc-two/410-regexp/160-regexp-lit-no-ptn-err.cobra
1 class RegexpLit 2 def main is shared 3 4 re = re'' # .error. no pattern specified 5 re1 = re'//' # .error. no pattern specified 6 CobraCore.noOp(re, re1) -
Tests/320-misc-two/410-regexp/140-regexp-lit-W.cobra
1 # .skip. Placeholder for regexp W flag suppressed pending multiline strings 2 3 class ReMatch 4 5 def main is shared 6 str = '@param farethee well\n @param fare thee well\n@param ftw my darling maid' 7 8 re = re'/^ # SOLN 9 \s* # any whitespace 10 @param # 11 \s+ # 1+ whitespace 12 (.*) # capture these 13 $ # EOLN 14 /mW' # flags - Multiline 15 16 assert re.isMatch(str) 17 m = re.match(str) 18 assert m.success 19 assert m.groups.count >0 20 assert m.groups[1].captures.count > 0 21 assert m.groups[1].captures[0].value == 'farethee well' -
Tests/320-misc-two/410-regexp/168-regexp-lit-flag-err.cobra
1 class RegexpLit 2 def main is shared 3 4 re = re'/foo/icsmxW' # all the flags 5 6 re = re'/foo/I' # .error. unrecognised Regexp flag "I" 7 re1 = re'/q.ack/u' # .error. unrecognised Regexp flag "u" 8 CobraCore.noOp(re, re1)