handle missing include file

This commit is contained in:
Kamil Zbrog 2017-03-31 22:07:34 +02:00
parent b273e25a17
commit de14ad2975
1 changed files with 13 additions and 5 deletions

18
foco65
View File

@ -63,6 +63,10 @@ class UnexpectedEndOfStream(ParseError):
def __init__(self, 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:
@ -360,11 +364,15 @@ class Forth:
self.word = Word(token.text, self.text_section, label=token.canon())
self.set_state("compile")
elif token == "[include]":
self.inputs.append((self.input, self.current_file_name))
include_file_name = self.next().replace('"', '')
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()
token = self.next();
include_file_name = token.text.replace('"', '')
try:
self.inputs.append((self.input, self.current_file_name))
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]":
self.items.append(self.parse_code())