// parses the ${param/pattern/string} string function
// parses the ${param//pattern/string} string function
// parses the ${param/#pattern/string} string function
// parses the ${param/%pattern/string} string function
965764
func (t *Tree) parseReplaceFunc(name string) (Node, error) {
	node := new(FuncNode)
	node.Param = name

	t.scanner.accept = acceptReplaceFunc
	t.scanner.mode = scanIdent
	switch t.scanner.scan() {
	case tokenIdent:
		node.Name = t.scanner.string()
	default:
		return nil, ErrBadSubstitution
	}

	// scan arg[1]
	{
		param, err := t.parseParam(acceptNotSlash, scanIdent|scanEscape)
		if err != nil {
			return nil, err
		}
		node.Args = append(node.Args, param)
	}

	// expect delimiter
	t.scanner.accept = acceptSlash
	t.scanner.mode = scanIdent
	switch t.scanner.scan() {
	case tokenIdent:
		// no-op
	default:
		return nil, ErrBadSubstitution
	}

	// check for blank string
	switch t.scanner.peek() {
	case '}':
		return node, t.consumeRbrack()
	}

	// scan arg[2]
	{
		param, err := t.parseParam(acceptNotClosing, scanIdent|scanEscape)
		if err != nil {
			return nil, err
		}
		node.Args = append(node.Args, param)
	}

	return node, t.consumeRbrack()
}
68951
func parseString(rest string, d *Directive) (*Node, map[string]bool, error) {
	if rest == "" {
		return nil, nil, nil
	}
	n := &Node{}
	n.Value = rest
	return n, nil, nil
}
212694
func Parse(s string) (map[string]interface{}, error) {
	vals := map[string]interface{}{}
	scanner := bytes.NewBufferString(s)
	t := newParser(scanner, vals, false)
	err := t.parse()
	return vals, err
}
1181795
func parsePrefix(s string) (string, string) {
	if strings.HasPrefix(s, "P") {
		return "P", s[1:]
	}
	if len(s) < 2 {
		return s, ""
	}
	return s[:2], s[2:]
}
1147990
func parseParam(line string, index int) (string, int, error) {
	newIndex := index

	if line[newIndex] != ' ' {
		return "", -1, fmt.Errorf("malformed param. No leading space")
	}

	newIndex++

	if len(line) == newIndex {
		return "", -1, fmt.Errorf("malformed parameter. End of string after space")
	}

	// SPACE ":" trailing
	if line[newIndex] == ':' {
		newIndex++

		if len(line) == newIndex {
			return "", -1, fmt.Errorf("malformed parameter. End of string after ':'")
		}

		// It is valid for there to be no characters. Because: trailing   =  *( ":"
		// / " " / nospcrlfcl )

		paramIndexStart := newIndex

		for newIndex < len(line) {
			if line[newIndex] == '\x00' || line[newIndex] == '\r' ||
				line[newIndex] == '\n' {
				break
			}
			newIndex++
		}

		return line[paramIndexStart:newIndex], newIndex, nil
	}

	// We know we are parsing a <middle> and that we've dealt with :. This means
	// we accept any character except NUL, CR, or LF. A space means we're at the
	// end of the param.

	// paramIndexStart points at the character after the space.
	paramIndexStart := newIndex

	for newIndex < len(line) {
		if line[newIndex] == '\x00' || line[newIndex] == '\r' ||
			line[newIndex] == '\n' || line[newIndex] == ' ' {
			break
		}
		newIndex++
	}

	// Must have at least one character in this case. See grammar for 'middle'.
	if paramIndexStart == newIndex {
		return "", -1, errEmptyParam
	}

	return line[paramIndexStart:newIndex], newIndex, nil
}
1059685
func (tokenizer *Tokenizer) parseString() (string, error) {
	start := tokenizer.scanner.Index()
	lastChar := tokenizer.scanner.Char()
	tokenizer.scanner.Advance()
	for tokenizer.CanRead() {
		if tokenizer.scanner.Char() == '"' {
			if lastChar == '\\' {
				lastChar = tokenizer.scanner.Char()
				tokenizer.scanner.Advance()
				continue
			}
			str := tokenizer.scanner.Substring(start, tokenizer.scanner.Index()+1)
			lang := ""
			datatype := ""
			canPeek, nextChar := tokenizer.scanner.Peek()
			var err error
			if canPeek {
				switch nextChar {
				case '@':
					tokenizer.scanner.Advance()
					lang = tokenizer.parseLanguage()
					str += lang
				case '^':
					tokenizer.scanner.Advance()
					datatype, err = tokenizer.parseType()
					str += datatype
				}
			}
			return str, err
		}
		lastChar = tokenizer.scanner.Char()
		tokenizer.scanner.Advance()
	}
	return "", tokenizer.Error("String did not end with \"")
}
618354
func (exp *FilterExpr) String() string {
	return fmt.Sprintf("FilterExpr(%s, %s)", exp.Name, exp.Args)
}
450119
func Parse(str, sep string) *Pair {
	return parse(str, strings.Index(str, sep))
}
1266877
func (value Value) String() string {
	result := ""
	catchPanic(func() {
		result = value.string()
	})
	return result
}
158681
func FindEllipsesPatterns(arg string) (ArgPattern, error) {
	var patterns []Pattern
	parts := regexpEllipses.FindStringSubmatch(arg)
	if len(parts) == 0 {
		// We throw an error if arg doesn't have any recognizable ellipses pattern.
		return nil, ErrInvalidEllipsesFormatFn(arg)
	}

	parts = parts[1:]
	patternFound := regexpEllipses.MatchString(parts[0])
	for patternFound {
		seq, err := parseEllipsesRange(parts[1])
		if err != nil {
			return patterns, err
		}
		patterns = append(patterns, Pattern{
			Prefix: "",
			Suffix: parts[2],
			Seq:    seq,
		})
		parts = regexpEllipses.FindStringSubmatch(parts[0])
		if len(parts) > 0 {
			parts = parts[1:]
			patternFound = HasEllipses(parts[0])
			continue
		}
		break
	}

	if len(parts) > 0 {
		seq, err := parseEllipsesRange(parts[1])
		if err != nil {
			return patterns, err
		}

		patterns = append(patterns, Pattern{
			Prefix: parts[0],
			Suffix: parts[2],
			Seq:    seq,
		})
	}

	// Check if any of the prefix or suffixes now have flower braces
	// left over, in such a case we generally think that there is
	// perhaps a typo in users input and error out accordingly.
	for _, pattern := range patterns {
		if strings.Count(pattern.Prefix, openBraces) > 0 || strings.Count(pattern.Prefix, closeBraces) > 0 {
			return nil, ErrInvalidEllipsesFormatFn(arg)
		}
		if strings.Count(pattern.Suffix, openBraces) > 0 || strings.Count(pattern.Suffix, closeBraces) > 0 {
			return nil, ErrInvalidEllipsesFormatFn(arg)
		}
	}

	return patterns, nil
}
737373
func Parse(arg string) (string, error) {
	if arg == "" {
		return "", errors.New("argument is empty")
	}

	if len(arg) == 1 {
		return "", errors.New("argument is too short")
	}

	if arg[0] != '-' {
		return "", errors.New("argument doesn't start with dash")
	}

	numMinuses := 1

	if arg[1] == '-' {
		numMinuses++
		if len(arg) == 2 {
			return "", errors.New("argument is too short")
		}
	}

	name := arg[numMinuses:]
	if len(name) == 0 || name[0] == '-' || name[0] == '=' {
		return "", fmt.Errorf("bad flag syntax: %s", arg)
	}

	return name, nil
}
410040
func (list *DefinitionList) parseAndAppendString(text string) {
	emptyLineRE := regexp.MustCompile("^[[:space:]]*$")
	depthRE := regexp.MustCompile("^( *)(.*)")

	startDefRE := regexp.MustCompile("^ *- (.*)")
	keyedDefRE := regexp.MustCompile("^([^:]+):[[:space:]]*(.*)")

	descTextRE := regexp.MustCompile("(?sUm:^(.*) *-)")

	var (
		currentList  *DefinitionList
		currentItem  *Definition
		currentDepth int
	)

	descMatch := descTextRE.FindStringSubmatch(text)
	if len(descMatch) > 1 {
		list.desc = descMatch[1]
		text = text[len(descMatch[1]):]
	}

	currentList = list

	for lineNo, line := range strings.Split(text, "\n") {
		// nesting depth == nr. of indentation spaces
		if emptyLineRE.MatchString(line) {
			continue
		}
		newDepth := len(depthRE.ReplaceAllString(line, "$1"))

		if startDefRE.MatchString(line) {
			// Start new definition
			start := startDefRE.ReplaceAllString(line, "$1")

			newItem := &Definition{}
			if keyedDefRE.MatchString(start) {
				newItem.name = keyedDefRE.ReplaceAllString(start, "$1")
				newItem.desc = keyedDefRE.ReplaceAllString(start, "$2") + "\n"

			} else {
				newItem.name = ""
				newItem.desc = start + "\n"
			}

			if newDepth == currentDepth {
				// Keep current nesting level
				if currentItem != nil {
					newItem.parent = currentItem.parent
				}

			} else if newDepth > currentDepth {
				// Dive deeper
				newItem.parent = currentItem
				currentItem.children = DefinitionList{}
				currentList = &currentItem.children

			} else if newDepth < currentDepth {
				// One up
				if currentItem.parent.parent != nil {
					currentList = &currentItem.parent.parent.children
				} else {
					currentList = list
				}
			}

			currentList.add(newItem)
			currentItem = newItem
			currentDepth = newDepth

		} else if currentItem == nil {
			// Parse error
			panic(fmt.Sprintf("Comment line %d: Parse error: currentItem == nil "+
				"near \"%s\" (plain paragraphs mixed with definition list?)\n", lineNo, line))

		} else if newDepth != currentDepth {
			// Parse error
			panic(fmt.Sprintf("Comment line %d: Indentation error: expected indentation "+
				"to remain at %d, found %d near \"%s\"\n", lineNo, currentDepth, newDepth, line))

		} else {
			// Append to current definition (without indentation)
			currentItem.desc += depthRE.ReplaceAllString(line, "$2") + "\n"
		}
	}
}
840219
func Regexp(expr string) (func(string) bool, error) {
	if expr == "" {
		return nil, fmt.Errorf("empty regex expression")
	}

	// add the last $ if missing (and not wildcard(?))
	if i := expr[len(expr)-1]; i != '$' && i != '*' {
		expr += "$"
	}

	r, err := regexp.Compile(expr)
	if err != nil {
		return nil, err
	}

	return r.MatchString, nil
}
1286937
func (f *File) String() string {
	return fmt.Sprintf("ParamName: %v; FileName: %v", f.ParamName, f.Name)
}
1046567
func (t *Template) ParseString(s string) error {
	return t.Parse(strings.NewReader(s))
}
561257
func ParseString(s string) (*Query, error) {
	return NewParser(strings.NewReader(s)).Parse()
}
338946
func (p *TerminalParams) String() string {
	return fmt.Sprintf("TerminalParams(w=%v, h=%v)", p.W, p.H)
}