style(parser): cutprefix, default case, comma and tokens rename
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
# Metadata (always first, per repo convention)
|
||||||
|
.idea/
|
||||||
|
.zcode/
|
||||||
|
.mimocode/
|
||||||
|
|
||||||
# Binaries
|
# Binaries
|
||||||
/gasm
|
/gasm
|
||||||
/bin/
|
/bin/
|
||||||
|
|||||||
+13
-11
@@ -30,8 +30,8 @@ func (e Error) Error() string {
|
|||||||
// Parse scans and parses src, returning the file and any diagnostics. The
|
// Parse scans and parses src, returning the file and any diagnostics. The
|
||||||
// returned file is usable even when errors is non-empty.
|
// returned file is usable even when errors is non-empty.
|
||||||
func Parse(path, src string) (*ast.File, []error) {
|
func Parse(path, src string) (*ast.File, []error) {
|
||||||
toks := lexer.Tokenize(src)
|
tokens := lexer.Tokenize(src)
|
||||||
lines := splitLines(toks)
|
lines := splitLines(tokens)
|
||||||
p := &state{path: path}
|
p := &state{path: path}
|
||||||
p.parse(lines)
|
p.parse(lines)
|
||||||
return p.file, p.errs
|
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.
|
// 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 lines [][]token.Token
|
||||||
var cur []token.Token
|
var cur []token.Token
|
||||||
for _, t := range toks {
|
for _, t := range tokens {
|
||||||
if t.Kind == token.EOF {
|
if t.Kind == token.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -403,7 +403,7 @@ func parseAddress(g []token.Token) ast.Address {
|
|||||||
}
|
}
|
||||||
// Symbol-with-pseudo form: name[<>][+off](PSEUDO).
|
// Symbol-with-pseudo form: name[<>][+off](PSEUDO).
|
||||||
// When the prefix is not a valid symbol name (e.g. a bare number like
|
// 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.
|
// operand parsing instead of returning an empty address.
|
||||||
if idx := findPseudoParen(g); idx >= 0 {
|
if idx := findPseudoParen(g); idx >= 0 {
|
||||||
sym, _ := parseSymbolPrefix(g[:idx+3])
|
sym, _ := parseSymbolPrefix(g[:idx+3])
|
||||||
@@ -549,6 +549,9 @@ func splitFirstComma(g []token.Token) (before, after []token.Token) {
|
|||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
return g[:i], g[i+1:]
|
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
|
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.
|
// commentText removes a leading // or /* marker from a comment token's text.
|
||||||
func commentText(s string) string {
|
func commentText(s string) string {
|
||||||
if strings.HasPrefix(s, "//") {
|
if body, ok := strings.CutPrefix(s, "//"); ok {
|
||||||
return strings.TrimSpace(strings.TrimPrefix(s, "//"))
|
return strings.TrimSpace(body)
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(s, "/*") {
|
if body, ok := strings.CutPrefix(s, "/*"); ok {
|
||||||
s = strings.TrimPrefix(s, "/*")
|
body = strings.TrimSuffix(body, "*/")
|
||||||
s = strings.TrimSuffix(s, "*/")
|
return strings.TrimSpace(body)
|
||||||
return strings.TrimSpace(s)
|
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user