CPF/CPF.ReoGrid/Formula/ExcelFormulaLexer.cs

126 lines
2.7 KiB
C#
Raw Normal View History

2024-06-24 10:15:59 +08:00
using System;
using System.Text.RegularExpressions;
namespace CPF.ReoGrid.Formula
{
internal class ExcelFormulaLexer
{
public IWorkbook Workbook { get; set; }
public Cell Cell { get; set; }
public string Input { get; set; }
public int Start { get; set; }
public int Length { get; set; }
public int Index
{
get
{
return (this.match == null) ? 0 : (this.match.Groups["token"].Success ? this.match.Groups["token"].Index : this.match.Index);
}
}
public int CommittedLength { get; set; }
public Match CurrentToken
{
get
{
return this.match;
}
}
public ExcelFormulaLexer(IWorkbook workbook, Cell cell, string input) : this(workbook, cell, input, 0, input.Length)
{
}
public ExcelFormulaLexer(IWorkbook workbook, Cell cell, string input, int start, int length)
{
this.Workbook = workbook;
this.Cell = cell;
this.Input = input;
this.Start = start;
this.Length = length;
this.Reset();
}
public void NextToken()
{
bool flag = this.match != null;
if (flag)
{
this.CommittedLength += this.match.Length;
bool flag2 = this.CommittedLength >= this.Length;
if (flag2)
{
this.match = null;
}
else
{
this.match = this.match.NextMatch();
}
}
else
{
this.match = null;
}
}
public bool SkipToken(string value)
{
return this.SkipToken("token", value);
}
public bool SkipToken(string groupName, string value)
{
bool flag = this.IsMatch(groupName, value);
bool result;
if (flag)
{
this.NextToken();
result = true;
}
else
{
result = false;
}
return result;
}
public bool IsToken(string token)
{
return this.IsMatch("token", token);
}
public bool IsMatch(string groupName)
{
return this.IsMatch(groupName, null);
}
public bool IsMatch(string groupName, string value)
{
return this.match != null && this.match.Groups[groupName].Success && (value == null || this.match.Groups[groupName].Value.Equals(value));
}
public void Reset()
{
this.CommittedLength = 0;
this.match = ExcelFormulaLexer.TokenRegex.Match(this.Input, this.Start);
}
private static readonly Regex TokenRegex = new Regex(string.Concat(new string[]
{
"\\s*((?<string>\"(?:\"\"|[^\"])*\")|(?<union_ranges>[A-Z]+[0-9]+:[A-Z]+[0-9]+(\\s[A-Z]+[0-9]+:[A-Z]+[0-9]+)+)|(?<range>\\$?[A-Z]+\\$?[0-9]*:\\$?[A-Z]+\\$?[0-9]*)|(?<cell>\\$?[A-Z]+\\$?[0-9]+)|(?<token>-)|(?<number>\\-?\\d*\\",
FormulaExtension.NumberDecimalSeparator,
"?\\d+)|(?<true>(?i)TRUE)|(?<false>(?i)FALSE)|(?<identifier>\\w+)|(?<token>\\=\\=|\\<\\>|\\<\\=|\\>\\=|\\<\\>|\\=|\\!|[\\=\\.\\",
FormulaExtension.ParameterSeparator,
"\\+\\-\\*\\/\\%\\<\\>\\(\\)\\&\\^]))"
}), RegexOptions.Compiled);
private Match match;
}
}