def _next(self) -> str: if self._position >= len(self._input): raise TranspilerError(self._source_position(), "Reached EOL while parsing.") result = self._peek() self._position += 1 return result
def _read_until(self, end_characters: List[str], error_on_eol: bool = True): result = [] while self._peek() not in end_characters: if error_on_eol and self._peek() == "\n": raise TranspilerError(self._source_position(), "Reached EOL while parsing.") result.append(self._next()) return "".join(result)
def _next(self) -> str: if self._position >= len(self._input): raise TranspilerError(self._source_position(), "Reached EOF while parsing.") result = self._peek() if result == "\n": self._line += 1 self._line_start = self._position + 1 self._position += 1 return result
def _parse_command(self, skip_whitespace_after_command=True): pos = self._position self._expect("$") command = self._scan_alpha() if command == "Nu" or command == "G" or command == "arg": self._position = pos self._parse_text() return if skip_whitespace_after_command: self._skip_whitespace() if command not in self._command_to_func: raise TranspilerError(self._source_position(), "Unknown command $" + command) command = self._command_to_func[command]() if isinstance(command, list): self._commands.extend(command) else: self._commands.append(command)
def _scan_command(self) -> List[Command]: self._next() # Consume the dollar sign. three_char_command = self._peek() + self._safe_lookahead( 1) + self._safe_lookahead(2) two_char_command = self._peek() + self._safe_lookahead(1) one_char_command = self._peek() if three_char_command in self._three_char_commands: self._position += 3 return self._three_char_commands[three_char_command]() elif two_char_command in self._two_char_commands: self._position += 2 return self._two_char_commands[two_char_command]() elif one_char_command in self._one_char_commands: self._position += 1 return self._one_char_commands[one_char_command]() else: raise TranspilerError( self._source_position(), "Unrecognized command " + self._input[self._position:])
def _expect(self, expected_char: str): next_char = self._next() if next_char != expected_char: raise TranspilerError( self._source_position(), "Expected %s found %s" % (expected_char, next_char))
def _convert_string_to_int(self, string: str): try: return int(string) except: raise TranspilerError(self._source_position(), "Expected a number.")
def _scan_gender_dependent_message_command(self): messages = self._scan_string().split(",") if len(messages) < 2: raise TranspilerError(self._source_position(), "Invalid $G command.") return [GenderDependentMessageCommand(messages[0], messages[1])]