Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Brian Paul | bb93870 | 2003-08-19 01:06:24 +0000 | [diff] [blame^] | 3 | # $Id: apiparser.py,v 1.2 2003/08/19 01:06:24 brianp Exp $ |
Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 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 | |
Brian Paul | bb93870 | 2003-08-19 01:06:24 +0000 | [diff] [blame^] | 74 | NO_OFFSET = -2 |
| 75 | |
Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 76 | # init some vars |
| 77 | prevCategory = '' |
| 78 | funcName = '' |
| 79 | returnType = '' |
| 80 | argTypeList = [ ] |
| 81 | argNameList = [ ] |
| 82 | maxOffset = 0 |
| 83 | table = { } |
| 84 | offset = -1 |
| 85 | alias = '' |
| 86 | |
| 87 | f = open(specFile) |
| 88 | for line in f.readlines(): |
| 89 | |
| 90 | # split line into tokens |
| 91 | tokens = string.split(line) |
| 92 | |
| 93 | if len(tokens) > 0 and line[0] != '#': |
| 94 | |
| 95 | if tokens[0] == 'name': |
| 96 | if funcName != '': |
| 97 | # Verify entry has offset or alias |
| 98 | pnts = 0 |
Brian Paul | bb93870 | 2003-08-19 01:06:24 +0000 | [diff] [blame^] | 99 | if offset == NO_OFFSET: |
Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 100 | pnts = pnts + 1 |
| 101 | if offset >= 0: |
| 102 | pnts = pnts + 1 |
| 103 | if alias != '': |
| 104 | pnts = pnts + 1 |
| 105 | if pnts != 1: |
| 106 | print 'XXXXXXXXXX bad entry for %s' % funcName |
| 107 | |
| 108 | # process the function now |
| 109 | userFunc (funcName, returnType, argTypeList, argNameList, alias, offset) |
| 110 | # reset the lists |
| 111 | argTypeList = [ ] |
| 112 | argNameList = [ ] |
| 113 | returnType = '' |
| 114 | offset = -1 |
| 115 | alias = '' |
| 116 | |
| 117 | funcName = tokens[1] |
| 118 | |
| 119 | elif tokens[0] == 'return': |
Brian Paul | bb93870 | 2003-08-19 01:06:24 +0000 | [diff] [blame^] | 120 | returnType = string.join(tokens[1:], ' ') |
Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 121 | |
| 122 | elif tokens[0] == 'param': |
| 123 | argNameList.append(tokens[1]) |
Brian Paul | bb93870 | 2003-08-19 01:06:24 +0000 | [diff] [blame^] | 124 | argTypeList.append(string.join(tokens[2:], ' ')) |
Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 125 | |
| 126 | elif tokens[0] == 'category': |
| 127 | category = tokens[1] |
| 128 | |
| 129 | elif tokens[0] == 'offset': |
| 130 | if tokens[1] == '?': |
Brian Paul | bb93870 | 2003-08-19 01:06:24 +0000 | [diff] [blame^] | 131 | offset = NO_OFFSET |
Brian Paul | 6c0d72f | 2001-11-18 22:42:57 +0000 | [diff] [blame] | 132 | else: |
| 133 | offset = int(tokens[1]) |
| 134 | if offset > maxOffset: |
| 135 | maxOffset = offset |
| 136 | # else: |
| 137 | # print 'Unassigned offset for %s' % funcName |
| 138 | |
| 139 | elif tokens[0] == 'alias': |
| 140 | alias = tokens[1] |
| 141 | |
| 142 | else: |
| 143 | print 'Invalid token %s after function %s' % (tokens[0], funcName) |
| 144 | #endif |
| 145 | #endif |
| 146 | #endfor |
| 147 | #enddef |