import os
from simpleparse import generator
#print dir()
file = os.path.join( os.path.split( generator.__file__ )[0], 'examples', 'vrml.def' )
#print file
VRMLPARSERDEF = open( file ).read()

def buildVRMLParser( declaration = VRMLPARSERDEF ):
	proc = generator._BaseGenerator( )
	proc.build()
	newgen = generator.Generator( declaration, proc.parserbyname( 'declarationset' ) )
	newgen.build()
	return newgen
def testdef( declaration = VRMLPARSERDEF, pname = 'declarationset' ):
	from TextTools import TextTools
	proc = generator._BaseGenerator( )
	proc.build()
	return TextTools.tag( declaration, proc.parserbyname( pname ) )

def test( eltype = 'declarationset'):
	#a = buildVRMLParser()
	#v = """dataType       := 'SFBool'/'SFString'/'SFFloat'/'SFTime'/'SFVec3f'/'SFVec2f'/'SFRotation'/'SFInt32'/'SFImage'/'SFRotation'/'SFColor'/'SFNode'/'MFBool'/'MFString'/'MFFloat'/'MFTime'/'MFVec3f'/'MFVec2f'/'MFRotation'/'MFInt32'/'MFRotation'/'MFColor'/'MFNode'"""
	#v = """'SFTime'/'SFVec3f'/'SFVec2f'/'SFRotation'/'SFInt32'/'SFImage'/'SFRotation'/'SFColor'/'SFNode'/'MFBool'/'MFString'/'MFFloat'/'MFTime'/'MFVec3f'/'MFVec2f'/'MFRotation'/'MFInt32'/'MFRotation'/'MFColor'/'MFNode'"""
	#v = """3f'/'SFVec2f'/'SFRotation'/'SFInt32'/'SFImage'/'SFRotation'/'SFColor'/'SFNode'/'MFBool'/'MFString'/'MFFloat'/'MFTime'/'MFVec3f'/'MFVec2f'/'MFRotation'/'MFInt32'/'MFRotation'/'MFColor'/'MFNode'"""
	#a = testdef( v, eltype )
	a = testdef()
	return a

sets = [
('23.4e-32 ', 'SFNumber', (1,[],8) ),
('"this\\""', 'SFString', (1, [('CHARNODBLQUOTE', 1, 5, []), ('ESCAPEDCHAR', 5, 7, [])], 8) ),
('FALSE', 'SFBool', (1,[],5) ),
('TRUE', 'SFBool', (1,[],4) ),
('test ', 'IS', (1, [('name', 0, 4, [])], 4) ),
('test ', 'USE', (1, [('name', 0, 4, [])], 4) ),
('NULL ', 'SFNull', (1, [], 5) ),
('s 234.5 ', 'Attr', (1, [('name', 0, 1, []), ('Field', 2, 8, [('SFNumber', 2, 7, [])])], 8) ),
('s "test" ', 'Attr', (1,
 [('name', 0, 1, []),
  ('Field', 2, 9, [('SFString', 2, 8, [('CHARNODBLQUOTE', 3, 7, [])])])],
 9)
),
('s Sphere { radius 2.4 } ', 'Attr', (1,
 [('name', 0, 1, []),
  ('Field',
   2,
   24,
   [('Node',
     2,
     24,
     [('nodegi', 2, 8, [('name', 2, 8, [])]),
      ('Attr',
       11,
       22,
       [('name', 11, 17, []),
        ('Field', 18, 22, [('SFNumber', 18, 21, [])])])])])],
 24)),
('PROTO Test [ ] { }  ', 'Proto', (1, [('nodegi', 6, 10, [('name', 6, 10, [])]), ('vrmlScene', 17, 17, [])], 20)),
('PROTO Test [ eventIn SFVec3f test1 ] { }  ', 'Proto', (1,
 [('nodegi', 6, 10, [('name', 6, 10, [])]),
  ('eventDecl',
   13,
   35,
   [('eventDirection', 13, 20, []),
    ('dataType', 21, 28, []),
    ('name', 29, 34, [])]),
  ('vrmlScene', 39, 39, [])],
 42)),
('PROTO Test [ eventOut SFVec3f test1 field MFString test2 [ ] ] { }', 'Proto', (1,
 [('nodegi', 6, 10, [('name', 6, 10, [])]),
  ('eventDecl',
   13,
   36,
   [('eventDirection', 13, 21, []),
    ('dataType', 22, 29, []),
    ('name', 30, 35, [])]),
  ('fieldDecl',
   36,
   61,
   [('fieldExposure', 36, 41, []),
    ('dataType', 42, 50, []),
    ('name', 51, 56, []),
    ('Field', 57, 61, [])]),
  ('vrmlScene', 65, 65, [])],
 66)),
('PROTO Test [ exposedField SFInt32 ja 23 ] { }', 'Proto', (1,
 [('nodegi', 6, 10, [('name', 6, 10, [])]),
  ('fieldDecl',
   13,
   40,
   [('fieldExposure', 13, 25, []),
    ('dataType', 26, 33, []),
    ('name', 34, 36, []),
    ('Field', 37, 40, [('SFNumber', 37, 39, [])])]),
  ('vrmlScene', 44, 44, [])],
 45) ),
('PROTO Test [ eventOut SFVec3f test1 field MFString test2 [ ] exposedField SFInt32 ja 23 ] { }', 'Proto', (1,
 [('nodegi', 6, 10, [('name', 6, 10, [])]),
  ('eventDecl',
   13,
   36,
   [('eventDirection', 13, 21, []),
    ('dataType', 22, 29, []),
    ('name', 30, 35, [])]),
  ('fieldDecl',
   36,
   61,
   [('fieldExposure', 36, 41, []),
    ('dataType', 42, 50, []),
    ('name', 51, 56, []),
    ('Field', 57, 61, [])]),
  ('fieldDecl',
   61,
   88,
   [('fieldExposure', 61, 73, []),
    ('dataType', 74, 81, []),
    ('name', 82, 84, []),
    ('Field', 85, 88, [('SFNumber', 85, 87, [])])]),
  ('vrmlScene', 92, 92, [])],
 93) ),
('PROTO Test [ ] {Node { } }', 'Proto', (1,
 [('nodegi', 6, 10, [('name', 6, 10, [])]),
  ('vrmlScene',
   16,
   25,
   [('Node', 16, 25, [('nodegi', 16, 20, [('name', 16, 20, [])])])])],
 26) ),
('PROTO Test [ ] {Node { children [ Script { } OtherNode { val 2.3 2.3 2.34 } ] } }', 'Proto', (1,
 [('nodegi', 6, 10, [('name', 6, 10, [])]),
  ('vrmlScene',
   16,
   80,
   [('Node',
     16,
     80,
     [('nodegi', 16, 20, [('name', 16, 20, [])]),
      ('Attr',
       23,
       78,
       [('name', 23, 31, []),
        ('Field',
         32,
         78,
         [('Script', 34, 45, []),
          ('Node',
           45,
           76,
           [('nodegi', 45, 54, [('name', 45, 54, [])]),
            ('Attr',
             57,
             74,
             [('name', 57, 60, []),
              ('Field',
               61,
               74,
               [('SFNumber', 61, 64, []),
                ('SFNumber', 65, 68, []),
                ('SFNumber', 69, 73, [])])])])])])])])],
 81) ),
('EXTERNPROTO Test [ ] { }  ', 'ExternProto', (0, [('nodegi', 12, 16, [('name', 12, 16, [])])], 0) ),
('EXTERNPROTO Test [eventOut SFVec2f test1  field MFString test2 exposedField SFInt32 ja ] { }  ', 'ExternProto', (0,
 [('nodegi', 12, 16, [('name', 12, 16, [])]),
  ('eventDecl',
   18,
   42,
   [('eventDirection', 18, 26, []),
    ('dataType', 27, 34, []),
    ('name', 35, 40, [])]),
  ('extFieldDecl',
   42,
   63,
   [('fieldExposure', 42, 47, []),
    ('dataType', 48, 56, []),
    ('name', 57, 62, [])]),
  ('extFieldDecl',
   63,
   87,
   [('fieldExposure', 63, 75, []),
    ('dataType', 76, 83, []),
    ('name', 84, 86, [])])],
 0)),
('eventIn SFBool set_isVisible IS set_isVisible ', 'ScriptEventDecl', (1,
 [('eventDirection', 0, 7, []),
  ('dataType', 8, 14, []),
  ('name', 15, 28, []),
  ('IS', 32, 45, [('name', 32, 45, [])])],
 46) ),
#('[ Node { } \n## testing harness\n#Transform { \n#this\n ]', 'Field', None),
#(' \n## testing harness\n#Transform { \n#this\n ]', '<ts>', None),

]

def bigtest( filename = 'z:\\temp\\test.wrl' ):
	from TextTools import TextTools
	file = open( filename).read()
	val = TextTools.tag( file, buildVRMLParser().parserbyname( 'vrmlFile' ) )
	print 'parsed %s characters of %s characters in file %s' % (val[-1], len(file), filename)
	return val

											     
class ProductionTester:
	def __init__( self, parsersource=None, sets=sets):
		self.parsersource = parsersource or buildVRMLParser().parserbyname
		self.sets = sets
		self.testproductions()
	def testproductions( self ):
		from TextTools import TextTools
		import pprint
		current = 1
		for instr, eltype, expected in self.sets:
			val = TextTools.tag( instr, self.parsersource( eltype) )
			if val != expected:
				print '''Test %s(%s) failed parsing:\n%s\n'''%(current,eltype, instr)
				pprint.pprint( val )
				raw_input( 'Press enter to continue' )
			else:
				print '''Test %s(%s) succeeded'''%(current, eltype)
			current = current+1
if __name__ == '__main__':

	a = ProductionTester()
	import sys
	if sys.argv[1:]:
		val = bigtest( sys.argv[1] ) 
	else:
		val = bigtest()
	