Skip to content

Commit bc6a7fb

Browse files
WangYuyang1013WangdahaiWangdahai
authored
constants and functions in Python stdlib (#55)
* constants and functions in Python stdlib * removed unintended imports * update error handling logic on stdlib * feat: add @Validate decorator, sublanguage violation error, and extract constants to py_s1_constants.json --------- Co-authored-by: Wangdahai <wangdahai@192.168.1.24> Co-authored-by: Wangdahai <wangdahai@WangdahaideMacBook-Air.local>
1 parent 79103ac commit bc6a7fb

File tree

4 files changed

+1661
-7
lines changed

4 files changed

+1661
-7
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)