Utils.py 931 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  2. # Use of this file is governed by the BSD 3-clause license that
  3. # can be found in the LICENSE.txt file in the project root.
  4. #
  5. from io import StringIO
  6. def str_list(val):
  7. with StringIO() as buf:
  8. buf.write('[')
  9. first = True
  10. for item in val:
  11. if not first:
  12. buf.write(', ')
  13. buf.write(str(item))
  14. first = False
  15. buf.write(']')
  16. return buf.getvalue()
  17. def escapeWhitespace(s:str, escapeSpaces:bool):
  18. with StringIO() as buf:
  19. for c in s:
  20. if c==' ' and escapeSpaces:
  21. buf.write('\u00B7')
  22. elif c=='\t':
  23. buf.write("\\t")
  24. elif c=='\n':
  25. buf.write("\\n")
  26. elif c=='\r':
  27. buf.write("\\r")
  28. else:
  29. buf.write(c)
  30. return buf.getvalue()