/// <summary> /// Add Delimiters Lex items, and recomand to add composite delimiters first. /// </summary> /// <param name="name">Name of Lex items</param> /// <param name="formatCapTextDelegate">The delegate of formatting the result of regExprs</param> /// <param name="regexOptions">Regular Expression Options</param> /// <param name="regExprs">Regular Expressions of Lex items</param> public void AddDelimiters(string name = "Delimiter", FormatCapTextDelegate formatCapTextDelegate = null, RegexOptions regexOptions = RegexOptions.None, params string[] regExprs) { for (int i = 0; i < regExprs.Length; i++) { AddLexItem(name, regExprs[i], formatCapTextDelegate, regexOptions, CurrentAddGroupNumber); } }
/// <summary> /// Construct method /// </summary> /// <param name="name">Name of Lex Item</param> /// <param name="formatCapText">The delegate of formatting the result of RegExpr</param> /// <exception cref="IlleagalNameException">if name start with '@'</exception> public LexItem(string name, FormatCapTextDelegate formatCapText) { if (Regex.Match(name, "^@.+").Success) { throw new IlleagalNameException(name, "Illeagal Name: " + name + ". Name cannot start with '@'."); } Name = name; FormatCapText = formatCapText; Regex = null; }
/// <summary> /// Add Constants Lex items, and recomand to add composite constants first. /// </summary> /// <param name="name">Name of Lex items</param> /// <param name="formatCapTextDelegate">The delegate of formatting the result of regExprs</param> /// <param name="regexOptions">Regular Expression Options</param> /// <param name="isAddZeroWidthAssertion">Is auto add <c>(?=\w|$)</c> to the end of RegExprs</param> /// <param name="regExprs">Regular Expressions of Lex items</param> public void AddConstants(string name = "Constant", FormatCapTextDelegate formatCapTextDelegate = null, RegexOptions regexOptions = RegexOptions.None, bool isAddZeroWidthAssertion = true, params string[] regExprs) { for (int i = 0; i < regExprs.Length; i++) { if (!regExprs[i].EndsWith(@"(?=\W|$)") && isAddZeroWidthAssertion) { regExprs[i] += @"(?=\W|$)"; } AddLexItem(name, regExprs[i], formatCapTextDelegate, regexOptions, CurrentAddGroupNumber); } }
/// <summary> /// Add reserved words Lex items, and recomand to do this before add indentifier Lex item. /// </summary> /// <param name="name">Name of Lex items</param> /// <param name="formatCapTextDelegate">The delegate of formatting the result of regExprs</param> /// <param name="regexOptions">Regular Expression Options</param> /// <param name="regExprs">Regular Expressions of Lex items, and auto add <c>(?=\W|$)</c> to the end</param> public void AddResWords(string name = "ResWord", FormatCapTextDelegate formatCapTextDelegate = null, RegexOptions regexOptions = RegexOptions.None, params string[] regExprs) { for (int i = 0; i < regExprs.Length; i++) { if (!regExprs[i].EndsWith(@"(?=\W|$)")) { regExprs[i] += @"(?=\W|$)"; } AddLexItem(name, regExprs[i], formatCapTextDelegate, regexOptions, CurrentAddGroupNumber); } }
public void Setup() { Lexeresults = new List <KeyValuePair <string, object> >(); LexerFramework = new LexerFramework(); formatNullDelegate = FormatNull; LexerFramework.AddLexItem("Null", @"[\s]", formatNullDelegate); LexerFramework.AddLexItem("Type", "int"); LexerFramework.AddLexItem("Operator", "="); LexerFramework.AddLexItem("Delimiter", ";"); formatDigitDelegate = FormateDigit; LexerFramework.AddLexItem("Digit", "[0-9]+", formatDigitDelegate); LexerFramework.AddLexItem("Identifier", "[A-Za-z][A-Za-z0-9_]*"); LexerFramework.OnLexedEventHandler += OnLexd; }
/// <summary> /// Add a Lex item, and Lex with this order. /// </summary> /// <param name="name">Name of item</param> /// <param name="regExpr">Regular Expression of Lex, and automately add "^" at head</param> /// <param name="regexOptions">Regular Expression Options</param> /// <param name="formatCapTextDelegate">The delegate of formatting the result of regExpr</param> /// <param name="group">LexGroup for advanced usage</param> /// <exception cref="GroupNumException">if Lex group number is out of limite or under zero</exception> public void AddLexItem(string name, string regExpr, FormatCapTextDelegate formatCapTextDelegate = null, RegexOptions regexOptions = RegexOptions.None, int group = 0) { if (name.Length == 0) { return; } if (regExpr.Length == 0) { return; } LexItem LexItem = new LexItem(name, formatCapTextDelegate); if (!regExpr.StartsWith('^')) { regExpr = '^' + regExpr; // reg expr must start from head of string } LexItem.SetRegex(regExpr, regexOptions); if (group >= LexGroupCount || group < 0) { throw new GroupNumException(group, "AddLexItem Error: illeagal Lex group number: " + group); } LexItems[group].Add(LexItem); }
/// <summary> /// Add identifier Lex item, and recomand to do this after add reserved words Lex item. /// </summary> /// <param name="name">Name of Lex item</param> /// <param name="formatCapTextDelegate">The delegate of formatting the result of regExprs</param> /// <param name="regexOptions">Regular Expression Options</param> /// <param name="regExpr">Regular Expression of Lex item</param> public void AddIdentifier(string name = "Identifier", FormatCapTextDelegate formatCapTextDelegate = null, RegexOptions regexOptions = RegexOptions.None, string regExpr = @"[A-Za-z]\w*") { AddLexItem(name, regExpr, formatCapTextDelegate, regexOptions, CurrentAddGroupNumber); }
/// <summary> /// Add a Lex item in group of AddCurrentGroupNumber, and Lex with this order. /// </summary> /// <param name="name">Name of item</param> /// <param name="regExpr">Regular Expression of Lex, and automately add "^" at head</param> /// <param name="regexOptions">Regular Expression Options</param> /// <param name="formatCapTextDelegate">The delegate of formatting the result of regExpr</param> public void AddLexItem(string name, string regExpr, FormatCapTextDelegate formatCapTextDelegate = null, RegexOptions regexOptions = RegexOptions.None) { AddLexItem(name, regExpr, formatCapTextDelegate, regexOptions, CurrentAddGroupNumber); }