stackish.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # stackish.py
  2. #
  3. # Stackish is a data representation syntax, similar to JSON or YAML. For more info on
  4. # stackish, see http://www.savingtheinternetwithhate.com/stackish.html
  5. #
  6. # Copyright 2008, Paul McGuire
  7. #
  8. """
  9. NUMBER A simple integer type that's just any series of digits.
  10. FLOAT A simple floating point type.
  11. STRING A string is double quotes with anything inside that's not a " or
  12. newline character. You can include \n and \" to include these
  13. characters.
  14. MARK Marks a point in the stack that demarcates the boundary for a nested
  15. group.
  16. WORD Marks the root node of a group, with the other end being the nearest
  17. MARK.
  18. GROUP Acts as the root node of an anonymous group.
  19. ATTRIBUTE Assigns an attribute name to the previously processed node.
  20. This means that just about anything can be an attribute, unlike in XML.
  21. BLOB A BLOB is unique to Stackish and allows you to record any content
  22. (even binary content) inside the structure. This is done by pre-
  23. sizing the data with the NUMBER similar to Dan Bernstein's netstrings
  24. setup.
  25. SPACE White space is basically ignored. This is interesting because since
  26. Stackish is serialized consistently this means you can use \n as the
  27. separation character and perform reasonable diffs on two structures.
  28. """
  29. from pyparsing import Suppress,Word,nums,alphas,alphanums,Combine,oneOf,\
  30. Optional,QuotedString,Forward,Group,ZeroOrMore,srange
  31. MARK,UNMARK,AT,COLON,QUOTE = map(Suppress,"[]@:'")
  32. NUMBER = Word(nums)
  33. NUMBER.setParseAction(lambda t:int(t[0]))
  34. FLOAT = Combine(oneOf("+ -") + Word(nums) + "." + Optional(Word(nums)))
  35. FLOAT.setParseAction(lambda t:float(t[0]))
  36. STRING = QuotedString('"', multiline=True)
  37. WORD = Word(alphas,alphanums+"_:")
  38. ATTRIBUTE = Combine(AT + WORD)
  39. strBody = Forward()
  40. def setBodyLength(tokens):
  41. strBody << Word(srange(r'[\0x00-\0xffff]'), exact=int(tokens[0]))
  42. return ""
  43. BLOB = Combine(QUOTE + Word(nums).setParseAction(setBodyLength) +
  44. COLON + strBody + QUOTE)
  45. item = Forward()
  46. def assignUsing(s):
  47. def assignPA(tokens):
  48. if s in tokens:
  49. tokens[tokens[s]] = tokens[0]
  50. del tokens[s]
  51. return assignPA
  52. GROUP = (MARK +
  53. Group( ZeroOrMore(
  54. (item +
  55. Optional(ATTRIBUTE)("attr")
  56. ).setParseAction(assignUsing("attr"))
  57. )
  58. ) +
  59. ( WORD("name") | UNMARK )
  60. ).setParseAction(assignUsing("name"))
  61. item << (NUMBER | FLOAT | STRING | BLOB | GROUP )
  62. tests = """\
  63. [ '10:1234567890' @name 25 @age +0.45 @percentage person:zed
  64. [ [ "hello" 1 child root
  65. [ "child" [ 200 '4:like' "I" "hello" things root
  66. [ [ "data" [ 2 1 ] @numbers child root
  67. [ [ 1 2 3 ] @test 4 5 6 root
  68. """.splitlines()
  69. for test in tests:
  70. if test:
  71. print(test)
  72. print(item.parseString(test).dump())
  73. print()