1bf215546Sopenharmony_ci# coding=utf-8 2bf215546Sopenharmony_ci# 3bf215546Sopenharmony_ci# Copyright © 2011 Intel Corporation 4bf215546Sopenharmony_ci# 5bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense, 9bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the 10bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci# 12bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci# Software. 15bf215546Sopenharmony_ci# 16bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22bf215546Sopenharmony_ci# DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci# This file contains helper functions for manipulating sexps in Python. 25bf215546Sopenharmony_ci# 26bf215546Sopenharmony_ci# We represent a sexp in Python using nested lists containing strings. 27bf215546Sopenharmony_ci# So, for example, the sexp (constant float (1.000000)) is represented 28bf215546Sopenharmony_ci# as ['constant', 'float', ['1.000000']]. 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ciimport re 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_cidef check_sexp(sexp): 33bf215546Sopenharmony_ci """Verify that the argument is a proper sexp. 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci That is, raise an exception if the argument is not a string or a 36bf215546Sopenharmony_ci list, or if it contains anything that is not a string or a list at 37bf215546Sopenharmony_ci any nesting level. 38bf215546Sopenharmony_ci """ 39bf215546Sopenharmony_ci if isinstance(sexp, list): 40bf215546Sopenharmony_ci for s in sexp: 41bf215546Sopenharmony_ci check_sexp(s) 42bf215546Sopenharmony_ci elif not isinstance(sexp, str): 43bf215546Sopenharmony_ci raise Exception('Not a sexp: {0!r}'.format(sexp)) 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_cidef parse_sexp(sexp): 46bf215546Sopenharmony_ci """Convert a string, of the form that would be output by mesa, 47bf215546Sopenharmony_ci into a sexp represented as nested lists containing strings. 48bf215546Sopenharmony_ci """ 49bf215546Sopenharmony_ci sexp_token_regexp = re.compile( 50bf215546Sopenharmony_ci '[a-zA-Z_]+(@[0-9]+)?|[0-9]+(\\.[0-9]+)?|[^ \r?\n]') 51bf215546Sopenharmony_ci stack = [[]] 52bf215546Sopenharmony_ci for match in sexp_token_regexp.finditer(sexp): 53bf215546Sopenharmony_ci token = match.group(0) 54bf215546Sopenharmony_ci if token == '(': 55bf215546Sopenharmony_ci stack.append([]) 56bf215546Sopenharmony_ci elif token == ')': 57bf215546Sopenharmony_ci if len(stack) == 1: 58bf215546Sopenharmony_ci raise Exception('Unmatched )') 59bf215546Sopenharmony_ci sexp = stack.pop() 60bf215546Sopenharmony_ci stack[-1].append(sexp) 61bf215546Sopenharmony_ci else: 62bf215546Sopenharmony_ci stack[-1].append(token) 63bf215546Sopenharmony_ci if len(stack) != 1: 64bf215546Sopenharmony_ci raise Exception('Unmatched (') 65bf215546Sopenharmony_ci if len(stack[0]) != 1: 66bf215546Sopenharmony_ci raise Exception('Multiple sexps') 67bf215546Sopenharmony_ci return stack[0][0] 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_cidef sexp_to_string(sexp): 70bf215546Sopenharmony_ci """Convert a sexp, represented as nested lists containing strings, 71bf215546Sopenharmony_ci into a single string of the form parseable by mesa. 72bf215546Sopenharmony_ci """ 73bf215546Sopenharmony_ci if isinstance(sexp, str): 74bf215546Sopenharmony_ci return sexp 75bf215546Sopenharmony_ci assert isinstance(sexp, list) 76bf215546Sopenharmony_ci result = '' 77bf215546Sopenharmony_ci for s in sexp: 78bf215546Sopenharmony_ci sub_result = sexp_to_string(s) 79bf215546Sopenharmony_ci if result == '': 80bf215546Sopenharmony_ci result = sub_result 81bf215546Sopenharmony_ci elif '\n' not in result and '\n' not in sub_result and \ 82bf215546Sopenharmony_ci len(result) + len(sub_result) + 1 <= 70: 83bf215546Sopenharmony_ci result += ' ' + sub_result 84bf215546Sopenharmony_ci else: 85bf215546Sopenharmony_ci result += '\n' + sub_result 86bf215546Sopenharmony_ci return '({0})'.format(result.replace('\n', '\n ')) 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_cidef sort_decls(sexp): 89bf215546Sopenharmony_ci """Sort all toplevel variable declarations in sexp. 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci This is used to work around the fact that 92bf215546Sopenharmony_ci ir_reader::read_instructions reorders declarations. 93bf215546Sopenharmony_ci """ 94bf215546Sopenharmony_ci assert isinstance(sexp, list) 95bf215546Sopenharmony_ci decls = [] 96bf215546Sopenharmony_ci other_code = [] 97bf215546Sopenharmony_ci for s in sexp: 98bf215546Sopenharmony_ci if isinstance(s, list) and len(s) >= 4 and s[0] == 'declare': 99bf215546Sopenharmony_ci decls.append(s) 100bf215546Sopenharmony_ci else: 101bf215546Sopenharmony_ci other_code.append(s) 102bf215546Sopenharmony_ci return sorted(decls) + other_code 103bf215546Sopenharmony_ci 104