1659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# coding=utf-8
2659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry#
3659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# Copyright © 2011 Intel Corporation
4659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry#
5659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# Permission is hereby granted, free of charge, to any person obtaining a
6659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# copy of this software and associated documentation files (the "Software"),
7659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# to deal in the Software without restriction, including without limitation
8659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# and/or sell copies of the Software, and to permit persons to whom the
10659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# Software is furnished to do so, subject to the following conditions:
11659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry#
12659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# The above copyright notice and this permission notice (including the next
13659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# paragraph) shall be included in all copies or substantial portions of the
14659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# Software.
15659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry#
16659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# DEALINGS IN THE SOFTWARE.
23659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
24659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# This file contains helper functions for manipulating sexps in Python.
25659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry#
26659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# We represent a sexp in Python using nested lists containing strings.
27659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# So, for example, the sexp (constant float (1.000000)) is represented
28659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry# as ['constant', 'float', ['1.000000']].
29659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
30659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berryimport re
31659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
32659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berrydef check_sexp(sexp):
33659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """Verify that the argument is a proper sexp.
34659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
35659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    That is, raise an exception if the argument is not a string or a
36659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    list, or if it contains anything that is not a string or a list at
37659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    any nesting level.
38659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """
39659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    if isinstance(sexp, list):
40659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        for s in sexp:
41659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            check_sexp(s)
42659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    elif not isinstance(sexp, basestring):
43659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        raise Exception('Not a sexp: {0!r}'.format(sexp))
44659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
45659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berrydef parse_sexp(sexp):
46659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """Convert a string, of the form that would be output by mesa,
47659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    into a sexp represented as nested lists containing strings.
48659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """
49659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    sexp_token_regexp = re.compile(
50659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        '[a-zA-Z_]+(@[0-9]+)?|[0-9]+(\\.[0-9]+)?|[^ \n]')
51659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    stack = [[]]
52659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    for match in sexp_token_regexp.finditer(sexp):
53659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        token = match.group(0)
54659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        if token == '(':
55659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            stack.append([])
56659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        elif token == ')':
57659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            if len(stack) == 1:
58659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry                raise Exception('Unmatched )')
59659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            sexp = stack.pop()
60659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            stack[-1].append(sexp)
61659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        else:
62659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            stack[-1].append(token)
63659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    if len(stack) != 1:
64659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        raise Exception('Unmatched (')
65659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    if len(stack[0]) != 1:
66659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        raise Exception('Multiple sexps')
67659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    return stack[0][0]
68659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
69659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berrydef sexp_to_string(sexp):
70659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """Convert a sexp, represented as nested lists containing strings,
71659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    into a single string of the form parseable by mesa.
72659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """
73659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    if isinstance(sexp, basestring):
74659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        return sexp
75659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    assert isinstance(sexp, list)
76659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    result = ''
77659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    for s in sexp:
78659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        sub_result = sexp_to_string(s)
79659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        if result == '':
80659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            result = sub_result
81659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        elif '\n' not in result and '\n' not in sub_result and \
82659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry                len(result) + len(sub_result) + 1 <= 70:
83659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            result += ' ' + sub_result
84659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        else:
85659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            result += '\n' + sub_result
86659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    return '({0})'.format(result.replace('\n', '\n '))
87659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
88659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berrydef sort_decls(sexp):
89659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """Sort all toplevel variable declarations in sexp.
90659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
91659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    This is used to work around the fact that
92659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    ir_reader::read_instructions reorders declarations.
93659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    """
94659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    assert isinstance(sexp, list)
95659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    decls = []
96659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    other_code = []
97659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    for s in sexp:
98659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        if isinstance(s, list) and len(s) >= 4 and s[0] == 'declare':
99659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            decls.append(s)
100659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry        else:
101659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry            other_code.append(s)
102659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry    return sorted(decls) + other_code
103659cdedb532e675da5676d40ee39278aadd8f0a1Paul Berry
104