Commit 65955d90 authored by Denis Filatov's avatar Denis Filatov
Browse files

final version for approval

parent 4d308d71
Loading
Loading
Loading
Loading
+6440 −6442

File changed and moved.

Preview size limit exceeded, changes collapsed.

+56 −55
Original line number Diff line number Diff line
@@ -16,12 +16,15 @@ RE_MODULE = re.compile( r'^\s*([A-Z][\w-]*)\s*({.*?})?\s*DEFINITIONS.*?::=\s*?

RE_SPACES   = re.compile(r'\s+')

RE_COMMENTS = re.compile(r'^\s*--.*?\n|(?:^\s*)?--.*?(?:--[\t ]*|$)|(?:^\s*)?/\*.*?\*/[\t ]*\n?', re.MULTILINE|re.DOTALL)
RE_COMMENTS = re.compile(r'^\s*--.*?\n|--.*?(?:--|$)|/\*.*?\*/[\t ]*\n?', re.MULTILINE|re.DOTALL)

RE_BASIC_TYPES = re.compile(r'^OCTET\s+STRING|BIT\s+STRING|BOOLEAN|INTEGER|FLOAT|SEQUENCE|SET|NULL')

#RE_TYPE_BODY_1 = re.compile(r'.*?{(.*)}\s*WITH', re.MULTILINE|re.DOTALL)
#RE_TYPE_BODY_2 = re.compile(r'.*?{(.*)}\s*(?:WITH.*|\(.*?\)|\s*$)', re.MULTILINE|re.DOTALL)
RE_TYPE_BODY = re.compile(r'.*?{(.*)}\s*(?:WITH.*|\(.*?\)|\s*$)', re.MULTILINE|re.DOTALL)

#RE_FIELDS = re.compile(r'^\s*(?:/\*\*.*?\*/)|^\s*([\w-]+?)\s+(OCTET\s+STRING|BIT\s+STRING|[A-Z][.\w-]+)?(.*?)(?:,((?:\s*--!?<.*?\n)*)|((?:--!?<.*?\n)*)$)', re.MULTILINE | re.DOTALL| re.VERBOSE)
RE_FIELDS = re.compile(r'^\s*/\*.*?\*/|^\s*--\!.*?\n|^[\s&]*([\w-]+)\s+(OCTET\s+STRING|BIT\s+STRING|[A-Z][\w-]+)?((?:{[^}]*}|\([^)]*\)|.)*?)(?:,|(--)|$)', re.MULTILINE | re.DOTALL)

RE_EXPORTS = re.compile(r'^\s*EXPORTS.*?;', re.DOTALL | re.MULTILINE)
@@ -37,8 +40,9 @@ RE_DOXY_ASN_COMMENTS = re.compile(r'^\s*--[-!#](:?$|\s(.*))', re.MULTILINE)
RE_DOXY_C_COMMENTS = re.compile(r'^\s*/\*\*\s(.*?)\*/', re.MULTILINE | re.DOTALL)

RE_DOXY_C_COMMENTS_I = re.compile(r'\s*\*+')
RE_STRIPSTAR = re.compile(r'^\s*\*', re.MULTILINE)
RE_POWER_SIGN = re.compile('\^(-?\w+|\(.*?\))')

RE_STRIPSTAR = re.compile(r'^\s*\*?', re.MULTILINE)

RE_DOXY_REF = re.compile(r'@ref\s+([\w-]+)')
RE_DOXY_CLASS = re.compile(r'@(?:class|struct|details):?\s+([\w-]+)')
@@ -56,11 +60,11 @@ RE_DOXY_SECTION = re.compile(r"^\s*@(brief|note|(class|struct|param|field|detail
# RE_TYPE = re.compile(r'(([A-Z][\w-]*)\s*::=[\w \t]+(?:{+(.*?)}+)?.*?)\n\s*\n', re.MULTILINE | re.DOTALL)
RE_TYPE = re.compile(r'^\s*([A-Z][\w-]*)?\s*([{} \t:\w-]*?)?::=([\w \t]+.*?)\n\s*\n', re.MULTILINE | re.DOTALL)
RE_OPTIONS = re.compile(r'^\s*@options[\s:]+(.+)', re.MULTILINE)
RE_DOXY_OTHER = re.compile(r'\^(\w+)', re.MULTILINE)

extTypes = {}
cpos = 0
o_args = []
m_options = []

def urlquote(s):
	if (sys.version_info > (3, 0)):
@@ -85,7 +89,7 @@ def parseText(content, indent=None):
	
	content = RE_DOXY_STRIP_SINGLE_TAG.sub('', content)

	content = re.sub('\^(\w+)', '<sup>\\1</sup>', content)
	content = RE_POWER_SIGN.sub('<sup>\\1</sup>', content)

	return indentLines(content, indent)

@@ -112,6 +116,14 @@ def parseDoxyComments(content:str):
		ret += RE_STRIPSTAR.sub('', m.group(1))
	return ret

def parseOptions(doc, opts):
	def repl_options(m):
		if m.group(1) is not None:
			for o in m.group(1).split(','):
				setattr(opts, o.strip(), True)
			return ''
	return RE_OPTIONS.sub(repl_options, doc)

def parseModule(mname, content):
	global cpos
	cpos = 0
@@ -139,43 +151,35 @@ def parseModule(mname, content):
	
	# parse types
	def repl_type (m, doc):
		title = typeName = m.group(1) # type name  
		global m_options
		title = t = m.group(1) # type name  
		f_params = {}
		s_unit = ''
		s_category = ''
		s_note = ''
		s_revision = ''
		options = copy.copy(o_args)

		# doc is the prepending comment. Check if not None and not Empty
		if doc : 
		options = copy.copy(m_options)
		if doc : # doc is the prepending comment. Check if not None and not Empty
			doc = parseDoxyComments(doc)
			
			# parse options
			def repl_options(m):
				nonlocal options
				if m.group(1) is not None:
					for o in m.group(1).split(','):
						setattr(options, o.strip().replace('-', '_'), True)
				return ''
			doc=RE_OPTIONS.sub(repl_options, doc)
			doc = parseOptions(doc, options)
		
			# parse @param, @details, @brief, etc...
			def repl_section (m):
				nonlocal title
				nonlocal typeName
				nonlocal t
				nonlocal f_params
				nonlocal s_note
				nonlocal options

				ret = ''
				l = m.group(4).lstrip(":, \t").lstrip('\n')
				if m.group(2) is not None:
					# this can be class|struct|details|param|field
					if m.group(3) == typeName:
					if m.group(3) == t:
						ret = parseText(l)
					else:
						if len(l):
							if len(f_params) == 0:
								ret = '--((FIELDS))--'
							f_params[m.group(3)] = parseText(l, 2)
							
				elif m.group(1) == 'brief':
@@ -183,47 +187,43 @@ def parseModule(mname, content):
						title = parseText(l)
					else:
						ret = parseText(l)
				
				elif m.group(1) == 'note':
					s_note = '\n>>>\n' + 'NOTE: ' + parseText(l).rstrip() + '\n>>>\n'
				
				else:
					ret = m.string[m.start():m.end()]
				return ret
			doc = RE_DOXY_SECTION.sub(repl_section, doc)

			# parse @category XXX [, YYY, ZZZ]
			def repl_category(m):
				nonlocal s_category
				s_category = '\n&nbsp;&nbsp;&nbsp;&nbsp;**Categories**: '
				if options.category_links:
					for l in m.group(1).split(','):
						s_category += '[{0}](#{1}) '.format(l.strip(), urlquote(l.strip()))
				else:
				for l in m.group(1).split(','):
						s_category += l.strip() + ' '
#					s_category += '[{0}](#{1}) '.format(l.strip(), urlquote(l.strip()))
					s_category += parseText(l).strip() + ' '
				s_category += '\n'
				return ''
			doc = RE_DOXY_CATEGORY.sub(repl_category, doc)

			def repl_unit(m):
				nonlocal s_unit
				s_unit = '\n&nbsp;&nbsp;&nbsp;&nbsp;**Unit**: _' + m.group(1).strip() + '_\n'
				s_unit = '\n&nbsp;&nbsp;&nbsp;&nbsp;**Unit**: _' + parseText(m.group(1)).strip() + '_\n'
				return ''
			doc = RE_DOXY_UNIT.sub(repl_unit, doc)

			def repl_revision(m):
				nonlocal s_revision
				s_revision = '\n&nbsp;&nbsp;&nbsp;&nbsp;**Revision**: _' + m.group(1).strip() + '_\n'
				s_revision = '\n&nbsp;&nbsp;&nbsp;&nbsp;**Revision**: _' + parseText(m.group(1)).strip() + '_\n'
				return ''
			doc = RE_DOXY_REVISION.sub(repl_revision, doc)
		else:
			doc = ''

		doc = [x.strip() for x in doc.split('--((FIELDS))--')]

		ret = ''
		if typeName is not None:
		if t is not None:
			fields = ''
			ret = '\n### <a name="{0}"></a>{1}\n'.format(typeName, title) + parseText(doc)
			ret = '\n### <a name="{0}"></a>{1}\n'.format(t, title) + parseText(doc[0])

			# parse fields and get out fields descriptions
			if m.group(3) is not None:
@@ -246,32 +246,33 @@ def parseModule(mname, content):
								if f in f_params:
									field = f_params.pop(f) + '\n\n'
								if fm.group(2) is not None:
									if len(field) or not options.no_auto_fields:
									fTitle = 'Fields:\n'
										fTypeName = fm.group(2).strip()
										if RE_BASIC_TYPES.match(fTypeName) is not None:
											field = '* {0} **{1}** {2}<br>\n'.format(f, fTypeName, ext) + field
									if len(field) or not options.no_auto_fields:
										t = fm.group(2).strip()
										if RE_BASIC_TYPES.match(t) is not None:
											field = '* {0} of type **{1}** {2}<br>\n'.format(f, t, ext) + field
										else:
											field = '* {0} [**{1}**]({2}#{1}) {3}<br>\n'.format(f, fTypeName, extTypes.get(fTypeName,''), ext) + field
											field = '* {0} of type [**{1}**]({2}#{1}) {3}<br>\n'.format(f, t, extTypes.get(t,''), ext) + field
									else:
										fTitle = 'Values:\n'
										if len(field) or not options.no_auto_values:
											field = '* **{0}** {1}<br>\n'.format(f, ext) + field
								if len(field):
									field += parseText(parseDoxyComments(fm.string[pos:fm.start()]).strip(), 3)
									field += parseText(fm.string[pos:fm.start()], 3)
								pos = fm.end()
								if fm.group(4) is not None:
									# keep '--' for the next round
									pos -= 2
							if len(field):
								fields += field
								field = ''
						if len(field):
							fields += parseInlineComments(typeBody[pos:], 3)
						# add all other fields defined as @params
						if 'force_all_fields' in options or 'force_all_fields' in o_args:
						if 'force-all-fields' in options:
							for f in f_params:
								fields += '* {}<br>\n{}\n\n'.format(f, f_params[f])							
						if 'no-fields-header' in options:
							fTitle = '' 
						if len(fields):
							ret = ret.strip() + '\n\n' + fTitle + fields
		else:
@@ -283,6 +284,12 @@ def parseModule(mname, content):
			for p in f_params:
				ret += '* `{0}` {1}\n'.format(p, f_params[p])
			
		try:
			if len(doc[1]):
				ret += doc[1] + '\n'
		except:
			pass

		return ret + s_unit + s_category + s_revision + s_note + '```asn1\n' + RE_COMMENTS.sub('', m.group(0).strip()) +'\n```\n\n'

	pos = 0
@@ -295,23 +302,17 @@ def parseModule(mname, content):

def parseAsn(outDir, content) :
	# iterate modules in the file
	global m_options
	pos= 0
	cnt = 0
	m_options = copy.copy(o_args)
	for m in RE_MODULE.finditer(content):
		ret = '# ASN.1 module {}\n OID: _{}_\n'.format(m.group(1), RE_SPACES.sub(' ', m.group(2)))
		ret += parseDoxyComments(content[pos:m.start()]) + '\n'
		ret += parseText(parseOptions(parseDoxyComments(content[pos:m.start()]), m_options)) + '\n'
		if m.group(3) is not None:
			ret += parseModule(m.group(1), m.group(3))
		ret += '\n\n'
		try:
			f = open(outDir + '/' + m.group(1) + '.md', mode='w',encoding='utf-8')
			f.write(ret)
		except OSError as err:
			print("OS error: {0}".format(err))
		except BaseException as err:
			print(f"Unexpected {err=}, {type(err)=}")
		finally:
			f.close()
		open(outDir + '/' + m.group(1) + '.md', "w",encoding='utf-8').write(ret)
		pos = m.end()
		cnt += 1
	return cnt
@@ -324,7 +325,7 @@ def main():
	ap.add_argument('--force-all-fields', '-f', default=False,action='store_true', help='Add all fields in the list even if empty')
	ap.add_argument('--no-auto-fields', '-F', default=False,action='store_true', help='Add fields only if @param or @field is defined')
	ap.add_argument('--no-auto-values', '-V', default=False,action='store_true', help='Do not add named values or enums')
	ap.add_argument('--category-links', '-c', default=False,action='store_true', help='Create links for categories')
	ap.add_argument('--no-fields-header', '-H', default=False,action='store_true', help='Do not add fields and values header')
	ap.add_argument('modules', action='store', nargs='+', help='ASN.1 files')
	o_args = ap.parse_args()