OILS / core / ui.py View on Github | oilshell.org

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