handle missing include file

This commit is contained in:
Kamil Zbrog 2017-03-31 22:07:34 +02:00
parent b273e25a17
commit de14ad2975

18
foco65
View File

@ -63,6 +63,10 @@ class UnexpectedEndOfStream(ParseError):
def __init__(self, filename, line, column): def __init__(self, filename, line, column):
ParseError.__init__(self, "unexpected end of input", filename, line, column) ParseError.__init__(self, "unexpected end of input", filename, line, column)
class NoSuchFile(ParseError):
def __init__(self, filename, token):
ParseError.__init__(self, "No such include file '%s'" % token.text, filename, token.line, token.column)
##### #####
class Token: class Token:
@ -360,11 +364,15 @@ class Forth:
self.word = Word(token.text, self.text_section, label=token.canon()) self.word = Word(token.text, self.text_section, label=token.canon())
self.set_state("compile") self.set_state("compile")
elif token == "[include]": elif token == "[include]":
self.inputs.append((self.input, self.current_file_name)) token = self.next();
include_file_name = self.next().replace('"', '') include_file_name = token.text.replace('"', '')
with open(include_file_name, "rt") as f: try:
self.parse_input(Input(f.read()), include_file_name) self.inputs.append((self.input, self.current_file_name))
self.input, self.current_file_name = self.inputs.pop() with open(include_file_name, "rt") as f:
self.parse_input(Input(f.read()), include_file_name)
self.input, self.current_file_name = self.inputs.pop()
except IOError:
raise NoSuchFile(self.current_file_name, token)
elif token == "[code]": elif token == "[code]":
self.items.append(self.parse_code()) self.items.append(self.parse_code())