import os
from simpleparse import generator
from TextTools import TextTools
LITPARSERDEF = open( 'findliterals.def' ).read()
parser = generator.buildParser( LITPARSERDEF ).parserbyname( 'myfile' )

declaration = r'''
myfile := (notliteral,literal)+, notliteral

# not-a-literal, not reported, repeating
<notliteral> := -( ("'",(CHARNOSNGLQUOTE/ESCAPEDCHAR)*,"'")  /  ('"',(CHARNODBLQUOTE/ESCAPEDCHAR)*,'"') )*

literal             :=  ("'",(CHARNOSNGLQUOTE/ESCAPEDCHAR)*,"'")  /  ('"',(CHARNODBLQUOTE/ESCAPEDCHAR)*,'"')

CHARNOSNGLQUOTE     :=  -[\\']+
CHARNODBLQUOTE      :=  -[\\"]+
ESCAPEDCHAR         :=  '\\',( SPECIALESCAPEDCHAR / OCTALESCAPEDCHAR )
SPECIALESCAPEDCHAR  :=  [\\abfnrtv]
OCTALESCAPEDCHAR    :=  [0-7],[0-7]?,[0-7]?
'''

def bigtest( file, parser = parser  ):
	val = TextTools.tag( file, parser )
	print 'parsed %s characters of %s characters in file %s' % (val[-1], len(file), filename)
	return val

usage =''' findliterals filename
Finds all single and double-quoted literals in a file and prints them to stdout.
Is not triple-quoted string aware.'''

if __name__ == '__main__':
	import sys
	if sys.argv[1:]:
		import time
		filename = sys.argv[1]
		file = open( filename ).read()
		t = time.time()
		val = bigtest( file )
		t = t-time.time()
		print '''Parsing Time:''', t
		for report, start, stop, children in val[1]:
			print file[ start: stop ]
	else:
		print usage
