1. Cat's Eye Technologies
  2. Xoomonk

Commits

Cat's Eye Technologies  committed bed3070

Implement scanner and tests for it.

  • Participants
  • Parent commits 4a4228c
  • Branches default

Comments (0)

Files changed (1)

File src/xoomonk.py

View file
  • Ignore whitespace
 import re
 
 class Scanner(object):
+    """
+    >>> a = Scanner("  {:= }  foo  ")
+    >>> a.token
+    '{'
+    >>> a.type
+    'SYMBOL'
+    >>> a.scan()
+    >>> a.on(":=")
+    True
+    >>> a.on_type('SYMBOL')
+    True
+    >>> a.scan()
+    >>> a.consume(".")
+    False
+    >>> a.consume("}")
+    True
+    >>> a.expect("foo")
+    >>> a.type
+    'EOF'
+    >>> a.expect("bar")
+    Traceback (most recent call last):
+    ...
+    SyntaxError: expected bar
+
+    """
     def __init__(self, text):
         self.text = text
+        self.token = None
+        self.type = None
         self.scan()
 
     def scan_pattern(self, pattern):
 
     def scan(self):
         self.scan_pattern(r'\s+')
-        # := ; { } * . ^ $
-        
+        if not self.text:
+            self.token = None
+            self.type = 'EOF'
+            return
+        if self.scan_pattern(r':=|\;|\{|\}|\*|\.|\^|\$'):
+            self.type = 'SYMBOL'
+            return
+        if self.scan_pattern(r'\d+'):
+            self.type = 'INTLIT'
+            return
+        if self.scan_pattern(r'\w+'):
+            self.type = 'IDENT'
+            return
+
     def expect(self, token):
         if self.token == token:
             self.scan()
     def on(self, token):
         return self.token == token
 
+    def on_type(self, type):
+        return self.type == type
+
     def consume(self, token):
         if self.token == token:
             self.scan()
         v = None
         if self.scanner.on("{"):
             v = self.block()
-        elif self.scanner.on_pattern(r'\d+'):
+        elif self.scanner.on_type('INTLIT'):
             v = int(self.token)
         else:
             v = self.ref()