1. Eric Snow
  2. pylt

Commits

Eric Snow  committed c45ff2a

getting the parsing module started

  • Participants
  • Parent commits 74512b8
  • Branches default

Comments (0)

Files changed (2)

File pylt/parsing/__init__.py

Empty file added.

File pylt/parsing/python.py

View file
+"""
+
+Based on: http://docs.python.org/dev/reference/grammar.html
+----
+
+file_input  => (NEWLINE | stmt)* ENDMARKER
+stmt        => simple_stmt | compound_stmt
+
+simple_stmt => small_stmt (';' small_stmt)* [';'] NEWLINE
+small_stmt  => (! delimiter) *
+
+compound_stmt => compound_preample ':' suite
+compound_preample => (! delimiter)+
+suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
+
+delimiter  => (';' | ':' | INDENT | DEDENT | NEWLINE | ENDMARKER)
+
+"""
+
+import token
+
+
+class ParseUnit:
+
+    def __init__(self, tokens):
+        self._tokens = list(tokens)
+
+    def get_tokens(self):
+        yield from iter(self._tokens)
+
+
+class Parser:
+    """A simple, customizable Python parser."""
+
+    def parse_source(tokens):
+        current = []
+        for tok in tokens:
+            current.append(tok)
+            if tok is token.ENDMARKER:
+                yield ParseUnit(current)
+                break
+
+            elif tok is token.COLON:
+                yield CompoundStatement(current, self.parse_suite(tokens))
+                current = []
+
+            elif tok is token.SEMI:
+                yield SimpleStatment(current)
+                yield from self.parse_simple_statements(tokens)
+                current = []
+
+            else:
+                # don't reset current
+                continue
+
+            current = []
+
+    def parse_suite(self, tokens):
+        ...
+
+    def parse_simple_statements(self, tokens):
+        ...