# SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia <contact@jookia.org> from src.syntax import Syntax, SyntaxType # Removes whitespace syntax tokens def strip_whitespace(syntax): output = [] for s in syntax: if s.type != SyntaxType.TOKEN or s.value not in ["\n", " "]: output.append(s) return output # 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 value = buffer.strip("\n\t ") type = SyntaxType.TEXT return Syntax(value, location, type) # Parses tokens def parse(tokens): stripped = strip_whitespace(tokens) return stripped