Commits

david_walker committed d4b6dff

handle quoted parenthesis
add has_parent method to node class

Comments (0)

Files changed (1)

         self._server = xmlrpclib.ServerProxy(Parser.SERVER_URL)
 
     def _build_tree(self, root, it, tokens):
-        # TODO: handle embedded parens
         s = ''
+        prev_char = ''
+        in_quotes = False
         for c in it:
-            if c == '(':
-                if s:
-                    root += self._make_list(s, tokens)
-                    s = ''
-                # create a new child of current parent
-                root.append(self._build_tree([], it, tokens))
-            elif c != ')':
+            # Detect transition between quoted and unquoted sections of
+            # text. Escaped quotes do not effect a transition.
+            if c == '"' and not prev_char == '\\':
+                in_quotes = not in_quotes
+
+            if in_quotes:
+                # parens are not significant within quoted strings
                 s += c
-            else:  # c == ')'
-                if s:
-                    root += self._make_list(s, tokens)
-                break
+            else:  # not within a quoted string
+                if c == '(':
+                    if s:
+                        root += self._make_list(s, tokens)
+                        s = ''
+                    # create a new child of current parent
+                    root.append(self._build_tree([], it, tokens))
+                elif c != ')':
+                    s += c
+                else: # c == ')'
+                    if s:
+                        root += self._make_list(s, tokens)
+                    break
+            prev_char = c
         return root
 
     def _create_pet_input_chart(self, tokens):
             else:
                 child._pprint(indent + 2)
 
+    def has_parent(self, name):
+        """Return True if this node has an ancestor with the given name,
+        False otherwise."""
+        if self._parent:
+            if self._parent.name == name:
+                return True
+            return self._parent.has_parent(name)
+        return False
+
     def node_from_token(self, token):
         """Return the ParseNode whose child is `token`."""
         # if children list is a leaf node, i.e. a Token, then either