===== ISTEM =====
Solve the following programming task.

TASK: Basic Expression Calculator

Write a function 'calculate(s)' that evaluates a mathematical expression string containing addition (+), subtraction (-), multiplication (*), integer division (//) and parentheses, with correct operator precedence, without using eval or exec.

STARTER CODE (python):
def calculate(s: str) -> int:
    # Write your code here
    pass


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```python
def calculate(s: str) -> int:
    tokens = []
    i = 0
    n = len(s)
    while i < n:
        c = s[i]
        if c.isspace():
            i += 1
        elif c.isdigit():
            val = 0
            while i < n and s[i].isdigit():
                val = val * 10 + int(s[i])
                i += 1
            tokens.append(val)
        elif c in '+-*()':
            tokens.append(c)
            i += 1
        elif c == '/':
            if i + 1 < n and s[i + 1] == '/':
                tokens.append('//')
                i += 2
            else:
                tokens.append('//')
                i += 1
        else:
            i += 1

    pos = 0

    def parse_expr():
        nonlocal pos
        res = parse_term()
        while pos < len(tokens) and tokens[pos] in ('+', '-'):
            op = tokens[pos]
            pos += 1
            right = parse_term()
            if op == '+':
                res += right
            else:
                res -= right
        return res

    def parse_term():
        nonlocal pos
        res = parse_factor()
        while pos < len(tokens) and tokens[pos] in ('*', '//'):
            op = tokens[pos]
            pos += 1
            right = parse_factor()
            if op == '*':
                res *= right
            else:
                res //= right
        return res

    def parse_factor():
        nonlocal pos
        if pos < len(tokens) and tokens[pos] in ('+', '-'):
            op = tokens[pos]
            pos += 1
            val = parse_factor()
            return val if op == '+' else -val
        return parse_primary()

    def parse_primary():
        nonlocal pos
        tok = tokens[pos]
        pos += 1
        if tok == '(':
            res = parse_expr()
            if pos < len(tokens) and tokens[pos] == ')':
                pos += 1
            return res
        return tok

    return parse_expr() if tokens else 0
```