/// <summary> /// Lex a single line string, if formated value equal null, then drop it and return true. /// </summary> /// <param name="LexObject">that string will be Lexd</param> /// <param name="LexItem">Lex item for Lex string</param> /// <param name="count">count of Lex result</param> /// <param name="line">line number</param> /// <param name="col">column number</param> /// <returns>match result</returns> /// <exception cref="ZeroLenghtMatchException">if match zero lenght string</exception> private bool LexString(ref string LexObject, LexItem LexItem, ref long count, long line, int col) { Match match = LexItem.Regex.Match(LexObject);// match reg expr if (!match.Success) { return(false); } string result = match.Value; if (result.Length == 0) { throw new ZeroLenghtMatchException(LexItem.Name, LexItem.Regex.ToString(), "Lex Error: Zero Length String Matched, " + "indicating end-less loop, by Name:" + LexItem.Name + " RegExpr:" + LexItem.Regex.ToString()); } object value; if (LexItem.FormatCapText == null) { value = result; } else { value = LexItem.FormatCapText(result); // format the result } if (value != null) { LexerResult Lexeresult = new LexerResult(count, LexItem.Name, value, line, col); // save the result // call event to notice user to receive it if (OnLexedEventHandler?.Invoke(this, Lexeresult) == true) { count++; // must be one result for the start with "^" parttern OnAcceptedEventHandler?.Invoke(this, Lexeresult); // to parse it. } } int restLenght = LexObject.Length - match.Length; LexObject = LexObject.Substring(match.Length, restLenght);// for continuous operate return(true); }
/// <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); }