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
        )


# Converts tokens to syntax
def import_tokens(tokens):
    output = []
    for t in tokens:
        output.append(Syntax(t, t.location, SyntaxType.TOKEN))
    return output


# Removes whitespace syntax tokens
def strip_whitespace(syntax):
    output = []
    for s in syntax:
        if s.type != SyntaxType.TOKEN:
            pass
        if s.value.type not in [tokenize.TokenType.SPACE, tokenize.TokenType.NEWLINE]:
            output.append(s)
    return output


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