module name for #39

This commit is contained in:
Basile Burg 2015-09-15 03:33:16 +02:00
parent f707fe90e9
commit eb05f0547f
4 changed files with 99 additions and 11 deletions

View File

@ -14,5 +14,6 @@ object CurrentProject: TCENativeProject
ConfigurationIndex = 0 ConfigurationIndex = 0
LibraryAliases.Strings = ( LibraryAliases.Strings = (
'libdparse' 'libdparse'
'iz'
) )
end end

View File

@ -1,21 +1,93 @@
module ast; module ast;
import std.d.lexer, std.d.parser, common; import std.d.lexer, std.d.parser, std.d.ast;
import common;
import iz.enumset;
private
{
enum AstInfos {moduleName, symbolList, todoList, WarnAndErr}
alias CachedInfos = EnumSet!(AstInfos, Set8);
}
struct Ast struct Ast
{ {
private:
ubyte[] src;
string fname;
LexerConfig config;
StringCache strcache;
Module mod;
CachedInfos cachedInfos;
bool scanned;
string modName;
final static void parserError(string fname, size_t line, size_t col, string msg, bool isErr)
{
}
final void resetCachedInfo()
{
cachedInfos = 0;
modName = "";
}
final void scan()
{
resetCachedInfo;
scanned = false;
scope(success) scanned = true;
config = LexerConfig(fname, StringBehavior.source, WhitespaceBehavior.skip);
mod = parseModule(getTokensForParser(src, config, &strcache), fname, null, &parserError);
}
public:
this(string filename) this(string filename)
{ {
mixin(logcall); mixin(logcall);
import std.file;
fname = filename;
src = cast(ubyte[]) read(fname, size_t.max);
strcache = StringCache(StringCache.defaultBucketCount);
scan;
} }
this(ubyte[] buffer) this(ubyte[] buffer)
{ {
mixin(logcall); mixin(logcall);
src = buffer.dup;
strcache = StringCache(StringCache.defaultBucketCount);
scan;
} }
void rescan() final void rescan()
{ {
mixin(logcall); mixin(logcall);
resetCachedInfo;
}
final string moduleName()
{
string result;
if (!scanned)
return result;
if (AstInfos.moduleName in cachedInfos)
return modName;
cachedInfos += AstInfos.moduleName;
if (mod.moduleDeclaration)
foreach(Token t; mod.moduleDeclaration.moduleName.identifiers)
result ~= t.text ~ ".";
if (result.length)
modName = result[0 .. $-1];
return modName;
} }
} }

View File

@ -84,10 +84,15 @@ void unleash(AstToken tok)
} }
extern(C) export extern(C) export
char* moduleName(AstToken tok) immutable(char*) moduleName(AstToken tok)
{ {
char* result = null; if (tok < 1 || tok > modules.length)
return result; return null;
Ast* mod = modules[tok - 1];
if (mod == null)
return null;
import std.string: toStringz;
return toStringz(mod.moduleName);
} }
extern(C) export extern(C) export

View File

@ -10,6 +10,7 @@ type
TScanBuffer = function (buffer: PByte; len: NativeUint): TAstToken; cdecl; TScanBuffer = function (buffer: PByte; len: NativeUint): TAstToken; cdecl;
TRescan = procedure (tok: TAstToken); cdecl; TRescan = procedure (tok: TAstToken); cdecl;
TUnleash = procedure (tok: TAstToken); cdecl; TUnleash = procedure (tok: TAstToken); cdecl;
TModuleName = function (tok: TAstToken): PChar; cdecl;
var var
dast: TLibHandle; dast: TLibHandle;
@ -17,30 +18,39 @@ var
scanbuffer: TScanBuffer; scanbuffer: TScanBuffer;
rescan: TRescan; rescan: TRescan;
unleash: TUnleash; unleash: TUnleash;
moduleName: TModuleName;
tok: TAstToken; tok: TAstToken;
const
testModule = 'module a.b.c.d.e.f.g.h; import std.stdio;';
begin begin
dast := LoadLibrary('cedast.dll'); dast := LoadLibrary('cedast');
if dast = NilHandle then if dast = NilHandle then
writeln('dast invalid handle') writeln('dast invalid handle')
else begin else begin
scanfile := TScanFile(GetProcAddress(dast, 'scanFile')); scanfile := TScanFile(GetProcAddress(dast, 'scanFile'));
if scanFile = nil then writeln('invalid scanfile proc ptr') if scanFile = nil then writeln('invalid scanfile proc ptr')
else tok := scanfile(PChar('blah')); else tok := scanfile(PChar('exception in call so ticket value is 0'));
scanbuffer := TScanBuffer(GetProcAddress(dast, 'scanBuffer'));
if scanbuffer = nil then writeln('invalid scanBuffer proc ptr')
else scanbuffer(nil, 0);
rescan := TRescan(GetProcAddress(dast, 'rescan')); rescan := TRescan(GetProcAddress(dast, 'rescan'));
if rescan = nil then writeln('invalid rescan proc ptr') if rescan = nil then writeln('invalid rescan proc ptr')
else rescan(tok); else rescan(tok);
scanbuffer := TScanBuffer(GetProcAddress(dast, 'scanBuffer'));
if scanbuffer = nil then writeln('invalid scanBuffer proc ptr')
else tok := scanbuffer(@testModule[1], length(testModule));
moduleName := TModuleName(GetProcAddress(dast, 'moduleName'));
if moduleName = nil then writeln('invalid moduleName proc ptr')
else if tok <> 0 then writeln(moduleName(tok));
unleash := TUnleash(GetProcAddress(dast, 'unleash')); unleash := TUnleash(GetProcAddress(dast, 'unleash'));
if unleash = nil then writeln('invalid unleash proc ptr') if unleash = nil then writeln('invalid unleash proc ptr')
else unleash(tok); else unleash(tok);
end; end;
readln; readln;