12
12
13
13
from _devbuild .gen import arg_types
14
14
from _devbuild .gen .syntax_asdl import (command , command_t , parse_result ,
15
- parse_result_e , loc , source )
15
+ parse_result_e , source )
16
16
from core import alloc
17
17
from core import error
18
18
from core import process
19
- from core import pyutil
20
19
from core import state
21
20
from core import util
22
21
from display import ui
28
27
import fanos
29
28
import posix_ as posix
30
29
31
- from typing import cast , Any , List , TYPE_CHECKING
30
+ from typing import cast , Any , List , Tuple , TYPE_CHECKING
32
31
if TYPE_CHECKING :
33
32
from core .comp_ui import _IDisplay
34
33
from core import process
@@ -324,8 +323,29 @@ def Interactive(
324
323
return status
325
324
326
325
327
- def Batch (cmd_ev , c_parser , errfmt , cmd_flags = 0 ):
328
- # type: (cmd_eval.CommandEvaluator, cmd_parse.CommandParser, ui.ErrorFormatter, int) -> int
326
+ def Batch (
327
+ cmd_ev , # type: cmd_eval.CommandEvaluator
328
+ c_parser , # type: cmd_parse.CommandParser
329
+ errfmt , # type: ui.ErrorFormatter
330
+ cmd_flags = 0 , # type: int
331
+ ):
332
+ # type: (...) -> int
333
+ """
334
+ source, eval, etc. treat parse errors as error code 2. But the --eval flag does not.
335
+ """
336
+ was_parsed , status = Batch2 (cmd_ev , c_parser , errfmt , cmd_flags = cmd_flags )
337
+ if not was_parsed :
338
+ return 2
339
+ return status
340
+
341
+
342
+ def Batch2 (
343
+ cmd_ev , # type: cmd_eval.CommandEvaluator
344
+ c_parser , # type: cmd_parse.CommandParser
345
+ errfmt , # type: ui.ErrorFormatter
346
+ cmd_flags = 0 , # type: int
347
+ ):
348
+ # type: (...) -> Tuple[bool, int]
329
349
"""Loop for batch execution.
330
350
331
351
Returns:
@@ -347,6 +367,7 @@ def Batch(cmd_ev, c_parser, errfmt, cmd_flags=0):
347
367
- In contrast, 'trap' should parse up front?
348
368
- What about $() ?
349
369
"""
370
+ was_parsed = True
350
371
status = 0
351
372
while True :
352
373
probe ('main_loop' , 'Batch_parse_enter' )
@@ -357,7 +378,8 @@ def Batch(cmd_ev, c_parser, errfmt, cmd_flags=0):
357
378
break
358
379
except error .Parse as e :
359
380
errfmt .PrettyPrintError (e )
360
- status = 2
381
+ was_parsed = False
382
+ status = - 1 # invalid value
361
383
break
362
384
363
385
# After every "logical line", no lines will be referenced by the Arena.
@@ -387,7 +409,7 @@ def Batch(cmd_ev, c_parser, errfmt, cmd_flags=0):
387
409
mylib .MaybeCollect () # manual GC point
388
410
probe ('main_loop' , 'Batch_collect_exit' )
389
411
390
- return status
412
+ return was_parsed , status
391
413
392
414
393
415
def ParseWholeFile (c_parser ):
@@ -416,8 +438,15 @@ def ParseWholeFile(c_parser):
416
438
else :
417
439
return command .CommandList (children )
418
440
419
- def EvalFile (fs_path , fd_state , parse_ctx , cmd_ev ):
420
- # type: (str, process.FdState, parse_lib.ParseContext, cmd_eval.CommandEvaluator) -> bool
441
+
442
+ def EvalFile (
443
+ fs_path , # type: str
444
+ fd_state , # type: process.FdState
445
+ parse_ctx , # type: parse_lib.ParseContext
446
+ cmd_ev , # type: cmd_eval.CommandEvaluator
447
+ lang , # type: str
448
+ ):
449
+ # type: (...) -> bool
421
450
"""Evaluate a disk file, for --eval --eval-pure
422
451
423
452
Copied and adapted from the 'source' builtin in builtin/meta_oils.py.
@@ -429,32 +458,27 @@ def EvalFile(fs_path, fd_state, parse_ctx, cmd_ev):
429
458
Returns:
430
459
ok: whether processing should continue
431
460
"""
432
- mem = cmd_ev .mem
433
- arena = cmd_ev .arena
434
- errfmt = cmd_ev .errfmt
435
-
436
- # _LoadDiskFile
437
- blame_loc = loc .Missing
438
461
try :
439
462
f = fd_state .Open (fs_path )
440
463
except (IOError , OSError ) as e :
441
- errfmt .Print_ (
442
- 'Error reading %r: %s' % (fs_path , pyutil .strerror (e )),
443
- blame_loc = blame_loc )
464
+ print_stderr ("%s: Couldn't open %r for --eval: %s" %
465
+ (lang , fs_path , posix .strerror (e .errno )))
444
466
return False
445
467
446
- line_reader = reader .FileLineReader (f , arena )
468
+ line_reader = reader .FileLineReader (f , cmd_ev . arena )
447
469
c_parser = parse_ctx .MakeOshParser (line_reader )
448
470
449
471
# TODO:
450
472
# - Improve error locations
451
473
# - parse error should be fatal
452
474
453
475
with process .ctx_FileCloser (f ):
454
- with state .ctx_ThisDir (mem , fs_path ):
476
+ with state .ctx_ThisDir (cmd_ev . mem , fs_path ):
455
477
src = source .MainFile (fs_path )
456
- with alloc .ctx_SourceCode (arena , src ):
478
+ with alloc .ctx_SourceCode (cmd_ev . arena , src ):
457
479
# May raise util.UserExit
458
- unused = Batch (cmd_ev , c_parser , errfmt )
480
+ was_parsed , unused = Batch2 (cmd_ev , c_parser , cmd_ev .errfmt )
481
+ if not was_parsed :
482
+ return False
459
483
460
484
return True
0 commit comments