📄 buildkeywords.pl
字号:
#!/bin/perl
# File names
$keyword_file = "CSharpKeywordList.txt";
$keywords_outfile = "Keywords.cs";
$tokens_outfile = "Tokens.cs";
$ATGTokensSection = "ATGTokensSection.gen";
#read infile
print "\n";
print "Reading keyword definition from '$keyword_file'.\n";
open(DAT, $keyword_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);
print "done.\n";
#analyse infile
print "starting analysation ... this could take a few minutes.\n";
foreach (@raw_data) {
if ($_=~/\A\s*\$(\w+)\s*=\s*(\S+)/) {
#properties form: $PROPERTY = "VALUE"
$properties{$1} = $2;
} elsif ($_=~/\A\s*(\w+)\s*=\s*\"(\S+)\"/) {
#special characters form: name = "VALUE"
$special_chars[$#special_chars + 1] = $1;
$special_values[$#special_values + 1] = $2;
} elsif ($_=~/\A\s*\"(\S+)\s*\"/) {
#special keywords form: "VALUE"
$keywords[$#keywords + 1] = $1
} elsif ($_=~/\A\s*(\w+)\s*/) {
#special terminal classes form: name
$terminals[$#terminals + 1] = $1
}
}
for ($i=0; $i <= $#keywords; $i++) {
$upperKeywords[$i] = uc $keywords[$i];
}
sort (ascend @upperKeywords);
sort (ascend @keywords);
print "done.\n";
#write output
print "writing output files.\nIf your computer doesn抰 respond, then press \"Ctrl-Alt-Delete\"\n";
print "\n";
&write_keywordfile;
print "\n";
&write_tokensfile;
print "\n";
&write_atgtokensfile;
print "\n";
print "finished.\n";
sub write_keywordfile {
print " ->Generating Keywords class to file '$keywords_outfile'\n";
open(DAT,">$keywords_outfile") || die("Cannot Open File");
print DAT "// this file was autogenerated by a tool.\n";
print DAT "using System;\n";
print DAT "using System.Collections;\n";
print DAT "using System.Text;\n";
print DAT "\n";
print DAT "namespace " . $properties{'Namespace'} . "\n";
print DAT "{\n";
print DAT " public class Keywords\n";
print DAT " {\n";
print DAT " static readonly string[] keywordList = {\n";
if ($properties{'UpperCaseKeywords'} eq "True") {
for ($i=0; $i <= $#upperKeywords; $i++) {
print DAT " \"$upperKeywords[$i]\"";
if ($i + 1 <= $#upperKeywords) {
print DAT ",";
}
print DAT "\n";
}
} else {
for ($i=0; $i <= $#keywords; $i++) {
print DAT " \"$keywords[$i]\"";
if ($i + 1 <= $#keywords) {
print DAT ",";
}
print DAT "\n";
}
}
print DAT " };\n";
print DAT " \n";
print DAT " static Hashtable keywords = new Hashtable();\n";
print DAT " \n";
print DAT " static Keywords()\n";
print DAT " {\n";
print DAT " for (int i = 0; i < keywordList.Length; ++i) {\n";
$up = ucfirst($keywords[0]);
print DAT " keywords.Add(keywordList[i], i + Tokens.$up);\n";
print DAT " }\n";
print DAT " }\n";
print DAT " \n";
print DAT " public static bool IsKeyword(string identifier)\n";
print DAT " {\n";
if ($properties{'UpperCaseKeywords'} eq "True") {
print DAT " return keywords[identifier.ToUpper()] != null;\n";
} else {
print DAT " return keywords[identifier] != null;\n";
}
print DAT " }\n";
print DAT " \n";
print DAT " public static int GetToken(string keyword)\n";
print DAT " {\n";
if ($properties{'UpperCaseKeywords'} eq "True") {
print DAT " return (int)keywords[keyword.ToUpper()];\n";
} else {
print DAT " return (int)keywords[keyword];\n";
}
print DAT " }\n";
print DAT " }\n";
print DAT "}\n";
close(DAT);
print " ->done.\n";
}
sub write_token {
$formattedString = sprintf("%-20s", ucfirst($tokenName));
if ($tokenName eq "GetType") {
print DAT " new public const int $formattedString = $tokenValue;";
} else {
print DAT " public const int $formattedString = $tokenValue;";
}
$tokenValue++;
}
sub write_tokensfile {
print " ->Generating Tokens class to file '$tokens_outfile'\n";
open(DAT,">$tokens_outfile") || die("Cannot Open File");
print DAT "// this file was autogenerated by a tool.\n";
print DAT "using System;\n";
print DAT "\n";
print DAT "namespace " . $properties{'Namespace'} . "\n";
print DAT "{\n";
print DAT " public sealed class Tokens\n";
print DAT " {\n";
$tokenValue = 0;
print DAT " // ----- terminal classes -----\n";
foreach (@terminals) {
$tokenName = $_;
write_token();
print DAT "\n";
}
print DAT "\n";
print DAT " // ----- special character -----\n";
for ($i=0; $i <= $#special_chars; $i++) {
$tokenName = $special_chars[$i];
write_token();
print DAT " // $special_values[$i] \n";
}
print DAT "\n";
print DAT " // ----- keywords -----\n";
foreach (@keywords) {
$tokenName = $_;
write_token();
print DAT "\n";
}
print DAT "\n";
print DAT " public static string GetTokenString(int token)\n";
print DAT " {\n";
print DAT " switch (token) {\n";
for ($i = 0; $i <= $#special_chars; $i++) {
print DAT " case $special_chars[$i]:\n";
print DAT " return \"$special_values[$i]\";\n";
}
foreach (@keywords) {
$up = ucfirst($_);
print DAT " case $up:\n";
print DAT " return \"$_\";\n";
}
print DAT " }\n";
print DAT " throw new System.NotSupportedException(\"Unknown token:\" + token);\n";
print DAT " }\n";
print DAT " }\n";
print DAT "}\n";
close(DAT);
print " ->done.\n";
}
sub write_atgtokensfile {
print " ->Generating ATG TOKENS section and writing it to file '$ATGTokensSection'\n";
open(DAT,">$ATGTokensSection") || die("Cannot Open File");
print DAT "/* START AUTOGENERATED TOKENS SECTION */\n";
print DAT "TOKENS\n";
print DAT " /* ----- terminal classes ----- */\n";
print DAT " /* EOF is 0 */\n";
foreach $term (@terminals) {
if ($term eq "EOF") {
} elsif ($term eq "Identifier") {
print DAT "\tident\n";
} else {
print DAT "\t$term\n";
}
}
print DAT "\n";
print DAT " /* ----- special character ----- */\n";
foreach (@special_values) {
print DAT "\t\"$_\"\n";
}
print DAT "\n";
print DAT " /* ----- keywords ----- */\n";
foreach (@keywords) {
print DAT "\t\"$_\"\n";
}
print DAT "/* END AUTOGENERATED TOKENS SECTION */\n";
close(DAT);
print " ->done.\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -