1 | from __future__ import print_function
|
2 | """
|
3 | ysh_ify.py: Roughly translate OSH to YSH. Doesn't respect semantics.
|
4 |
|
5 | ESSENTIAL
|
6 |
|
7 | Command:
|
8 |
|
9 | then/fi, do/done -> { }
|
10 |
|
11 | new case statement
|
12 |
|
13 | f() { } -> proc f { } (changes scope)
|
14 |
|
15 | subshell -> forkwait, because () is taken
|
16 | { } to fopen { }?
|
17 |
|
18 | Approximate: var declaration:
|
19 | local a=b -> var a = 'b', I think
|
20 |
|
21 | <<EOF here docs to '''
|
22 |
|
23 | Word:
|
24 | "$@" -> @ARGV
|
25 |
|
26 | Not common: unquoted $x -> @[split(x)]
|
27 |
|
28 | LEGACY that I don't personally use
|
29 |
|
30 | Builtins:
|
31 | [ -> test
|
32 | . -> source
|
33 |
|
34 | Word:
|
35 | backticks -> $() (I don't use this)
|
36 | quote removal "$foo" -> $foo
|
37 | brace removal ${foo} and "${foo}" -> $foo
|
38 |
|
39 | --tool format
|
40 |
|
41 | fix indentation and spacing, like clang-format
|
42 | can "lower" the LST to a rough representation with keywords / "first words",
|
43 | { } ( ), and comments
|
44 | - the "atoms" should not have newlines
|
45 | """
|
46 |
|
47 | from _devbuild.gen.id_kind_asdl import Id, Id_str
|
48 | from _devbuild.gen.runtime_asdl import word_style_e, word_style_t
|
49 | from _devbuild.gen.syntax_asdl import (
|
50 | loc,
|
51 | CompoundWord,
|
52 | Token,
|
53 | NameTok,
|
54 | BracedVarSub,
|
55 | CommandSub,
|
56 | DoubleQuoted,
|
57 | SingleQuoted,
|
58 | word_e,
|
59 | word_t,
|
60 | word_part,
|
61 | word_part_e,
|
62 | word_part_t,
|
63 | rhs_word_e,
|
64 | rhs_word_t,
|
65 | sh_lhs,
|
66 | sh_lhs_e,
|
67 | command,
|
68 | command_e,
|
69 | BraceGroup,
|
70 | for_iter_e,
|
71 | case_arg_e,
|
72 | case_arg,
|
73 | condition,
|
74 | condition_e,
|
75 | redir_param,
|
76 | redir_param_e,
|
77 | Redir,
|
78 | )
|
79 | from asdl import runtime
|
80 | from core.error import p_die
|
81 | from frontend import lexer
|
82 | from frontend import location
|
83 | from osh import word_
|
84 | from mycpp import mylib
|
85 | from mycpp.mylib import log, print_stderr, tagswitch
|
86 |
|
87 | from typing import Dict, cast, TYPE_CHECKING
|
88 | if TYPE_CHECKING:
|
89 | from _devbuild.gen.syntax_asdl import command_t
|
90 | from core import alloc
|
91 |
|
92 |
|
93 | class Cursor(object):
|
94 | """
|
95 | API to print/transform a complete source file, stored in a single arena.
|
96 | """
|
97 |
|
98 | def __init__(self, arena, f):
|
99 | # type: (alloc.Arena, mylib.Writer) -> None
|
100 | self.arena = arena
|
101 | self.f = f
|
102 | self.next_span_id = 0
|
103 |
|
104 | def PrintUntilSpid(self, until_span_id):
|
105 | # type: (int) -> None
|
106 |
|
107 | # Sometimes we add +1
|
108 | if until_span_id == runtime.NO_SPID:
|
109 | assert 0, 'Missing span ID, got %d' % until_span_id
|
110 |
|
111 | for span_id in xrange(self.next_span_id, until_span_id):
|
112 | span = self.arena.GetToken(span_id)
|
113 |
|
114 | # A span for Eof may not have a line when the file is completely empty.
|
115 | if span.line is None:
|
116 | continue
|
117 |
|
118 | piece = span.line.content[span.col:span.col + span.length]
|
119 | self.f.write(piece)
|
120 |
|
121 | self.next_span_id = until_span_id
|
122 |
|
123 | def SkipUntilSpid(self, next_span_id):
|
124 | # type: (int) -> None
|
125 | """Skip everything before next_span_id.
|
126 |
|
127 | Printing will start at next_span_id
|
128 | """
|
129 | if (next_span_id == runtime.NO_SPID or
|
130 | next_span_id == runtime.NO_SPID + 1):
|
131 | assert 0, 'Missing span ID, got %d' % next_span_id
|
132 | self.next_span_id = next_span_id
|
133 |
|
134 | def SkipUntil(self, tok):
|
135 | # type: (Token) -> None
|
136 | self.SkipUntilSpid(tok.span_id)
|
137 |
|
138 | def SkipPast(self, tok):
|
139 | # type: (Token) -> None
|
140 | self.SkipUntilSpid(tok.span_id + 1)
|
141 |
|
142 | def PrintUntil(self, tok):
|
143 | # type: (Token) -> None
|
144 | self.PrintUntilSpid(tok.span_id)
|
145 |
|
146 | def PrintIncluding(self, tok):
|
147 | # type: (Token) -> None
|
148 | self.PrintUntilSpid(tok.span_id + 1)
|
149 |
|
150 |
|
151 | def LosslessCat(arena):
|
152 | # type: (alloc.Arena) -> None
|
153 | """
|
154 | For testing the lossless invariant: the tokens "add up" to the original
|
155 | doc.
|
156 | """
|
157 | cursor = Cursor(arena, mylib.Stdout())
|
158 | cursor.PrintUntilSpid(arena.LastSpanId())
|
159 |
|
160 |
|
161 | def PrintTokens(arena):
|
162 | # type: (alloc.Arena) -> None
|
163 | """Debugging tool to see tokens."""
|
164 |
|
165 | if len(arena.tokens) == 1: # Special case for line_id == -1
|
166 | print('Empty file with EOF token on invalid line:')
|
167 | print('%s' % arena.tokens[0])
|
168 | return
|
169 |
|
170 | for i, tok in enumerate(arena.tokens):
|
171 | piece = tok.line.content[tok.col:tok.col + tok.length]
|
172 | print('%5d %-20s %r' % (i, Id_str(tok.id), piece))
|
173 | print_stderr('(%d tokens)' % len(arena.tokens))
|
174 |
|
175 |
|
176 | def Ysh_ify(arena, node):
|
177 | # type: (alloc.Arena, command_t) -> None
|
178 | cursor = Cursor(arena, mylib.Stdout())
|
179 | fixer = YshPrinter(cursor, arena, mylib.Stdout())
|
180 | fixer.DoCommand(node, None, at_top_level=True) # no local symbols yet
|
181 | fixer.End()
|
182 |
|
183 |
|
184 | # PROBLEM: ~ substitution. That is disabled by "".
|
185 | # You can turn it into $HOME I guess
|
186 | # const foo = "$HOME/src"
|
187 | # const foo = %( ~/src )[0] # does this make sense?
|
188 |
|
189 |
|
190 | def _GetRhsStyle(w):
|
191 | # type: (rhs_word_t) -> word_style_t
|
192 | """Determine what style an assignment should use. '' or "", or an
|
193 | expression.
|
194 |
|
195 | SQ foo= setglobal foo = ''
|
196 | SQ foo='' setglobal foo = ''
|
197 | DQ foo="" setglobal foo = "" # Or we could normalize it if no subs?
|
198 | DQ foo="" setglobal foo = "" # Or we could normalize it if no subs?
|
199 |
|
200 | # Need these too.
|
201 | # Or honestly should C strings be the default? And then raw strings are
|
202 | # optional? Because most usages of \n and \0 can turn into Oil?
|
203 | # Yeah I want the default to be statically parseable, so we subvert the \t
|
204 | # and \n of command line tools?
|
205 | # As long as we are fully analyzing the strings, we might as well go all the
|
206 | # way!
|
207 | # I think I need a PartialStaticEval() to paper over this.
|
208 | #
|
209 | # The main issue is regex and globs, because they use escape for a different
|
210 | # purpose. I think just do
|
211 | # grep r'foo\tbar' or something.
|
212 |
|
213 | C_SQ foo=$'\n' setglobal foo = C'\n'
|
214 | C_DQ foo=$'\n'"$bar" setglobal foo = C"\n$(bar)"
|
215 |
|
216 | Expr path=${1:-} setglobal path = $1 or ''
|
217 | Expr host=${2:-$(hostname)} setglobal host = $2 or $[hostname]
|
218 |
|
219 | What's the difference between Expr and Unquoted? I think they're the same/
|
220 | """
|
221 | # Actually splitting NEVER HAPPENS ON ASSIGNMENT. LEAVE IT OFF.
|
222 |
|
223 | UP_w = w
|
224 | with tagswitch(w) as case:
|
225 | if case(rhs_word_e.Empty):
|
226 | return word_style_e.SQ
|
227 |
|
228 | elif case(rhs_word_e.Compound):
|
229 | w = cast(CompoundWord, UP_w)
|
230 | if len(w.parts) == 0:
|
231 | raise AssertionError(w)
|
232 |
|
233 | elif len(w.parts) == 1:
|
234 | part0 = w.parts[0]
|
235 | UP_part0 = part0
|
236 | with tagswitch(part0) as case:
|
237 | # VAR_SUBS
|
238 | if case(word_part_e.TildeSub):
|
239 | # x=~andy/src
|
240 | # -> setvar x = homedir('andy') + '/src'
|
241 | return word_style_e.Expr
|
242 |
|
243 | elif case(word_part_e.Literal):
|
244 | # local x=y
|
245 | # -> var x = 'y'
|
246 | return word_style_e.SQ
|
247 |
|
248 | elif case(word_part_e.SimpleVarSub):
|
249 | # local x=$myvar
|
250 | # -> var x = "$myvar"
|
251 | # or var x = ${myvar}
|
252 | # or var x = myvar
|
253 | return word_style_e.DQ
|
254 |
|
255 | elif case(word_part_e.BracedVarSub, word_part_e.CommandSub,
|
256 | word_part_e.ArithSub):
|
257 | # x=$(hostname)
|
258 | # -> setvar x = $(hostname)
|
259 | return word_style_e.Unquoted
|
260 |
|
261 | elif case(word_part_e.DoubleQuoted):
|
262 | part0 = cast(DoubleQuoted, UP_part0)
|
263 |
|
264 | # TODO: remove quotes in single part like "$(hostname)" -> $(hostname)
|
265 | return word_style_e.DQ
|
266 |
|
267 | else:
|
268 | # multiple parts use YSTR in general?
|
269 | # Depends if there are subs
|
270 | return word_style_e.DQ
|
271 |
|
272 | # Default
|
273 | return word_style_e.SQ
|
274 |
|
275 |
|
276 | class YshPrinter(object):
|
277 | """Prettify OSH to YSH."""
|
278 |
|
279 | def __init__(self, cursor, arena, f):
|
280 | # type: (Cursor, alloc.Arena, mylib.Writer) -> None
|
281 | self.cursor = cursor
|
282 | self.arena = arena
|
283 | self.f = f
|
284 |
|
285 | def _DebugSpid(self, spid):
|
286 | # type: (int) -> None
|
287 | span = self.arena.GetToken(spid)
|
288 | s = span.line.content[span.col:span.col + span.length]
|
289 | print_stderr('SPID %d = %r' % (spid, s))
|
290 |
|
291 | def End(self):
|
292 | # type: () -> None
|
293 | """Make sure we print until the end of the file."""
|
294 | self.cursor.PrintUntilSpid(self.arena.LastSpanId())
|
295 |
|
296 | def DoRedirect(self, node, local_symbols):
|
297 | # type: (Redir, Dict[str, bool]) -> None
|
298 | """
|
299 | Currently Unused
|
300 | TODO: It would be nice to change here docs to <<< '''
|
301 | """
|
302 | #print(node, file=sys.stderr)
|
303 | op_id = node.op.id
|
304 | self.cursor.PrintUntil(node.op)
|
305 |
|
306 | if node.arg.tag() == redir_param_e.HereDoc:
|
307 | here_doc = cast(redir_param.HereDoc, node.arg)
|
308 |
|
309 | here_begin = here_doc.here_begin
|
310 | ok, delimiter, delim_quoted = word_.StaticEval(here_begin)
|
311 | if not ok:
|
312 | p_die('Invalid here doc delimiter', loc.Word(here_begin))
|
313 |
|
314 | # Turn everything into <<<. We just change the quotes
|
315 | self.f.write('<<<')
|
316 |
|
317 | if delim_quoted:
|
318 | self.f.write(" '''")
|
319 | else:
|
320 | self.f.write(' """')
|
321 |
|
322 | delim_end_tok = location.RightTokenForWord(here_begin)
|
323 | self.cursor.SkipPast(delim_end_tok)
|
324 |
|
325 | # Now print the lines. TODO: Have a flag to indent these to the level of
|
326 | # the owning command, e.g.
|
327 | # cat <<EOF
|
328 | # EOF
|
329 | # Or since most here docs are the top level, you could just have a hack
|
330 | # for a fixed indent? TODO: Look at real use cases.
|
331 | for part in here_doc.stdin_parts:
|
332 | self.DoWordPart(part, local_symbols)
|
333 |
|
334 | self.cursor.SkipPast(here_doc.here_end_tok)
|
335 | if delim_quoted:
|
336 | self.f.write("'''\n")
|
337 | else:
|
338 | self.f.write('"""\n')
|
339 |
|
340 | else:
|
341 | pass
|
342 |
|
343 | # cat << EOF
|
344 | # hello $name
|
345 | # EOF
|
346 | # cat <<< """
|
347 | # hello $name
|
348 | # """
|
349 |
|
350 | # cat << 'EOF'
|
351 | # no expansion
|
352 | # EOF
|
353 |
|
354 | # cat <<< '''
|
355 | # no expansion
|
356 | # '''
|
357 |
|
358 | def DoShAssignment(self, node, at_top_level, local_symbols):
|
359 | # type: (command.ShAssignment, bool, Dict[str, bool]) -> None
|
360 | """
|
361 | local_symbols:
|
362 | - Add every 'local' declaration to it
|
363 | - problem: what if you have local in an "if" ?
|
364 | - we could treat it like nested scope and see what happens? Do any
|
365 | programs have a problem with it?
|
366 | case/if/for/while/BraceGroup all define scopes or what?
|
367 | You don't want inconsistency of variables that could be defined at
|
368 | any point.
|
369 | - or maybe you only need it within "if / case" ? Well I guess
|
370 | for/while can break out of the loop and cause problems. A break is
|
371 | an "if".
|
372 |
|
373 | - for subsequent
|
374 | """
|
375 | # Change RHS to expression language. Bare words not allowed. foo -> 'foo'
|
376 |
|
377 | has_rhs = False # TODO: Should be on a per-variable basis.
|
378 | # local a=b c=d, or just punt on those
|
379 | defined_locally = False # is it a local variable in this function?
|
380 | # can't tell if global
|
381 |
|
382 | if True:
|
383 | self.cursor.PrintUntil(node.pairs[0].left)
|
384 |
|
385 | # For now, just detect whether the FIRST assignment on the line has been
|
386 | # declared locally. We might want to split every line into separate
|
387 | # statements.
|
388 | if local_symbols is not None:
|
389 | lhs0 = node.pairs[0].lhs
|
390 | #if lhs0.tag() == sh_lhs_e.Name and lhs0.name in local_symbols:
|
391 | # defined_locally = True
|
392 |
|
393 | #print("CHECKING NAME", lhs0.name, defined_locally, local_symbols)
|
394 |
|
395 | # TODO: Avoid translating these
|
396 | has_array_index = [
|
397 | pair.lhs.tag() == sh_lhs_e.UnparsedIndex for pair in node.pairs
|
398 | ]
|
399 |
|
400 | # need semantic analysis.
|
401 | # Would be nice to assume that it's a local though.
|
402 | if at_top_level:
|
403 | self.f.write('setvar ')
|
404 | elif defined_locally:
|
405 | self.f.write('set ')
|
406 | #self.f.write('[local mutated]')
|
407 | else:
|
408 | # We're in a function, but it's not defined locally, so we must be
|
409 | # mutating a global.
|
410 | self.f.write('setvar ')
|
411 |
|
412 | # foo=bar spam=eggs -> foo = 'bar', spam = 'eggs'
|
413 | n = len(node.pairs)
|
414 | for i, pair in enumerate(node.pairs):
|
415 | lhs = pair.lhs
|
416 | UP_lhs = lhs
|
417 | with tagswitch(lhs) as case:
|
418 | if case(sh_lhs_e.Name):
|
419 | lhs = cast(sh_lhs.Name, UP_lhs)
|
420 |
|
421 | self.cursor.PrintUntil(pair.left)
|
422 | # Assume skipping over one Lit_VarLike token
|
423 | self.cursor.SkipPast(pair.left)
|
424 |
|
425 | # Replace name. I guess it's Lit_Chars.
|
426 | self.f.write(lhs.name)
|
427 | self.f.write(' = ')
|
428 |
|
429 | # TODO: This should be translated from Empty.
|
430 | if pair.rhs.tag() == rhs_word_e.Empty:
|
431 | self.f.write("''") # local i -> var i = ''
|
432 | else:
|
433 | self.DoRhsWord(pair.rhs, local_symbols)
|
434 |
|
435 | elif case(sh_lhs_e.UnparsedIndex):
|
436 | # --one-pass-parse gives us this node, instead of IndexedName
|
437 | pass
|
438 |
|
439 | else:
|
440 | raise AssertionError(pair.lhs.__class__.__name__)
|
441 |
|
442 | if i != n - 1:
|
443 | self.f.write(',')
|
444 |
|
445 | def DoCommand(self, node, local_symbols, at_top_level=False):
|
446 | # type: (command_t, Dict[str, bool], bool) -> None
|
447 |
|
448 | UP_node = node
|
449 |
|
450 | with tagswitch(node) as case:
|
451 | if case(command_e.CommandList):
|
452 | node = cast(command.CommandList, UP_node)
|
453 |
|
454 | # TODO: How to distinguish between echo hi; echo bye; and on separate
|
455 | # lines
|
456 | for child in node.children:
|
457 | self.DoCommand(child,
|
458 | local_symbols,
|
459 | at_top_level=at_top_level)
|
460 |
|
461 | elif case(command_e.Simple):
|
462 | node = cast(command.Simple, UP_node)
|
463 |
|
464 | # How to preserve spaces between words? Do you want to do it?
|
465 | # Well you need to test this:
|
466 | #
|
467 | # echo foo \
|
468 | # bar
|
469 |
|
470 | if len(node.more_env):
|
471 | # We only need to transform the right side, not left side.
|
472 | for pair in node.more_env:
|
473 | self.DoRhsWord(pair.val, local_symbols)
|
474 |
|
475 | if len(node.words):
|
476 | first_word = node.words[0]
|
477 | ok, val, quoted = word_.StaticEval(first_word)
|
478 | word0_tok = location.LeftTokenForWord(first_word)
|
479 | if ok and not quoted:
|
480 | if val == '[':
|
481 | last_word = node.words[-1]
|
482 | # Check if last word is ]
|
483 | ok, val, quoted = word_.StaticEval(last_word)
|
484 | if ok and not quoted and val == ']':
|
485 | # Replace [ with 'test'
|
486 | self.cursor.PrintUntil(word0_tok)
|
487 | self.cursor.SkipPast(word0_tok)
|
488 | self.f.write('test')
|
489 |
|
490 | for w in node.words[1:-1]:
|
491 | self.DoWordInCommand(w, local_symbols)
|
492 |
|
493 | # Now omit ]
|
494 | rbrack_tok = location.LeftTokenForWord(
|
495 | last_word)
|
496 | # Skip the space token before ]
|
497 | self.cursor.PrintUntilSpid(rbrack_tok.span_id -
|
498 | 1)
|
499 | self.cursor.SkipPast(
|
500 | rbrack_tok) # ] takes one spid
|
501 | return
|
502 | else:
|
503 | raise RuntimeError('Got [ without ]')
|
504 |
|
505 | elif val == '.':
|
506 | self.cursor.PrintUntil(word0_tok)
|
507 | self.cursor.SkipPast(word0_tok)
|
508 | self.f.write('source')
|
509 | return
|
510 |
|
511 | for w in node.words:
|
512 | self.DoWordInCommand(w, local_symbols)
|
513 |
|
514 | # It would be nice to convert here docs to multi-line strings
|
515 | for r in node.redirects:
|
516 | self.DoRedirect(r, local_symbols)
|
517 |
|
518 | # TODO: Print the terminator. Could be \n or ;
|
519 | # Need to print env like PYTHONPATH = 'foo' && ls
|
520 | # Need to print redirects:
|
521 | # < > are the same. << is here string, and >> is assignment.
|
522 | # append is >+
|
523 |
|
524 | # TODO: static_eval of simple command
|
525 | # - [ -> "test". Eliminate trailing ].
|
526 | # - . -> source, etc.
|
527 |
|
528 | elif case(command_e.ShAssignment):
|
529 | node = cast(command.ShAssignment, UP_node)
|
530 |
|
531 | self.DoShAssignment(node, at_top_level, local_symbols)
|
532 |
|
533 | elif case(command_e.Pipeline):
|
534 | node = cast(command.Pipeline, UP_node)
|
535 |
|
536 | for child in node.children:
|
537 | self.DoCommand(child, local_symbols)
|
538 |
|
539 | elif case(command_e.AndOr):
|
540 | node = cast(command.AndOr, UP_node)
|
541 |
|
542 | for child in node.children:
|
543 | self.DoCommand(child, local_symbols)
|
544 |
|
545 | elif case(command_e.Sentence):
|
546 | node = cast(command.Sentence, UP_node)
|
547 |
|
548 | # 'ls &' to 'fork ls'
|
549 | # Keep ; the same.
|
550 | self.DoCommand(node.child, local_symbols)
|
551 |
|
552 | # This has to be different in the function case.
|
553 | elif case(command_e.BraceGroup):
|
554 | node = cast(BraceGroup, UP_node)
|
555 |
|
556 | # { echo hi; } -> do { echo hi }
|
557 | # For now it might be OK to keep 'do { echo hi; }
|
558 | self.cursor.PrintUntil(node.left)
|
559 | self.cursor.SkipPast(node.left)
|
560 | self.f.write('do {')
|
561 |
|
562 | for child in node.children:
|
563 | self.DoCommand(child, local_symbols)
|
564 |
|
565 | elif case(command_e.Subshell):
|
566 | node = cast(command.Subshell, UP_node)
|
567 |
|
568 | # (echo hi) -> shell echo hi
|
569 | # (echo hi; echo bye) -> shell {echo hi; echo bye}
|
570 |
|
571 | self.cursor.PrintUntil(node.left)
|
572 | self.cursor.SkipPast(node.left)
|
573 | self.f.write('shell {')
|
574 |
|
575 | self.DoCommand(node.child, local_symbols)
|
576 |
|
577 | #self._DebugSpid(right_spid)
|
578 | #self._DebugSpid(right_spid + 1)
|
579 |
|
580 | #print('RIGHT SPID', right_spid)
|
581 | self.cursor.PrintUntil(node.right)
|
582 | self.cursor.SkipPast(node.right)
|
583 | self.f.write('}')
|
584 |
|
585 | elif case(command_e.ShFunction):
|
586 | node = cast(command.ShFunction, UP_node)
|
587 |
|
588 | # TODO: skip name
|
589 | #self.f.write('proc %s' % node.name)
|
590 |
|
591 | # New symbol table for every function.
|
592 | new_local_symbols = {} # type: Dict[str, bool]
|
593 |
|
594 | # Should be the left most span, including 'function'
|
595 | if node.keyword: # function foo { ...
|
596 | self.cursor.PrintUntil(node.keyword)
|
597 | else: # foo() { ...
|
598 | self.cursor.PrintUntil(node.name_tok)
|
599 |
|
600 | self.f.write('proc %s ' % node.name)
|
601 |
|
602 | UP_body = node.body
|
603 | with tagswitch(UP_body) as case:
|
604 | if case(command_e.BraceGroup):
|
605 | body = cast(BraceGroup, UP_body)
|
606 | self.cursor.SkipUntil(body.left)
|
607 |
|
608 | # Don't add "do" like a standalone brace group. Just use {}.
|
609 | for child in body.children:
|
610 | self.DoCommand(child, new_local_symbols)
|
611 | else:
|
612 | # very rare cases like f() ( subshell )
|
613 | pass
|
614 |
|
615 | elif case(command_e.DoGroup):
|
616 | node = cast(command.DoGroup, UP_node)
|
617 |
|
618 | self.cursor.PrintUntil(node.left)
|
619 | self.cursor.SkipPast(node.left)
|
620 | self.f.write('{')
|
621 |
|
622 | for child in node.children:
|
623 | self.DoCommand(child, local_symbols)
|
624 |
|
625 | self.cursor.PrintUntil(node.right)
|
626 | self.cursor.SkipPast(node.right)
|
627 | self.f.write('}')
|
628 |
|
629 | elif case(command_e.ForEach):
|
630 | node = cast(command.ForEach, UP_node)
|
631 |
|
632 | # Need to preserve spaces between words, because there can be line
|
633 | # wrapping.
|
634 | # for x in a b c \
|
635 | # d e f; do
|
636 |
|
637 | UP_iterable = node.iterable
|
638 | with tagswitch(node.iterable) as case:
|
639 | if case(for_iter_e.Args):
|
640 | self.f.write('for %s in @ARGV ' % node.iter_names[0])
|
641 |
|
642 | # note: command_t doesn't have .spids
|
643 | body_tok = location.TokenForCommand(node.body)
|
644 | self.cursor.SkipUntil(body_tok)
|
645 |
|
646 | elif case(for_iter_e.Words):
|
647 | pass
|
648 |
|
649 | elif case(for_iter_e.YshExpr):
|
650 | pass
|
651 |
|
652 | if node.semi_tok is not None:
|
653 | self.cursor.PrintUntil(node.semi_tok)
|
654 | self.cursor.SkipPast(node.semi_tok)
|
655 |
|
656 | self.DoCommand(node.body, local_symbols)
|
657 |
|
658 | elif case(command_e.WhileUntil):
|
659 | node = cast(command.WhileUntil, UP_node)
|
660 |
|
661 | # Skip 'until', and replace it with 'while not'
|
662 | if node.keyword.id == Id.KW_Until:
|
663 | self.cursor.PrintUntil(node.keyword)
|
664 | self.cursor.SkipPast(node.keyword)
|
665 | self.f.write('while !')
|
666 |
|
667 | if node.cond.tag() == condition_e.Shell:
|
668 | commands = cast(condition.Shell, node.cond).commands
|
669 | # Skip the semi-colon in the condition, which is usually a Sentence
|
670 | if len(commands) == 1 and commands[0].tag(
|
671 | ) == command_e.Sentence:
|
672 | sentence = cast(command.Sentence, commands[0])
|
673 | self.DoCommand(sentence.child, local_symbols)
|
674 | self.cursor.SkipPast(sentence.terminator)
|
675 |
|
676 | self.DoCommand(node.body, local_symbols)
|
677 |
|
678 | elif case(command_e.If):
|
679 | node = cast(command.If, UP_node)
|
680 |
|
681 | if node.else_kw:
|
682 | else_spid = node.else_kw.span_id
|
683 | else:
|
684 | else_spid = runtime.NO_SPID
|
685 |
|
686 | # if foo; then -> if foo {
|
687 | # elif foo; then -> } elif foo {
|
688 | for i, arm in enumerate(node.arms):
|
689 | elif_spid = arm.spids[0]
|
690 | then_spid = arm.spids[1]
|
691 |
|
692 | if i != 0: # 'if' not 'elif' on the first arm
|
693 | self.cursor.PrintUntilSpid(elif_spid)
|
694 | self.f.write('} ')
|
695 |
|
696 | cond = arm.cond
|
697 | if cond.tag() == condition_e.Shell:
|
698 | commands = cast(condition.Shell, cond).commands
|
699 | if len(commands) == 1 and commands[0].tag(
|
700 | ) == command_e.Sentence:
|
701 | sentence = cast(command.Sentence, commands[0])
|
702 | self.DoCommand(sentence, local_symbols)
|
703 |
|
704 | # Remove semi-colon
|
705 | self.cursor.PrintUntil(sentence.terminator)
|
706 | self.cursor.SkipPast(sentence.terminator)
|
707 | else:
|
708 | for child in commands:
|
709 | self.DoCommand(child, local_symbols)
|
710 |
|
711 | self.cursor.PrintUntilSpid(then_spid)
|
712 | self.cursor.SkipUntilSpid(then_spid + 1)
|
713 | self.f.write('{')
|
714 |
|
715 | for child in arm.action:
|
716 | self.DoCommand(child, local_symbols)
|
717 |
|
718 | # else -> } else {
|
719 | if len(node.else_action):
|
720 | self.cursor.PrintUntil(node.else_kw)
|
721 | self.f.write('} ')
|
722 | self.cursor.PrintUntilSpid(node.else_kw.span_id + 1)
|
723 | self.f.write(' {')
|
724 |
|
725 | for child in node.else_action:
|
726 | self.DoCommand(child, local_symbols)
|
727 |
|
728 | # fi -> }
|
729 | self.cursor.PrintUntil(node.fi_kw)
|
730 | self.cursor.SkipPast(node.fi_kw)
|
731 | self.f.write('}')
|
732 |
|
733 | elif case(command_e.Case):
|
734 | node = cast(command.Case, UP_node)
|
735 |
|
736 | to_match = None # type: word_t
|
737 | with tagswitch(node.to_match) as case:
|
738 | if case(case_arg_e.YshExpr):
|
739 | #self.cursor.PrintUntilSpid(arms_end_spid)
|
740 | #self.cursor.SkipUntilSpid(arms_end_spid + 1)
|
741 | return
|
742 | elif case(case_arg_e.Word):
|
743 | to_match = cast(case_arg.Word, node.to_match).w
|
744 | else:
|
745 | raise AssertionError()
|
746 |
|
747 | self.cursor.PrintIncluding(node.case_kw)
|
748 |
|
749 | # Figure out the variable name, so we can translate
|
750 | # - $var to (var)
|
751 | # - "$var" to (var)
|
752 | var_part = None # type: NameTok
|
753 | with tagswitch(to_match) as case:
|
754 | if case(word_e.Compound):
|
755 | w = cast(CompoundWord, to_match)
|
756 | part0 = w.parts[0]
|
757 |
|
758 | with tagswitch(part0) as case2:
|
759 | if case2(word_part_e.SimpleVarSub):
|
760 | var_part = cast(NameTok, part0)
|
761 |
|
762 | elif case2(word_part_e.DoubleQuoted):
|
763 | dq_part = cast(DoubleQuoted, part0)
|
764 | if len(dq_part.parts) == 1:
|
765 | dq_part0 = dq_part.parts[0]
|
766 |
|
767 | # Nesting is annoying -- it would be nice to use pattern
|
768 | # matching, but mycpp won't like it.
|
769 | # TODO: extract into a common function
|
770 | with tagswitch(dq_part0) as case3:
|
771 | if case3(word_part_e.SimpleVarSub):
|
772 | var_part = cast(NameTok, dq_part0)
|
773 | #log("VAR PART %s", var_part)
|
774 |
|
775 | if var_part:
|
776 | self.f.write(' (')
|
777 | self.f.write(var_part.var_name)
|
778 | self.f.write(') ')
|
779 |
|
780 | self.cursor.SkipPast(node.arms_start) # Skip past 'in'
|
781 | self.f.write('{')
|
782 |
|
783 | missing_last_dsemi = False
|
784 |
|
785 | for case_arm in node.arms:
|
786 | # Replace ) with {
|
787 | self.cursor.PrintUntil(case_arm.middle)
|
788 | self.f.write(' {')
|
789 | self.cursor.SkipPast(case_arm.middle)
|
790 |
|
791 | for child in case_arm.action:
|
792 | self.DoCommand(child, local_symbols)
|
793 |
|
794 | if case_arm.right:
|
795 | # Change ;; to }
|
796 | self.cursor.PrintUntil(case_arm.right)
|
797 | self.f.write('}')
|
798 | self.cursor.SkipPast(case_arm.right)
|
799 | else:
|
800 | # valid: case $x in pat) echo hi ; esac
|
801 | missing_last_dsemi = True
|
802 |
|
803 | self.cursor.PrintUntil(node.arms_end) # 'esac' or }
|
804 |
|
805 | if missing_last_dsemi: # Print } for missing ;;
|
806 | self.f.write('}\n')
|
807 |
|
808 | self.cursor.SkipPast(node.arms_end) # 'esac' or }
|
809 |
|
810 | self.f.write('}') # in place of 'esac'
|
811 |
|
812 | elif case(command_e.TimeBlock):
|
813 | node = cast(command.TimeBlock, UP_node)
|
814 |
|
815 | self.DoCommand(node.pipeline, local_symbols)
|
816 |
|
817 | elif case(command_e.DParen):
|
818 | node = cast(command.DParen, UP_node)
|
819 | # TODO: arith expressions can words with command subs
|
820 | pass
|
821 |
|
822 | elif case(command_e.DBracket):
|
823 | node = cast(command.DBracket, UP_node)
|
824 |
|
825 | # TODO: bool_expr_t can have words with command subs
|
826 | pass
|
827 |
|
828 | else:
|
829 | pass
|
830 | #log('Command not handled: %s', node)
|
831 | #raise AssertionError(node.__class__.__name__)
|
832 |
|
833 | def DoRhsWord(self, node, local_symbols):
|
834 | # type: (rhs_word_t, Dict[str, bool]) -> None
|
835 | """For the RHS of assignments.
|
836 |
|
837 | TODO: for complex cases of word joining:
|
838 | local a=unquoted'single'"double"'"'
|
839 |
|
840 | We can try to handle it:
|
841 | var a = y"unquotedsingledouble\""
|
842 |
|
843 | Or simply abort and LEAVE IT ALONE. We should only translate things we
|
844 | recognize.
|
845 | """
|
846 | UP_node = node
|
847 | with tagswitch(node) as case:
|
848 | if case(rhs_word_e.Empty):
|
849 | self.f.write("''")
|
850 |
|
851 | elif case(rhs_word_e.Compound):
|
852 | node = cast(CompoundWord, UP_node)
|
853 |
|
854 | # TODO: This is wrong!
|
855 | style = _GetRhsStyle(node)
|
856 | if style == word_style_e.SQ:
|
857 | self.f.write("'")
|
858 | self.DoWordInCommand(node, local_symbols)
|
859 | self.f.write("'")
|
860 | elif style == word_style_e.DQ:
|
861 | self.f.write('"')
|
862 | self.DoWordInCommand(node, local_symbols)
|
863 | self.f.write('"')
|
864 | # TODO: Put these back
|
865 | #elif style == word_style_e.Expr:
|
866 | # pass
|
867 | #elif style == word_style_e.Unquoted:
|
868 | # pass
|
869 | else:
|
870 | # "${foo:-default}" -> foo or 'default'
|
871 | # ${foo:-default} -> @split(foo or 'default')
|
872 | # @(foo or 'default') -- implicit split.
|
873 |
|
874 | if word_.IsVarSub(node): # ${1} or "$1"
|
875 | # Do it in expression mode
|
876 | pass
|
877 | # NOTE: ArithSub with $(1 +2 ) is different than 1 + 2 because of
|
878 | # conversion to string.
|
879 |
|
880 | # For now, just stub it out
|
881 | self.DoWordInCommand(node, local_symbols)
|
882 |
|
883 | def DoWordInCommand(self, node, local_symbols):
|
884 | # type: (word_t, Dict[str, bool]) -> None
|
885 | """E.g. remove unquoted.
|
886 |
|
887 | echo "$x" -> echo $x
|
888 | """
|
889 | UP_node = node
|
890 |
|
891 | with tagswitch(node) as case:
|
892 | if case(word_e.Compound):
|
893 | node = cast(CompoundWord, UP_node)
|
894 |
|
895 | # UNQUOTE simple var subs
|
896 |
|
897 | # Special case for "$@".
|
898 | # TODO:
|
899 | # "$foo" -> $foo
|
900 | # "${foo}" -> $foo
|
901 |
|
902 | if (len(node.parts) == 1 and
|
903 | node.parts[0].tag() == word_part_e.DoubleQuoted):
|
904 | dq_part = cast(DoubleQuoted, node.parts[0])
|
905 |
|
906 | # NOTE: In double quoted case, this is the begin and end quote.
|
907 | # Do we need a HereDoc part?
|
908 |
|
909 | right_spid = dq_part.right.span_id
|
910 |
|
911 | # This is not set in the case of here docs? Why not?
|
912 | assert right_spid != runtime.NO_SPID, right_spid
|
913 |
|
914 | if len(dq_part.parts) == 1:
|
915 | part0 = dq_part.parts[0]
|
916 | if part0.tag() == word_part_e.SimpleVarSub:
|
917 | vsub_part = cast(NameTok, dq_part.parts[0])
|
918 | if vsub_part.left.id == Id.VSub_At:
|
919 | self.cursor.PrintUntil(dq_part.left)
|
920 | self.cursor.SkipPast(
|
921 | dq_part.right) # " then $@ then "
|
922 | self.f.write('@ARGV')
|
923 | return # Done replacing
|
924 |
|
925 | # "$1" -> $1, "$foo" -> $foo
|
926 | if vsub_part.left.id in (Id.VSub_Number,
|
927 | Id.VSub_DollarName):
|
928 | self.cursor.PrintUntil(dq_part.left)
|
929 | self.cursor.SkipPast(dq_part.right)
|
930 | self.f.write(lexer.TokenVal(vsub_part.left))
|
931 | return
|
932 |
|
933 | # Single arith sub, command sub, etc.
|
934 | # On the other hand, an unquoted one needs to turn into
|
935 | #
|
936 | # $(echo one two) -> @[echo one two]
|
937 | # `echo one two` -> @[echo one two]
|
938 | #
|
939 | # ${var:-'the default'} -> @$(var or 'the default')
|
940 | #
|
941 | # $((1 + 2)) -> $(1 + 2) -- this is OK unquoted
|
942 |
|
943 | elif part0.tag() == word_part_e.BracedVarSub:
|
944 | # Skip over quote
|
945 | self.cursor.PrintUntil(dq_part.left)
|
946 | self.cursor.SkipPast(dq_part.left)
|
947 | self.DoWordPart(part0, local_symbols)
|
948 | self.cursor.SkipPast(dq_part.right)
|
949 | return
|
950 |
|
951 | elif part0.tag() == word_part_e.CommandSub:
|
952 | self.cursor.PrintUntil(dq_part.left)
|
953 | self.cursor.SkipPast(dq_part.left)
|
954 | self.DoWordPart(part0, local_symbols)
|
955 | self.cursor.SkipPast(dq_part.right)
|
956 | return
|
957 |
|
958 | # TODO: 'foo'"bar" should be "foobar", etc.
|
959 | # If any part is double quoted, you can always double quote the whole
|
960 | # thing?
|
961 | for part in node.parts:
|
962 | self.DoWordPart(part, local_symbols)
|
963 |
|
964 | elif case(word_e.BracedTree):
|
965 | # Not doing anything now
|
966 | pass
|
967 |
|
968 | else:
|
969 | raise AssertionError(node.__class__.__name__)
|
970 |
|
971 | def DoWordPart(self, node, local_symbols, quoted=False):
|
972 | # type: (word_part_t, Dict[str, bool], bool) -> None
|
973 |
|
974 | left_tok = location.LeftTokenForWordPart(node)
|
975 | if left_tok:
|
976 | self.cursor.PrintUntil(left_tok)
|
977 |
|
978 | UP_node = node
|
979 |
|
980 | with tagswitch(node) as case:
|
981 | if case(word_part_e.ShArrayLiteral, word_part_e.BashAssocLiteral,
|
982 | word_part_e.TildeSub, word_part_e.ExtGlob):
|
983 | pass
|
984 |
|
985 | elif case(word_part_e.EscapedLiteral):
|
986 | node = cast(word_part.EscapedLiteral, UP_node)
|
987 | if quoted:
|
988 | pass
|
989 | else:
|
990 | # If unquoted \e, it should quoted instead. ' ' vs. \<invisible space>
|
991 | # Hm is this necessary though? I think the only motivation is changing
|
992 | # \{ and \( for macros. And ' ' to be readable/visible.
|
993 | t = node.token
|
994 | val = lexer.TokenSliceLeft(t, 1)
|
995 | assert len(val) == 1, val
|
996 | if val != '\n':
|
997 | self.cursor.PrintUntil(t)
|
998 | self.cursor.SkipPast(t)
|
999 | self.f.write("'%s'" % val)
|
1000 |
|
1001 | elif case(word_part_e.Literal):
|
1002 | node = cast(Token, UP_node)
|
1003 |
|
1004 | # Print it literally.
|
1005 | # TODO: We might want to do it all on the word level though. For
|
1006 | # example, foo"bar" becomes "foobar" in oil.
|
1007 | spid = node.span_id
|
1008 | if spid == runtime.NO_SPID:
|
1009 | #raise RuntimeError('%s has no span_id' % node.token)
|
1010 | # TODO: Fix word_.TildeDetect to construct proper tokens.
|
1011 | log('WARNING: %s has no span_id' % node)
|
1012 | else:
|
1013 | self.cursor.PrintIncluding(node)
|
1014 |
|
1015 | elif case(word_part_e.SingleQuoted):
|
1016 | node = cast(SingleQuoted, UP_node)
|
1017 |
|
1018 | # TODO:
|
1019 | # '\n' is '\\n'
|
1020 | # $'\n' is '\n'
|
1021 | # TODO: Should print until right_spid
|
1022 | # left_spid, right_spid = node.spids
|
1023 | if len(node.tokens): # Empty string has no tokens
|
1024 | self.cursor.PrintIncluding(node.tokens[-1])
|
1025 |
|
1026 | elif case(word_part_e.DoubleQuoted):
|
1027 | node = cast(DoubleQuoted, UP_node)
|
1028 | for part in node.parts:
|
1029 | self.DoWordPart(part, local_symbols, quoted=True)
|
1030 |
|
1031 | elif case(word_part_e.SimpleVarSub):
|
1032 | node = cast(NameTok, UP_node)
|
1033 |
|
1034 | spid = node.left.span_id
|
1035 | op_id = node.left.id
|
1036 |
|
1037 | if op_id == Id.VSub_DollarName:
|
1038 | self.cursor.PrintIncluding(node.left)
|
1039 |
|
1040 | elif op_id == Id.VSub_Number:
|
1041 | self.cursor.PrintIncluding(node.left)
|
1042 |
|
1043 | elif op_id == Id.VSub_At: # $@ -- handled quoted case above
|
1044 | self.f.write('$[join(ARGV)]')
|
1045 | self.cursor.SkipPast(node.left)
|
1046 |
|
1047 | elif op_id == Id.VSub_Star: # $*
|
1048 | # PEDANTIC: Depends if quoted or unquoted
|
1049 | self.f.write('$[join(ARGV)]')
|
1050 | self.cursor.SkipPast(node.left)
|
1051 |
|
1052 | elif op_id == Id.VSub_Pound: # $#
|
1053 | # len(ARGV) ?
|
1054 | self.f.write('$Argc')
|
1055 | self.cursor.SkipPast(node.left)
|
1056 |
|
1057 | else:
|
1058 | pass
|
1059 |
|
1060 | elif case(word_part_e.BracedVarSub):
|
1061 | node = cast(BracedVarSub, UP_node)
|
1062 |
|
1063 | # NOTE: Why do we need this but we don't need it in command sub?
|
1064 | self.cursor.PrintUntil(node.left)
|
1065 |
|
1066 | if node.bracket_op:
|
1067 | # a[1]
|
1068 | # These two change the sigil! ${a[@]} is now @a!
|
1069 | # a[@]
|
1070 | # a[*]
|
1071 | pass
|
1072 |
|
1073 | if node.prefix_op:
|
1074 | # len()
|
1075 | pass
|
1076 | if node.suffix_op:
|
1077 | pass
|
1078 |
|
1079 | op_id = node.token.id
|
1080 | if op_id == Id.VSub_QMark:
|
1081 | self.cursor.PrintIncluding(node.token)
|
1082 |
|
1083 | self.cursor.PrintIncluding(node.right)
|
1084 |
|
1085 | elif case(word_part_e.CommandSub):
|
1086 | node = cast(CommandSub, UP_node)
|
1087 |
|
1088 | if node.left_token.id == Id.Left_Backtick:
|
1089 | self.cursor.PrintUntil(node.left_token)
|
1090 | self.f.write('$(')
|
1091 | self.cursor.SkipPast(node.left_token)
|
1092 |
|
1093 | self.DoCommand(node.child, local_symbols)
|
1094 |
|
1095 | # Skip over right `
|
1096 | self.cursor.SkipPast(node.right)
|
1097 | self.f.write(')')
|
1098 |
|
1099 | else:
|
1100 | self.cursor.PrintIncluding(node.right)
|
1101 |
|
1102 | else:
|
1103 | pass
|