chore: handle other token types and special symbols

This commit is contained in:
2026-04-20 21:28:26 +01:00
parent 3af11b0a2a
commit 33fb7d863a
+27 -9
View File
@@ -1,27 +1,45 @@
package parser package parser
import ( import (
"strings"
"github.com/hazemKrimi/jack-compiler/internal/tokenizer" "github.com/hazemKrimi/jack-compiler/internal/tokenizer"
) )
func ParseTokens(tokens []tokenizer.Token) string { func ParseTokens(tokens []tokenizer.Token) string {
output := "<tokens>\n" var output strings.Builder
output.WriteString("<tokens>\n")
for _, token := range tokens { for _, token := range tokens {
switch token.Type { switch token.Type {
case tokenizer.SYMBOL: case tokenizer.SYMBOL:
{ var value string
output += "<symbol>" + token.Value + "</symbol>\n"
} switch token.Value {
case tokenizer.KEYWORD: case "<":
{ value = "&lt;"
output += "<keyword>" + token.Value + "</keyword>\n" case ">":
value = "&gt;"
case "&":
value = "&amp;"
default:
value = token.Value
} }
output.WriteString("<symbol> " + value + " </symbol>\n")
case tokenizer.KEYWORD:
output.WriteString("<keyword> " + token.Value + " </keyword>\n")
case tokenizer.IDENTIFIER:
output.WriteString("<identifier> " + token.Value + " </identifier>\n")
case tokenizer.INT_CONST:
output.WriteString("<integerConstant> " + token.Value + " </integerConstant>\n")
case tokenizer.STR_CONST:
output.WriteString("<stringConstant> " + token.Value + " </stringConstant>\n")
} }
} }
output += "</tokens>\n" output.WriteString("</tokens>\n")
return output return output.String()
} }