1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import re
20 from qpid.lexer import Lexicon, LexError
21 from qpid.parser import Parser, ParseError
22
23 l = Lexicon()
24
25 LBRACE = l.define("LBRACE", r"\{")
26 RBRACE = l.define("RBRACE", r"\}")
27 LBRACK = l.define("LBRACK", r"\[")
28 RBRACK = l.define("RBRACK", r"\]")
29 COLON = l.define("COLON", r":")
30 SEMI = l.define("SEMI", r";")
31 SLASH = l.define("SLASH", r"/")
32 COMMA = l.define("COMMA", r",")
33 NUMBER = l.define("NUMBER", r'[+-]?[0-9]*\.?[0-9]+')
34 ID = l.define("ID", r'[a-zA-Z_](?:[a-zA-Z0-9_-]*[a-zA-Z0-9_])?')
35 STRING = l.define("STRING", r""""(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*'""")
36 ESC = l.define("ESC", r"\\[^ux]|\\x[0-9a-fA-F][0-9a-fA-F]|\\u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]")
37 SYM = l.define("SYM", r"[.#*%@$^!+-]")
38 WSPACE = l.define("WSPACE", r"[ \n\r\t]+")
39 EOF = l.eof("EOF")
40
41 LEXER = l.compile()
42
45
47 if tok.type is STRING:
48 return eval(tok.value)
49 elif tok.type is ESC:
50 if tok.value[1] == "x":
51 return eval('"%s"' % tok.value)
52 elif tok.value[1] == "u":
53 return eval('u"%s"' % tok.value)
54 else:
55 return tok.value[1]
56 else:
57 return tok.value
58
59 CONSTANTS = {
60 "True": True,
61 "true": True,
62 "False": False,
63 "false": False,
64 "None": None
65 }
66
68 if tok.type == ID:
69 return CONSTANTS.get(tok.value, tok.value)
70 elif tok.type in (STRING, NUMBER):
71 return eval(tok.value)
72 else:
73 return tok.value
74
76 if toks:
77 return "".join(map(tok2str, toks))
78 else:
79 return None
80
82
85
87 result = self.address()
88 self.eat(EOF)
89 return result
90
92 name = toks2str(self.eat_until(SLASH, SEMI, EOF))
93
94 if name is None:
95 raise ParseError(self.next())
96
97 if self.matches(SLASH):
98 self.eat(SLASH)
99 subject = toks2str(self.eat_until(SEMI, EOF))
100 else:
101 subject = None
102
103 if self.matches(SEMI):
104 self.eat(SEMI)
105 options = self.map()
106 else:
107 options = None
108 return name, subject, options
109
111 self.eat(LBRACE)
112
113 result = {}
114 while True:
115 if self.matches(NUMBER, STRING, ID, LBRACE, LBRACK):
116 n, v = self.keyval()
117 result[n] = v
118 if self.matches(COMMA):
119 self.eat(COMMA)
120 elif self.matches(RBRACE):
121 break
122 else:
123 raise ParseError(self.next(), COMMA, RBRACE)
124 elif self.matches(RBRACE):
125 break
126 else:
127 raise ParseError(self.next(), NUMBER, STRING, ID, LBRACE, LBRACK,
128 RBRACE)
129
130 self.eat(RBRACE)
131 return result
132
134 key = self.value()
135 self.eat(COLON)
136 val = self.value()
137 return (key, val)
138
148
150 self.eat(LBRACK)
151
152 result = []
153
154 while True:
155 if self.matches(RBRACK):
156 break
157 else:
158 result.append(self.value())
159 if self.matches(COMMA):
160 self.eat(COMMA)
161 elif self.matches(RBRACK):
162 break
163 else:
164 raise ParseError(self.next(), COMMA, RBRACK)
165
166 self.eat(RBRACK)
167 return result
168
170 return AddressParser(lex(addr)).parse()
171
172 __all__ = ["parse", "ParseError"]
173