Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 KB | None | 0 0
  1. import json
  2. import weakref
  3.  
  4. from uc.io.proto.parser import *
  5.  
  6. __all__ = ["dogparser"]
  7.  
  8. class dogparser (parser):
  9.     other = [
  10.         (r"\[", "other::block::start,;string::literal,list"),
  11.         (r"\]", "other::block::end,"),
  12.         (r"\|", "other::whitespace,")
  13.     ]
  14.     def __init__ (self):
  15.         super(dogparser, self).__init__(
  16.             (r'^#.*',                  "comment"),
  17.             (r'(?<=\s)#.*',            "comment"),
  18.             (r"\{",                    "block::start"),
  19.             (r"\}",                    "block::end"),
  20.             (r"\;",                    "block::split"),
  21.             (r"\d{1,}#[\da-f]*",       "number::withbase"),
  22.             (r"\d{1,}",                "number::decimal"),
  23.             (r"'(.*?[^\\]|)'"  ,       "string::raw"),
  24.             (r'"(.*?[^\\]|)"',         "string::json"),
  25.             (r'[^\s\'"\{:]{1,}:(?!:)', "string::keyword"),
  26.             (r'[^\s\'"\{\}\;\d]{1,}',  "string::literal"),
  27.             (r'\s{1,}',                "whitespace")
  28.         )
  29.         self.insert(2, *self.other)
  30.         self.ignore |= {'comment', 'whitespace'}
  31.     def apply (self, text):
  32.         for token in super(dogparser, self).apply(text):
  33.             if token.name.startswith("other::"):
  34.                 tokens = token.name.split("::", 1)[1].split(";")
  35.                 for subtoken in tokens:
  36.                     token.name, token.value = subtoken.split(",", 1)
  37.                     yield token
  38.             else: yield token
  39.     def parse (self, text):
  40.         return self.tostruct(self.apply(text))
  41.     @classmethod
  42.     def tostruct (cls, tokens, parent=None):
  43.         block = cls.block(parent)
  44.         entry = cls.item()
  45.         kw = None
  46.         for token in tokens:
  47.             arg = None
  48.             if token == "block::start": arg = cls.tostruct(tokens, block)
  49.             elif token.name.startswith("block::"):
  50.                 if entry.args: block.append(entry)
  51.                 entry, kw = cls.item(), None
  52.                 if token == "block::end": break
  53.             elif token == "number::withbase":
  54.                 base, num = token.value.split("#")
  55.                 arg = int(num, int(base))
  56.             elif token == "number::decimal": arg = int(token.value)
  57.             elif token == "string::raw": arg = token.value[1:-1]
  58.             elif token == "string::json": arg = json.loads(token.value)
  59.             elif token == "string::literal": arg = entry.link(token.value)
  60.             elif token == "string::keyword": kw = token.value[:-1]
  61.             if arg:
  62.                 if kw: entry.kwargs[kw] = arg
  63.                 else: entry.args.append(arg)
  64.                 kw = None
  65.         return block
  66.  
  67.     class block (list):
  68.         def __init__ (self, parent, *entries):
  69.             super(dogparser.block, self).__init__(entries)
  70.             self.parent = lambda: None if parent is None else weakref.ref(parent)
  71.         def __repr__ (self):
  72.             hp = bool(self.parent())
  73.             res = "{" if hp else ""
  74.             for cmd in self:
  75.                 res += str(cmd) + "; "
  76.             if hp: res = res.rstrip("; ") + "}"
  77.             return res
  78.  
  79.     class item (object):
  80.         def __init__ (self, *args, **kwargs):
  81.             super(dogparser.item, self).__init__()
  82.             self.args = list(args)
  83.             self.kwargs = kwargs
  84.         def __repr__ (self):
  85.             res  = "dogparser.item("
  86.             res += ", ".join(repr(s) for s in self.args)
  87.             if self.kwargs:
  88.                 res += ", "
  89.                 res += ", ".join("{!s}={!r}".format(*i) for i in self.kwargs.items())
  90.             return res + ")"
  91.         def __repr__ (self):
  92.             res = " ".join(repr(a) for a in self.args)
  93.             if self.kwargs:
  94.                 res += " "
  95.                 res += " ".join("{!s}: {!r}".format(*i) for i in self.kwargs.items())
  96.             return res
  97.  
  98.         class link (object):
  99.             def __init__ (self, to):
  100.                 super(dogparser.item.link, self).__init__()
  101.                 self.to = to
  102.             def __repr__ (self):
  103.                 return str(self.to)
  104.                 return "@" + repr(self.to)
  105.  
  106. if __name__ == "__main__":
  107.     from sys import stdin
  108.     p = dogparser()
  109.     while True:
  110.         data = stdin.read() or exit(0)
  111.         print(p.parse(data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement