~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/lib2to3/patcomp.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import os
15
15
 
16
16
# Fairly local imports
17
 
from .pgen2 import driver
18
 
from .pgen2 import literals
19
 
from .pgen2 import token
20
 
from .pgen2 import tokenize
 
17
from .pgen2 import driver, literals, token, tokenize, parse, grammar
21
18
 
22
19
# Really local imports
23
20
from . import pytree
28
25
                                     "PatternGrammar.txt")
29
26
 
30
27
 
 
28
class PatternSyntaxError(Exception):
 
29
    pass
 
30
 
 
31
 
31
32
def tokenize_wrapper(input):
32
33
    """Tokenizes a string suppressing significant whitespace."""
33
 
    skip = (token.NEWLINE, token.INDENT, token.DEDENT)
 
34
    skip = set((token.NEWLINE, token.INDENT, token.DEDENT))
34
35
    tokens = tokenize.generate_tokens(driver.generate_lines(input).next)
35
36
    for quintuple in tokens:
36
37
        type, value, start, end, line_text = quintuple
54
55
    def compile_pattern(self, input, debug=False):
55
56
        """Compiles a pattern string to a nested pytree.*Pattern object."""
56
57
        tokens = tokenize_wrapper(input)
57
 
        root = self.driver.parse_tokens(tokens, debug=debug)
 
58
        try:
 
59
            root = self.driver.parse_tokens(tokens, debug=debug)
 
60
        except parse.ParseError as e:
 
61
            raise PatternSyntaxError(str(e))
58
62
        return self.compile_node(root)
59
63
 
60
64
    def compile_node(self, node):
133
137
        assert len(nodes) >= 1
134
138
        node = nodes[0]
135
139
        if node.type == token.STRING:
136
 
            value = literals.evalString(node.value)
137
 
            return pytree.LeafPattern(content=value)
 
140
            value = unicode(literals.evalString(node.value))
 
141
            return pytree.LeafPattern(_type_of_literal(value), value)
138
142
        elif node.type == token.NAME:
139
143
            value = node.value
140
144
            if value.isupper():
141
145
                if value not in TOKEN_MAP:
142
 
                    raise SyntaxError("Invalid token: %r" % value)
 
146
                    raise PatternSyntaxError("Invalid token: %r" % value)
 
147
                if nodes[1:]:
 
148
                    raise PatternSyntaxError("Can't have details for token")
143
149
                return pytree.LeafPattern(TOKEN_MAP[value])
144
150
            else:
145
151
                if value == "any":
147
153
                elif not value.startswith("_"):
148
154
                    type = getattr(self.pysyms, value, None)
149
155
                    if type is None:
150
 
                        raise SyntaxError("Invalid symbol: %r" % value)
 
156
                        raise PatternSyntaxError("Invalid symbol: %r" % value)
151
157
                if nodes[1:]: # Details present
152
158
                    content = [self.compile_node(nodes[1].children[1])]
153
159
                else:
173
179
             "TOKEN": None}
174
180
 
175
181
 
 
182
def _type_of_literal(value):
 
183
    if value[0].isalpha():
 
184
        return token.NAME
 
185
    elif value in grammar.opmap:
 
186
        return grammar.opmap[value]
 
187
    else:
 
188
        return None
 
189
 
 
190
 
176
191
def pattern_convert(grammar, raw_node_info):
177
192
    """Converts raw node information to a Node or Leaf instance."""
178
193
    type, value, context, children = raw_node_info