| 1 | module regex |
| 2 | |
| 3 | import strings |
| 4 | |
| 5 | // compile_opt compile RE pattern string |
| 6 | pub fn (mut re RE) compile_opt(pattern string) ! { |
| 7 | re_err, err_pos := re.impl_compile(pattern) |
| 8 | |
| 9 | if re_err != compile_ok { |
| 10 | mut err_msg := strings.new_builder(300) |
| 11 | err_msg.write_string('\nquery: ${pattern}\n') |
| 12 | line := '-'.repeat(err_pos) |
| 13 | err_msg.write_string('err : ${line}^\n') |
| 14 | err_str := re.get_parse_error_string(re_err) |
| 15 | err_msg.write_string('ERROR: ${err_str}\n') |
| 16 | return error_with_code(err_msg.str(), re_err) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // new create a RE of small size, usually sufficient for ordinary use |
| 21 | pub fn new() RE { |
| 22 | // init regex |
| 23 | mut re := RE{} |
| 24 | re.prog = []Token{len: max_code_len + 1} // max program length, can not be longer then the pattern |
| 25 | re.cc = []CharClass{len: max_code_len} // can not be more char class the length of the pattern |
| 26 | re.group_csave_flag = false // enable continuos group saving |
| 27 | re.group_max_nested = 128 // set max 128 group nested |
| 28 | re.group_max = max_code_len >> 1 // we can't have more groups than the half of the pattern legth |
| 29 | |
| 30 | re.group_stack = []int{len: re.group_max, init: -1} |
| 31 | re.group_data = []int{len: re.group_max, init: -1} |
| 32 | |
| 33 | return re |
| 34 | } |
| 35 | |
| 36 | // regex_opt create new RE object from RE pattern string |
| 37 | pub fn regex_opt(pattern string) !RE { |
| 38 | // init regex |
| 39 | mut re := RE{} |
| 40 | re.prog = []Token{len: pattern.len + 1} // max program length, can not be longer then the pattern |
| 41 | re.cc = []CharClass{len: pattern.len} // can not be more char class the length of the pattern |
| 42 | re.group_csave_flag = false // enable continuos group saving |
| 43 | re.group_max_nested = pattern.len >> 1 // set max 128 group nested |
| 44 | re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth |
| 45 | |
| 46 | re.group_stack = []int{len: re.group_max, init: -1} |
| 47 | re.group_data = []int{len: re.group_max, init: -1} |
| 48 | |
| 49 | // compile the pattern |
| 50 | re.compile_opt(pattern)! |
| 51 | |
| 52 | return re |
| 53 | } |
| 54 | |