text: #!/usr/bin/env python
  
  from __future__ import print_function
  
  import os, sys, json
  
  def produce(f, *keywords):
  	out = {}
  	for k in keywords:
  		out[k] = {}
  
  	type = None
  	key = None
  	value = None
  
  	ln = 0
  	for line in f:
  		ln += 1
  		line = line.strip()
  		if line == '%%':
  			if type is not None and type != "" and len(value) > 0:
  				out[type][key] = value[0]
  			type = None
  			key = None
  			value = None
  			continue
  		pair = line.split(":", 1)
  		if len(pair) != 2: continue
  
  		pair[0] = pair[0].strip()
  		pair[1] = pair[1].strip()
  
  		if pair[0] == "Type":
  			if pair[1] in keywords: type = pair[1]
  			else: type = ""
  		elif pair[0] == "Subtag":
  			if type is None: print("line %s: error: no type in record" % ln)
  			elif type == "": continue
  			key = pair[1]
  			value = []
  		elif pair[0] == "Description":
  			if type is None: print("line %s: error: no type in record" % ln)
  			elif type == "": continue
  			if key is None: print("line %s: error: no subtag in record" % ln)
  			if pair[1] == "Private use":
  				type = ""
  			else:
  				value.append(pair[1])
  		elif pair[0] == "Scope":
  			type = ""
  		elif pair[0] == "File-Date":
  			out['File-Date'] = pair[1]
  
  	if type is not None and type != "":
  		out[type][key] = value
  
  	return out
  
  # src: http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
  with open("language-subtag-registry.txt") as f:
  	list = produce(f, "language", "script", "region")
  
  	scripts = sorted(list["script"].keys())
  	del list["script"]
  
  	file_date = list['File-Date']
  	del list['File-Date']
  
  	with open("lang_codes.cpp", "w") as out:
  		print( "// THIS FILE IS AUTOGENERATED", file=out)
  		print( "// DATA SRC: http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry", file=out)
  		print( "// FILE DATE: %s" % file_date, file=out)
  		print( "// clang-format off", file=out)
  		print("""
  namespace {
      struct token {
          const char* key;
          const char* value;
      };
  """, file=out)
  		for key in sorted(list.keys()):
  			print("    token %ss[] = {" % key, file=out)
  			for sub in sorted(list[key].keys()):
  				print("        { u8%s, u8%s }," % (json.dumps(sub), json.dumps(list[key][sub])), file=out)
  			print("    };", file=out)
  			print(file=out)
  		print("    const char* scripts[] = {", file=out)
  		for script in scripts:
  			print("        u8%s," % (json.dumps(script),), file=out)
  		out.write("""    };
  }
  // clang-format on
  
  #include "lang_codes.inl"
  """)