paul@1031 | 1 | #!/usr/bin/env python |
paul@1031 | 2 | |
paul@1031 | 3 | """ |
paul@1031 | 4 | Parsing of textual content. |
paul@1031 | 5 | |
paul@1031 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1031 | 7 | |
paul@1031 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1031 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1031 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1031 | 11 | version. |
paul@1031 | 12 | |
paul@1031 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1031 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1031 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1031 | 16 | details. |
paul@1031 | 17 | |
paul@1031 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1031 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1031 | 20 | """ |
paul@1031 | 21 | |
paul@1031 | 22 | import re |
paul@1031 | 23 | |
paul@1031 | 24 | # Parsing of lines to obtain functions and arguments. |
paul@1031 | 25 | |
paul@1031 | 26 | line_pattern_str = r"(?:" \ |
paul@1031 | 27 | r"(?:'(.*?)')" \ |
paul@1031 | 28 | r"|" \ |
paul@1031 | 29 | r'(?:"(.*?)")' \ |
paul@1031 | 30 | r"|" \ |
paul@1031 | 31 | r"([^\s]+)" \ |
paul@1031 | 32 | r")+" \ |
paul@1031 | 33 | r"(?:\s+|$)" |
paul@1031 | 34 | line_pattern = re.compile(line_pattern_str) |
paul@1031 | 35 | |
paul@1031 | 36 | def parse_line(text): |
paul@1031 | 37 | |
paul@1031 | 38 | """ |
paul@1031 | 39 | Parse the given 'text', returning a list of words separated by whitespace in |
paul@1031 | 40 | the input, where whitespace may occur inside words if quoted using single or |
paul@1031 | 41 | double quotes. |
paul@1031 | 42 | """ |
paul@1031 | 43 | |
paul@1031 | 44 | parts = [] |
paul@1031 | 45 | |
paul@1031 | 46 | # Match the components of each part. |
paul@1031 | 47 | |
paul@1031 | 48 | for match in line_pattern.finditer(text): |
paul@1031 | 49 | |
paul@1031 | 50 | # Combine the components by traversing the matching groups. |
paul@1031 | 51 | |
paul@1031 | 52 | parts.append(reduce(lambda a, b: (a or "") + (b or ""), match.groups())) |
paul@1031 | 53 | |
paul@1031 | 54 | return parts |
paul@1031 | 55 | |
paul@1031 | 56 | # vim: tabstop=4 expandtab shiftwidth=4 |