replace cesyms by dastworx, #82

This commit is contained in:
Basile Burg 2016-07-02 07:20:09 +02:00
parent c5e1cf513a
commit afa35e11d1
9 changed files with 16 additions and 533 deletions

View File

@ -1,466 +0,0 @@
/**
Usage
=====
- In Coedit:
the program must be located somewhere in the PATH.
- Elsewhere:
invoke with `[-j] [<filename>]`.
- `-j`: optional, if set then the program outputs the list (in stdout) in JSON
otherwise in Pascal streaming text format.
- `<filename>`: optional, the D module filename, if not set then the program
reads the module from stdin.
- see the source for more information about how to use the output.
It's basically a tree of struct with 3 members: symbol type, name and location
- Test in CE as a runnable module:
click `Compile file and run ...` and type either `<CFF>` or `-j <CFF>` in the
input query dialog. Note that this will only work if libdparse is setup in the
library manager.
*/
module cesyms;
import std.stdio, std.path, std.file, std.array, std.string, std.algorithm;
import std.getopt, std.json, std.conv, std.format;
import dparse.lexer, dparse.ast, dparse.parser, dparse.rollback_allocator;
import std.traits;
enum ListFmt
{
Pas,
Json
}
__gshared bool deep;
void main(string[] args)
{
// format
bool asJson;
getopt(args, config.passThrough, "j", &asJson, "d", &deep);
// get either the module from stdin or from first arg
string fname;
ubyte[] source;
if (args.length == 1)
{
version (runnable_module)
{
source = cast(ubyte[]) read(__FILE__, size_t.max);
}
else
foreach (buff; stdin.byChunk(1024))
source ~= buff;
}
else if (args.length == 2)
{
fname = args[$ - 1];
if (!fname.exists)
return;
source = cast(ubyte[]) read(fname, size_t.max);
}
else
return;
// parses, visits and writes to stdout
RollbackAllocator alloc;
auto config = LexerConfig(fname, StringBehavior.source, WhitespaceBehavior.skip);
auto scache = StringCache(StringCache.defaultBucketCount);
if (!asJson)
{
SymbolListBuilder!(ListFmt.Pas) slb = construct!(SymbolListBuilder!(ListFmt.Pas));
auto ast = parseModule(getTokensForParser(source, config, &scache), fname,
&alloc, &slb.astError);
slb.visit(ast);
write(slb.serialize);
slb.destruct;
}
else
{
SymbolListBuilder!(ListFmt.Json) slb = construct!(SymbolListBuilder!(ListFmt.Json));
auto ast = parseModule(getTokensForParser(source, config, &scache), fname,
&alloc, &slb.astError);
slb.visit(ast);
write(slb.serialize);
slb.destruct;
}
}
// libdparse warnings includes some "'", which in Pascal are string delim
string patchPasStringLitteral(string p)
{
import std.range : empty, front, popFront;
string result;
while (!p.empty)
{
dchar curr = p.front;
switch (curr)
{
default:
result ~= curr;
break;
case 10, 13:
result ~= ' ';
break;
case '\'':
result ~= "'#39'";
}
p.popFront;
}
return result;
}
// Memory utils ---------------------------------------------------------------+
void* getMem(size_t size) nothrow
{
import std.c.stdlib: malloc;
auto result = malloc(size);
assert(result, "Out of memory");
return result;
}
CT construct(CT, A...)(A a)
if (is(CT == class) && !isAbstractClass!CT)
{
auto size = typeid(CT).init.length;
auto memory = getMem(size);
memory[0 .. size] = typeid(CT).init[];
static if (__traits(hasMember, CT, "__ctor"))
(cast(CT)(memory)).__ctor(a);
import core.memory : GC;
GC.addRange(memory, size, typeid(CT));
return cast(CT) memory;
}
void destruct(T)(ref T instance)
if (is(T == class))
{
if (!instance)
return;
destroy(instance);
instance = null;
}
//----
enum SymbolType
{
_alias,
_class,
_enum,
_error,
_function,
_interface,
_import,
_mixin, // (template decl)
_struct,
_template,
_union,
_unittest,
_variable,
_warning
}
string makeSymbolTypeArray()
{
string result = "string[SymbolType.max + 1] symbolTypeStrings = [";
foreach(st; EnumMembers!SymbolType)
result ~= `"` ~ to!string(st) ~ `",`;
result ~= "];";
return result;
}
mixin(makeSymbolTypeArray);
// AST visitor/Symbol list ----------------------------------------------------+
class SymbolListBuilder(ListFmt Fmt): ASTVisitor
{
static if (Fmt == ListFmt.Pas)
{
static Appender!string pasStream;
}
else
{
static JSONValue json;
static JSONValue* jarray;
}
static uint utc;
alias visit = ASTVisitor.visit;
static this()
{
static if (Fmt == ListFmt.Pas)
{
pasStream.put("object TSymbolList\rsymbols = <");
}
else
{
json = parseJSON("[]");
jarray = &json;
}
}
static void astError(string fname, size_t line, size_t col, string msg, bool isErr)
{
string type = isErr ? symbolTypeStrings[SymbolType._error] :
symbolTypeStrings[SymbolType._warning];
static if (Fmt == ListFmt.Pas)
{
pasStream.put("\ritem\r");
pasStream.put(format("line = %d\r", line));
pasStream.put(format("col = %d\r", col));
pasStream.put(format("name = '%s'\r", patchPasStringLitteral(msg)));
pasStream.put(format("symType = %s\r", type));
pasStream.put("end");
}
else
{
JSONValue item = parseJSON("{}");
item["line"] = JSONValue(line);
item["col"] = JSONValue(col);
item["name"] = JSONValue(msg);
item["type"] = JSONValue(type);
jarray.array ~= item;
}
}
final string serialize()
{
static if (Fmt == ListFmt.Pas)
{
pasStream.put(">\rend\r\n");
return pasStream.data;
}
else
{
JSONValue result = parseJSON("{}");
result["items"] = json;
version (assert)
return result.toPrettyString;
else
return result.toString;
}
}
/// visitor implementation if the declaration has a "name".
final void namedVisitorImpl(DT, SymbolType st, bool dig = true)(const(DT) dt)
if (__traits(hasMember, DT, "name"))
{
static if (Fmt == ListFmt.Pas)
{
pasStream.put("\ritem\r");
pasStream.put(format("line = %d\r", dt.name.line));
pasStream.put(format("col = %d\r", dt.name.column));
pasStream.put(format("name = '%s'\r", dt.name.text));
pasStream.put("symType = " ~ symbolTypeStrings[st] ~ "\r");
static if (dig) if (deep)
{
pasStream.put("subs = <");
dt.accept(this);
pasStream.put(">\r");
}
pasStream.put("end");
}
else
{
JSONValue item = parseJSON("{}");
item["line"] = JSONValue(dt.name.line);
item["col"] = JSONValue(dt.name.column);
item["name"] = JSONValue(dt.name.text);
item["type"] = JSONValue(symbolTypeStrings[st]);
static if (dig) if (deep)
{
JSONValue subs = parseJSON("[]");
JSONValue* old = jarray;
jarray = &subs;
dt.accept(this);
item["items"] = subs;
jarray = old;
}
json.array ~= item;
}
}
/// visitor implementation for special cases.
final void otherVisitorImpl(DT, bool dig = true)
(const(DT) dt, SymbolType st, string name, size_t line, size_t col)
{
static if (Fmt == ListFmt.Pas)
{
pasStream.put("\ritem\r");
pasStream.put(format("line = %d\r", line));
pasStream.put(format("col = %d\r", col));
pasStream.put(format("name = '%s'\r", name));
pasStream.put("symType = " ~ symbolTypeStrings[st] ~ "\r");
static if (dig)
{
pasStream.put("subs = <");
dt.accept(this);
pasStream.put(">\r");
}
pasStream.put("end");
}
else
{
JSONValue item = parseJSON("{}");
item["line"] = JSONValue(line);
item["col"] = JSONValue(col);
item["name"] = JSONValue(name);
item["type"] = JSONValue(symbolTypeStrings[st]);
static if (dig)
{
JSONValue subs = parseJSON("[]");
JSONValue* old = jarray;
jarray = &subs;
dt.accept(this);
item["items"] = subs;
jarray = old;
}
json.array ~= item;
}
}
final override void visit(const AliasDeclaration decl)
{
if (decl.initializers.length)
namedVisitorImpl!(AliasInitializer, SymbolType._alias)(decl.initializers[0]);
}
final override void visit(const AnonymousEnumMember decl)
{
namedVisitorImpl!(AnonymousEnumMember, SymbolType._enum)(decl);
}
final override void visit(const AnonymousEnumDeclaration decl)
{
decl.accept(this);
}
final override void visit(const AutoDeclaration decl)
{
if (decl.identifiers.length)
{
otherVisitorImpl(decl, SymbolType._variable, decl.identifiers[0].text,
decl.identifiers[0].line, decl.identifiers[0].column);
}
}
final override void visit(const ClassDeclaration decl)
{
namedVisitorImpl!(ClassDeclaration, SymbolType._class)(decl);
}
final override void visit(const Constructor decl)
{
otherVisitorImpl(decl, SymbolType._function, "ctor", decl.line, decl.column);
}
final override void visit(const Destructor decl)
{
otherVisitorImpl(decl, SymbolType._function, "dtor", decl.line, decl.column);
}
final override void visit(const EnumDeclaration decl)
{
namedVisitorImpl!(EnumDeclaration, SymbolType._enum)(decl);
}
final override void visit(const EponymousTemplateDeclaration decl)
{
namedVisitorImpl!(EponymousTemplateDeclaration, SymbolType._template)(decl);
}
final override void visit(const FunctionDeclaration decl)
{
namedVisitorImpl!(FunctionDeclaration, SymbolType._function)(decl);
}
final override void visit(const InterfaceDeclaration decl)
{
namedVisitorImpl!(InterfaceDeclaration, SymbolType._interface)(decl);
}
final override void visit(const ImportDeclaration decl)
{
foreach (const(SingleImport) si; decl.singleImports)
{
if (!si.identifierChain.identifiers.length)
continue;
otherVisitorImpl(decl, SymbolType._import,
si.identifierChain.identifiers.map!(a => a.text).join("."),
si.identifierChain.identifiers[0].line,
si.identifierChain.identifiers[0].column);
}
if (decl.importBindings) with (decl.importBindings.singleImport)
otherVisitorImpl(decl, SymbolType._import,
identifierChain.identifiers.map!(a => a.text).join("."),
identifierChain.identifiers[0].line,
identifierChain.identifiers[0].column);
}
final override void visit(const MixinTemplateDeclaration decl)
{
namedVisitorImpl!(TemplateDeclaration, SymbolType._mixin)(decl.templateDeclaration);
}
final override void visit(const StructDeclaration decl)
{
namedVisitorImpl!(StructDeclaration, SymbolType._struct)(decl);
}
final override void visit(const TemplateDeclaration decl)
{
namedVisitorImpl!(TemplateDeclaration, SymbolType._template)(decl);
}
final override void visit(const UnionDeclaration decl)
{
namedVisitorImpl!(UnionDeclaration, SymbolType._union)(decl);
}
final override void visit(const Unittest decl)
{
otherVisitorImpl(decl, SymbolType._unittest, format("test%.4d",utc++),
decl.line, decl.column);
}
final override void visit(const VariableDeclaration decl)
{
if (decl.declarators)
foreach (elem; decl.declarators)
namedVisitorImpl!(Declarator, SymbolType._variable, false)(elem);
else if (decl.autoDeclaration)
visit(decl.autoDeclaration);
}
final override void visit(const StaticConstructor decl)
{
otherVisitorImpl(decl, SymbolType._function, "static ctor", decl.line, decl.column);
}
final override void visit(const StaticDestructor decl)
{
otherVisitorImpl(decl, SymbolType._function, "static dtor", decl.line, decl.column);
}
final override void visit(const SharedStaticConstructor decl)
{
otherVisitorImpl(decl, SymbolType._function, "shared static ctor", decl.line, decl.column);
}
final override void visit(const SharedStaticDestructor decl)
{
otherVisitorImpl(decl, SymbolType._function, "shared static dtor", decl.line, decl.column);
}
}
//----

View File

@ -1,18 +0,0 @@
object CurrentProject: TCENativeProject
OptionsCollection = <
item
name = 'release'
outputOptions.inlining = True
outputOptions.boundsCheck = offAlways
outputOptions.optimizations = True
outputOptions.release = True
pathsOptions.outputFilename = '../bin/cesyms'
end>
Sources.Strings = (
'cesyms.d'
)
ConfigurationIndex = 0
LibraryAliases.Strings = (
'libdparse'
)
end

View File

@ -1,17 +0,0 @@
object CurrentProject: TCENativeProject
OptionsCollection = <
item
name = 'release'
outputOptions.inlining = True
outputOptions.boundsCheck = offAlways
outputOptions.release = True
pathsOptions.outputFilename = '../bin/cesyms'
pathsOptions.extraSources.Strings = (
'../etc/libdparse/src/*'
)
end>
Sources.Strings = (
'cesyms.d'
)
ConfigurationIndex = 0
end

View File

@ -1,17 +0,0 @@
ceSyms
======
Tool designed to build a symbol tree for a given D module.
It's written in D using Coedit.
To build it, either [libdparse](https://github.com/Hackerpilot/libdparse)
must be setup in the [libman](https://github.com/BBasile/Coedit/wiki#library-manager-widget)
as described in this [tutorial](https://github.com/BBasile/Coedit/wiki#lets-build-a-static-library),
or *libdparse* submodule must be cloned with Coedit repository (`git submodule init` or `update`).
Notice that *libdparse* can be easily build in Coedit using the [metad project](https://github.com/BBasile/metad).
- `cesyms_submodule.coedit`: coedit project based on *libdparse* as a submodule.
- `cesyms_libman.coedit`: coedit project based on *libdparse* as a *libman* entry.
This tool is mandatory to enable the _symbol list widget_.

View File

@ -2,10 +2,8 @@ object CurrentProject: TCENativeProject
OptionsCollection = <
item
name = 'devel'
outputOptions.inlining = True
outputOptions.boundsCheck = offAlways
outputOptions.optimizations = True
outputOptions.release = True
debugingOptions.generateInfos = True
outputOptions.boundsCheck = onAlways
outputOptions.versionIdentifiers.Strings = (
'devel'
)

View File

@ -70,6 +70,7 @@ void handleSymListOption()
mixin(logCall);
bool deep;
storeAstErrors = true;
lex!false;
parseTokens;
listSymbols(module_, errors.data, deepSymList);
}

View File

@ -13,9 +13,9 @@ inherited CEInfoWidget: TCEInfoWidget
ClientHeight = 502
ClientWidth = 411
inherited Content: TPanel
Height = 468
Height = 466
Width = 411
ClientHeight = 468
ClientHeight = 466
ClientWidth = 411
object GroupBox1: TGroupBox[0]
Left = 4
@ -36,7 +36,7 @@ inherited CEInfoWidget: TCEInfoWidget
Align = alClient
Alignment = taCenter
AutoSize = False
Caption = 'Coedit 2 - update 6'
Caption = 'Coedit 3 - devel'
Font.Height = -16
Font.Style = [fsBold]
Layout = tlCenter
@ -46,18 +46,18 @@ inherited CEInfoWidget: TCEInfoWidget
end
object GroupBox2: TGroupBox[1]
Left = 4
Height = 351
Height = 349
Top = 113
Width = 403
Align = alClient
BorderSpacing.Around = 4
Caption = 'tools status'
ClientHeight = 321
ClientHeight = 319
ClientWidth = 399
TabOrder = 1
object boxTools: TScrollBox
Left = 4
Height = 313
Height = 311
Top = 4
Width = 391
HorzScrollBar.Page = 1
@ -70,7 +70,7 @@ inherited CEInfoWidget: TCEInfoWidget
end
end
inherited toolbar: TCEToolBar
Width = 407
Width = 403
end
end
end

View File

@ -210,8 +210,8 @@ begin
'mandatory, provides IDE-level features such as the completion');
toolItem.Parent := boxTools;
toolItem.ReAlign;
toolItem := TToolInfo.Construct(self, tikFindable, 'cesyms',
'background tool that collects information for the symbol list widget');
toolItem := TToolInfo.Construct(self, tikFindable, 'dastworx',
'background tool that works on the D modules AST');
toolItem.Parent := boxTools;
toolItem.ReAlign;
toolItem := TToolInfo.Construct(self, tikFindable, 'cetodo',

View File

@ -189,7 +189,7 @@ implementation
const
OptsFname = 'symbollist.txt';
toolExeName = 'cesyms' + exeExt;
toolExeName = 'dastworx' + exeExt;
{$REGION Serializable symbols---------------------------------------------------}
constructor TSymbol.create(ACollection: TCollection);
@ -684,7 +684,9 @@ begin
fToolProc.Executable := fToolExeName;
fToolProc.OnTerminate := @toolTerminated;
fToolProc.CurrentDirectory := Application.ExeName.extractFileDir;
if fDeep then fToolProc.Parameters.Add('-d');
if fDeep then
fToolProc.Parameters.Add('-d');
fToolProc.Parameters.Add('-s');
fToolProc.Execute;
str := fDoc.Text;
fToolProc.Input.Write(str[1], str.length);