有没有办法在运行时通过程序识别代码文件中的保留字?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有没有办法在运行时通过程序识别代码文件中的保留字?相关的知识,希望对你有一定的参考价值。
有没有办法通过c#程序识别代码文件中的保留字?我认为有一个地方,c#保留其令牌。在哪里我可以匹配我的文件字并将它们标识为保留字或不是保留字。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string filepath = @"C:UsersyasirDocumentsVisual Studio
2017ProjectsConsoleApp1ConsoleApp1ProductsController.cs";
StreamReader sreader = new StreamReader(filepath);
//path of the file
var lineCount = File.ReadAllLines(filepath).Count(line =>
!string.IsNullOrWhiteSpace(line));
string strFileContent = sreader.ReadToEnd(); //Read all the content
sreader.Close();
string[] words = strFileContent.Split(new char[] { ' ', '
', '
'
}, StringSplitOptions.RemoveEmptyEntries);
//Split by words and remove new lines empty entries
foreach( string ew in words)
{
//here i have to write check reserved word logic...
}
Console.Write(lineCount);
Console.ReadKey();
}
}
}
答案
首先,我将外部文件中的所有单词都列入列表
string[] words = strFileContent.Split(new char[] { ' ', '
', '
',
'.', ';' }, StringSplitOptions.RemoveEmptyEntries);
然后我列出了令牌
List<string> keywords = new List<string>
{"id","set","return","Id","EventHandler<EventArgs>",
"eventhandler","event","collections","system",
"abstract", "as", "base", "bool", "break", "byte", "case", "catch",
"char", "checked", "class", "const", "continue", "decimal", "default",
"delegate", "do", "double", "else", "enum", "event", "explicit",
"extern", "false", "finally", "fixed", "float", "for", "foreach",
"goto", "if", "implicit", "in", "int", "interface", "internal", "is",
"lock", "long", "namespace", "new", "null", "object", "operator", "out",
"override", "params", "private", "protected", "public", "readonly",
"ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc",
"static", "string", "struct", "switch", "this", " throw", "true", "try",
"typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using",
"using", "static", "virtual", "void", "volatile", "while" };
然后将文件中的每个单词与此列表中的每个元素进行比较,我们将得到保留字
var ikeywords = words.Select(i =>
i.ToString()).Intersect(keywords).ToList();
我写了这个完整的程序
- 识别保留字
- 计算外部文件行
-identify和删除评论
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string filepath = @"C:UsersyasirDocumentsVisual Studio 2017ProjectsConsoleApp1ConsoleApp1Product.cs";
//1.getting file count
int linecount = CountLines(filepath);
Console.WriteLine($"total number of lines in program file is:{linecount}");
//2.identifying reserved words
ReservedWords(filepath);
//discarding comments
Console.WriteLine( DeleteComments(filepath));
Console.ReadKey();
}
public static void ReservedWords(string filepath)
{
StreamReader sreader = new StreamReader(filepath);
string strFileContent = sreader.ReadToEnd();
string[] words = strFileContent.Split(new char[] { ' ', '
', '
', '.', ';' }, StringSplitOptions.RemoveEmptyEntries);
List<string> keywords = new List<string> { "id","set","return","Id","EventHandler<EventArgs>","eventhandler","event","collections","system","abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", " throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "using", "static", "virtual", "void", "volatile", "while" };
var ikeywords = words.Select(i => i.ToString()).Intersect(keywords).ToList();
int result = 0;
foreach (string ew in ikeywords)
{
result = result + 1;
Console.WriteLine(ew + Environment.NewLine);
}
Console.WriteLine($"total number of keywords in program file is:{result}");
}
public static int CountLines(string filepath)
{
StreamReader sreader = new StreamReader(filepath);
string strFileContent = sreader.ReadToEnd(); //Read all the content
string[] words = strFileContent.Split(new char[] { ' ', '
', '
', '.', ';' }, StringSplitOptions.RemoveEmptyEntries);
//Split by words and remove new lines empty entries
sreader.Close();
var lineCount = File.ReadAllLines(filepath).Count(line => !string.IsNullOrWhiteSpace(line));
return lineCount;
}
public static string DeleteComments(string filename)
{
Regex comments1 = new Regex(@"//.*?
",RegexOptions.Multiline);
Regex comments2 = new Regex(@"s*/*.*? */s*",RegexOptions.Singleline);
string all_text = File.ReadAllText(filename);
string afterRemoval = comments1.Replace(all_text, " ");
afterRemoval = comments2.Replace(afterRemoval, " ");
return afterRemoval;
}
}
}
以上是关于有没有办法在运行时通过程序识别代码文件中的保留字?的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法在 TypeScript 类定义中转义和使用保留字?