code cleanup

This commit is contained in:
Hackerpilot 2013-02-20 22:36:55 -08:00
parent 89d18755b8
commit 119c6b917d
3 changed files with 2046 additions and 2138 deletions

View File

@ -1,4 +1,4 @@
create table files (path, mtime, id);
create table modules (path, mtime, id);
create table publicImports (importerId, importedId);
create table containers (name, protection, fileId, id);
create table containers (name, protection, moduleId, id);
create table symbols (name, type, kind, containerId, id);

183
main.d
View File

@ -110,9 +110,9 @@ int main(string[] args)
try
{
getopt(args, "I", &importDirs,/+ "dotComplete", &dotComplete,+/ "sloc", &sloc,
/+"json", &json,+/ /+"parenComplete", &parenComplete,+/ "highlight", &highlight,
"ctags", &ctags, "recursive|r|R", &recursiveCtags, "help|h", &help,
getopt(args, "I", &importDirs,/+ "dotComplete|d", &dotComplete,+/ "sloc|l", &sloc,
/+"json|j", &json,+/ /+"parenComplete|p", &parenComplete,+/ "highlight", &highlight,
"ctags|c", &ctags, "recursive|r|R", &recursiveCtags, "help|h", &help,
"tokenCount", &tokenCount, "frequencyCount", &frequencyCount);
}
catch (Exception e)
@ -120,188 +120,96 @@ int main(string[] args)
stderr.writeln(e.msg);
}
if (help || (!sloc && /+!dotComplete &&+/ /+!json &&+/ /+!parenComplete &&+/ !highlight
&& !ctags && !format && !tokenCount && !frequencyCount))
if (help)
{
printHelp();
printHelp(args[0]);
return 0;
}
importDirs ~= loadConfig();
if (tokenCount)
auto optionCount = count!"a"([sloc, highlight, ctags, json, tokenCount]);
if (optionCount > 1)
{
import core.memory;
/+if (args.length == 1)
{
writeln((cast (ubyte[]) stdin.byLine(KeepTerminator.yes).join()).byToken().walkLength());
stderr.writeln("Too many options specified");
return 1;
}
else
{+/
else if (optionCount < 1)
{
printHelp(args[0]);
return 1;
}
if (tokenCount || sloc)
{
LexerConfig config;
config.tokenStyle = TokenStyle.doNotReplaceSpecial;
foreach (arg; args[1..$])
ulong[] counts = new ulong[args.length - 1];
foreach (i, arg; parallel(args[1..$]))
{
config.fileName = arg;
uint count;
auto f = File(arg);
ubyte[] buffer = uninitializedArray!(ubyte[])(f.size);
foreach(t; byToken(f.rawRead(buffer), config))
foreach (t; byToken(f.rawRead(buffer), config))
{
++count;
if (tokenCount)
++counts[i];
else if (isLineOfCode(t.type))
++counts[i];
}
writefln("%s: %d", arg, count);
}
/+}+/
}
/+else if (frequencyCount)
{
uint[TokenType] frequency;
foreach (t; byToken(cast(ubyte[]) File(args[1]).byLine(KeepTerminator.yes).join()))
{
frequency[t.type]++;
}
foreach (k, v; frequency)
{
writeln(v, ":", cast(TokenType) k);
}
}+/
/+if (sloc)
{
if (args.length == 1)
{
writeln(stdin.byLine(KeepTerminator.yes).join().byToken().count!(a => isLineOfCode(a.type))());
}
else
{
writeln(args[1..$].map!(a => File(a).byLine(KeepTerminator.yes).join().byToken(a))()
.joiner().count!(a => isLineOfCode(a.type))());
foreach(i; 0 .. counts.length)
writefln("%s: %d", args[i + 1], counts[i]);
}
return 0;
}+/
if (highlight)
else if (highlight)
{
LexerConfig config;
config.iterStyle = IterationStyle.everything;
config.tokenStyle = TokenStyle.source;
File f = args.length == 1 ? stdin : File(args[1]);
highlighter.highlight((cast(ubyte[]) f.byLine(KeepTerminator.yes).join()).byToken(config),
ubyte[] buffer = uninitializedArray!(ubyte[])(f.size);
highlighter.highlight(byToken(f.rawRead(buffer), config),
args.length == 1 ? "stdin" : args[1]);
return 0;
}
/+if (dotComplete || parenComplete)
{
if (isAbsolute(args[1]))
importDirs ~= dirName(args[1]);
else
importDirs ~= getcwd();
Token[] tokens;
try
{
to!size_t(args[1]);
auto f = appender!string();
char[] buf;
while (stdin.readln(buf))
f.put(buf);
tokens = f.data.byToken().array();
}
catch(ConvException e)
{
tokens = args[1].readText().byToken().array();
args.popFront();
}
auto mod = parseModule(tokens);
CompletionContext context = new CompletionContext(mod);
context.importDirectories = importDirs;
foreach (im; parallel(mod.imports))
{
auto p = findAbsPath(importDirs, im);
if (p is null || !p.exists())
continue;
context.addModule(p.readText().byToken().array().parseModule());
}
auto complete = AutoComplete(tokens, context);
if (parenComplete)
writeln(complete.parenComplete(to!size_t(args[1])));
else if (dotComplete)
writeln(complete.dotComplete(to!size_t(args[1])));
return 0;
}+/
/+if (json)
{
CircularBuffer!(Token) tokens;
File f = args.length == 1 ? stdin : File(args[1]);
tokens = new CircularBuffer!(Token)(CIRC_BUFF_SIZE,
f.byLine(KeepTerminator.yes).join().byToken!(char[])());
auto mod = parseModule(tokens);
mod.writeJSONTo(stdout);
return 0;
}+/
// if (ctags)
// {
// if (!recursiveCtags)
// {
// auto tokens = byToken(readText(args[1]));
// auto mod = parseModule(tokens.array());
// mod.writeCtagsTo(stdout, args[1]);
// }
// else
// {
// Module m;
// foreach (dirEntry; dirEntries(args[1], SpanMode.breadth))
// {
// if (!dirEntry.name.endsWith(".d", ".di"))
// continue;
// stderr.writeln("Generating tags for ", dirEntry.name);
// auto tokens = byToken(readText(dirEntry.name));
// if (m is null)
// m = parseModule(tokens.array());
// else
// {
// auto mod = parseModule(tokens.array());
// m.merge(mod);
// }
// }
// m.writeCtagsTo(stdout, "");
// }
// }
return 0;
}
void printHelp()
void printHelp(string programName)
{
writeln(
q{
Usage: dscanner options
writefln(
`
Usage: %s options
options:
--help | -h
Prints this help message
--sloc [sourceFiles]
--sloc | -l [sourceFiles]
count the number of logical lines of code in the given
source files. If no files are specified, a file is read from stdin.
--json [sourceFile]
--json | -j [sourceFile]
Generate a JSON summary of the given source file. If no file is
specifed, the file is read from stdin.
--dotComplete [sourceFile] cursorPosition
--dotComplete | -d [sourceFile] cursorPosition
Provide autocompletion for the insertion of the dot operator. The cursor
position is the character position in the *file*, not the position in
the line. If no file is specified, the file is read from stdin.
--parenComplete [sourceFile] cursorPosition
Provides a listing of function parameters or pre-defined version
--parenComplete | -p [sourceFile] cursorPosition
Provide a listing of function parameters or pre-defined version
identifiers at the cursor position. The cursor position is the character
position in the *file*, not the line. If no file is specified, the
contents are read from stdin.
--symbolComplete | -s [sourceFile] cursorPosition
Provide a listing of classes, structs, interfaces, variables, functions,
and methods available in the current scope that begin with the text
before the cursor position.
--highlight [sourceFile] - Syntax-highlight the given source file. The
resulting HTML will be written to standard output.
@ -311,12 +219,13 @@ options:
well as any paths specified in /etc/dmd.conf. This is only used for the
--parenComplete and --dotComplete options.
--ctags sourceFile
--ctags | -c sourceFile
Generates ctags information from the given source code file. Note that
ctags information requires a filename, so stdin cannot be used in place
of a filename.
--recursive | -R | -r directory
When used with --ctags, dscanner will produce ctags output for all .d
and .di files contained within directory and its sub-directories.});
and .di files contained within directory and its sub-directories.`,
programName);
}

View File

@ -1100,10 +1100,8 @@ private:
foreach (b; std.range.chunks(src.slice[2 .. $ - 1], 2))
{
auto s = cast(char[])b;
dchar ch = cast(dchar)parse!uint(s, 16);
char[4] utf8Buf;
size_t utf8Size = encode(utf8Buf, ch);
a.put(utf8Buf[0..utf8Size]);
ubyte ch = cast(ubyte)parse!uint(s, 16);
a.put(ch);
}
// can safely assume ownership of data
current.value = cast(string)a.data;
@ -1915,18 +1913,19 @@ private:
}
body
{
static size_t reencodeNumeric(ubyte[] src, int radix, OutputRange dest)
size_t reencodeNumeric(ubyte[] src, int radix, OutputRange dest)
{
/*scope(failure) //TODO: get rid of std.stdio in lexer
{
import std.stdio;
("Failed on line ", lineNumber, " of file ",
config.fileName);
}*/
char[] chunk = cast(char[])src;
char[4] utfBuf;
uint codepoint = parse!uint(chunk, radix);
size_t len = encode(utfBuf, codepoint);
size_t len;
try
len = encode(utfBuf, codepoint);
catch (UTFException ex)
{
errorMessage(ex.msg);
return 0;
}
dest.put(cast(ubyte[]) utfBuf[0..len]);
return len;
}
@ -2158,169 +2157,169 @@ private:
}
/**
* Returns: true if the token is an operator
*/
* Returns: true if the token is an operator
*/
pure nothrow bool isOperator(const TokenType t)
{
return t >= TokenType.assign && t <= TokenType.xorEquals;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isOperator(ref const Token t)
{
return isOperator(t.type);
}
/**
* Returns: true if the token is a keyword
*/
* Returns: true if the token is a keyword
*/
pure nothrow bool isKeyword(const TokenType t)
{
return t >= TokenType.bool_ && t <= TokenType.with_;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isKeyword(ref const Token t)
{
return isKeyword(t.type);
}
/**
* Returns: true if the token is a built-in type
*/
* Returns: true if the token is a built-in type
*/
pure nothrow bool isType(const TokenType t)
{
return t >= TokenType.bool_ && t <= TokenType.wchar_;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isType(ref const Token t)
{
return isType(t.type);
}
/**
* Returns: true if the token is an attribute
*/
* Returns: true if the token is an attribute
*/
pure nothrow bool isAttribute(const TokenType t)
{
return t >= TokenType.align_ && t <= TokenType.static_;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isAttribute(ref const Token t)
{
return isAttribute(t.type);
}
/**
* Returns: true if the token is a protection attribute
*/
* Returns: true if the token is a protection attribute
*/
pure nothrow bool isProtection(const TokenType t)
{
return t >= TokenType.export_ && t <= TokenType.public_;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isProtection(ref const Token t)
{
return isProtection(t.type);
}
/**
* Returns: true if the token is a compile-time constant such as ___DATE__
*/
* Returns: true if the token is a compile-time constant such as ___DATE__
*/
pure nothrow bool isConstant(const TokenType t)
{
return t >= TokenType.date && t <= TokenType.traits;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isConstant(ref const Token t)
{
return isConstant(t.type);
}
/**
* Returns: true if the token is a string or number literal
*/
* Returns: true if the token is a string or number literal
*/
pure nothrow bool isLiteral(const TokenType t)
{
return t >= TokenType.doubleLiteral && t <= TokenType.wstringLiteral;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isLiteral(ref const Token t)
{
return isLiteral(t.type);
}
/**
* Returns: true if the token is a number literal
*/
* Returns: true if the token is a number literal
*/
pure nothrow bool isNumberLiteral(const TokenType t)
{
return t >= TokenType.doubleLiteral && t <= TokenType.ulongLiteral;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isNumberLiteral(ref const Token t)
{
return isNumberLiteral(t.type);
}
/**
* Returns: true if the token is a string literal
*/
* Returns: true if the token is a string literal
*/
pure nothrow bool isStringLiteral(const TokenType t)
{
return t >= TokenType.dstringLiteral && t <= TokenType.wstringLiteral;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isStringLiteral(ref const Token t)
{
return isStringLiteral(t.type);
}
/**
* Returns: true if the token is whitespace, a commemnt, a special token
* sequence, or an identifier
*/
* Returns: true if the token is whitespace, a commemnt, a special token
* sequence, or an identifier
*/
pure nothrow bool isMisc(const TokenType t)
{
return t >= TokenType.comment && t <= TokenType.specialTokenSequence;
}
/**
* ditto
*/
* ditto
*/
pure nothrow bool isMisc(ref const Token t)
{
return isMisc(t.type);
}
/**
* Listing of all the tokens in the D language.
*/
* Listing of all the tokens in the D language.
*/
enum TokenType: ushort
{
assign, /// =