// 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 }
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 } } }
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") } } }
// 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") } } }
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)) } }
// 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 } } }
// 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 } } }
func lexRightBracket(l *lex.Lexer) lex.StateFn { l.Accept("]", lex.Emit(tRightBracket)) return lexStart }
func lexLeftBracket(l *lex.Lexer) lex.StateFn { l.Accept("[", lex.Emit(tLeftBracket)) return lexSection }
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 } } }
func lexEqualSign(l *lex.Lexer) lex.StateFn { l.Accept("=", lex.Emit(tEqualSign)) return lexValue }