package parser

import (
	
	
	
	
)

const (
	WhitespaceChars = " \f\n\r\t\v\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
	Re2Dot          = "[^\r\n\u2028\u2029]"
)

type regexpParseError struct {
	offset int
	err    string
}

type RegexpErrorIncompatible struct {
	regexpParseError
}
type RegexpSyntaxError struct {
	regexpParseError
}

func ( regexpParseError) () string {
	return .err
}

type _RegExp_parser struct {
	str    string
	length int

	chr       rune // The current character
	chrOffset int  // The offset of current character
	offset    int  // The offset after current character (may be greater than 1)

	err error

	goRegexp   strings.Builder
	passOffset int

	dotAll  bool // Enable dotAll mode
	unicode bool
}

// TransformRegExp transforms a JavaScript pattern into  a Go "regexp" pattern.
//
// re2 (Go) cannot do backtracking, so the presence of a lookahead (?=) (?!) or
// backreference (\1, \2, ...) will cause an error.
//
// re2 (Go) has a different definition for \s: [\t\n\f\r ].
// The JavaScript definition, on the other hand, also includes \v, Unicode "Separator, Space", etc.
//
// If the pattern is valid, but incompatible (contains a lookahead or backreference),
// then this function returns an empty string an error of type RegexpErrorIncompatible.
//
// If the pattern is invalid (not valid even in JavaScript), then this function
// returns an empty string and a generic error.
func ( string, ,  bool) ( string,  error) {

	if  == "" {
		return "", nil
	}

	 := _RegExp_parser{
		str:     ,
		length:  len(),
		dotAll:  ,
		unicode: ,
	}
	 = .parse()
	if  != nil {
		return "", 
	}

	return .ResultString(), nil
}

func ( *_RegExp_parser) () string {
	if .passOffset != -1 {
		return .str[:.passOffset]
	}
	return .goRegexp.String()
}

func ( *_RegExp_parser) () ( error) {
	.read() // Pull in the first character
	.scan()
	return .err
}

func ( *_RegExp_parser) () {
	if .offset < .length {
		.chrOffset = .offset
		,  := rune(.str[.offset]), 1
		if  >= utf8.RuneSelf { // !ASCII
			,  = utf8.DecodeRuneInString(.str[.offset:])
			if  == utf8.RuneError &&  == 1 {
				.error(true, "Invalid UTF-8 character")
				return
			}
		}
		.offset += 
		.chr = 
	} else {
		.chrOffset = .length
		.chr = -1 // EOF
	}
}

func ( *_RegExp_parser) () {
	.goRegexp.Grow(3 * len(.str) / 2)
	.goRegexp.WriteString(.str[:.passOffset])
	.passOffset = -1
}

func ( *_RegExp_parser) ( []byte) {
	if .passOffset != -1 {
		.stopPassing()
	}
	.goRegexp.Write()
}

func ( *_RegExp_parser) ( byte) {
	if .passOffset != -1 {
		.stopPassing()
	}
	.goRegexp.WriteByte()
}

func ( *_RegExp_parser) ( string) {
	if .passOffset != -1 {
		.stopPassing()
	}
	.goRegexp.WriteString()
}

func ( *_RegExp_parser) () {
	for .chr != -1 {
		switch .chr {
		case '\\':
			.read()
			.scanEscape(false)
		case '(':
			.pass()
			.scanGroup()
		case '[':
			.scanBracket()
		case ')':
			.error(true, "Unmatched ')'")
			return
		case '.':
			if .dotAll {
				.pass()
				break
			}
			.writeString(Re2Dot)
			.read()
		default:
			.pass()
		}
	}
}

// (...)
func ( *_RegExp_parser) () {
	 := .str[.chrOffset:]
	if len() > 1 { // A possibility of (?= or (?!
		if [0] == '?' {
			 := [1]
			switch {
			case  == '=' ||  == '!':
				.error(false, "re2: Invalid (%s) <lookahead>", .str[.chrOffset:.chrOffset+2])
				return
			case  == '<':
				.error(false, "re2: Invalid (%s) <lookbehind>", .str[.chrOffset:.chrOffset+2])
				return
			case  != ':':
				.error(true, "Invalid group")
				return
			}
		}
	}
	for .chr != -1 && .chr != ')' {
		switch .chr {
		case '\\':
			.read()
			.scanEscape(false)
		case '(':
			.pass()
			.()
		case '[':
			.scanBracket()
		case '.':
			if .dotAll {
				.pass()
				break
			}
			.writeString(Re2Dot)
			.read()
		default:
			.pass()
			continue
		}
	}
	if .chr != ')' {
		.error(true, "Unterminated group")
		return
	}
	.pass()
}

// [...]
func ( *_RegExp_parser) () {
	 := .str[.chrOffset:]
	if strings.HasPrefix(, "[]") {
		// [] -- Empty character class
		.writeString("[^\u0000-\U0001FFFF]")
		.offset += 1
		.read()
		return
	}

	if strings.HasPrefix(, "[^]") {
		.writeString("[\u0000-\U0001FFFF]")
		.offset += 2
		.read()
		return
	}

	.pass()
	for .chr != -1 {
		if .chr == ']' {
			break
		} else if .chr == '\\' {
			.read()
			.scanEscape(true)
			continue
		}
		.pass()
	}
	if .chr != ']' {
		.error(true, "Unterminated character class")
		return
	}
	.pass()
}

// \...
func ( *_RegExp_parser) ( bool) {
	 := .chrOffset

	var ,  uint32
	switch .chr {

	case '0', '1', '2', '3', '4', '5', '6', '7':
		var  int64
		 := 0
		for {
			 := int64(digitValue(.chr))
			if  >= 8 {
				// Not a valid digit
				break
			}
			 = *8 + 
			.read()
			 += 1
		}
		if  == 1 { // The number of characters read
			if  != 0 {
				// An invalid backreference
				.error(false, "re2: Invalid \\%d <backreference>", )
				return
			}
			.passString(-1, .chrOffset)
			return
		}
		 := []byte{'\\', 'x', '0', 0}
		if  >= 16 {
			 = [0:2]
		} else {
			 = [0:3]
		}
		 = strconv.AppendInt(, , 16)
		.write()
		return

	case '8', '9':
		.read()
		.error(false, "re2: Invalid \\%s <backreference>", .str[:.chrOffset])
		return

	case 'x':
		.read()
		,  = 2, 16

	case 'u':
		.read()
		if .chr == '{' && .unicode {
			.read()
			,  = 0, 16
		} else {
			,  = 4, 16
		}

	case 'b':
		if  {
			.write([]byte{'\\', 'x', '0', '8'})
			.read()
			return
		}
		fallthrough

	case 'B':
		fallthrough

	case 'd', 'D', 'w', 'W':
		// This is slightly broken, because ECMAScript
		// includes \v in \s, \S, while re2 does not
		fallthrough

	case '\\':
		fallthrough

	case 'f', 'n', 'r', 't', 'v':
		.passString(-1, .offset)
		.read()
		return

	case 'c':
		.read()
		var  int64
		if 'a' <= .chr && .chr <= 'z' {
			 = int64(.chr - 'a' + 1)
		} else if 'A' <= .chr && .chr <= 'Z' {
			 = int64(.chr - 'A' + 1)
		} else {
			.writeByte('c')
			return
		}
		 := []byte{'\\', 'x', '0', 0}
		if  >= 16 {
			 = [0:2]
		} else {
			 = [0:3]
		}
		 = strconv.AppendInt(, , 16)
		.write()
		.read()
		return
	case 's':
		if  {
			.writeString(WhitespaceChars)
		} else {
			.writeString("[" + WhitespaceChars + "]")
		}
		.read()
		return
	case 'S':
		if  {
			.error(false, "S in class")
			return
		} else {
			.writeString("[^" + WhitespaceChars + "]")
		}
		.read()
		return
	default:
		// $ is an identifier character, so we have to have
		// a special case for it here
		if .chr == '$' || .chr < utf8.RuneSelf && !isIdentifierPart(.chr) {
			// A non-identifier character needs escaping
			.passString(-1, .offset)
			.read()
			return
		}
		// Unescape the character for re2
		.pass()
		return
	}

	// Otherwise, we're a \u.... or \x...
	 := .chrOffset

	if  > 0 {
		for  := ;  > 0; -- {
			 := uint32(digitValue(.chr))
			if  >=  {
				// Not a valid digit
				goto 
			}
			.read()
		}
	} else {
		for .chr != '}' && .chr != -1 {
			 := uint32(digitValue(.chr))
			if  >=  {
				// Not a valid digit
				.error(true, "Invalid Unicode escape")
				return
			}
			.read()
		}
	}

	if  == 4 ||  == 0 {
		.write([]byte{
			'\\',
			'x',
			'{',
		})
		.passString(, .chrOffset)
		if  != 0 {
			.writeByte('}')
		}
	} else if  == 2 {
		.passString(-1, +2)
	} else {
		// Should never, ever get here...
		.error(true, "re2: Illegal branch in scanEscape")
		return
	}

	return

:
	.passString(, .chrOffset)
}

func ( *_RegExp_parser) () {
	if .passOffset == .chrOffset {
		.passOffset = .offset
	} else {
		if .passOffset != -1 {
			.stopPassing()
		}
		if .chr != -1 {
			.goRegexp.WriteRune(.chr)
		}
	}
	.read()
}

func ( *_RegExp_parser) (,  int) {
	if .passOffset ==  {
		.passOffset = 
		return
	}
	if .passOffset != -1 {
		.stopPassing()
	}
	.goRegexp.WriteString(.str[:])
}

func ( *_RegExp_parser) ( bool,  string,  ...interface{}) {
	if .err != nil {
		return
	}
	 := regexpParseError{
		offset: .offset,
		err:    fmt.Sprintf(, ...),
	}
	if  {
		.err = RegexpSyntaxError{}
	} else {
		.err = RegexpErrorIncompatible{}
	}
	.offset = .length
	.chr = -1
}