Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Token_Stream class as new lexer and ast_link to every token #61

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion trlc/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def __init__(self, location):
assert isinstance(location, Location)
self.location = location

def set_ast_link(self, tok):
assert isinstance(tok, Token)
tok.ast_link = self

def write_indent(self, indent, message): # pragma: no cover
# lobster-exclude: Debugging feature
assert isinstance(indent, int)
Expand Down Expand Up @@ -266,7 +270,9 @@ def resolve_imports(self, mh, stab):
# We can ignore errors here, because that just means we
# generate more error later.
try:
self.imports.add(stab.lookup(mh, t_import, Package))
a_import = stab.lookup(mh, t_import, Package)
self.imports.add(a_import)
a_import.set_ast_link(t_import)
except TRLC_Error:
pass

Expand Down Expand Up @@ -2399,6 +2405,10 @@ def dump(self, indent=0): # pragma: no cover
self.write_indent(indent + 1, "Declared_Late: %s" % self.declared_late)
self.symbols.dump(indent + 1, omit_heading=True)

def __repr__(self):
return "%s<%s>" % (self.__class__.__name__,
self.name)


class Composite_Type(Concrete_Type, metaclass=ABCMeta):
"""Abstract base for record and tuple types, as they share some
Expand Down Expand Up @@ -2495,6 +2505,11 @@ def dump(self, indent=0): # pragma: no cover
self.write_indent(indent + 1, "Optional: %s" % self.optional)
self.write_indent(indent + 1, "Type: %s" % self.n_typ.name)

def __repr__(self):
return "%s<%s>" % (self.__class__.__name__,
self.member_of.fully_qualified_name() + "." +
self.name)


class Record_Type(Composite_Type):
"""A user-defined record type.
Expand Down Expand Up @@ -2950,6 +2965,12 @@ def perform_checks(self, mh):

return ok

def __repr__(self):
return "%s<%s>" % (self.__class__.__name__,
self.n_package.name + "." +
self.n_typ.name + "." +
self.name)


class Section(Entity):
# lobster-trace: LRM.Section_Declaration
Expand Down
13 changes: 12 additions & 1 deletion trlc/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class Token(Token_Base):
"STRING" : "string literal",
}

def __init__(self, location, kind, value=None):
def __init__(self, location, kind, value=None, ast_link=None):
assert kind in Token.KIND
if kind in ("COMMENT", "IDENTIFIER", "BUILTIN",
"KEYWORD", "OPERATOR", "STRING"):
Expand All @@ -171,6 +171,7 @@ def __init__(self, location, kind, value=None):
else:
assert value is None
super().__init__(location, kind, value)
self.ast_link = ast_link

def __repr__(self):
if self.value is None:
Expand All @@ -186,6 +187,7 @@ def __init__(self, mh, content):
self.mh = mh
self.content = content
self.length = len(self.content)
self.tokens = []

self.lexpos = -3
self.line_no = 0
Expand Down Expand Up @@ -643,6 +645,15 @@ def token(self):
return Token(sref, kind, value)


class Token_Stream(TRLC_Lexer):

def token(self):
tok = super().token()
if tok is not None:
self.tokens.append(tok)
return tok


def sanity_test():
# lobster-exclude: Developer test function
mh = Message_Handler()
Expand Down
Loading
Loading