Skip to content

Commit

Permalink
add context.help and context.note
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 30, 2024
1 parent d4b0691 commit 40a2b80
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
10 changes: 10 additions & 0 deletions compiler/context/report.v
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ pub:
pos ?ast.FilePos
}

@[inline]
pub fn note(msg string) Hint {
return Hint{.note, msg, none}
}

@[inline]
pub fn help(msg string) Hint {
return Hint{.help, msg, none}
}

@[params]
struct ReportParams {
pos ?ast.FilePos
Expand Down
10 changes: 2 additions & 8 deletions compiler/parser/stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ fn (mut p Parser) parse_stmts() []ast.Stmt {
p.expect_semicolon = false
if p.tok.kind != .lbrace {
p.abort = true
context.error('expected block, found ${p.tok}', p.tok.pos, context.Hint{
kind: .help
msg: 'if you want to write a single-statement, use `:`: `if (is_online): player.kick()`'
})
context.error('expected block, found ${p.tok}', p.tok.pos, context.help('if you want to write a single-statement, use `:`: `if (is_online): player.kick()`'))
return []
}

Expand Down Expand Up @@ -252,10 +249,7 @@ fn (mut p Parser) parse_defer_stmt() ast.DeferStmt {
defer_mode = .error
}
else {
context.error('unknown `defer` mode', mode_pos, context.Hint{
kind: .note
msg: 'valid `defer` modes are `success` and `error`'
})
context.error('unknown `defer` mode', mode_pos, context.note('valid `defer` modes are `success` and `error`'))
}
}
p.expect(.rparen)
Expand Down
5 changes: 1 addition & 4 deletions compiler/sema/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ fn (mut sema Sema) fn_stmt(mut stmt ast.FnStmt) {
is_arg: true
type: arg.type
}) or {
context.error(err.msg(), arg.pos, context.Hint{
kind: .note
msg: 'inside function `${stmt.name}`'
})
context.error(err.msg(), arg.pos, context.note('inside function `${stmt.name}`'))
}
}
sema.stmts(mut stmt.stmts)
Expand Down
15 changes: 3 additions & 12 deletions compiler/token/tokenizer/read.v
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ fn (mut t Tokenizer) read_number_mode(mode NumberMode) string {
t.pos--
fl := t.text[start..t.pos]
context.error('float literals should have a digit after the decimal point',
t.current_pos(), context.Hint{
kind: .help
msg: 'use `${fl}.0` instead of `${fl}`'
})
t.current_pos(), context.help('use `${fl}.0` instead of `${fl}`'))
t.pos++
}
}
Expand Down Expand Up @@ -177,10 +174,7 @@ fn (mut t Tokenizer) read_number_mode(mode NumberMode) string {
old_pos := t.pos
t.pos = start
context.error('zeros are not allowed at the beginning of a decimal literal', t.current_pos(),
context.Hint{
kind: .note
msg: 'use the prefix `0o` to denote an octal number'
})
context.note('use the prefix `0o` to denote an octal number'))
t.pos = old_pos
}
t.pos-- // fix pos
Expand Down Expand Up @@ -225,10 +219,7 @@ fn (mut t Tokenizer) read_char() string {
context.error('empty character literal', t.current_pos())
} else if len != 1 {
context.error('character literal may only contain one codepoint', t.current_pos(),
context.Hint{
kind: .help
msg: 'if you meant to write a string literal, use double quotes'
})
context.help('if you meant to write a string literal, use double quotes'))
}
return ch
}
Expand Down

0 comments on commit 40a2b80

Please sign in to comment.