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

import enum
from src.ast_types import Bool, Reference, Text
from src.token import TokenStream


# Words that can't be used as references
# This should include keywords and literals
reserved_names = [
    "Done",
    "Set",
    "To",
    "EndSet",
    "If",
    "Then",
    "Else",
    "EndIf",
    "StartNote",
    "EndNote",
    "StartText",
    "EndText",
    "True",
    "False",
]


# Tasks that happen during parsing
class ParseTask(enum.Enum):
    TEST_TASK = enum.auto()  # pragma: no mutate
    PARSE_NOTE = enum.auto()  # pragma: no mutate
    CLEAR_NOTES = enum.auto()  # pragma: no mutate
    PARSE_TEXT = enum.auto()  # pragma: no mutate
    PARSE_BOOL = enum.auto()  # pragma: no mutate
    PARSE_REFERENCE = enum.auto()  # pragma: no mutate


# Context used for parse error exception
class ParseContext:
    def __init__(self, task, token, parent):
        self.task = task
        self.token = token
        self.parent = parent

    def __repr__(self):
        return (
            "ParseContext(task %s, token %s, parent %s)"  # pragma: no mutate
            % (  # pragma: no mutate
                self.task,
                self.token,
                self.parent,
            )
        )

    def __eq__(self, other):
        return (
            self.task == other.task
            and self.token == other.token
            and self.parent == other.parent
        )


# Errors that can happen when parsing
class ParseError(enum.Enum):
    TEST_ERROR = enum.auto()  # pragma: no mutate
    NO_TOKEN = enum.auto()  # pragma: no mutate
    WRONG_TOKEN = enum.auto()  # pragma: no mutate
    FOUND_STARTTEXT = enum.auto()  # pragma: no mutate
    FOUND_STARTNOTE = enum.auto()  # pragma: no mutate
    NOT_BOOL = enum.auto()  # pragma: no mutate
    FOUND_ENDNOTE = enum.auto()  # pragma: no mutate
    RESERVED_NAME = enum.auto()  # pragma: no mutate


# Exception thrown when a parse error is encountered
class ParseErrorException(BaseException):
    def __init__(self, error, token, expected, context):
        self.error = error
        self.token = token
        self.expected = expected
        self.context = context

    def __repr__(self):
        return (
            "ParseErrorException(error %s, token %s, expected %s, context %s)"  # pragma: no mutate
            % (  # pragma: no mutate
                self.error,
                self.token,
                self.expected,
                self.context,
            )
        )

    def __eq__(self, other):
        return (
            self.error == other.error
            and self.token == other.token
            and self.expected == other.expected
            and self.context == other.context
        )


# Reads a token, possibly of a certain value
def read_token(stream, value, context):
    t = stream.pop()
    if t is None:
        raise ParseErrorException(ParseError.NO_TOKEN, None, None, context)
    elif value is not None and t.value != value:
        raise ParseErrorException(ParseError.WRONG_TOKEN, t, value, context)
    return t


# The note skipper in a wrapper class for easy testing
class NoteSkipper:
    # Skip a note
    def skip_note(self, stream, parent_context):
        context = ParseContext(ParseTask.PARSE_NOTE, stream.peek(), parent_context)
        read_token(stream, "StartNote", context)
        while True:
            t = read_token(stream, None, context)
            # Don't allow StartNote in notes
            if t.value in ["StartNote"]:
                raise ParseErrorException(ParseError.FOUND_STARTNOTE, t, None, context)
            # EndNote found, end things
            elif t.value == "EndNote":
                break
        return None

    # Clear notes
    def clear_notes(self, stream, parent_context):
        context = ParseContext(ParseTask.CLEAR_NOTES, stream.peek(), parent_context)
        tokens = []
        token = stream.peek()
        while token is not None:
            # Found a note, skip it
            if token.value == "StartNote":
                self.skip_note(stream, context)
            # EndNote found outside note
            elif token.value == "EndNote":
                raise ParseErrorException(
                    ParseError.FOUND_ENDNOTE, token, None, context
                )
            # Add the token if it's not note related
            else:
                tokens.append(stream.pop())
            token = stream.peek()
        return tokens


# The recursive descent parser in a wrapper class for easy testing
class Parser:
    # Parses a text node
    def parse_text(self, stream, parent_context):
        context = ParseContext(ParseTask.PARSE_TEXT, stream.peek(), parent_context)
        buffer = ""
        t = read_token(stream, "StartText", context)
        # Parse following tokens
        while True:
            t = read_token(stream, None, context)
            # Don't allow StartText in text
            if t.value in ["StartText"]:
                raise ParseErrorException(ParseError.FOUND_STARTTEXT, t, None, context)
            # EndText found, end things
            elif t.value == "EndText":
                break
            else:
                buffer += t.value + " "
        value = buffer[:-1]  # Drop trailing space
        return Text(value)

    # Parses a boolean node
    def parse_bool(self, stream, parent_context):
        context = ParseContext(ParseTask.PARSE_BOOL, stream.peek(), parent_context)
        t = read_token(stream, None, context)
        if t.value == "True":
            return Bool(True)
        elif t.value == "False":
            return Bool(False)
        else:
            raise ParseErrorException(ParseError.NOT_BOOL, t, None, context)

    # Parses a reference node
    def parse_reference(self, stream, parent_context):
        context = ParseContext(ParseTask.PARSE_REFERENCE, stream.peek(), parent_context)
        t = read_token(stream, None, context)
        if t.value in reserved_names:
            raise ParseErrorException(ParseError.RESERVED_NAME, t, None, context)
        return Reference(t.value)


# Parses tokens
def parse(tokens, context):
    stream = TokenStream(tokens)
    cleared = NoteSkipper().clear_notes(stream, context)
    return cleared