# SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia <contact@jookia.org> from src.syntax import Syntax, SyntaxType # Reads a token, possibly of a certain value def read_token(stream, value): s = stream.pop() if s is None: return None elif s.type != SyntaxType.TOKEN: return None elif value is not None and s.value != value: return None return s # Parses a text syntax node def parse_text(stream): buffer = "" s = read_token(stream, "StartText") if s is None: return None location = s.location # Parse following tokens while True: s = read_token(stream, None) if s is None: return None # Don't allow StartText in text elif s.value in ["StartText"]: return None # EndText found, end things elif s.value == "EndText": break else: buffer += s.value + " " type = SyntaxType.TEXT value = buffer[:-1] # Drop trailing space return Syntax(value, location, type) # Parses tokens def parse(tokens): return tokens