ifre_runes[i]=='['&&(i==0||re_runes[i-1]!='\\'){// We do not touch things inside brackets, unless they are escaped. Inside this block, the only task is to expand character ranges into their constituent characters.
re_postfix[len(re_postfix)-1]=LBRACKET// Replace the '[' character with LBRACKET. This allows for easier parsing of all characters (including opening and closing brackets) within the character class
toAppend:=make([]rune,0)// Holds all the runes in the current character class
ifi<len(re_runes)-1&&re_runes[i+1]=='^'{// Inverting class - match everything NOT in brackets
i++// Skip all characters inside _unescaped_ brackets (we are _not_ at a closing bracket, or if we are, the previous character is a backslash)
ifre_runes[i]==LBRACKET&&(i==0||re_runes[i-1]!='\\'){// We do not touch things inside brackets, unless they are escaped.
toAppend:=make([]rune,0)// Holds all the runes in the current character class
i++// Skip past LBRACKET, because it was already added
ifi>=len(re_runes){// Sanity check before we start
returnnil,fmt.Errorf("Opening bracket without closing bracket.")
}
forre_runes[i]!=RBRACKET||i==0||re_runes[i-1]=='\\'{// Skip all characters inside _unescaped_ brackets (we are _not_ at a closing bracket, or if we are, the previous character is a backslash)
// Make sure we haven't exceeded the length of the string. If we did, then the regex doesn't actually have a closing bracket and we should throw an error.
ifi>=len(re_runes){
returnnil,fmt.Errorf("Opening bracket without closing bracket.")
}
ifre_runes[i]=='-'&&(i>0&&re_runes[i-1]!='\\')&&(i<len(re_runes)-1&&re_runes[i+1]!=']'){// Unescaped hyphen, that has some character (not a RBRACKET) after it - This represents a character range, so we replace with CHAR_RANGE. This metacharacter will be used later on to construct the range
ifre_runes[i]=='-'&&(i>0&&re_runes[i-1]!='\\')&&(i<len(re_runes)-1&&re_runes[i+1]!=RBRACKET){// Unescaped hyphen, that has some character (not a RBRACKET) after it - This represents a character range, so we replace with CHAR_RANGE. This metacharacter will be used later on to construct the range
re_runes[i]=CHAR_RANGE
}
toAppend=append(toAppend,re_runes[i])
i++
}
// Replace the last character (which should have been ']', with RBRACKET
toAppend[len(toAppend)-1]=RBRACKET
// Add in the RBRACKET
toAppend=append(toAppend,RBRACKET)
re_postfix=append(re_postfix,toAppend...)
}
ifi<len(re_runes)&&re_runes[i]=='{'&&(i>0&&re_runes[i-1]!='\\'){// We don't touch things inside braces, either