File size: 11,081 Bytes
62150ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# parser/parser.py
from __future__ import annotations
from lexer.lexer import tokenize, Token
from mantra_ast.nodes import *
from typing import List, Iterator

class ParseError(Exception):
    pass

class Parser:
    def __init__(self, tokens):
        self.tokens = list(tokens)
        self.i = 0

    def peek(self) -> Token:
        return self.tokens[self.i] if self.i < len(self.tokens) else Token('EOF', '', 0, 0)

    def peek2(self) -> Token:
        return self.tokens[self.i+1] if self.i+1 < len(self.tokens) else Token('EOF', '', 0, 0)

    def advance(self) -> Token:
        t = self.peek()
        self.i += 1
        return t

    def expect(self, typ: str) -> Token:
        t = self.advance()
        if t.type != typ:
            raise ParseError(f"Expected {typ} at {t.line}:{t.col}, got {t.type!r} ({t.value!r})")
        return t

    def match(self, *types) -> bool:
        return self.peek().type in types

    # ── top level ─────────────────────────────────────────────────────────

    def parse(self) -> Program:
        modules, items = [], []
        while not self.match('EOF'):
            if self.match('MODULE'):
                modules.append(self._parse_module())
            else:
                items.append(self._parse_item())
        if not modules:
            modules = [Module(name='main', items=items)]
        return Program(modules=modules)

    def _parse_module(self) -> Module:
        self.expect('MODULE')
        name = self.expect('IDENT').value
        self.expect('LBRACE')
        items = []
        while not self.match('RBRACE', 'EOF'):
            items.append(self._parse_item())
        self.expect('RBRACE')
        return Module(name=name, items=items)

    # ── item ──────────────────────────────────────────────────────────────

    def _parse_item(self) -> Node:
        ann = self._parse_annotations()
        t = self.peek()
        if t.type == 'CONST':
            return self._parse_const(ann)
        if t.type == 'LET':
            return self._parse_let(ann)
        if t.type == 'DEFINE':
            return self._parse_define(ann)
        if t.type == 'ASSERT':
            self.advance()
            return Assert(expr=self._parse_expr())
        # expression statement
        return self._parse_expr()

    def _parse_annotations(self) -> List[Annotation]:
        ann = []
        while self.match('ANNOT'):
            raw = self.advance().value
            if '(' in raw:
                name, rest = raw.split('(', 1)
                ann.append(Annotation(name=name[1:], args=rest.rstrip(')')))
            else:
                ann.append(Annotation(name=raw[1:], args=None))
        return ann

    def _parse_const(self, ann) -> ConstDecl:
        self.expect('CONST')
        name = self.expect('IDENT').value
        self.expect('ASSIGN')
        val = self._parse_expr()
        return ConstDecl(name=name, value=val, annotations=ann)

    def _parse_let(self, ann) -> LetDecl:
        self.expect('LET')
        name = self.expect('IDENT').value
        self.expect('ASSIGN')
        val = self._parse_expr()
        return LetDecl(name=name, value=val, annotations=ann)

    def _parse_define(self, ann) -> FunctionDecl:
        self.expect('DEFINE')
        name = self.expect('IDENT').value
        self.expect('LPAREN')
        params = []
        while not self.match('RPAREN', 'EOF'):
            params.append(self.expect('IDENT').value)
            if self.match('COMMA'):
                self.advance()
        self.expect('RPAREN')
        self.expect('BEGIN')
        body = []
        while not self.match('END', 'EOF'):
            if self.match('RETURN'):
                self.advance()
                body.append(Return(expr=self._parse_expr()))
            elif self.match('ASSERT'):
                self.advance()
                body.append(Assert(expr=self._parse_expr()))
            elif self.match('LET'):
                body.append(self._parse_let([]))
            elif self.match('CONST'):
                body.append(self._parse_const([]))
            else:
                body.append(self._parse_expr())
        self.expect('END')
        return FunctionDecl(name=name, params=params, body=body, annotations=ann)

    # ── expressions (Pratt-style precedence climbing) ─────────────────────

    def _parse_expr(self) -> Node:
        return self._parse_or()

    def _parse_or(self) -> Node:
        left = self._parse_and()
        while self.match('OR', 'PIPE'):
            op = self.advance().value
            left = BinaryExpr(op='or', left=left, right=self._parse_and())
        return left

    def _parse_and(self) -> Node:
        left = self._parse_equality()
        while self.match('AND', 'AMP'):
            op = self.advance().value
            left = BinaryExpr(op='and', left=left, right=self._parse_equality())
        return left

    def _parse_equality(self) -> Node:
        left = self._parse_relational()
        while self.match('EQ', 'NEQ'):
            op = self.advance().value
            left = BinaryExpr(op=op, left=left, right=self._parse_relational())
        return left

    def _parse_relational(self) -> Node:
        left = self._parse_shift()
        while self.match('LT', 'GT', 'LE', 'GE'):
            op = self.advance().value
            left = BinaryExpr(op=op, left=left, right=self._parse_shift())
        return left

    def _parse_shift(self) -> Node:
        left = self._parse_bitor()
        while self.match('LSH', 'RSH'):
            op = self.advance().value
            left = BinaryExpr(op=op, left=left, right=self._parse_bitor())
        return left

    def _parse_bitor(self) -> Node:
        left = self._parse_bitxor()
        while self.match('PIPE'):
            self.advance()
            left = BinaryExpr(op='|', left=left, right=self._parse_bitxor())
        return left

    def _parse_bitxor(self) -> Node:
        left = self._parse_bitand()
        while self.match('CARET'):
            self.advance()
            left = BinaryExpr(op='^', left=left, right=self._parse_bitand())
        return left

    def _parse_bitand(self) -> Node:
        left = self._parse_add()
        while self.match('AMP'):
            self.advance()
            left = BinaryExpr(op='&', left=left, right=self._parse_add())
        return left

    def _parse_add(self) -> Node:
        left = self._parse_mul()
        while self.match('PLUS', 'MINUS'):
            op = self.advance().value
            left = BinaryExpr(op=op, left=left, right=self._parse_mul())
        return left

    def _parse_mul(self) -> Node:
        left = self._parse_unary()
        while self.match('STAR', 'SLASH', 'PERCENT', 'POW'):
            op = self.advance().value
            left = BinaryExpr(op=op, left=left, right=self._parse_unary())
        return left

    def _parse_unary(self) -> Node:
        if self.match('MINUS', 'TILDE', 'NOT'):
            op = self.advance().value
            return UnaryExpr(op=op, operand=self._parse_unary())
        return self._parse_postfix()

    def _parse_postfix(self) -> Node:
        node = self._parse_primary()
        while self.match('DOT'):
            self.advance()
            field = self.expect('IDENT').value
            node = FieldAccess(obj=node, field=field)
        return node

    def _parse_primary(self) -> Node:
        t = self.peek()

        if t.type == 'NUMBER':
            self.advance()
            return IntegerLiteral(int(t.value))

        if t.type == 'HEX':
            self.advance()
            return HexLiteral(raw=t.value)

        if t.type in ('TRUE', 'FALSE'):
            self.advance()
            return BoolLiteral(t.type == 'TRUE')

        if t.type == 'STRING':
            self.advance()
            s = t.value[1:-1].encode('utf-8').decode('unicode_escape')
            return StringLiteral(s)

        if t.type == 'BYTES':
            self.advance()
            raw = t.value[2:-1]
            b = raw.encode('utf-8').decode('unicode_escape').encode('latin1')
            return BytesLiteral(b)

        if t.type == 'SOME':
            self.advance()
            self.expect('LPAREN')
            v = self._parse_expr()
            self.expect('RPAREN')
            return OptionLiteral(tag='some', value=v)

        if t.type == 'NONE':
            self.advance()
            return OptionLiteral(tag='none', value=None)

        # identifiers and keyword-named builtins (concat, len, slice, sha256_hex, etc.)
        _CALLABLE_KEYWORDS = {'CONCAT', 'LEN', 'SLICE', 'SOME', 'OK', 'ERR',
                               'BYTES', 'BYTE', 'OPTION', 'RESULT'}
        if t.type == 'IDENT' or t.type in _CALLABLE_KEYWORDS:
            name = self.advance().value
            if self.match('LPAREN'):
                self.advance()
                args = []
                while not self.match('RPAREN', 'EOF'):
                    args.append(self._parse_expr())
                    if self.match('COMMA'):
                        self.advance()
                self.expect('RPAREN')
                return Call(callee=name, args=args)
            return Identifier(name=name)

        if t.type == 'LPAREN':
            self.advance()
            expr = self._parse_expr()
            self.expect('RPAREN')
            return expr

        if t.type == 'LBRACE':
            return self._parse_dict_or_list()

        if t.type == 'LBRACKET':
            self.advance()
            items = []
            while not self.match('RBRACKET', 'EOF'):
                items.append(self._parse_expr())
                if self.match('COMMA'):
                    self.advance()
            self.expect('RBRACKET')
            return ListLiteral(items=items)

        raise ParseError(f"Unexpected token {t.type!r} ({t.value!r}) at {t.line}:{t.col}")

    def _parse_dict_or_list(self) -> Node:
        self.expect('LBRACE')
        # peek: if next is IDENT COLON → dict
        if self.peek().type == 'IDENT' and self.peek2().type == 'COLON':
            pairs = []
            while not self.match('RBRACE', 'EOF'):
                key = self.expect('IDENT').value
                self.expect('COLON')
                val = self._parse_expr()
                pairs.append((key, val))
                if self.match('COMMA'):
                    self.advance()
            self.expect('RBRACE')
            return DictLiteral(pairs=pairs)
        else:
            items = []
            while not self.match('RBRACE', 'EOF'):
                items.append(self._parse_expr())
                if self.match('COMMA'):
                    self.advance()
            self.expect('RBRACE')
            return ListLiteral(items=items)


def parse(code: str) -> Program:
    tokens = tokenize(code)
    return Parser(tokens).parse()