From a779aac9db96b05acb2c4e1b62417bbf37f160f8 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 28 Apr 2012 17:12:39 +1200 Subject: Make specification language more terse, and more regular. --- libpathod/rparse.py | 27 +++++++++++----- notes | 91 +++++++++++++++-------------------------------------- test/test_rparse.py | 57 +++++++++++++++++---------------- 3 files changed, 73 insertions(+), 102 deletions(-) diff --git a/libpathod/rparse.py b/libpathod/rparse.py index 5c6d0257..18d05a69 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -15,6 +15,9 @@ class ParseException(Exception): def marked(self): return "%s\n%s"%(self.s, " "*(self.col-1) + "^") + def __str__(self): + return self.msg + class ServerError(Exception): pass @@ -41,16 +44,17 @@ v_integer = pp.Regex(r"[+-]?\d+")\ .setName("integer")\ .setParseAction(lambda toks: int(toks[0])) -v_string = pp.MatchFirst( + +v_literal = pp.MatchFirst( [ pp.QuotedString("\"", escChar="\\", unquoteResults=True), pp.QuotedString("'", escChar="\\", unquoteResults=True), ] ) -v_literal = pp.MatchFirst( +v_naked_literal = pp.MatchFirst( [ - v_string, + v_literal, pp.Word("".join(i for i in pp.printables if i not in ",:")) ] ) @@ -121,6 +125,13 @@ class ValueLiteral: return self.val +class ValueNakedLiteral(ValueLiteral): + @classmethod + def expr(klass): + e = v_naked_literal.copy() + return e.setParseAction(lambda x: klass(*x)) + + class ValueGenerate: UNITS = dict( b = 1024**0, @@ -163,7 +174,7 @@ class ValueFile: @classmethod def expr(klass): e = pp.Literal("<").suppress() - e = e + v_literal + e = e + v_naked_literal return e.setParseAction(lambda x: klass(*x)) def get_generator(self, settings): @@ -197,7 +208,7 @@ class Body: @classmethod def expr(klass): - e = pp.Literal("b:").suppress() + e = pp.Literal("b").suppress() e = e + Value return e.setParseAction(lambda x: klass(*x)) @@ -208,7 +219,7 @@ class _Pause: @classmethod def expr(klass): - e = pp.Literal("p%s:"%klass.sub).suppress() + e = pp.Literal("p%s"%klass.sub).suppress() e = e + pp.MatchFirst( [ v_integer, @@ -273,7 +284,7 @@ class Header: @classmethod def expr(klass): - e = pp.Literal("h:").suppress() + e = pp.Literal("h").suppress() e += Value e += pp.Literal(":").suppress() e += Value @@ -294,7 +305,7 @@ class Code: def expr(klass): e = v_integer e = e + pp.Optional( - pp.Literal(":").suppress() + Value + Value ) return e.setParseAction(lambda x: klass(*x)) diff --git a/notes b/notes index 0513cb42..bd315ebe 100644 --- a/notes +++ b/notes @@ -1,92 +1,53 @@ -Value Specifiers: - - !500k - 500k of random data - !500k:utf8 - 500k of utf8. Other specifiers: utf8,alphanum,alpha,printable - "foo" - literal - foo - literal - destination - code>destination - - 301:VALUE>VALUE - 301>http://foo.bar - - - Content-type: - - t:content-type + !500k - 500k of random data + !500k:utf8 - 500k of utf8. Other specifiers: utf8,alphanum,alpha,printable + "foo" - literal +