Module margo_parser.api.classes.MargoBlock

Expand source code
from ...tokenizer import tokenize
from .MargoStatement import MargoStatement
from .MargoAssignment import MargoAssignment
from .MargoDirective import MargoDirective
from typing import List


class MargoBlock:
    """A collection of Margo statements."""

    def __init__(self, source: str):
        """Parses source immediately, raising an exception if parsing
        fails.
        :param source: The source code string
        :raises: MargoParseException if the source string cannot be parsed
        :raises: MargoLangException if there's some other error"""

        # This is what raises the MargoParseException if it fails
        parsed = tokenize(source)
        self.__statements = []
        # TODO - Test statement for valid structure
        for statement in parsed["BODY"]:
            # statement_type = statement["TYPE"]
            statement_name = statement["NAME"]
            if "VALUE" in statement:
                statement_value = statement["VALUE"]
                statement = MargoAssignment(statement_name, statement_value)
            else:
                statement = MargoDirective(statement_name)

            self.__statements.append(statement)

    @property
    def statements(self) -> List[MargoStatement]:
        """List of Margo statements"""
        return self.__statements

Classes

class MargoBlock (source: str)

A collection of Margo statements.

Parses source immediately, raising an exception if parsing fails. :param source: The source code string :raises: MargoParseException if the source string cannot be parsed :raises: MargoLangException if there's some other error

Expand source code
class MargoBlock:
    """A collection of Margo statements."""

    def __init__(self, source: str):
        """Parses source immediately, raising an exception if parsing
        fails.
        :param source: The source code string
        :raises: MargoParseException if the source string cannot be parsed
        :raises: MargoLangException if there's some other error"""

        # This is what raises the MargoParseException if it fails
        parsed = tokenize(source)
        self.__statements = []
        # TODO - Test statement for valid structure
        for statement in parsed["BODY"]:
            # statement_type = statement["TYPE"]
            statement_name = statement["NAME"]
            if "VALUE" in statement:
                statement_value = statement["VALUE"]
                statement = MargoAssignment(statement_name, statement_value)
            else:
                statement = MargoDirective(statement_name)

            self.__statements.append(statement)

    @property
    def statements(self) -> List[MargoStatement]:
        """List of Margo statements"""
        return self.__statements

Subclasses

Instance variables

var statements : List[MargoStatement]

List of Margo statements

Expand source code
@property
def statements(self) -> List[MargoStatement]:
    """List of Margo statements"""
    return self.__statements