Skip to content

Commit 117fb00

Browse files
committed
Add function token count comments (rough draft)
1 parent 6249e2e commit 117fb00

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pico_process.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,18 @@ def __str__(m):
133133
def print_token_count(num_tokens, **kwargs):
134134
print_size("tokens", num_tokens, 8192, **kwargs)
135135

136-
def process_code(ctxt, source, input_count=False, count=False, lint=False, minify=False, rename=False, fail=True, want_count=True):
136+
def process_code(ctxt, source, input_count=False, count=False, lint=False, minify=False, rename=False, fail=True, want_count=True, annotate=False):
137137
need_lint = lint not in (None, False)
138138
need_minify = minify not in (None, False)
139139
need_rename = rename not in (None, False)
140+
need_annotate = annotate not in (None, False)
140141

141-
if not need_lint and not need_minify and not (want_count and (count or input_count)):
142+
if not need_lint and not need_minify and not (want_count and (count or input_count)) and not need_annotate:
142143
return True, ()
143144

144145
ok = False
145146
tokens, errors = tokenize(source, ctxt)
146-
if not errors and (need_lint or need_minify):
147+
if not errors and (need_lint or need_minify or need_annotate):
147148
root, errors = parse(source, tokens)
148149

149150
if not errors:
@@ -164,10 +165,30 @@ def process_code(ctxt, source, input_count=False, count=False, lint=False, minif
164165
if count:
165166
print_token_count(count_tokens(tokens), handler=count)
166167

168+
if annotate:
169+
annotate_code(ctxt, source, root)
170+
167171
if fail and errors:
168172
raise Exception("\n".join(map(str, errors)))
169173
return ok, errors
170174

175+
# Calls function for the descendants of node, then for node
176+
def apply_node_tree(node, func):
177+
if hasattr(node, "extra_children"):
178+
for child in reversed(node.extra_children):
179+
apply_node_tree(child, func)
180+
for child in reversed(node.children):
181+
apply_node_tree(child, func)
182+
func(node)
183+
184+
def annotate_code(ctxt, source, root):
185+
def comment_before_function(node, source=source):
186+
if node.type==NodeType.function:
187+
tokens, errors = tokenize(PicoSource("temp", source.text[node.idx:node.endidx]), ctxt)
188+
count = count_tokens(tokens)
189+
source.text = f'{source.text[:node.idx]}-- token count: {count}\n{source.text[node.idx:]}'
190+
apply_node_tree(root, comment_before_function)
191+
171192
def echo_code(code, echo=True):
172193
code = from_pico_chars(code)
173194
if echo == True:
@@ -181,6 +202,7 @@ def echo_code(code, echo=True):
181202
from pico_lint import lint_code
182203
from pico_minify import minify_code
183204
from pico_rename import rename_tokens
205+
from pico_parse import NodeType
184206

185207
# re-export some things for examples/etc.
186208
from pico_tokenize import is_identifier, is_ident_char, CustomPreprocessor

shrinko8.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def ParsableCountHandler(prefix, name, size, limit):
4949
pgroup.add_argument("--no-count-compress", action="store_true", help="do not compress the cart just to print the compressed size")
5050
pgroup.add_argument("--no-count-tokenize", action="store_true", help="do not tokenize the cart just to print the token count")
5151

52+
pgroup = parser.add_argument_group("annotate options")
53+
pgroup.add_argument("-a", "--annotate", action="store_true", help="enable annotating source during output")
54+
5255
pgroup = parser.add_argument_group("script options")
5356
pgroup.add_argument("-s", "--script", help="manipulate the cart via a custom python script - see README for api details")
5457
pgroup.add_argument("--script-args", nargs=argparse.REMAINDER, help="send arguments directly to --script", default=())
@@ -178,7 +181,7 @@ def fail(msg):
178181

179182
ok, errors = process_code(ctxt, src, input_count=args.input_count, count=args.count,
180183
lint=args.lint, minify=args.minify, rename=args.rename,
181-
fail=False, want_count=not args.no_count_tokenize)
184+
fail=False, want_count=not args.no_count_tokenize, annotate=args.annotate)
182185
if errors:
183186
print("Lint errors:" if ok else "Compilation errors:")
184187
for error in errors:

0 commit comments

Comments
 (0)