#!/usr/bin/python
#This greps the source for SET_NAMESPACE, REGISTER_CLASS_NAME2,
#sinit definition and the following ->set{Getter,Setter,Method}ByQName calls

#It greps the documentation for all properties and methods
#and distinquishes read-only from write-only and dual-access properties

import sys
import re
from subprocess import *
import os
import pickle

def getFullname(cls,name):
	if cls != "":
		return cls + "." + name
	else:
		return name

if len(sys.argv) < 2:
	print("Call by langref_parser langref-directory")
	print("If you do not have a langref directory,")
	print("please download http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/standalone.zip")
	print("and unpack it.")
	exit(1)

langref = sys.argv[1]
if not os.path.exists(langref):
	print("The langref directory was not found!")
	print("Please download http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/standalone.zip")
	print("and unpack it here.")
	exit(1)

dclasses = set([])
dproperties = set([])
dmethods = set([])
curClass = ""

#We cannot destinguish public properties and public consts just by their definition,
#so we wait for an "Public Constants" to be matched, and all properties after that
#are constants
isConst = False

toplevelfiles = [ "ArgumentError.html", "Array.html", "Boolean.html", "Class.html", "Date.html", "DefinitionError.html", "Error.html",
                  "EvalError.html", "Function.html", "int.html", "JSON.html", "Math.html", "Namespace.html", "Number.html", "Object.html", "QName.html", "RangeError.html",
                  "ReferenceError.html", "RegExp.html", "SecurityError.html", "String.html", "SyntaxError.html",
                  "TypeError.html", "uint.html", "URIError.html", "Vector.html", "VerifyError.html", "XML.html", "XMLList.html" ]

airfiles = ['EncryptedLocalStore.html',
'SQLCollationType.html',
'SQLColumnNameStyle.html',
'SQLColumnSchema.html',
'SQLConnection.html',
'SQLIndexSchema.html',
'SQLMode.html',
'SQLResult.html',
'SQLSchema.html',
'SQLSchemaResult.html',
'SQLStatement.html',
'SQLTableSchema.html',
'SQLTransactionLockType.html',
'SQLTriggerSchema.html',
'SQLViewSchema.html',
'IFilePromise.html',
'DockIcon.html',
'Icon.html',
'InteractiveIcon.html',
'InvokeEventReason.html',
'NativeApplication.html',
'NativeDragActions.html',
'NativeDragManager.html',
'NativeDragOptions.html',
'NativeProcess.html',
'NativeProcessStartupInfo.html',
'NotificationType.html',
'SystemIdleMode.html',
'SystemTrayIcon.html',
'Updater.html',
'FocusDirection.html',
'Screen.html',
'ScreenMode.html',
'NativeMenu.html',
'NativeMenuItem.html',
'NativeWindow.html',
'NativeWindowDisplayState.html',
'NativeWindowInitOptions.html',
'NativeWindowRenderMode.html',
'NativeWindowResize.html',
'NativeWindowSystemChrome.html',
'NativeWindowType.html',
'StageAspectRatio.html',
'StageOrientation.html',
'DRMManagerError.html',
'SQLError.html',
'SQLErrorOperation.html',
'BrowserInvokeEvent.html',
'DatagramSocketDataEvent.html',
'DRMAuthenticateEvent.html',
'FileListEvent.html',
'GameInputEvent.html',
'HTMLUncaughtScriptExceptionEvent.html',
'InvokeEvent.html',
'LocationChangeEvent.html',
'MediaEvent.html',
'NativeDragEvent.html',
'NativeProcessExitEvent.html',
'NativeWindowBoundsEvent.html',
'NativeWindowDisplayStateEvent.html',
'OutputProgressEvent.html',
'RemoteNotificationEvent.html',
'ScreenMouseEvent.html',
'ServerSocketConnectEvent.html',
'SQLErrorEvent.html',
'SQLEvent.html',
'SQLUpdateEvent.html',
'StageOrientationEvent.html',
'StorageVolumeChangeEvent.html',
'TouchEventIntent.html',
'ExtensionContext.html',
'File.html',
'File.html',
'FileStream.html',
'StorageVolume.html',
'StorageVolumeInfo.html',
'HTMLHistoryItem.html',
'HTMLHost.html',
'HTMLLoader.html',
'HTMLPDFCapability.html',
'HTMLSWFCapability.html',
'HTMLWindowCreateOptions.html',
'AudioPlaybackMode.html',
'CameraPosition.html',
'CameraRollBrowseOptions.html',
'MediaPromise.html',
'MediaType.html',
'StageWebView.html',
'DatagramSocket.html',
'InterfaceAddress.html',
'NetworkInfo.html',
'NetworkInterface.html',
'ServerSocket.html',
'AAAARecord.html',
'ARecord.html',
'DNSResolver.html',
'MXRecord.html',
'PTRRecord.html',
'ResourceRecord.html',
'SRVRecord.html',
'NotificationStyle.html',
'RemoteNotifier.html',
'RemoteNotifierSubscribeOptions.html',
'PaperSize.html',
'PrintUIOptions.html',
'IURIDereferencer.html',
'ReferencesValidationSetting.html',
'RevocationCheckSettings.html',
'SignatureStatus.html',
'SignerTrustSettings.html',
'XMLSignatureValidator.html',
'ImageDecodingPolicy.html',
'AutoCapitalize.html',
'ReturnKeyLabel.html',
'StageText.html',
'StageTextInitOptions.html',
'GameInput.html',
'GameInputControl.html',
'GameInputDevice.html']

regclass = re.compile(r'<title>([a-zA-Z0-9\.]*)')
regconst = re.compile(r'Public Constants')
notinherited = r'<td class="summaryTableInheritanceCol">&nbsp;</td>'
regproperty = re.compile(notinherited + r'<td class="summaryTableSignatureCol"><a href="[^"]*" class="signatureLink">([^<]*)</a>.*?<div class="summaryTableDescription">(\[override\])?(\[static\] )?(\[read-only\])?(\[write-only\])?')
regmethod = re.compile(notinherited + r'<td class="summaryTableSignatureCol"><div class="summarySignature"><a href="[^"]*" class="signatureLink">([^<]*)</a>\(')

for dirpath, dirnames, filenames in os.walk(langref):
	if dirpath==langref:
		#In the toplevel dir, just include the specified files
		filenames=toplevelfiles
	elif not dirpath.startswith(os.path.join(langref,"flash/")):
		#only descent to flash/
		continue
	for filename in filenames:
		if filename == "package-detail.html" or filename == "package.html" or filename=="class-list.html":
			continue
		#print os.path.join(dirpath, filename)
		f = open(os.path.join(dirpath, filename), 'r')
		
		if filename not in airfiles:
			for line in f:
				#There maybe multiple matches on one line (seen in one case)
				while(True):
					m = regclass.search(line)
					if m:
						if dirpath==langref:
							curClass = m.group(1)
							dclasses.add(curClass)
							isConst = False
						else:
							curClass = os.path.relpath(dirpath, langref).replace("/",".") +"."+ m.group(1)
							dclasses.add(curClass)
							isConst = False
						break
					m = regconst.search(line)
					if m:
						isConst = True
						line = line[m.end():]
						continue
						#there may be another match on this line
					m = regproperty.search(line)
					if m:
						name = m.group(1).replace("#","").replace("(","").replace(")","")
						isOverride = m.group(2) != None
						isStatic = m.group(3) != None
						isReadOnly = m.group(4) != None
						isWriteOnly = m.group(5) != None
						dproperties.add((curClass,name,isConst,isStatic,isReadOnly,isWriteOnly,isOverride))
						line = line[m.end():]
						continue
					m = regmethod.search(line)
					if m:
						name = m.group(1).replace("#","").replace("(","").replace(")","")
						dmethods.add((curClass,name))
						line = line[m.end():]
						continue
					break

		f.close()

version = 1
langRefDB = (version, dclasses, dproperties, dmethods)

dbFile = open("langref.db", 'wb')
pickle.dump( langRefDB, dbFile )
dbFile.close()
print("Wrote langref.db")
