From 747eafd10703d5a93003e410469771f883df68b5 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 29 Oct 2012 16:31:35 +1300 Subject: Add a .spec method to the token ABC, and to all tokens. --- libpathod/language.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'libpathod') diff --git a/libpathod/language.py b/libpathod/language.py index 6fa39f6a..2dceefa5 100644 --- a/libpathod/language.py +++ b/libpathod/language.py @@ -309,6 +309,12 @@ class _Spec(object): """ return None + def spec(self): # pragma: no cover + """ + A parseable specification for this token. + """ + return None + class Raw(_Spec): @classmethod @@ -316,6 +322,9 @@ class Raw(_Spec): e = pp.Literal("r").suppress() return e.setParseAction(lambda x: klass(*x)) + def spec(self): + return "r" + class _Component(_Spec): """ @@ -357,6 +366,9 @@ class Header(_Header): e += Value return e.setParseAction(lambda x: klass(*x)) + def spec(self): + return "h%s=%s"%(self.key.spec(), self.value.spec()) + class ShortcutContentType(_Header): def __init__(self, value): @@ -368,6 +380,9 @@ class ShortcutContentType(_Header): e = e + Value return e.setParseAction(lambda x: klass(*x)) + def spec(self): + return "c%s"%(self.value.spec()) + class ShortcutLocation(_Header): def __init__(self, value): @@ -379,6 +394,9 @@ class ShortcutLocation(_Header): e = e + Value return e.setParseAction(lambda x: klass(*x)) + def spec(self): + return "l%s"%(self.value.spec()) + class Body(_Component): def __init__(self, value): @@ -395,6 +413,9 @@ class Body(_Component): self.value.get_generator(settings), ] + def spec(self): + return "b%s"%(self.value.spec()) + class Path(_Component): def __init__(self, value): @@ -412,6 +433,9 @@ class Path(_Component): self.value.get_generator(settings), ] + def spec(self): + return "%s"%(self.value.spec()) + class Method(_Component): methods = [ @@ -445,6 +469,12 @@ class Method(_Component): self.value.get_generator(settings) ] + def spec(self): + s = self.value.spec() + if s[1:-1].lower() in self.methods: + s = s[1:-1].lower() + return "%s"%s + class Code(_Component): def __init__(self, code): @@ -458,12 +488,14 @@ class Code(_Component): def values(self, settings): return [LiteralGenerator(self.code)] + def spec(self): + return "%s"%(self.code) + class Reason(_Component): def __init__(self, value): self.value = value - @classmethod def expr(klass): e = pp.Literal("m").suppress() @@ -473,6 +505,9 @@ class Reason(_Component): def values(self, settings): return [self.value.get_generator(settings)] + def spec(self): + return "m%s"%(self.value.spec()) + class _Action(_Spec): """ -- cgit v1.2.3