Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # $Id: apiparser.py,v 1.1 2001/11/18 22:42:57 brianp Exp $ |
| 4 | |
| 5 | # Mesa 3-D graphics library |
| 6 | # Version: 4.1 |
| 7 | # |
| 8 | # Copyright (C) 1999-2001 Brian Paul All Rights Reserved. |
| 9 | # |
| 10 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | # copy of this software and associated documentation files (the "Software"), |
| 12 | # to deal in the Software without restriction, including without limitation |
| 13 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 | # and/or sell copies of the Software, and to permit persons to whom the |
| 15 | # Software is furnished to do so, subject to the following conditions: |
| 16 | # |
| 17 | # The above copyright notice and this permission notice shall be included |
| 18 | # in all copies or substantial portions of the Software. |
| 19 | # |
| 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 21 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | # BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 24 | # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | |
| 27 | |
| 28 | # These helper functions are used by the other Mesa Python scripts. |
| 29 | # The main function is ProcessSpecFile(spedFile, function) which parses |
| 30 | # the named spec file and calls function() for each entry in the spec file. |
| 31 | |
| 32 | |
| 33 | import string |
| 34 | |
| 35 | |
| 36 | # Given parallel arrays of types and names, make a C-style parameter string |
| 37 | def MakeArgList(typeList, nameList): |
| 38 | result = '' |
| 39 | i = 1 |
| 40 | n = len(typeList) |
| 41 | for typ in typeList: |
| 42 | result = result + typ + ' ' + nameList[i - 1] |
| 43 | if i < n: |
| 44 | result = result + ', ' |
| 45 | i = i + 1 |
| 46 | #endfor |
| 47 | if result == '': |
| 48 | result = 'void' |
| 49 | #endif |
| 50 | return result |
| 51 | #enddef |
| 52 | |
| 53 | |
| 54 | prevCatagory = '' |
| 55 | |
| 56 | # |
| 57 | # Example callback function for ProcessSpecFile() |
| 58 | # |
| 59 | def PrintRecord(name, returnType, argTypeList, argNameList, alias, offset): |
| 60 | argList = MakeArgList(argTypeList, argNameList) |
| 61 | if category != prevCategory or prevCategory == '': |
| 62 | print '\n/* %s */' % category |
| 63 | prevCategory = category |
| 64 | #endif |
| 65 | print '%s gl%s(%s); /* %d */' % (returnType, name, argList, offset) |
| 66 | #endfor |
| 67 | |
| 68 | |
| 69 | # |
| 70 | # Process the api spec file |
| 71 | # |
| 72 | def ProcessSpecFile(specFile, userFunc): |
| 73 | |
| 74 | # init some vars |
| 75 | prevCategory = '' |
| 76 | funcName = '' |
| 77 | returnType = '' |
| 78 | argTypeList = [ ] |
| 79 | argNameList = [ ] |
| 80 | maxOffset = 0 |
| 81 | table = { } |
| 82 | offset = -1 |
| 83 | alias = '' |
| 84 | |
| 85 | f = open(specFile) |
| 86 | for line in f.readlines(): |
| 87 | |
| 88 | # split line into tokens |
| 89 | tokens = string.split(line) |
| 90 | |
| 91 | if len(tokens) > 0 and line[0] != '#': |
| 92 | |
| 93 | if tokens[0] == 'name': |
| 94 | if funcName != '': |
| 95 | # Verify entry has offset or alias |
| 96 | pnts = 0 |
| 97 | if offset == -2: |
| 98 | pnts = pnts + 1 |
| 99 | if offset >= 0: |
| 100 | pnts = pnts + 1 |
| 101 | if alias != '': |
| 102 | pnts = pnts + 1 |
| 103 | if pnts != 1: |
| 104 | print 'XXXXXXXXXX bad entry for %s' % funcName |
| 105 | |
| 106 | # process the function now |
| 107 | userFunc (funcName, returnType, argTypeList, argNameList, alias, offset) |
| 108 | # reset the lists |
| 109 | argTypeList = [ ] |
| 110 | argNameList = [ ] |
| 111 | returnType = '' |
| 112 | offset = -1 |
| 113 | alias = '' |
| 114 | |
| 115 | funcName = tokens[1] |
| 116 | |
| 117 | elif tokens[0] == 'return': |
| 118 | returnType = tokens[1] |
| 119 | if len(tokens) > 2: |
| 120 | returnType = returnType + ' ' + tokens[2] |
| 121 | if len(tokens) > 3: |
| 122 | returnType = returnType + ' ' + tokens[3] |
| 123 | |
| 124 | elif tokens[0] == 'param': |
| 125 | argNameList.append(tokens[1]) |
| 126 | type = tokens[2] |
| 127 | if len(tokens) > 3: |
| 128 | type = type + ' ' + tokens[3] |
| 129 | if len(tokens) > 4: |
| 130 | type = type + ' ' + tokens[4] |
| 131 | argTypeList.append(type) |
| 132 | |
| 133 | elif tokens[0] == 'category': |
| 134 | category = tokens[1] |
| 135 | |
| 136 | elif tokens[0] == 'offset': |
| 137 | if tokens[1] == '?': |
| 138 | offset = -2 |
| 139 | else: |
| 140 | offset = int(tokens[1]) |
| 141 | if offset > maxOffset: |
| 142 | maxOffset = offset |
| 143 | # else: |
| 144 | # print 'Unassigned offset for %s' % funcName |
| 145 | |
| 146 | elif tokens[0] == 'alias': |
| 147 | alias = tokens[1] |
| 148 | |
| 149 | else: |
| 150 | print 'Invalid token %s after function %s' % (tokens[0], funcName) |
| 151 | #endif |
| 152 | #endif |
| 153 | #endfor |
| 154 | #enddef |