Example #1
0
// LexBegin is the initial lexer function
func LexBegin(lexer *lex.Lexer) lex.StateFn {
	lexer.AcceptWhitespace(lex.Ignore)

	if strings.HasPrefix(lexer.InputToEnd(), MinusSign) {
		return LexSingleLineComment
	}
	return LexCode
}
Example #2
0
func lexSection(l *lex.Lexer) lex.StateFn {
	for {
		switch l.Next() {
		case lex.EOF:
			return l.Errorf("Unexpected EOF in section")
		case '\n':
			return l.Errorf("Section: Missing right bracket")
		case rightBracket:
			l.Backup()
			l.Emit(tSection)
			return lexRightBracket
		}
	}
}
Example #3
0
func lexKey(l *lex.Lexer) lex.StateFn {
	for {
		switch l.Next() {
		case lex.EOF:
			return l.Errorf("Unexpected EOF")
		case equalSign:
			if l.Pos > l.Start {
				l.Backup()
				l.Emit(tKey)
				return lexEqualSign
			}
			return l.Errorf("Unexpected equal sign")
		}
	}
}
Example #4
0
// LexMultiLineComment lexer
func LexMultiLineComment(lexer *lex.Lexer) lex.StateFn {
	lexer.Step(2, lex.Ignore)

	for {
		if strings.HasPrefix(lexer.InputToEnd(), RightBracket) {
			lexer.Emit(TComment)
			return LexBegin(lexer)
		}

		lexer.Next()

		if lexer.IsEOF() {
			panic("EndOfFile")
		}
	}
}
Example #5
0
func lexStart(l *lex.Lexer) lex.StateFn {
	l.AcceptWhitespace(lex.Ignore)

	r := l.Next()
	switch r {
	case lex.EOF:
		l.Emit(lex.TEOF)
		return nil
	case leftBracket:
		l.Backup()
		return lexLeftBracket
	default:
		if lex.IsAlphaNumeric(r) {
			return lexKey
		}
		return l.Errorf("Unknown charecter: %v", string(r))
	}
}
Example #6
0
// LexCode parsers random code
func LexCode(lexer *lex.Lexer) lex.StateFn {
	for {
		if strings.HasPrefix(lexer.InputToEnd(), MinusSign) {
			lexer.Start = lexer.Pos
			return LexSingleLineComment(lexer)
		}

		if strings.HasPrefix(lexer.InputToEnd(), string(lex.NewLine)) {
			lexer.Next()
			lexer.Start = lexer.Pos
			return LexCode(lexer)
		}

		lexer.Next()

		if lexer.IsEOF() {
			lexer.Emit(lex.TEOF)
			lexer.Shutdown()
			return nil
		}
	}
}
Example #7
0
// LexSingleLineComment lexer
func LexSingleLineComment(lexer *lex.Lexer) lex.StateFn {
	lexer.Step(2, lex.Ignore)
	if strings.HasPrefix(lexer.InputToEnd(), LeftBracket) {
		return LexMultiLineComment(lexer)
	}

	for {
		if strings.HasPrefix(lexer.InputToEnd(), string(lex.NewLine)) {
			lexer.Emit(TComment)
			return LexBegin(lexer)
		}

		lexer.Next()

		if lexer.IsEOF() {
			lexer.Emit(TComment)
			lexer.Emit(lex.TEOF)
			lexer.Shutdown()
			return nil
		}
	}
}
Example #8
0
func lexRightBracket(l *lex.Lexer) lex.StateFn {
	l.Accept("]", lex.Emit(tRightBracket))
	return lexStart
}
Example #9
0
func lexLeftBracket(l *lex.Lexer) lex.StateFn {
	l.Accept("[", lex.Emit(tLeftBracket))
	return lexSection
}
Example #10
0
func lexValue(l *lex.Lexer) lex.StateFn {
	for {
		switch l.Next() {
		case lex.EOF:
			if l.Pos > l.Start {
				l.Backup()
				l.Emit(tValue)
				l.Emit(lex.TEOF)
				return nil
			}
			return l.Errorf("Unexpected equal sign")
		case '\n':
			l.Backup()
			l.Emit(tValue)
			l.Next(lex.Ignore)
			return lexStart
		}
	}
}
Example #11
0
func lexEqualSign(l *lex.Lexer) lex.StateFn {
	l.Accept("=", lex.Emit(tEqualSign))
	return lexValue
}