Package qm :: Package external :: Package DocumentTemplate :: Module gparse
[hide private]
[frames] | no frames]

Source Code for Module qm.external.DocumentTemplate.gparse

  1  ############################################################################## 
  2  #  
  3  # Zope Public License (ZPL) Version 1.0 
  4  # ------------------------------------- 
  5  #  
  6  # Copyright (c) Digital Creations.  All rights reserved. 
  7  #  
  8  # This license has been certified as Open Source(tm). 
  9  #  
 10  # Redistribution and use in source and binary forms, with or without 
 11  # modification, are permitted provided that the following conditions are 
 12  # met: 
 13  #  
 14  # 1. Redistributions in source code must retain the above copyright 
 15  #    notice, this list of conditions, and the following disclaimer. 
 16  #  
 17  # 2. Redistributions in binary form must reproduce the above copyright 
 18  #    notice, this list of conditions, and the following disclaimer in 
 19  #    the documentation and/or other materials provided with the 
 20  #    distribution. 
 21  #  
 22  # 3. Digital Creations requests that attribution be given to Zope 
 23  #    in any manner possible. Zope includes a "Powered by Zope" 
 24  #    button that is installed by default. While it is not a license 
 25  #    violation to remove this button, it is requested that the 
 26  #    attribution remain. A significant investment has been put 
 27  #    into Zope, and this effort will continue if the Zope community 
 28  #    continues to grow. This is one way to assure that growth. 
 29  #  
 30  # 4. All advertising materials and documentation mentioning 
 31  #    features derived from or use of this software must display 
 32  #    the following acknowledgement: 
 33  #  
 34  #      "This product includes software developed by Digital Creations 
 35  #      for use in the Z Object Publishing Environment 
 36  #      (http://www.zope.org/)." 
 37  #  
 38  #    In the event that the product being advertised includes an 
 39  #    intact Zope distribution (with copyright and license included) 
 40  #    then this clause is waived. 
 41  #  
 42  # 5. Names associated with Zope or Digital Creations must not be used to 
 43  #    endorse or promote products derived from this software without 
 44  #    prior written permission from Digital Creations. 
 45  #  
 46  # 6. Modified redistributions of any form whatsoever must retain 
 47  #    the following acknowledgment: 
 48  #  
 49  #      "This product includes software developed by Digital Creations 
 50  #      for use in the Z Object Publishing Environment 
 51  #      (http://www.zope.org/)." 
 52  #  
 53  #    Intact (re-)distributions of any official Zope release do not 
 54  #    require an external acknowledgement. 
 55  #  
 56  # 7. Modifications are encouraged but must be packaged separately as 
 57  #    patches to official Zope releases.  Distributions that do not 
 58  #    clearly separate the patches from the original work must be clearly 
 59  #    labeled as unofficial distributions.  Modifications which do not 
 60  #    carry the name Zope may be packaged in any form, as long as they 
 61  #    conform to all of the clauses above. 
 62  #  
 63  #  
 64  # Disclaimer 
 65  #  
 66  #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY 
 67  #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 68  #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 69  #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS 
 70  #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 71  #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 72  #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
 73  #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 74  #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 75  #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 76  #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 77  #   SUCH DAMAGE. 
 78  #  
 79  #  
 80  # This software consists of contributions made by Digital Creations and 
 81  # many individuals on behalf of Digital Creations.  Specific 
 82  # attributions are listed in the accompanying credits file. 
 83  #  
 84  ############################################################################## 
 85  "$Id: gparse.py 694 2003-04-16 02:53:50Z sc $" 
 86  import sys, parser, symbol, token 
 87   
 88  from symbol import test, suite, argument, arith_expr, shift_expr 
 89  from symbol import subscriptlist, subscript, comparison, trailer, xor_expr 
 90  from symbol import term, not_test, factor, atom, expr, arglist 
 91  from symbol import power, and_test, and_expr 
 92   
 93  from token import STAR, NAME, RPAR, LPAR, NUMBER, DOT, STRING, COMMA 
 94  from token import ISTERMINAL, LSQB, COLON 
 95   
 96  from parser import sequence2ast, compileast, ast2list 
 97   
 98  ParseError='Expression Parse Error' 
 99   
100 -def munge(ast, STAR=STAR, DOT=DOT, LSQB=LSQB, COLON=COLON, trailer=trailer):
101 ast0=ast[0] 102 if ISTERMINAL(ast0): return 103 else: 104 if ast0==term and len(ast) > 2: 105 keep_going=1 106 while keep_going: 107 keep_going=0 108 start=2 109 for i in range(start,len(ast)-1): 110 if ast[i][0]==STAR: 111 ast[i-1:i+2]=[multi_munge(ast[i-1:i+2])] 112 keep_going=1 113 break 114 115 for a in ast[1:]: munge(a) 116 117 elif ast0==power: 118 keep_going=1 119 while keep_going: 120 keep_going=0 121 start=2 122 for i in range(start,len(ast)): 123 a=ast[i] 124 if a[0]==trailer: 125 if a[1][0]==DOT: 126 ast[:i+1]=dot_munge(ast,i) 127 keep_going=1 128 start=3 129 break 130 if a[1][0]==LSQB: 131 if (a[2][0] != subscriptlist or 132 a[2][1][0] != subscript): 133 raise ParseError, ( 134 'Unexpected form after left square brace') 135 136 slist=a[2] 137 if len(slist)==2: 138 # One subscript, check for range and ... 139 sub=slist[1] 140 if sub[1][0]==DOT: 141 raise ParseError, ( 142 'ellipses are not supported') 143 l=len(sub) 144 if l < 3 and sub[1][0] != COLON: 145 ast[:i+1]=item_munge(ast, i) 146 elif l < 5: ast[:i+1]=slice_munge(ast, i) 147 else: raise ParseError, 'Invalid slice' 148 149 else: ast[:i+1]=item_munge(ast, i) 150 keep_going=1 151 start=3 152 break 153 154 for a in ast[1:]: munge(a) 155 else: 156 for a in ast[1:]: munge(a) 157 return ast
158
159 -def slice_munge(ast, i):
160 # Munge a slice access into a function call 161 # Note that we must account for a[:], a[b:], a[:b], and a[b:c] 162 name=ast[i][2][1] 163 args=[arglist] 164 append=args.append 165 166 a=(factor, (power, (atom, (NAME, '_vars')))) 167 a=(argument, (test, (and_test, (not_test, (comparison, 168 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 169 (term, a))))))))))) 170 append(a) 171 append([COMMA,',']) 172 173 a=ast[:i] 174 a=(argument, (test, (and_test, (not_test, (comparison, 175 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 176 (term, (factor, a)))))))))))) 177 append(a) 178 179 sub=ast[i][2][1] 180 l=len(sub) 181 if sub[1][0]==COLON: 182 if l==3: 183 append([COMMA,',']) 184 a=(argument, (test, (and_test, (not_test, (comparison, 185 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 186 (term, (factor, (power, (atom, (NUMBER, '0'))))))))))))))) 187 append(a) 188 append([COMMA,',']) 189 append([argument, sub[2]]) 190 else: 191 append([COMMA,',']) 192 append([argument, sub[1]]) 193 if l==4: 194 append([COMMA,',']) 195 append([argument, sub[3]]) 196 197 return [power, [atom, [NAME, '__guarded_getslice__']], 198 [trailer, [LPAR, '('], args, [RPAR, ')'], 199 ] 200 ]
201
202 -def item_munge(ast, i):
203 # Munge an item access into a function call 204 name=ast[i][2][1] 205 args=[arglist] 206 append=args.append 207 208 a=(factor, (power, (atom, (NAME, '_vars')))) 209 a=(argument, (test, (and_test, (not_test, (comparison, 210 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 211 (term, a))))))))))) 212 append(a) 213 append([COMMA,',']) 214 215 a=ast[:i] 216 a=(argument, (test, (and_test, (not_test, (comparison, 217 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 218 (term, (factor, a)))))))))))) 219 append(a) 220 append([COMMA,',']) 221 222 for sub in ast[i][2][1:]: 223 if sub[0]==COMMA: append(sub) 224 else: 225 if len(sub) != 2: raise ParseError, 'Invalid slice in subscript' 226 append([argument, sub[1]]) 227 228 return [power, [atom, [NAME, '__guarded_getitem__']], 229 [trailer, [LPAR, '('], args, [RPAR, ')'], 230 ] 231 ]
232
233 -def dot_munge(ast, i):
234 # Munge an attribute access into a function call 235 name=ast[i][2][1] 236 args=[arglist] 237 append=args.append 238 239 a=(factor, (power, (atom, (NAME, '_vars')))) 240 a=(argument, (test, (and_test, (not_test, (comparison, 241 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 242 (term, a))))))))))) 243 append(a) 244 append([COMMA,',']) 245 246 a=ast[:i] 247 a=(argument, (test, (and_test, (not_test, (comparison, 248 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 249 (term, (factor, a)))))))))))) 250 append(a) 251 append([COMMA,',']) 252 253 a=(factor, (power, (atom, (STRING, `name`)))) 254 a=(argument, (test, (and_test, (not_test, (comparison, 255 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 256 (term, a))))))))))) 257 append(a) 258 259 return [power, [atom, [NAME, '__guarded_getattr__']], 260 [trailer, [LPAR, '('], args, [RPAR, ')']], 261 ]
262
263 -def multi_munge(ast):
264 # Munge a multiplication into a function call: __guarded_mul__ 265 args=[arglist] 266 267 append=args.append 268 a=(factor, (power, (atom, (NAME, '_vars')))) 269 a=(argument, (test, (and_test, (not_test, (comparison, 270 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 271 (term, a))))))))))) 272 append(a) 273 append([COMMA,',']) 274 275 for a in ast: 276 if a[0]==STAR: args.append([COMMA,',']) 277 else: 278 a=(argument, (test, (and_test, (not_test, (comparison, 279 (expr, (xor_expr, (and_expr, (shift_expr, (arith_expr, 280 (term, a))))))))))) 281 append(a) 282 283 return [factor, [power, 284 [atom, [NAME, '__guarded_mul__']], 285 [trailer, [LPAR, '('], args, [RPAR, ')'], 286 ]]]
287
288 -def compile(src, file_name, ctype):
289 if ctype=='eval': ast=parser.expr(src) 290 elif ctype=='exec': ast=parser.suite(src) 291 l=ast2list(ast) 292 l=munge(l) 293 ast=sequence2ast(l) 294 return parser.compileast(ast, file_name)
295