Exemplo n.º 1
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
		}
	}
}
Exemplo n.º 2
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")
		}
	}
}
Exemplo n.º 3
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")
		}
	}
}
Exemplo n.º 4
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))
	}
}
Exemplo n.º 5
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
		}
	}
}
Exemplo n.º 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
		}
	}
}
Exemplo n.º 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
		}
	}
}