Beispiel #1
0
func stStart(l lex.Lexer) lex.StateFn {
	char := l.PeekRune(0)

	switch true {
	case char == lex.RuneEOF:
		l.EmitEOF()
		return nil
	// Read comments
	case char == '-' && l.PeekRune(1) == '-':
		return stComment
	// Double-quote string
	case char == '"' || char == '\'':
		return stReadString
	// Read 3char symbols
	case inMapR(char, symbols_3c_1) && inMap(concatRunes(char, l.PeekRune(1), l.PeekRune(2)), symbols_3c):
		l.NextRune()
		l.NextRune()
		l.NextRune()
		emit(l, Symbol)
		return stStart
	// 2char symbols
	case inMapR(char, symbols_2c_1) && inMap(concatRunes(char, l.PeekRune(1)), symbols_2c):
		l.NextRune()
		l.NextRune()
		emit(l, Symbol)
		return stStart
	// 1char symbols
	case inMapR(char, symbols_1c):
		l.NextRune()
		emit(l, Symbol)
		return stStart
	// Read a Name identifier
	case inMapR(char, nameFirstChar):
		return stReadName
	// Read whitespace
	case inMapR(char, whitespaceChars):
		emit(l, Whitespace)
		return stWhitespace
	// Read Long strings
	case longStringEquals('[', l) >= 0:
		return stLongString
	// Read hex numbers
	case char == '0' && (unicode.ToLower(l.PeekRune(1)) == 'x'):
		return stHex
	// Read normal numbers
	case inMapR(char, digits):
		return stNumber
	}
	return nil
}
Beispiel #2
0
func stReadString(l lex.Lexer) lex.StateFn {
	delim := l.NextRune()
	if !(delim == '\'' || delim == '"') {
		emitError("Expected `\"` or `'` to start quote-string.")
	}
	for {
		char := l.NextRune()
		switch char {
		case delim:
			// end of string
			fmt.Println("End of the string!")
			emit(l, String)
			return stStart
		case '\\':
			l.NextRune()
		case lex.RuneEOF:
			l.EmitEOF()
		case '\n':
			l.NewLine()
		}
	}
	return stStart
}