displayTokens displays token information
(tokens []models.TokenWithSpan)
| 120 | |
| 121 | // displayTokens displays token information |
| 122 | func (p *Parser) displayTokens(tokens []models.TokenWithSpan) error { |
| 123 | type TokenDisplay struct { |
| 124 | Type string `json:"type" yaml:"type"` |
| 125 | Value string `json:"value" yaml:"value"` |
| 126 | Line int `json:"line" yaml:"line"` |
| 127 | Column int `json:"column" yaml:"column"` |
| 128 | Position int `json:"position" yaml:"position"` |
| 129 | } |
| 130 | |
| 131 | var tokenList []TokenDisplay |
| 132 | for _, token := range tokens { |
| 133 | tokenList = append(tokenList, TokenDisplay{ |
| 134 | Type: token.Token.Type.String(), |
| 135 | Value: token.Token.Value, |
| 136 | Line: token.Start.Line, |
| 137 | Column: token.Start.Column, |
| 138 | Position: token.Start.Line*1000 + token.Start.Column, |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | switch strings.ToLower(p.Opts.Format) { |
| 143 | case "json": |
| 144 | encoder := json.NewEncoder(p.Out) |
| 145 | encoder.SetIndent("", " ") |
| 146 | return encoder.Encode(map[string]interface{}{ |
| 147 | "tokens": tokenList, |
| 148 | "count": len(tokenList), |
| 149 | }) |
| 150 | case "yaml": |
| 151 | encoder := yaml.NewEncoder(p.Out) |
| 152 | defer func() { |
| 153 | if err := encoder.Close(); err != nil { |
| 154 | fmt.Fprintf(p.Err, "Warning: failed to close YAML encoder: %v\n", err) |
| 155 | } |
| 156 | }() |
| 157 | return encoder.Encode(map[string]interface{}{ |
| 158 | "tokens": tokenList, |
| 159 | "count": len(tokenList), |
| 160 | }) |
| 161 | default: |
| 162 | fmt.Fprintf(p.Out, "Tokens (%d total):\n", len(tokenList)) |
| 163 | fmt.Fprintf(p.Out, "%-20s %-15s %8s %8s %8s\n", "Type", "Value", "Line", "Column", "Pos") |
| 164 | fmt.Fprintf(p.Out, "%s\n", strings.Repeat("-", 70)) |
| 165 | for _, token := range tokenList { |
| 166 | value := token.Value |
| 167 | if len(value) > 15 { |
| 168 | value = value[:12] + "..." |
| 169 | } |
| 170 | fmt.Fprintf(p.Out, "%-20s %-15s %8d %8d %8d\n", |
| 171 | token.Type, value, token.Line, token.Column, token.Position) |
| 172 | } |
| 173 | return nil |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // StatementDisplay represents a simplified statement for display |
| 178 | type StatementDisplay struct { |