~jasper/type_stack_calc

ref: 6aca66fb1a7cf1e10f7dd3263f04537c8ff37590 type_stack_calc/type_stack_calc/sc_parser.py -rw-r--r-- 1.6 KiB
6aca66fb — Jasper den Ouden Apparently `to_c` can already read ahead one, hopefully its this simple... 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Fairly simple parser."""

def whitespace_split(code):
    for ln in code.split('\n'):
        i = ln.find('#')
        for c in (ln if i==-1 else ln[:i]).split():
            if c!='': yield c

def parser(code):
    """Splits on whitespace, `.` reads in a componet-accessing symbol. Identifies strings, comments. (comments currently entirely removed)
Non- `_` or `.isalnum()` characters from single-characters symbols."""
    s, n = code, 0
    while len(s) > 0:
        if s[0] in (' ', '\t'):
            s = s[1:]
        elif s[0].isdigit() or s[0] == '-' and s[1:2].isdigit(): # Numbers.
            i = 1
            while i < len(s) and s[i].isdigit(): i += 1

            if not i ==len(s) and s[i] == '.':  # Floating point.
                i += 1
                while i < len(s) and s[i].isdigit(): i += 1
            yield s[:i]
            s = s[i:]
        elif s[0] in ('.', '_', '!') or s[0].isalnum():  # Symbol
            i = 1  # Ends with non-alnum/non-underscore.
            while i < len(s) and (s[i].isalnum() or s[i] == '_'):
                i += 1
            yield s[:i]
            s = s[i:]
        elif s[0] == '"':  # String.
            i = 1  # Ends with other ".
            while i < len(s) and s[i] != '"':
                if s[i] == '\\': i += 1   # Escapable.
                i += 1
            yield s[:i]
            s = s[i+1:]
        elif s[0] == '#':  # Comment.
            i = 1  # Ends with newline.
            while i < len(s) and s[i] != '\n':
                i += 1
            s = s[i+1:]
        else:  # Single character thing.
            yield s[0]
            s = s[1:]

        n += 1