style(parser): cutprefix, default case, comma and tokens rename

This commit is contained in:
2026-08-29 15:14:49 +02:00
parent 970df7c32a
commit 9a34733615
2 changed files with 18 additions and 11 deletions
+13 -11
View File
@@ -30,8 +30,8 @@ func (e Error) Error() string {
// Parse scans and parses src, returning the file and any diagnostics. The
// returned file is usable even when errors is non-empty.
func Parse(path, src string) (*ast.File, []error) {
toks := lexer.Tokenize(src)
lines := splitLines(toks)
tokens := lexer.Tokenize(src)
lines := splitLines(tokens)
p := &state{path: path}
p.parse(lines)
return p.file, p.errs
@@ -52,10 +52,10 @@ func (p *state) errorf(pos token.Position, format string, args ...any) {
}
// splitLines groups the token stream into lines, dropping the Newline tokens.
func splitLines(toks []token.Token) [][]token.Token {
func splitLines(tokens []token.Token) [][]token.Token {
var lines [][]token.Token
var cur []token.Token
for _, t := range toks {
for _, t := range tokens {
if t.Kind == token.EOF {
break
}
@@ -403,7 +403,7 @@ func parseAddress(g []token.Token) ast.Address {
}
// Symbol-with-pseudo form: name[<>][+off](PSEUDO).
// When the prefix is not a valid symbol name (e.g. a bare number like
// 0(SP) in RISC-V), sym is nil and we fall through to regular memory
// 0(SP) in RISC-V), sym is nil, and we fall through to regular memory
// operand parsing instead of returning an empty address.
if idx := findPseudoParen(g); idx >= 0 {
sym, _ := parseSymbolPrefix(g[:idx+3])
@@ -549,6 +549,9 @@ func splitFirstComma(g []token.Token) (before, after []token.Token) {
if depth == 0 {
return g[:i], g[i+1:]
}
default:
// Every other token kind is inert at the top level of the
// group; the scan just keeps looking for the first comma.
}
}
return g, nil
@@ -615,13 +618,12 @@ func joinRaw(g []token.Token) string {
// commentText removes a leading // or /* marker from a comment token's text.
func commentText(s string) string {
if strings.HasPrefix(s, "//") {
return strings.TrimSpace(strings.TrimPrefix(s, "//"))
if body, ok := strings.CutPrefix(s, "//"); ok {
return strings.TrimSpace(body)
}
if strings.HasPrefix(s, "/*") {
s = strings.TrimPrefix(s, "/*")
s = strings.TrimSuffix(s, "*/")
return strings.TrimSpace(s)
if body, ok := strings.CutPrefix(s, "/*"); ok {
body = strings.TrimSuffix(body, "*/")
return strings.TrimSpace(body)
}
return s
}