Beispiel #1
0
func stHex(l lex.Lexer) lex.StateFn {
	if l.NextRune() != '0' {
		emitError("Hex numbers should start with 0")
		return nil
	}
	if unicode.ToLower(l.NextRune()) != 'x' {
		emitError("Malformatted hex number (no x)")
		return nil
	}

	baseChars := 0
	for ; inMapR(l.PeekRune(0), hexdigits); baseChars++ {
		l.NextRune()
	}

	fmt.Println("Base Chars:", baseChars)

	if l.PeekRune(0) == '.' {
		l.NextRune()
		if !l.MatchOneOrMoreBytes(hexdigits_b) {
			emitError("Malformed hex number.")
			return nil
		}
	} else if baseChars == 0 {
		emitError("Malformed hex number (no base chars or decimal).")
		return nil
	}

	if unicode.ToLower(l.PeekRune(0)) == 'p' {
		l.NextRune()

		next := l.PeekRune(0)
		if next == '+' || next == '-' {
			l.NextRune()

		}
		if !l.MatchOneOrMoreBytes(digits_b) {
			emitError("Malformed hex number (no digits after p)")
			return nil
		}
	}

	emit(l, HexNumber)
	return stStart
}
Beispiel #2
0
func stNumber(l lex.Lexer) lex.StateFn {
	if !l.MatchOneOrMoreBytes(digits_b) {
		emitError("Malformatted number: no principle number.")
	}
	if l.MatchOneRune('.') {
		if !l.MatchOneOrMoreBytes(digits_b) {
			emitError("Malformatted number: no fractional part in float.")
		}
	}
	if l.MatchOneRunes([]rune{'e', 'E'}) {
		l.MatchZeroOrOneRunes([]rune{'-', '+'})
		if !l.MatchOneOrMoreBytes(digits_b) {
			emitError("Malformatted number: no exponential part in float.")
		}
	}
	emit(l, Number)
	return stStart
}