use more python builtins; dont use reserved keywords

svn:r266
This commit is contained in:
Niels Provos 2006-11-18 21:27:42 +00:00
parent 31ba30abfe
commit 226fd50a99

View File

@ -3,7 +3,7 @@
# Copyright (c) 2005 Niels Provos <provos@citi.umich.edu> # Copyright (c) 2005 Niels Provos <provos@citi.umich.edu>
# All rights reserved. # All rights reserved.
# #
# Generates marshalling code based on libevent. # Generates marshaling code based on libevent.
import sys import sys
import re import re
@ -16,8 +16,6 @@ _STRUCT_RE = '[a-z][a-z_0-9]*'
# Globals # Globals
line_count = 0 line_count = 0
leading = re.compile(r'^\s+')
trailing = re.compile(r'\s+$')
white = re.compile(r'^\s+') white = re.compile(r'^\s+')
cppcomment = re.compile(r'\/\/.*$') cppcomment = re.compile(r'\/\/.*$')
cppdirect = [] cppdirect = []
@ -1041,14 +1039,11 @@ class EntryArray(Entry):
return dcl return dcl
def NormalizeLine(line): def NormalizeLine(line):
global leading
global trailing
global white global white
global cppcomment global cppcomment
line = cppcomment.sub('', line) line = cppcomment.sub('', line)
line = leading.sub('', line) line = line.strip()
line = trailing.sub('', line)
line = white.sub(' ', line) line = white.sub(' ', line)
return line return line
@ -1056,7 +1051,7 @@ def NormalizeLine(line):
def ProcessOneEntry(newstruct, entry): def ProcessOneEntry(newstruct, entry):
optional = 0 optional = 0
array = 0 array = 0
type = '' entry_type = ''
name = '' name = ''
tag = '' tag = ''
tag_set = None tag_set = None
@ -1068,7 +1063,7 @@ def ProcessOneEntry(newstruct, entry):
token = tokens[0] token = tokens[0]
tokens = tokens[1:] tokens = tokens[1:]
if not type: if not entry_type:
if not optional and token == 'optional': if not optional and token == 'optional':
optional = 1 optional = 1
continue continue
@ -1077,8 +1072,8 @@ def ProcessOneEntry(newstruct, entry):
array = 1 array = 1
continue continue
if not type: if not entry_type:
type = token entry_type = token
continue continue
if not name: if not name:
@ -1117,22 +1112,23 @@ def ProcessOneEntry(newstruct, entry):
sys.exit(1) sys.exit(1)
# Create the right entry # Create the right entry
if type == 'bytes': if entry_type == 'bytes':
if fixed_length: if fixed_length:
newentry = EntryBytes(type, name, tag, fixed_length) newentry = EntryBytes(entry_type, name, tag, fixed_length)
else: else:
newentry = EntryVarBytes(type, name, tag) newentry = EntryVarBytes(entry_type, name, tag)
elif type == 'int' and not fixed_length: elif entry_type == 'int' and not fixed_length:
newentry = EntryInt(type, name, tag) newentry = EntryInt(entry_type, name, tag)
elif type == 'string' and not fixed_length: elif entry_type == 'string' and not fixed_length:
newentry = EntryString(type, name, tag) newentry = EntryString(entry_type, name, tag)
else: else:
res = re.match(r'^struct\[(%s)\]$' % _STRUCT_RE, type, re.IGNORECASE) res = re.match(r'^struct\[(%s)\]$' % _STRUCT_RE,
entry_type, re.IGNORECASE)
if res: if res:
# References another struct defined in our file # References another struct defined in our file
newentry = EntryStruct(type, name, tag, res.group(1)) newentry = EntryStruct(entry_type, name, tag, res.group(1))
else: else:
print >>sys.stderr, 'Bad type: "%s" in "%s"' % (type, entry) print >>sys.stderr, 'Bad type: "%s" in "%s"' % (entry_type, entry)
sys.exit(1) sys.exit(1)
structs = [] structs = []
@ -1335,6 +1331,10 @@ def BodyPreamble(name):
return pre return pre
def main(argv): def main(argv):
if len(argv) < 2 or not argv[1]:
print >>sys.stderr, 'Need RPC description file as first argument.'
sys.exit(1)
filename = argv[1] filename = argv[1]
if filename.split('.')[-1] != 'rpc': if filename.split('.')[-1] != 'rpc':