1 | # Copyright 2016 Andy Chu. All rights reserved.
|
2 | # Licensed under the Apache License, Version 2.0 (the "License");
|
3 | # you may not use this file except in compliance with the License.
|
4 | # You may obtain a copy of the License at
|
5 | #
|
6 | # http://www.apache.org/licenses/LICENSE-2.0
|
7 | """
|
8 | ui.py - User interface constructs.
|
9 | """
|
10 | from __future__ import print_function
|
11 |
|
12 | from _devbuild.gen.id_kind_asdl import Id, Id_t, Id_str
|
13 | from _devbuild.gen.syntax_asdl import (
|
14 | Token,
|
15 | SourceLine,
|
16 | loc,
|
17 | loc_e,
|
18 | loc_t,
|
19 | command_t,
|
20 | command_str,
|
21 | source,
|
22 | source_e,
|
23 | )
|
24 | from _devbuild.gen.value_asdl import value_t
|
25 | from asdl import format as fmt
|
26 | from data_lang import pretty
|
27 | from frontend import lexer
|
28 | from frontend import location
|
29 | from mycpp import mylib
|
30 | from mycpp.mylib import print_stderr, tagswitch, log
|
31 | from data_lang import j8_lite
|
32 | import libc
|
33 |
|
34 | from typing import List, Optional, Any, cast, TYPE_CHECKING
|
35 | if TYPE_CHECKING:
|
36 | from _devbuild.gen import arg_types
|
37 | from core import error
|
38 | from core.error import _ErrorWithLocation
|
39 |
|
40 | _ = log
|
41 |
|
42 |
|
43 | def ValType(val):
|
44 | # type: (value_t) -> str
|
45 | """For displaying type errors in the UI."""
|
46 |
|
47 | # TODO: consolidate these functions
|
48 | return pretty.ValType(val)
|
49 |
|
50 |
|
51 | def CommandType(cmd):
|
52 | # type: (command_t) -> str
|
53 | """For displaying commands in the UI."""
|
54 |
|
55 | # Displays 'command.Simple' for now, maybe change it.
|
56 | return command_str(cmd.tag())
|
57 |
|
58 |
|
59 | def PrettyId(id_):
|
60 | # type: (Id_t) -> str
|
61 | """For displaying type errors in the UI."""
|
62 |
|
63 | # Displays 'Id.BoolUnary_v' for now
|
64 | return Id_str(id_)
|
65 |
|
66 |
|
67 | def PrettyToken(tok):
|
68 | # type: (Token) -> str
|
69 | """Returns a readable token value for the user.
|
70 |
|
71 | For syntax errors.
|
72 | """
|
73 | if tok.id == Id.Eof_Real:
|
74 | return 'EOF'
|
75 |
|
76 | val = tok.line.content[tok.col:tok.col + tok.length]
|
77 | # TODO: Print length 0 as 'EOF'?
|
78 | return repr(val)
|
79 |
|
80 |
|
81 | def PrettyDir(dir_name, home_dir):
|
82 | # type: (str, Optional[str]) -> str
|
83 | """Maybe replace the home dir with ~.
|
84 |
|
85 | Used by the 'dirs' builtin and the prompt evaluator.
|
86 | """
|
87 | if home_dir is not None:
|
88 | if dir_name == home_dir or dir_name.startswith(home_dir + '/'):
|
89 | return '~' + dir_name[len(home_dir):]
|
90 |
|
91 | return dir_name
|
92 |
|
93 |
|
94 | def _PrintCodeExcerpt(line, col, length, f):
|
95 | # type: (str, int, int, mylib.Writer) -> None
|
96 |
|
97 | buf = mylib.BufWriter()
|
98 |
|
99 | buf.write(' ')
|
100 |
|
101 | # TODO: Be smart about horizontal space when printing code snippet
|
102 | # - Accept max_width param, which is terminal width or perhaps 100
|
103 | # when there's no terminal
|
104 | # - If 'length' of token is greater than max_width, then perhaps print 10
|
105 | # chars on each side
|
106 | # - If len(line) is less than max_width, then print everything normally
|
107 | # - If len(line) is greater than max_width, then print up to max_width
|
108 | # but make sure to include the entire token, with some context
|
109 | # Print > < or ... to show truncation
|
110 | #
|
111 | # ^col 80 ^~~~~ error
|
112 |
|
113 | buf.write(line.rstrip())
|
114 |
|
115 | buf.write('\n ')
|
116 | # preserve tabs
|
117 | for c in line[:col]:
|
118 | buf.write('\t' if c == '\t' else ' ')
|
119 | buf.write('^')
|
120 | buf.write('~' * (length - 1))
|
121 | buf.write('\n')
|
122 |
|
123 | # Do this all in a single write() call so it's less likely to be
|
124 | # interleaved. See test/runtime-errors.sh errexit_multiple_processes
|
125 | f.write(buf.getvalue())
|
126 |
|
127 |
|
128 | def _PrintTokenTooLong(loc_tok, f):
|
129 | # type: (loc.TokenTooLong, mylib.Writer) -> None
|
130 | line = loc_tok.line
|
131 | col = loc_tok.col
|
132 |
|
133 | buf = mylib.BufWriter()
|
134 |
|
135 | buf.write(' ')
|
136 | # Only print 10 characters, since it's probably very long
|
137 | buf.write(line.content[:col + 10].rstrip())
|
138 | buf.write('\n ')
|
139 |
|
140 | # preserve tabs, like _PrintCodeExcerpt
|
141 | for c in line.content[:col]:
|
142 | buf.write('\t' if c == '\t' else ' ')
|
143 |
|
144 | buf.write('^\n')
|
145 |
|
146 | source_str = GetLineSourceString(loc_tok.line, quote_filename=True)
|
147 | buf.write(
|
148 | '%s:%d: Token starting at column %d is too long: %d bytes (%s)\n' %
|
149 | (source_str, line.line_num, loc_tok.col, loc_tok.length,
|
150 | Id_str(loc_tok.id)))
|
151 |
|
152 | # single write() call
|
153 | f.write(buf.getvalue())
|
154 |
|
155 |
|
156 | def GetLineSourceString(line, quote_filename=False):
|
157 | # type: (SourceLine, bool) -> str
|
158 | """Returns a human-readable string for dev tools.
|
159 |
|
160 | This function is RECURSIVE because there may be dynamic parsing.
|
161 | """
|
162 | src = line.src
|
163 | UP_src = src
|
164 |
|
165 | with tagswitch(src) as case:
|
166 | if case(source_e.Interactive):
|
167 | s = '[ interactive ]' # This might need some changes
|
168 | elif case(source_e.Headless):
|
169 | s = '[ headless ]'
|
170 | elif case(source_e.CFlag):
|
171 | s = '[ -c flag ]'
|
172 | elif case(source_e.Stdin):
|
173 | src = cast(source.Stdin, UP_src)
|
174 | s = '[ stdin%s ]' % src.comment
|
175 |
|
176 | elif case(source_e.MainFile):
|
177 | src = cast(source.MainFile, UP_src)
|
178 | # This will quote a file called '[ -c flag ]' to disambiguate it!
|
179 | # also handles characters that are unprintable in a terminal.
|
180 | s = src.path
|
181 | if quote_filename:
|
182 | s = j8_lite.EncodeString(s, unquoted_ok=True)
|
183 | elif case(source_e.SourcedFile):
|
184 | src = cast(source.SourcedFile, UP_src)
|
185 | # ditto
|
186 | s = src.path
|
187 | if quote_filename:
|
188 | s = j8_lite.EncodeString(s, unquoted_ok=True)
|
189 |
|
190 | elif case(source_e.ArgvWord):
|
191 | src = cast(source.ArgvWord, UP_src)
|
192 |
|
193 | # Note: _PrintWithLocation() uses this more specifically
|
194 |
|
195 | # TODO: check loc.Missing; otherwise get Token from loc_t, then line
|
196 | blame_tok = location.TokenFor(src.location)
|
197 | if blame_tok is None:
|
198 | s = '[ %s word at ? ]' % src.what
|
199 | else:
|
200 | line = blame_tok.line
|
201 | line_num = line.line_num
|
202 | outer_source = GetLineSourceString(
|
203 | line, quote_filename=quote_filename)
|
204 | s = '[ %s word at line %d of %s ]' % (src.what, line_num,
|
205 | outer_source)
|
206 |
|
207 | elif case(source_e.Variable):
|
208 | src = cast(source.Variable, UP_src)
|
209 |
|
210 | if src.var_name is None:
|
211 | var_name = '?'
|
212 | else:
|
213 | var_name = repr(src.var_name)
|
214 |
|
215 | if src.location.tag() == loc_e.Missing:
|
216 | where = '?'
|
217 | else:
|
218 | blame_tok = location.TokenFor(src.location)
|
219 | assert blame_tok is not None
|
220 | line_num = blame_tok.line.line_num
|
221 | outer_source = GetLineSourceString(
|
222 | blame_tok.line, quote_filename=quote_filename)
|
223 | where = 'line %d of %s' % (line_num, outer_source)
|
224 |
|
225 | s = '[ var %s at %s ]' % (var_name, where)
|
226 |
|
227 | elif case(source_e.VarRef):
|
228 | src = cast(source.VarRef, UP_src)
|
229 |
|
230 | orig_tok = src.orig_tok
|
231 | line_num = orig_tok.line.line_num
|
232 | outer_source = GetLineSourceString(orig_tok.line,
|
233 | quote_filename=quote_filename)
|
234 | where = 'line %d of %s' % (line_num, outer_source)
|
235 |
|
236 | var_name = lexer.TokenVal(orig_tok)
|
237 | s = '[ contents of var %r at %s ]' % (var_name, where)
|
238 |
|
239 | elif case(source_e.Alias):
|
240 | src = cast(source.Alias, UP_src)
|
241 | s = '[ expansion of alias %r ]' % src.argv0
|
242 |
|
243 | elif case(source_e.Reparsed):
|
244 | src = cast(source.Reparsed, UP_src)
|
245 | span2 = src.left_token
|
246 | outer_source = GetLineSourceString(span2.line,
|
247 | quote_filename=quote_filename)
|
248 | s = '[ %s in %s ]' % (src.what, outer_source)
|
249 |
|
250 | elif case(source_e.Synthetic):
|
251 | src = cast(source.Synthetic, UP_src)
|
252 | s = '-- %s' % src.s # use -- to say it came from a flag
|
253 |
|
254 | else:
|
255 | raise AssertionError(src)
|
256 |
|
257 | return s
|
258 |
|
259 |
|
260 | def _PrintWithLocation(prefix, msg, blame_loc, show_code):
|
261 | # type: (str, str, loc_t, bool) -> None
|
262 | """Should we have multiple error formats:
|
263 |
|
264 | - single line and verbose?
|
265 | - and turn on "stack" tracing? For 'source' and more?
|
266 | """
|
267 | f = mylib.Stderr()
|
268 | if blame_loc.tag() == loc_e.TokenTooLong:
|
269 | _PrintTokenTooLong(cast(loc.TokenTooLong, blame_loc), f)
|
270 | return
|
271 |
|
272 | blame_tok = location.TokenFor(blame_loc)
|
273 | if blame_tok is None: # When does this happen?
|
274 | f.write('[??? no location ???] %s%s\n' % (prefix, msg))
|
275 | return
|
276 |
|
277 | orig_col = blame_tok.col
|
278 | src = blame_tok.line.src
|
279 | line = blame_tok.line.content
|
280 | line_num = blame_tok.line.line_num # overwritten by source__LValue case
|
281 |
|
282 | if show_code:
|
283 | UP_src = src
|
284 |
|
285 | with tagswitch(src) as case:
|
286 | if case(source_e.Reparsed):
|
287 | # Special case for LValue/backticks
|
288 |
|
289 | # We want the excerpt to look like this:
|
290 | # a[x+]=1
|
291 | # ^
|
292 | # Rather than quoting the internal buffer:
|
293 | # x+
|
294 | # ^
|
295 |
|
296 | # Show errors:
|
297 | # test/parse-errors.sh text-arith-context
|
298 |
|
299 | src = cast(source.Reparsed, UP_src)
|
300 | tok2 = src.left_token
|
301 | line_num = tok2.line.line_num
|
302 |
|
303 | line2 = tok2.line.content
|
304 | lbracket_col = tok2.col + tok2.length
|
305 | # NOTE: The inner line number is always 1 because of reparsing.
|
306 | # We overwrite it with the original token.
|
307 | _PrintCodeExcerpt(line2, orig_col + lbracket_col, 1, f)
|
308 |
|
309 | elif case(source_e.ArgvWord):
|
310 | src = cast(source.ArgvWord, UP_src)
|
311 | # Special case for eval, unset, printf -v, etc.
|
312 |
|
313 | # Show errors:
|
314 | # test/runtime-errors.sh test-assoc-array
|
315 |
|
316 | #print('OUTER blame_loc', blame_loc)
|
317 | #print('OUTER tok', blame_tok)
|
318 | #print('INNER src.location', src.location)
|
319 |
|
320 | # Print code and location for MOST SPECIFIC location
|
321 | _PrintCodeExcerpt(line, blame_tok.col, blame_tok.length, f)
|
322 | source_str = GetLineSourceString(blame_tok.line,
|
323 | quote_filename=True)
|
324 | f.write('%s:%d\n' % (source_str, line_num))
|
325 | f.write('\n')
|
326 |
|
327 | # Now print OUTER location, with error message
|
328 | _PrintWithLocation(prefix, msg, src.location, show_code)
|
329 | return
|
330 |
|
331 | else:
|
332 | _PrintCodeExcerpt(line, blame_tok.col, blame_tok.length, f)
|
333 |
|
334 | source_str = GetLineSourceString(blame_tok.line, quote_filename=True)
|
335 |
|
336 | # TODO: If the line is blank, it would be nice to print the last non-blank
|
337 | # line too?
|
338 | f.write('%s:%d: %s%s\n' % (source_str, line_num, prefix, msg))
|
339 |
|
340 |
|
341 | class ctx_Location(object):
|
342 |
|
343 | def __init__(self, errfmt, location):
|
344 | # type: (ErrorFormatter, loc_t) -> None
|
345 | errfmt.loc_stack.append(location)
|
346 | self.errfmt = errfmt
|
347 |
|
348 | def __enter__(self):
|
349 | # type: () -> None
|
350 | pass
|
351 |
|
352 | def __exit__(self, type, value, traceback):
|
353 | # type: (Any, Any, Any) -> None
|
354 | self.errfmt.loc_stack.pop()
|
355 |
|
356 |
|
357 | # TODO:
|
358 | # - ColorErrorFormatter
|
359 | # - BareErrorFormatter? Could just display the foo.sh:37:8: and not quotation.
|
360 | #
|
361 | # Are these controlled by a flag? It's sort of like --comp-ui. Maybe
|
362 | # --error-ui.
|
363 |
|
364 |
|
365 | class ErrorFormatter(object):
|
366 | """Print errors with code excerpts.
|
367 |
|
368 | Philosophy:
|
369 | - There should be zero or one code quotation when a shell exits non-zero.
|
370 | Showing the same line twice is noisy.
|
371 | - When running parallel processes, avoid interleaving multi-line code
|
372 | quotations. (TODO: turn off in child processes?)
|
373 | """
|
374 |
|
375 | def __init__(self):
|
376 | # type: () -> None
|
377 | self.loc_stack = [] # type: List[loc_t]
|
378 | self.one_line_errexit = False # root process
|
379 |
|
380 | def OneLineErrExit(self):
|
381 | # type: () -> None
|
382 | """Unused now.
|
383 |
|
384 | For SubprogramThunk.
|
385 | """
|
386 | self.one_line_errexit = True
|
387 |
|
388 | # A stack used for the current builtin. A fallback for UsageError.
|
389 | # TODO: Should we have PushBuiltinName? Then we can have a consistent style
|
390 | # like foo.sh:1: (compopt) Not currently executing.
|
391 | def _FallbackLocation(self, blame_loc):
|
392 | # type: (Optional[loc_t]) -> loc_t
|
393 | if blame_loc is None or blame_loc.tag() == loc_e.Missing:
|
394 | if len(self.loc_stack):
|
395 | return self.loc_stack[-1]
|
396 | return loc.Missing
|
397 |
|
398 | return blame_loc
|
399 |
|
400 | def PrefixPrint(self, msg, prefix, blame_loc):
|
401 | # type: (str, str, loc_t) -> None
|
402 | """Print a hard-coded message with a prefix, and quote code."""
|
403 | _PrintWithLocation(prefix,
|
404 | msg,
|
405 | self._FallbackLocation(blame_loc),
|
406 | show_code=True)
|
407 |
|
408 | def Print_(self, msg, blame_loc=None):
|
409 | # type: (str, loc_t) -> None
|
410 | """Print message and quote code."""
|
411 | _PrintWithLocation('',
|
412 | msg,
|
413 | self._FallbackLocation(blame_loc),
|
414 | show_code=True)
|
415 |
|
416 | def PrintMessage(self, msg, blame_loc=None):
|
417 | # type: (str, loc_t) -> None
|
418 | """Print a message WITHOUT quoting code."""
|
419 | _PrintWithLocation('',
|
420 | msg,
|
421 | self._FallbackLocation(blame_loc),
|
422 | show_code=False)
|
423 |
|
424 | def StderrLine(self, msg):
|
425 | # type: (str) -> None
|
426 | """Just print to stderr."""
|
427 | print_stderr(msg)
|
428 |
|
429 | def PrettyPrintError(self, err, prefix=''):
|
430 | # type: (_ErrorWithLocation, str) -> None
|
431 | """Print an exception that was caught, with a code quotation.
|
432 |
|
433 | Unlike other methods, this doesn't use the GetLocationForLine()
|
434 | fallback. That only applies to builtins; instead we check
|
435 | e.HasLocation() at a higher level, in CommandEvaluator.
|
436 | """
|
437 | # TODO: Should there be a special span_id of 0 for EOF? runtime.NO_SPID
|
438 | # means there is no location info, but 0 could mean that the location is EOF.
|
439 | # So then you query the arena for the last line in that case?
|
440 | # Eof_Real is the ONLY token with 0 span, because it's invisible!
|
441 | # Well Eol_Tok is a sentinel with span_id == runtime.NO_SPID. I think that
|
442 | # is OK.
|
443 | # Problem: the column for Eof could be useful.
|
444 |
|
445 | _PrintWithLocation(prefix, err.UserErrorString(), err.location, True)
|
446 |
|
447 | def PrintErrExit(self, err, pid):
|
448 | # type: (error.ErrExit, int) -> None
|
449 |
|
450 | # TODO:
|
451 | # - Don't quote code if you already quoted something on the same line?
|
452 | # - _PrintWithLocation calculates the line_id. So you need to remember that?
|
453 | # - return it here?
|
454 | prefix = 'errexit PID %d: ' % pid
|
455 | _PrintWithLocation(prefix, err.UserErrorString(), err.location,
|
456 | err.show_code)
|
457 |
|
458 |
|
459 | def PrintAst(node, flag):
|
460 | # type: (command_t, arg_types.main) -> None
|
461 |
|
462 | if flag.ast_format == 'none':
|
463 | print_stderr('AST not printed.')
|
464 | if 0:
|
465 | from _devbuild.gen.id_kind_asdl import Id_str
|
466 | from frontend.lexer import ID_HIST, LAZY_ID_HIST
|
467 |
|
468 | print(LAZY_ID_HIST)
|
469 | print(len(LAZY_ID_HIST))
|
470 |
|
471 | for id_, count in ID_HIST.most_common(10):
|
472 | print('%8d %s' % (count, Id_str(id_)))
|
473 | print()
|
474 | total = sum(ID_HIST.values())
|
475 | uniq = len(ID_HIST)
|
476 | print('%8d total tokens' % total)
|
477 | print('%8d unique tokens IDs' % uniq)
|
478 | print()
|
479 |
|
480 | for id_, count in LAZY_ID_HIST.most_common(10):
|
481 | print('%8d %s' % (count, Id_str(id_)))
|
482 | print()
|
483 | total = sum(LAZY_ID_HIST.values())
|
484 | uniq = len(LAZY_ID_HIST)
|
485 | print('%8d total tokens' % total)
|
486 | print('%8d tokens with LazyVal()' % total)
|
487 | print('%8d unique tokens IDs' % uniq)
|
488 | print()
|
489 |
|
490 | if 0:
|
491 | from osh.word_parse import WORD_HIST
|
492 | #print(WORD_HIST)
|
493 | for desc, count in WORD_HIST.most_common(20):
|
494 | print('%8d %s' % (count, desc))
|
495 |
|
496 | else: # text output
|
497 | f = mylib.Stdout()
|
498 |
|
499 | afmt = flag.ast_format # note: mycpp rewrite to avoid 'in'
|
500 | if afmt in ('text', 'abbrev-text'):
|
501 | ast_f = fmt.DetectConsoleOutput(f)
|
502 | elif afmt in ('html', 'abbrev-html'):
|
503 | ast_f = fmt.HtmlOutput(f)
|
504 | else:
|
505 | raise AssertionError()
|
506 |
|
507 | if 'abbrev-' in afmt:
|
508 | # ASDL "abbreviations" are only supported by asdl/gen_python.py
|
509 | if mylib.PYTHON:
|
510 | tree = node.AbbreviatedTree()
|
511 | else:
|
512 | tree = node.PrettyTree()
|
513 | else:
|
514 | tree = node.PrettyTree()
|
515 |
|
516 | ast_f.FileHeader()
|
517 | fmt.PrintTree(tree, ast_f)
|
518 | ast_f.FileFooter()
|
519 | ast_f.write('\n')
|
520 |
|
521 |
|
522 | def PrettyPrintValue(val, f):
|
523 | # type: (value_t, mylib.Writer) -> None
|
524 | """For the = keyword"""
|
525 |
|
526 | encoder = pretty.ValueEncoder()
|
527 | encoder.SetUseStyles(f.isatty())
|
528 | encoder.SetYshStyle()
|
529 |
|
530 | doc = encoder.Value(val)
|
531 |
|
532 | printer = pretty.PrettyPrinter()
|
533 | try:
|
534 | width = libc.get_terminal_width()
|
535 | if width > 0:
|
536 | printer.SetMaxWidth(width)
|
537 | except (IOError, OSError):
|
538 | pass
|
539 |
|
540 | buf = mylib.BufWriter()
|
541 | printer.PrintDoc(doc, buf)
|
542 | f.write(buf.getvalue())
|
543 | f.write('\n')
|