?? lexicalanalyser.cs
字號:
?using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Lane.Compiler.WordAnalyser.Core
{
public class LexicalAnalyser
{
private string source;
private Regex re = new Regex(Properties.Resources.CRegex, RegexOptions.IgnorePatternWhitespace);
private List<Token> tokens = new List<Token>();
public string Source
{
get { return source; }
set { SetSource(value); }
}
public Token[] Tokens
{
get { return tokens.ToArray(); }
}
public LexicalAnalyser() { }
private void SetSource(string value)
{
// Prepare
source = value;
tokens.Clear();
// Lexical Analysis
Token t = null;
Match m = re.Match(source);
while (m.Success)
{
t = NewToken(m);
System.Diagnostics.Trace.WriteLineIf(t == null, m);
tokens.Add(t);
m = m.NextMatch();
}
}
private Token NewToken(Match match)
{
string gname = null;
if ((gname = GetGroupName(match.Groups)) != null)
{
string group = gname;
string value = match.Value;
int index = match.Index;
Token t = new Token(group, value, index);
return t;
}
else
return null;
}
private string GetGroupName(GroupCollection gc)
{
int i = 0;
foreach (string s in re.GetGroupNames())
{
Group g = gc[s];
if (int.TryParse(s, out i))
continue;
if (g.Success)
return s;
}
return null;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -