From a5a436c4783fb75a0d089a6483219c06db91f593 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 9 Aug 2017 12:57:37 -0700 Subject: Make source location lightweight Fixes #24 So far the code has used a representation for source locations that is heavy-weight, but typical of research or hobby compilers: a `struct` type containing a line number and a (heap-allocated) string. This is actually very convenient for debugging, but it means that any data structure that might contain a source location needs careful memory management (because of those strings) and has a tendency to bloat. The new represnetation is that a source location is just a pointer-sized integer. In the simplest mental model, you can think of this as just counting every byte of source text that is passed in, and using those to name locations. Finding the path and line number that corresponds to a location involves a lookup step, but we can arrange to store all the files in an array sorted by their start locations, and do a binary search. Finding line numbers inside a file is similarly fast (one you pay a one-time cost to build an array of starting offsets for lines). More advanced compilers like clang actually go further and create a unique range of source locations to represent a file each time it gets included, so that they can track the include stack and reproduce it in diagnostic messages. I'm not doing anything that clever here. --- source/slang/token.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'source/slang/token.h') diff --git a/source/slang/token.h b/source/slang/token.h index f29f2b4c6..306d4a2f2 100644 --- a/source/slang/token.h +++ b/source/slang/token.h @@ -18,8 +18,9 @@ char const* TokenTypeToString(TokenType type); enum TokenFlag : unsigned int { - AtStartOfLine = 1 << 0, - AfterWhitespace = 1 << 1, + AtStartOfLine = 1 << 0, + AfterWhitespace = 1 << 1, + SuppressMacroExpansion = 1 << 2, }; typedef unsigned int TokenFlags; @@ -28,15 +29,21 @@ class Token public: TokenType type = TokenType::Unknown; String Content; - CodePosition Position; + SourceLoc Position; TokenFlags flags = 0; + Token() = default; - Token(TokenType type, const String & content, int line, int col, int pos, String fileName, TokenFlags flags = 0) + + Token( + TokenType type, + const String & content, + SourceLoc loc, + TokenFlags flags = 0) : flags(flags) { type = type; Content = content; - Position = CodePosition(line, col, pos, fileName); + Position = loc; } }; -- cgit v1.2.3