Skip to content

Commit 7ddc955

Browse files
WangdahaiWangdahai
authored andcommitted
feat: add @Validate decorator, sublanguage violation error, and extract constants to py_s1_constants.json
1 parent c4f2fd0 commit 7ddc955

File tree

4 files changed

+1509
-1638
lines changed

4 files changed

+1509
-1638
lines changed

src/errors/errors.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,34 @@ export class TypeError extends RuntimeSourceError {
268268
this.message = msg;
269269
}
270270
}
271+
272+
export class SublanguageError extends RuntimeSourceError {
273+
constructor (
274+
source: string,
275+
node: es.Node,
276+
context: Context,
277+
functionName: string,
278+
chapter: string,
279+
details?: string
280+
) {
281+
super(node)
282+
283+
this.type = ErrorType.TYPE
284+
285+
const index = (node as any).loc?.start?.index
286+
?? (node as any).srcNode?.loc?.start?.index
287+
?? 0
288+
const { line, fullLine } = getFullLine(source, index)
289+
const snippet = (node as any).loc?.source
290+
?? (node as any).srcNode?.loc?.source
291+
?? '<unknown source>'
292+
const offset = fullLine.indexOf(snippet)
293+
const indicator = createErrorIndicator(snippet, '@')
294+
295+
const name = 'SublanguageError'
296+
const hint = 'Feature not supported in Python §' + chapter + '. '
297+
const suggestion = `The call to '${functionName}()' relies on behaviour that is valid in full Python but outside the Python §1 sublanguage${details ? ': ' + details : ''}.`
298+
299+
this.message = `${name} at line ${line}\n\n ${fullLine}\n ${' '.repeat(offset)}${indicator}\n${hint}${suggestion}`
300+
}
301+
}

0 commit comments

Comments
 (0)