Ejemplo n.º 1
0
// ScanEscape writes escaped sequence to buffer.
func ScanEscape(scanner scanner.Scanner, buffer *bytes.Buffer) error {
	escapedRune, err := scanner.PeekRune()
	if err != nil {
		return err
	}
	scanner.NextRune()
	switch escapedRune {
	case '"':
		buffer.WriteString("\"")
	case '\\':
		buffer.WriteString("\\")
	case '/':
		buffer.WriteString("/")
	case 'b':
		buffer.WriteString("\b")
	case 'f':
		buffer.WriteString("\f")
	case 'n':
		buffer.WriteString("\n")
	case 'r':
		buffer.WriteString("\r")
	case 't':
		buffer.WriteString("\t")
	case 'u':
		unicodes, err := ReadUnicodeSequence(scanner)
		if err != nil {
			return err
		}
		buffer.WriteString(unicodes)
	default:
		return fmt.Errorf("%s is invalid escaped rune", string(escapedRune))

	}
	return nil
}
Ejemplo n.º 2
0
// ScanNumber returns string.
func ScanNumber(scanner scanner.Scanner, firstRune rune) (string, error) {
	nam := NewNAMState()
	err := nam.Input(firstRune)
	if err != nil {
		return "", err
	}
	for {
		peekedRune, err := scanner.PeekRune()
		if err == io.EOF {
			nam.EOF()
			break
		}
		if err != nil {
			return "", err
		}
		err = nam.Input(peekedRune)
		if err != nil {
			return "", err
		}
		if nam.state != -1 {
			scanner.NextRune()
		} else {
			break
		}
	}
	if !nam.Finished() {
		return "", fmt.Errorf("Invalid number")
	}
	return nam.Result(), nil
}
Ejemplo n.º 3
0
// ScanString returns string.
func ScanString(scanner scanner.Scanner) (string, error) {
	var buffer bytes.Buffer
	for {
		peekedRune, err := scanner.PeekRune()
		if err != nil {
			return "", err
		}
		if peekedRune == '"' {
			scanner.NextRune()
			break
		}
		if peekedRune != '\\' {
			scanner.NextRune()
			buffer.WriteRune(peekedRune)
		} else {
			scanner.NextRune()
			ScanEscape(scanner, &buffer)
		}
	}
	return buffer.String(), nil
}