Commits

Eric Snow committed cb60179

stepping toward sensible parsing of Python statements

Comments (0)

Files changed (1)

pylt/parsing/python.py

         yield from iter(self._tokens)
 
 
+class SimpleStatement(ParseUnit):
+    pass
+
+
+class CompoundStatement(ParseUnit):
+    pass
+
+
 class Parser:
     """A simple, customizable Python parser."""
 
         current = []
         for tok in tokens:
             current.append(tok)
-            if tok is token.ENDMARKER:
+
+            if tok.type is token.ENDMARKER:
                 if current != [tok]:
-                    raise ParseError
+                    raise ParseError(tok)
                 yield ParseUnit(current)
                 break
 
-            elif tok is token.SEMI:
+            elif tok.type is token.SEMI:
                 yield SimpleStatment(current)
                 yield from self.parse_simple_statements(tokens)
                 current = []
                 # don't reset current
                 continue
 
-            current = []
-
     def parse_suite(self, tokens):
         try:
             tok = next(tokens)
         current = []
         for tok in tokens:
             current.append(tok)
-            if tok is token.ENDMARKER:
+
+            if tok.type is token.ENDMARKER:
+                raise ParseError(tok)
+
+            elif tok.type is token.DEDENT:
                 if current != [tok]:
-                    raise ParseError
+                    raise ParseError(tok)
                 yield ParseUnit(current)
                 break
 
-            elif tok is token.SEMI:
+            elif tok.type is token.SEMI:
                 yield SimpleStatment(current)
                 yield from self.parse_simple_statements(tokens)
                 current = []
 
-            elif tok is token.NEWLINE:
+            elif tok.type is token.NEWLINE:
                 if len(current) > 1 and current[-2] == token.COLON:
                     yield CompoundStatement(current, self.parse_suite(tokens))
                 else:
                 # don't reset current
                 continue
 
-            current = []
+    def parse_simple_statements(self, tokens):
+        current = []
+        for tok in tokens:
+            current.append(tok)
 
-    def parse_simple_statements(self, tokens):
-        ...
+            if tok.type in [token.ENDMARKER, token.INDENT, token.DEDENT]:
+                raise ParseError(tok)
+
+            elif tok.type is token.SEMI:
+                yield SimpleStatment(current)
+                current = []
+
+            elif tok.type is token.NEWLINE:
+                yield SimpleStatment(current)
+                break
+
+            else:
+                # don't reset current
+                continue