func TestParseIni(t *testing.T) { var token lex.Token expected := []struct { typ lex.TokenType line int start int width int value string }{ {tKey, 2, 1, 3, "key"}, {tEqualSign, 2, 4, 1, "="}, {tValue, 2, 5, 7, "abcdefg"}, {tLeftBracket, 4, 14, 1, "["}, {tSection, 4, 15, 4, "User"}, {tRightBracket, 4, 19, 1, "]"}, {tKey, 5, 21, 8, "userName"}, {tEqualSign, 5, 29, 1, "="}, {tValue, 5, 30, 3, "otm"}, } i := 0 l := lex.NewLexer("", str, lexStart) lex.UpdateTokenTypes(tokenMap) defer lex.RestoreTokenTypes() for { token = l.NextToken() switch token.Type { case lex.TError: fmt.Println(token) return case lex.TEOF: return default: if expected[i].typ != token.Type { t.Errorf("Incorrect toke.Type, expected: %v, got: %v, value: %v", expected[i].typ, token.Type, token.Value) } if expected[i].line != token.Line { t.Errorf("Incorrect toke.Line, expected: %v, got: %v, value: %v", expected[i].line, token.Line, token.Value) } if expected[i].start != token.Start { t.Errorf("Incorrect toke.Start, expected: %v, got: %v, value: %v", expected[i].start, token.Start, token.Value) } if expected[i].width != token.Width { t.Errorf("Incorrect toke.Width, expected: %v, got: %v, value: %v", expected[i].width, token.Width, token.Value) } if expected[i].value != token.Value { t.Errorf("Incorrect toke.Value, expected: %v, got: %v, value: %v", expected[i].value, token.Value, token.Value) } } i++ } }
// Parse parses a string func Parse(str string) Comments { var token lex.Token comments := make(Comments, 0) l := lex.NewLexer("", str, LexBegin) for { token = l.NextToken() if token.Type == TComment { comment := Comment{Value: token.Value, Row: token.Line} comments = append(comments, comment) } if token.Type == lex.TEOF { return comments } } }