Skip to content

Commit

Permalink
Semantic: print notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 16, 2024
1 parent dbdcdf5 commit 0b058c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export const colorWarning = (message: string): string => {
return yellow(message)
}

export const prettySourceMessage = (message: string, span: Span, source: Source): string => {
export const prettySourceMessage = (message: string, span: Span, source: Source, notes: string[] = []): string => {
const start = indexToLocation(span.start, source)!
const locationStr = `${source.filepath}:${locationToString(start)}`
const locationMsg = ` at ${locationStr}`
return [message, locationMsg, prettyLineAt(span, source)].join('\n')
const notesStr = notes.length > 0 ? notes.map(n => ` note: ${n}`).join('\n') : ''
return [message, locationMsg, prettyLineAt(span, source), notesStr].filter(s => s.length > 0).join('\n')
}
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,25 @@ assert(ctx.moduleStack.length === 0, ctx.moduleStack.length.toString())
if (ctx.errors.length > 0) {
for (const error of ctx.errors) {
console.error(
prettySourceMessage(colorError(error.message), getSpan(error.node.parseNode), error.module.source)
prettySourceMessage(
colorError(error.message),
getSpan(error.node.parseNode),
error.module.source,
error.notes
)
)
}
process.exit(1)
}

for (const warning of ctx.warnings) {
console.error(
prettySourceMessage(colorWarning(warning.message), getSpan(warning.node.parseNode), warning.module.source)
prettySourceMessage(
colorWarning(warning.message),
getSpan(warning.node.parseNode),
warning.module.source,
warning.notes
)
)
}

Expand Down
9 changes: 7 additions & 2 deletions src/semantic/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export const semanticError = (
notes: string[] = []
): SemanticError => ({ code, module: ctx.moduleStack.at(-1)!, node, message, notes })

export const notFoundError = (ctx: Context, node: AstNode<any>, id: string, kind: string = node.kind): SemanticError =>
semanticError(1, ctx, node, `${kind} \`${id}\` not found`)
export const notFoundError = (
ctx: Context,
node: AstNode<any>,
id: string,
kind: string = 'identifier',
notes?: string[]
): SemanticError => semanticError(1, ctx, node, `${kind} \`${id}\` not found`, notes)

export const notImplementedError = (ctx: Context, node: AstNode<any>, message?: string): SemanticError =>
semanticError(2, ctx, node, `not implemented:${message ? ` ${message}` : ''}`)
Expand Down
4 changes: 2 additions & 2 deletions src/semantic/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ const checkMethodCallExpr = (
const fieldType = checkFieldAccessExpr(lOperand, identifier, ctx)
ctx.silent = false
if (fieldType) {
// TODO: add note
addError(ctx, notFoundError(ctx, identifier, vidToString(methodVid), 'method'))
const note = `to access field \`${methodName}\`, surround operand in parentheses`
addError(ctx, notFoundError(ctx, identifier, vidToString(methodVid), 'method', [note]))
} else {
addError(ctx, notFoundError(ctx, identifier, vidToString(methodVid), 'method'))
}
Expand Down

0 comments on commit 0b058c9

Please sign in to comment.