parse.py revision 10259
110259SAndrew.Bardsley@arm.com# Copyright (c) 2013 ARM Limited 210259SAndrew.Bardsley@arm.com# All rights reserved 310259SAndrew.Bardsley@arm.com# 410259SAndrew.Bardsley@arm.com# The license below extends only to copyright in the software and shall 510259SAndrew.Bardsley@arm.com# not be construed as granting a license to any other intellectual 610259SAndrew.Bardsley@arm.com# property including but not limited to intellectual property relating 710259SAndrew.Bardsley@arm.com# to a hardware implementation of the functionality of the software 810259SAndrew.Bardsley@arm.com# licensed hereunder. You may use the software subject to the license 910259SAndrew.Bardsley@arm.com# terms below provided that you ensure that this notice is replicated 1010259SAndrew.Bardsley@arm.com# unmodified and in its entirety in all distributions of the software, 1110259SAndrew.Bardsley@arm.com# modified or unmodified, in source code or in binary form. 1210259SAndrew.Bardsley@arm.com# 1310259SAndrew.Bardsley@arm.com# Redistribution and use in source and binary forms, with or without 1410259SAndrew.Bardsley@arm.com# modification, are permitted provided that the following conditions are 1510259SAndrew.Bardsley@arm.com# met: redistributions of source code must retain the above copyright 1610259SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer; 1710259SAndrew.Bardsley@arm.com# redistributions in binary form must reproduce the above copyright 1810259SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer in the 1910259SAndrew.Bardsley@arm.com# documentation and/or other materials provided with the distribution; 2010259SAndrew.Bardsley@arm.com# neither the name of the copyright holders nor the names of its 2110259SAndrew.Bardsley@arm.com# contributors may be used to endorse or promote products derived from 2210259SAndrew.Bardsley@arm.com# this software without specific prior written permission. 2310259SAndrew.Bardsley@arm.com# 2410259SAndrew.Bardsley@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2510259SAndrew.Bardsley@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2610259SAndrew.Bardsley@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2710259SAndrew.Bardsley@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2810259SAndrew.Bardsley@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2910259SAndrew.Bardsley@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3010259SAndrew.Bardsley@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3110259SAndrew.Bardsley@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3210259SAndrew.Bardsley@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3310259SAndrew.Bardsley@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3410259SAndrew.Bardsley@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3510259SAndrew.Bardsley@arm.com# 3610259SAndrew.Bardsley@arm.com# Authors: Andrew Bardsley 3710259SAndrew.Bardsley@arm.com 3810259SAndrew.Bardsley@arm.comimport re 3910259SAndrew.Bardsley@arm.com 4010259SAndrew.Bardsley@arm.comdef list_parser(names): 4110259SAndrew.Bardsley@arm.com """Parse a list of elements, some of which might be one-level sublists 4210259SAndrew.Bardsley@arm.com within parentheses, into a a list of lists of those elements. For 4310259SAndrew.Bardsley@arm.com example: list_parser('(a,b),c') -> [['a', 'b'], 'c']""" 4410259SAndrew.Bardsley@arm.com elems = re.split(',', names) 4510259SAndrew.Bardsley@arm.com ret = [] 4610259SAndrew.Bardsley@arm.com accum = [] 4710259SAndrew.Bardsley@arm.com for elem in elems: 4810259SAndrew.Bardsley@arm.com if re.search('^\((.*)\)$', elem): 4910259SAndrew.Bardsley@arm.com accum.append(re.sub('^\((.*)\)', '\\1', elem)) 5010259SAndrew.Bardsley@arm.com ret.append(accum) 5110259SAndrew.Bardsley@arm.com accum = [] 5210259SAndrew.Bardsley@arm.com elif re.search('^\(', elem): 5310259SAndrew.Bardsley@arm.com accum.append(re.sub('^\(', '', elem)) 5410259SAndrew.Bardsley@arm.com elif re.search('\)$', elem): 5510259SAndrew.Bardsley@arm.com accum.append(re.sub('\)$', '', elem)) 5610259SAndrew.Bardsley@arm.com ret.append(accum) 5710259SAndrew.Bardsley@arm.com accum = [] 5810259SAndrew.Bardsley@arm.com elif len(accum) != 0: 5910259SAndrew.Bardsley@arm.com accum.append(elem) 6010259SAndrew.Bardsley@arm.com else: 6110259SAndrew.Bardsley@arm.com ret.append([elem]) 6210259SAndrew.Bardsley@arm.com 6310259SAndrew.Bardsley@arm.com if len(accum) > 0: 6410259SAndrew.Bardsley@arm.com print 'Non matching brackets in', names 6510259SAndrew.Bardsley@arm.com 6610259SAndrew.Bardsley@arm.com return ret 6710259SAndrew.Bardsley@arm.com 6810259SAndrew.Bardsley@arm.comdef map2(f, ls): 6910259SAndrew.Bardsley@arm.com """map to a depth of 2. That is, given a list of lists, apply 7010259SAndrew.Bardsley@arm.com f to those innermost elements """ 7110259SAndrew.Bardsley@arm.com return map(lambda l: map(f, l), ls) 7210259SAndrew.Bardsley@arm.com 7310259SAndrew.Bardsley@arm.comdef remove_trailing_ws(line): 7410259SAndrew.Bardsley@arm.com return re.sub('\s*$', '', line) 7510259SAndrew.Bardsley@arm.com 7610259SAndrew.Bardsley@arm.comdef remove_leading_and_trailing_ws(line): 7710259SAndrew.Bardsley@arm.com return re.sub('\s*$', '', re.sub('^\s*', '', line)) 7810259SAndrew.Bardsley@arm.com 7910259SAndrew.Bardsley@arm.comdef parse_pairs_list(pairString): 8010259SAndrew.Bardsley@arm.com """parse a string like 'name=value name2=value2' into a 8110259SAndrew.Bardsley@arm.com list of pairs of ('name', 'value') ...""" 8210259SAndrew.Bardsley@arm.com ret = [] 8310259SAndrew.Bardsley@arm.com pairs = re.finditer('(\w+)(=("[^"]*"|[^\s]*))?', pairString) 8410259SAndrew.Bardsley@arm.com for pair in pairs: 8510259SAndrew.Bardsley@arm.com name, rest, value = pair.groups() 8610259SAndrew.Bardsley@arm.com if value is not None: 8710259SAndrew.Bardsley@arm.com value = re.sub('^"(.*)"$', '\\1', value) 8810259SAndrew.Bardsley@arm.com ret.append((name, value)) 8910259SAndrew.Bardsley@arm.com else: 9010259SAndrew.Bardsley@arm.com ret.append((name, '')) 9110259SAndrew.Bardsley@arm.com return ret 9210259SAndrew.Bardsley@arm.com 9310259SAndrew.Bardsley@arm.comdef parse_indexed_list(string): 9410259SAndrew.Bardsley@arm.com """parse a string of the form "(index,value),(index,value)..." 9510259SAndrew.Bardsley@arm.com into a list of index, value pairs""" 9610259SAndrew.Bardsley@arm.com 9710259SAndrew.Bardsley@arm.com ret = [] 9810259SAndrew.Bardsley@arm.com pairs = list_parser(string) 9910259SAndrew.Bardsley@arm.com for pair in pairs: 10010259SAndrew.Bardsley@arm.com if len(pair) == 2: 10110259SAndrew.Bardsley@arm.com index, value = pair 10210259SAndrew.Bardsley@arm.com ret.append((int(index), value)) 10310259SAndrew.Bardsley@arm.com 10410259SAndrew.Bardsley@arm.com return ret 10510259SAndrew.Bardsley@arm.com 10610259SAndrew.Bardsley@arm.comdef parse_pairs(pairString): 10710259SAndrew.Bardsley@arm.com """parse a string like 'name=value name2=value2' into a 10810259SAndrew.Bardsley@arm.com dictionary of {'name': 'value', 'name2': 'value2'} """ 10910259SAndrew.Bardsley@arm.com return dict(parse_pairs_list(pairString)) 110