Debian Codegraph @

README.md

raw symbols SCIP
1 # sedparse
2
3 - Author: Aurelio Jargas
4 - License: GPLv3
5 - Tested with Python 2.7, 3.4, 3.5, 3.6, 3.7 and 3.8 (see [.travis.yml](https://github.com/aureliojargas/sedparse/blob/master/.travis.yml))
6
7 A translation from C to Python of GNU sed's parser for sed scripts.
8
9 After running sedparse in your sed script, the resulting "[AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)" is available in different formats:
10
11 - List of objects (translated C structs)
12 - List of dictionaries
13 - JSON
14
15 For a complete reference on how the different sed commands are mapped by the parser, see:
16
17 - [tests/reference.sed](https://github.com/aureliojargas/sedparse/blob/master/tests/reference.sed) - original sed script
18 - [tests/reference.json](https://github.com/aureliojargas/sedparse/blob/master/tests/reference.json) - JSON generated by sedparse
19
20
21 ## About the translation
22
23 I copied the original code in C and translated everything to Python, line by line.
24
25 To make it feasible to keep this code updated with future GNU sed code, this is a literal translation, trying to mimic as much as possible of the original code. That includes using the same API, same logic, same variable and method names and same data structures. Pythonic code? Sorry, not here.
26
27 The accuracy of the parser is checked by extensive unit tests in [tests/](https://github.com/aureliojargas/sedparse/tree/master/tests).
28
29 Sedparse was translated from this GNU sed version:
30
31 http://git.savannah.gnu.org/cgit/sed.git/commit/?id=a9cb52bcf39f0ee307301ac73c11acb24372b9d8
32
33 commit a9cb52bcf39f0ee307301ac73c11acb24372b9d8
34 Author: Assaf Gordon <[email protected]>
35 Date: Sun Jun 2 01:14:00 2019 -0600
36
37 > Note that this is not a full GNU sed implementation.
38 > Only the parser for sed scripts was translated.
39 > Check https://github.com/GillesArcas/PythonSed for a working sed in Python.
40
41
42 ## Sedparse extensions to the original parser
43
44 - Preserves comments
45 - Preserves blank lines between commands
46 - Preserves original flags for the `s` command
47 - Preserves original flags for regex addresses
48
49
50 ## Installation
51
52 pip install --user sedparse
53 sedparse --help
54
55 Alternatively, you can just download and run the [sedparse.py](https://raw.githubusercontent.com/aureliojargas/sedparse/master/sedparse.py) file, since it is self-contained with no external dependencies.
56
57
58 ## Usage from the command line
59
60 The informed sed script will be parsed and checked for syntax errors. If everything is fine, a JSON representation of the script is sent to STDOUT.
61
62 Just like in sed, you can inform the sed script using one or more `-e` options:
63
64 ```console
65 $ sedparse -e "s/foo/bar/g" -e "5d"
66 [
67 {
68 "cmd": "s",
69 "line": 1,
70 "x": {
71 "cmd_subst": {
72 "regx": {
73 "flags": "g",
74 "pattern": "foo",
75 "slash": "/"
76 },
77 "replacement": {
78 "text": "bar"
79 }
80 }
81 }
82 },
83 {
84 "a1": {
85 "addr_number": 5,
86 "addr_type": 3
87 },
88 "cmd": "d",
89 "line": 1
90 }
91 ]
92 $
93 ```
94
95 Or you can inform the sed script as a file argument using `-f`:
96
97 ```console
98 $ echo '1,10!d' > head.sed
99 $ sedparse -f head.sed
100 [
101 {
102 "a1": {
103 "addr_number": 1,
104 "addr_type": 3
105 },
106 "a2": {
107 "addr_number": 10,
108 "addr_type": 3
109 },
110 "addr_bang": true,
111 "cmd": "d",
112 "line": 1
113 }
114 ]
115 $ rm head.sed
116 $
117 ```
118
119 Or even as text coming from STDIN when using the special `-` file:
120
121 ```console
122 $ echo '\EXTREMITIES' | sedparse -f -
123 [
124 {
125 "a1": {
126 "addr_regex": {
127 "flags": "MI",
128 "pattern": "XTR",
129 "slash": "E"
130 },
131 "addr_type": 2
132 },
133 "cmd": "T",
134 "line": 1,
135 "x": {
136 "label_name": "IES"
137 }
138 }
139 ]
140 $
141 ```
142
143
144 ## Usage as a Python module
145
146 Use `sedparse.compile_string()` to parse a string as a sed script. You must inform a list that will be appended in-place with the parsed commands.
147
148 ```python
149 >>> import sedparse
150 >>> sedscript = """\
151 ... 11,/foo/ {
152 ... $!N
153 ... s/\\n/-/gi
154 ... }
155 ... """
156 >>> parsed = []
157 >>> sedparse.compile_string(parsed, sedscript)
158 >>>
159 ```
160
161 Each sed command is represented by a `struct_sed_cmd` instance.
162
163 ```python
164 >>> import pprint
165 >>> pprint.pprint(parsed) # doctest:+ELLIPSIS
166 [struct_sed_cmd(line=1, cmd='{', ...),
167 struct_sed_cmd(line=2, cmd='N', ...),
168 struct_sed_cmd(line=3, cmd='s', ...),
169 struct_sed_cmd(line=4, cmd='}', ...)]
170 >>>
171 ```
172
173 You can `str()` each command, or any of its inner structs, to get their "human readable" representation.
174
175 ```python
176 >>> [str(x) for x in parsed]
177 ['11,/foo/ {', '$ !N', 's/\\n/-/gi', '}']
178 >>> str(parsed[0])
179 '11,/foo/ {'
180 >>> str(parsed[0].a1)
181 '11'
182 >>> str(parsed[0].a2)
183 '/foo/'
184 >>>
185 ```
186
187 Use `.to_dict()` to convert a command into a Python dictionary.
188
189 ```python
190 >>> cmd_n = parsed[1]
191 >>> str(cmd_n)
192 '$ !N'
193 >>> pprint.pprint(cmd_n.to_dict())
194 {'a1': {'addr_type': 7}, 'addr_bang': True, 'cmd': 'N', 'line': 2}
195 >>>
196 >>> pprint.pprint(cmd_n.to_dict(remove_empty=False))
197 {'a1': {'addr_number': 0, 'addr_regex': None, 'addr_step': 0, 'addr_type': 7},
198 'a2': None,
199 'addr_bang': True,
200 'cmd': 'N',
201 'line': 2,
202 'x': {'cmd_subst': {'outf': {'name': ''},
203 'regx': {'flags': '', 'pattern': '', 'slash': ''},
204 'replacement': {'text': ''}},
205 'cmd_txt': {'text': []},
206 'comment': '',
207 'fname': '',
208 'int_arg': -1,
209 'label_name': ''}}
210 >>>
211 ```
212
213 Use `.to_json()` to convert a command into JSON.
214
215 ```python
216 >>> print(cmd_n.to_json())
217 {
218 "a1": {
219 "addr_type": 7
220 },
221 "addr_bang": true,
222 "cmd": "N",
223 "line": 2
224 }
225 >>>
226 ```
227
228 Have fun!
229
230 ```python
231 >>> [x.cmd for x in parsed] # list of commands
232 ['{', 'N', 's', '}']
233 >>> [str(x) for x in parsed if x.a1 is None] # commands with no address
234 ['s/\\n/-/gi', '}']
235 >>> [str(x) for x in parsed if x.addr_bang] # commands with bang !
236 ['$ !N']
237 >>> [x.x.comment for x in parsed if x.x.comment] # extract all comments
238 []
239 >>> [x.x.fname for x in parsed if x.cmd in "rRwW"] # list of read/write filenames
240 []
241 >>>
242 ```