Skip to content

constants and functions in Python stdlib #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,34 @@ export class TypeError extends RuntimeSourceError {
this.message = msg;
}
}

export class SublanguageError extends RuntimeSourceError {
constructor (
source: string,
node: es.Node,
context: Context,
functionName: string,
chapter: string,
details?: string
) {
super(node)

this.type = ErrorType.TYPE

const index = (node as any).loc?.start?.index
?? (node as any).srcNode?.loc?.start?.index
?? 0
const { line, fullLine } = getFullLine(source, index)
const snippet = (node as any).loc?.source
?? (node as any).srcNode?.loc?.source
?? '<unknown source>'
const offset = fullLine.indexOf(snippet)
const indicator = createErrorIndicator(snippet, '@')

const name = 'SublanguageError'
const hint = 'Feature not supported in Python §' + chapter + '. '
const suggestion = `The call to '${functionName}()' relies on behaviour that is valid in full Python but outside the Python §1 sublanguage${details ? ': ' + details : ''}.`

this.message = `${name} at line ${line}\n\n ${fullLine}\n ${' '.repeat(offset)}${indicator}\n${hint}${suggestion}`
}
}
Loading