Massive code refactoring.

This commit is contained in:
Hackerpilot 2013-02-02 20:13:19 +00:00
parent fe7a6bf4b0
commit 9e0c334276
5 changed files with 1992 additions and 2407 deletions

View File

@ -182,10 +182,7 @@ string[] opkwds = [
null, // Comment null, // Comment
null, // Identifier null, // Identifier
null, // ScriptLine null, // ScriptLine
"__argTypes",
"__thread",
"__traits", "__traits",
"__overloadset",
"__parameters", "__parameters",
"__vector", "__vector",
null, // Whitespace null, // Whitespace
@ -216,7 +213,7 @@ immutable string opKwdValues =
~ "newnothrownulloverridepurerefreturnstructsuperswitchtemplatethistruetry" ~ "newnothrownulloverridepurerefreturnstructsuperswitchtemplatethistruetry"
~ "typedeftypeidtypeofunionunittestversionvolatilewhilewith__traits" ~ "typedeftypeidtypeofunionunittestversionvolatilewhilewith__traits"
~ "__vector__parameters__DATE__EOF__TIME__TIMESTAMP__VENDOR__VERSION__" ~ "__vector__parameters__DATE__EOF__TIME__TIMESTAMP__VENDOR__VERSION__"
~ "FILE__LINE__overloadset__argTypes__thread"; ~ "FILE__LINE__";
void main(string[] args) void main(string[] args)
{ {

View File

@ -19,7 +19,8 @@ void writeSpan(string cssClass, string value)
// http://ethanschoonover.com/solarized // http://ethanschoonover.com/solarized
void highlight(R)(R tokens) void highlight(R)(R tokens)
{ {
stdout.writeln(q"[<!DOCTYPE html> stdout.writeln(q"EOS
<!DOCTYPE html>
<html> <html>
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
@ -34,7 +35,8 @@ html { background-color: #fdf6e3; color: #002b36; }
.type { color: #268bd2; font-weight: bold; } .type { color: #268bd2; font-weight: bold; }
.cons { color: #859900; font-weight: bold; } .cons { color: #859900; font-weight: bold; }
</style> </style>
<pre>]"); <pre>
EOS");
foreach (Token t; tokens) foreach (Token t; tokens)
{ {
@ -55,3 +57,13 @@ html { background-color: #fdf6e3; color: #002b36; }
} }
stdout.writeln("</pre>\n</body></html>"); stdout.writeln("</pre>\n</body></html>");
} }
/+void main(string[] args)
{
LexerConfig config;
config.tokenStyle = TokenStyle.source;
config.iterStyle = IterationStyle.everything;
config.fileName = args[1];
auto f = File(args[1]);
(cast(ubyte[]) f.byLine(KeepTerminator.yes).join()).byToken(config).highlight();
}+/

23
main.d
View File

@ -146,10 +146,21 @@ int main(string[] args)
} }
else else
{+/ {+/
writeln(args[1..$].map!(a => byToken(cast(ubyte[]) File(a).byLine(KeepTerminator.yes).join(), a).walkLength())()); LexerConfig config;
foreach (arg; args[1..$])
{
config.fileName = arg;
uint count;
foreach(t; byToken(cast(ubyte[]) File(arg).byLine(KeepTerminator.yes).join(), config))
{
writeln(t);
++count;
}
writefln("%s: %d", arg, count);
}
/+}+/ /+}+/
} }
else if (frequencyCount) /+else if (frequencyCount)
{ {
uint[TokenType] frequency; uint[TokenType] frequency;
foreach (t; byToken(cast(ubyte[]) File(args[1]).byLine(KeepTerminator.yes).join())) foreach (t; byToken(cast(ubyte[]) File(args[1]).byLine(KeepTerminator.yes).join()))
@ -160,7 +171,7 @@ int main(string[] args)
{ {
writeln(v, ":", cast(TokenType) k); writeln(v, ":", cast(TokenType) k);
} }
} }+/
/+if (sloc) /+if (sloc)
{ {
@ -178,9 +189,11 @@ int main(string[] args)
if (highlight) if (highlight)
{ {
LexerConfig config;
config.iterStyle = IterationStyle.everything;
config.tokenStyle = TokenStyle.source;
File f = args.length == 1 ? stdin : File(args[1]); File f = args.length == 1 ? stdin : File(args[1]);
highlighter.highlight((cast(ubyte[]) f.byLine(KeepTerminator.yes).join()).byToken( highlighter.highlight((cast(ubyte[]) f.byLine(KeepTerminator.yes).join()).byToken(config));
"", IterationStyle.everything, TokenStyle.source));
return 0; return 0;
} }

View File

@ -463,7 +463,7 @@ Module parseModule(TokenBuffer tokens, string protection = "public", string[] at
if (tokens.front == TokenType.lParen) if (tokens.front == TokenType.lParen)
{ {
mod.functions ~= parseFunction(tokens, type, name, mod.functions ~= parseFunction(tokens, type, name,
tokens.front.lineNumber, tokens.front.line,
localProtection.empty() ? protection : localProtection, localProtection.empty() ? protection : localProtection,
attributes ~ localAttributes); attributes ~ localAttributes);
} }
@ -474,7 +474,7 @@ Module parseModule(TokenBuffer tokens, string protection = "public", string[] at
v.type = type; v.type = type;
v.attributes = localAttributes ~ attributes; v.attributes = localAttributes ~ attributes;
v.protection = localProtection.empty() ? protection : localProtection; v.protection = localProtection.empty() ? protection : localProtection;
v.line = tokens.front.lineNumber; v.line = tokens.front.line;
mod.variables ~= v; mod.variables ~= v;
} }
resetLocals(); resetLocals();
@ -500,7 +500,7 @@ Module parseModule(TokenBuffer tokens, string protection = "public", string[] at
if (!tokens.empty && tokens.front == TokenType.lParen) if (!tokens.empty && tokens.front == TokenType.lParen)
{ {
mod.functions ~= parseFunction(tokens, "", name, mod.functions ~= parseFunction(tokens, "", name,
tokens.peek(-1).lineNumber, tokens.peek(-1).line,
localProtection.empty() ? protection : localProtection, localProtection.empty() ? protection : localProtection,
localAttributes ~ attributes); localAttributes ~ attributes);
} }
@ -565,7 +565,7 @@ in
body body
{ {
Enum e = new Enum; Enum e = new Enum;
e.line = tokens.front.lineNumber; e.line = tokens.front.line;
tokens.popFront(); tokens.popFront();
string enumType; string enumType;
e.protection = protection; e.protection = protection;
@ -581,7 +581,7 @@ body
EnumMember m; EnumMember m;
m.type = tokens.front.value; m.type = tokens.front.value;
tokens.popFront(); tokens.popFront();
m.line = tokens.front.lineNumber; m.line = tokens.front.line;
e.name = m.name = tokens.front.value; e.name = m.name = tokens.front.value;
e.members ~= m; e.members ~= m;
tokens.skipBlockStatement(); tokens.skipBlockStatement();
@ -593,7 +593,7 @@ body
e.name = tokens.front.value; e.name = tokens.front.value;
EnumMember m; EnumMember m;
m.name = e.name; m.name = e.name;
m.line = tokens.front.lineNumber; m.line = tokens.front.line;
m.type = getTypeFromToken(tokens.peek(2)); m.type = getTypeFromToken(tokens.peek(2));
e.members ~= m; e.members ~= m;
tokens.skipBlockStatement(); tokens.skipBlockStatement();
@ -627,7 +627,7 @@ enumBody:
// EnumMember m; // EnumMember m;
// if (isIdentifierOrType(r.front) && i + 1 < r.length && isIdentifierOrType(r[i + 1])) // if (isIdentifierOrType(r.front) && i + 1 < r.length && isIdentifierOrType(r[i + 1]))
// { // {
// m.line = r[i + 1].lineNumber; // m.line = r[i + 1].line;
// m.name = r[i + 1].value; // m.name = r[i + 1].value;
// m.type = r.front.value; // m.type = r.front.value;
// } // }
@ -637,12 +637,12 @@ enumBody:
// m.type = getTypeFromToken(r[i + 2]); // m.type = getTypeFromToken(r[i + 2]);
// else // else
// m.type = enumType; // m.type = enumType;
// m.line = r.front.lineNumber; // m.line = r.front.line;
// m.name = r.front.value; // m.name = r.front.value;
// } // }
// else // else
// { // {
// m.line = r.front.lineNumber; // m.line = r.front.line;
// m.name = r.front.value; // m.name = r.front.value;
// m.type = enumType == null ? "int" : enumType; // m.type = enumType == null ? "int" : enumType;
// } // }
@ -787,7 +787,7 @@ body
} }
else else
{ {
v.line = r.front.lineNumber; v.line = r.front.line;
v.name = r.front.value; v.name = r.front.value;
r.popFront(); r.popFront();
appender.put(v); appender.put(v);
@ -845,7 +845,7 @@ Struct parseStructOrUnion(TokenBuffer tokens, string protection,
string[] attributes) string[] attributes)
{ {
Struct s = new Struct; Struct s = new Struct;
s.line = tokens.front.lineNumber; s.line = tokens.front.line;
s.attributes = attributes; s.attributes = attributes;
s.protection = protection; s.protection = protection;
s.name = tokens.front.value; s.name = tokens.front.value;
@ -893,7 +893,7 @@ body
Inherits parseInherits(TokenBuffer tokens, string protection, string[] attributes) Inherits parseInherits(TokenBuffer tokens, string protection, string[] attributes)
{ {
auto i = new Inherits; auto i = new Inherits;
i.line = tokens.front.lineNumber; i.line = tokens.front.line;
i.name = tokens.front.value; i.name = tokens.front.value;
tokens.popFront(); tokens.popFront();
i.protection = protection; i.protection = protection;
@ -966,7 +966,7 @@ body
if (tokens.front == TokenType.identifier) if (tokens.front == TokenType.identifier)
{ {
a.name = tokens.front.value; a.name = tokens.front.value;
a.line = tokens.front.lineNumber; a.line = tokens.front.line;
skipBlockStatement(tokens); skipBlockStatement(tokens);
} }
else else

File diff suppressed because it is too large Load Diff