Newer
Older
NewLang / src / parse.py
# SPDX-License-Identifier: LGPL-2.1-only
# Copyright 2022 Jookia <contact@jookia.org>

from src import tokenize
import enum


# The type of syntax
class SyntaxType(enum.Enum):
    TOKEN = enum.auto()  # pragma: no mutate


# Represents a syntax node
class Syntax:
    def __init__(self, value, location, type):
        self.value = value
        self.location = location
        self.type = type

    def __repr__(self):
        return "Syntax(value %s, location %s, type %s)" % (  # pragma: no mutate
            repr(self.value),
            repr(self.location),
            str(self.type),
        )

    def __eq__(self, other):
        return (
            self.value == other.value
            and self.location == other.location
            and self.type == other.type
        )


# Removes whitespace tokens
def strip_whitespace(tokens):
    output = []
    for t in tokens:
        if t.type not in [tokenize.TokenType.SPACE, tokenize.TokenType.NEWLINE]:
            output.append(t)
    return output


# Parses tokens
def parse(tokens):
    stripped = strip_whitespace(tokens)
    return stripped