private void Identifier() { while (IsAlphaUnderscoreOrNumeric(Peek())) { Advance(); } // See if the identifier is a reserved word. string text = Source.Substring(Start, Current - Start); int? keyword = LoxTokenTypes.Get(text); if (keyword != null) { AddToken(keyword.Value); } else if (_PreProcessorDefines.TryGetValue(text, out string replace)) { SourceBegin(CurrentFile.Path, replace, Line); // Source = Source.Remove(Start, text.Length).Insert(Start, replace); // Current = Start; } else { AddToken(TokenTypes.IDENTIFIER); } }
private void PreProcessorDefine() { while (Peek() == ' ' || Peek() == '\t') { Advance(); } int nameStart = Current; while (IsAlphaUnderscoreOrNumeric(Peek())) { Advance(); } // See if the name is a reserved word. string name = Source.Substring(nameStart, Current - nameStart); int? keyword = LoxTokenTypes.Get(name); if (keyword != null) { throw new CompilerException(new Token(TokenTypes.ERROR, Line), $"Cannot redefine keyword '{name}'."); } while (Peek() == ' ' || Peek() == '\t') { Advance(); } int contentStart = Current; while (Peek() != '\r' && Peek() != '\n') { Advance(); } string content = Source.Substring(contentStart, Current - contentStart); _PreProcessorDefines[name] = content; }