genperf.py 861 B

123456789101112131415161718192021222324252627282930
  1. import sys, re
  2. gperf_output = sys.stdin.read()
  3. hash_func = re.search(
  4. "static unsigned int\s+hash\s+\((.*?)\)\s{(.*?)\n}",
  5. gperf_output, re.DOTALL)
  6. if not hash_func:
  7. raise "Failed to detect hash function in GPerf output"
  8. wordlist = re.search(
  9. "wordlist\[\]\s+=\s+{(.*?)}",
  10. gperf_output, re.DOTALL)
  11. if not wordlist:
  12. raise "Failed to detect word list in GPerf output"
  13. def process_wordlist(text):
  14. wordlist = [w.strip().replace('"', '') for w in text.split(',')]
  15. taglist = [
  16. "\tGUMBO_TAG_" + (w.upper().replace('-', '_') if w else 'LAST')
  17. for w in wordlist]
  18. return taglist
  19. print "static unsigned int tag_hash(%s)\n{%s\n}" % (
  20. hash_func.group(1), hash_func.group(2))
  21. print ""
  22. print "static const unsigned char kGumboTagMap[] = {\n%s\n};" % (
  23. ',\n'.join(process_wordlist(wordlist.group(1))))