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