Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/src/output/comments.py
1398 views
1
# vim:fileencoding=utf-8
2
# License: BSD Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
3
from __python__ import hash_literals
4
5
from ast_types import AST_Exit, is_node_type
6
7
8
def output_comments(comments, output, nlb):
9
for comm in comments:
10
if comm.type is "comment1":
11
output.print("//" + comm.value + "\n")
12
output.indent()
13
elif comm.type is "comment2":
14
output.print("/*" + comm.value + "*/")
15
if nlb:
16
output.print("\n")
17
output.indent()
18
else:
19
output.space()
20
21
22
def print_comments(self, output):
23
c = output.options.comments
24
if c:
25
start = self.start
26
if start and not start._comments_dumped:
27
start._comments_dumped = True
28
comments = start.comments_before
29
# XXX: ugly fix for https://github.com/mishoo/RapydScript2/issues/112
30
# if this node is `return` or `throw`, we cannot allow comments before
31
# the returned or thrown value.
32
if is_node_type(
33
self, AST_Exit
34
) and self.value and self.value.start.comments_before and self.value.start.comments_before.length > 0:
35
comments = (comments
36
or []).concat(self.value.start.comments_before)
37
self.value.start.comments_before = []
38
39
if c.test:
40
comments = comments.filter(
41
lambda comment: c.test(comment.value))
42
elif jstype(c) is "function":
43
comments = comments.filter(lambda comment: c(self, comment))
44
45
output_comments(comments, output, start.nlb)
46
47