i++// Skip all characters inside _unescaped_ brackets (we are _not_ at a closing bracket, or if we are, the previous character is a backslash)
i++// Skip all characters inside _unescaped_ brackets (we are _not_ at a closing bracket, or if we are, the previous character is a backslash)
// TODO: Check for escaped characters
// TODO: Check for escaped characters
ifre_runes[i]=='-'&&i>0&&re_runes[i-1]!='\\'{// Unescaped hyphen - replace with CHAR_RANGE. This metacharacter will be used later on to construct the range
ifendOfRange{// The previous character was an unescaped hyphen, which (in the context of a character class) means the character that was last appended is the end of a character range
// Things to note:
// 1. In PCRE and Go's regex engine, a letter _can_ be surrounded by hyphens in a character class.
// Eg. [a-b-c]
// While you might think this leads to a syntax error (I thought so), the engine picks 'a-b' as a range,
// then treats the second '-' and 'c' as regular characters in the character class.
// So this regex becomes "Match a character from 'a' to 'b', a literal hyphen, or 'c' ".
// 2. To account for this, the following logic is followed:
// a. If the second-to-last postfixNode ie. the start of the range has only one element, then we are in a range.
// i. If it has more than one element, then we are actually looking at a literal hyphen, and we will treat is as such.
// b. The last postfixNode added to 'chars' _must_ only have one character (because it's the end of the range).
endRangePostfixNode:=mustPop(&chars)
startRangePostfixNode:=mustPop(&chars)
iflen(endRangePostfixNode.contents)!=1{
returnnil,fmt.Errorf("Error parsing character range.")
}elseiflen(startRangePostfixNode.contents)!=1{// This is actually a regular hyphen