forked from binpash/pash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
52 lines (42 loc) · 1.64 KB
/
parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import config
import subprocess
import sys
from shell_ast.ast_util import UnparsedScript
from shasta.ast_node import ast_node_to_untyped_deep
from shasta.json_to_ast import to_ast_node
from shasta.ast_node import string_of_arg
from util import *
import libdash.parser
## Parses straight a shell script to an AST
## through python without calling it as an executable
def parse_shell_to_asts(input_script_path):
try:
new_ast_objects = libdash.parser.parse(input_script_path)
## Transform the untyped ast objects to typed ones
typed_ast_objects = []
for untyped_ast, original_text, linno_before, linno_after, in new_ast_objects:
typed_ast = to_ast_node(untyped_ast)
typed_ast_objects.append((typed_ast, original_text, linno_before, linno_after))
return typed_ast_objects
except libdash.parser.ParsingException as e:
log("Parsing error!", e)
sys.exit(1)
def parse_shell_to_asts_interactive(input_script_path: str):
return libdash.parser.parse(input_script_path)
def from_ast_objects_to_shell(asts):
shell_list = []
for ast in asts:
# log("Ast:", ast)
if(isinstance(ast, UnparsedScript)):
shell_list.append(ast.text)
else:
shell_list.append(ast.pretty())
return "\n".join(shell_list) + "\n"
def from_ast_objects_to_shell_file(asts, new_shell_filename):
script = from_ast_objects_to_shell(asts)
with open(new_shell_filename, 'w') as new_shell_file:
new_shell_file.write(script)
## Simply wraps the string_of_arg
def pash_string_of_arg(arg, quoted=False):
return string_of_arg(arg, quoted)