/// <summary> /// Tries to parse all specified items except some others. /// </summary> public bool ParseExcept(LexicalState state, LexicalItem main, LexicalItem exception) { int index = state.Position; if (!main.Parse(state)) { return(false); } string parsed = state.Get(main.Key, index); LexicalState check = new LexicalState(parsed); if (exception.Parse(check)) { if (check.IsEndOfData) { state.Reset(index); return(false); } } state.AddBack(Key, index); return(true); }
/// <summary> /// Tries to parse a batch of similar lexical items with specified delimiter. /// </summary> public bool ParseMany(LexicalState state, LexicalItem part, LexicalItem delimiter) { int index = state.Position; if (!part.Parse(state)) { return(false); } while (true) { int lastIndex = state.Position; if (delimiter != null) { if (!delimiter.Parse(state)) { break; } } if (!part.Parse(state)) { state.Reset(lastIndex); break; } } state.AddBack(Key, index); return(true); }
/// <summary> /// Performs tests for letter lexem. /// </summary> private void CheckLetter(LexicalItem item, string letter, bool canEscape) { string lower = letter.ToLowerInvariant(); string upper = letter.ToUpperInvariant(); string lowerHex = ((int)lower[0]).ToString("x"); string upperHex = ((int)upper[0]).ToString("x"); string lowerHex1 = lowerHex.Substring(0, 1); string lowerHex2 = lowerHex.Substring(1, 1); string upperHex1 = upperHex.Substring(0, 1); string upperHex2 = upperHex.Substring(1, 1); string[] hexVariants = new[] { lowerHex1.ToLower() + lowerHex2.ToLower(), lowerHex1.ToLower() + lowerHex2.ToUpper(), lowerHex1.ToUpper() + lowerHex2.ToLower(), lowerHex1.ToUpper() + lowerHex2.ToUpper(), upperHex1.ToLower() + upperHex2.ToLower(), upperHex1.ToLower() + upperHex2.ToUpper(), upperHex1.ToUpper() + upperHex2.ToLower(), upperHex1.ToUpper() + upperHex2.ToUpper(), }; Check(true, item, lower); Check(true, item, upper); Check(false, item, lower + " "); Check(false, item, " " + lower); Check(canEscape, item, "\\" + lower); Check(canEscape, item, "\\" + upper); Check(false, item, "\\\\" + lower); Check(false, item, lower + "\\"); foreach (string hexVariant in hexVariants) { Check(true, item, "\\" + hexVariant); Check(true, item, "\\0" + hexVariant); Check(true, item, "\\00" + hexVariant); Check(true, item, "\\000" + hexVariant); Check(true, item, "\\0000" + hexVariant); Check(false, item, "\\00000" + hexVariant); Check(true, item, "\\" + hexVariant + " "); Check(false, item, "\\" + hexVariant + " "); Check(true, item, "\\0" + hexVariant + "\r\n"); Check(false, item, "\\0" + hexVariant + "\n\r"); Check(true, item, "\\00" + hexVariant + "\r"); Check(true, item, "\\00" + hexVariant + "\n"); Check(true, item, "\\00" + hexVariant + "\t"); Check(true, item, "\\00" + hexVariant + "\f"); } }
/// <summary> /// Checks whether specified data string can be fully parsed to the lexical item. /// </summary> public void Check(bool expected, LexicalItem item, string data) { bool result = Parse(item, data); if (expected != result) { throw new AssertFailedException( String.Format( "Checking for [{0}] failed. Expected:<{1}>. Actual:<{2}>.", data.ToDisplay(), expected, !expected)); } }
/// <summary> /// Tries to parses specified data string into lexical item. /// </summary> protected override bool Parse(LexicalItem item, string data) { LexicalState state = new LexicalState(data); return item.ParseFull(state); }
/// <summary> /// Tries to parse all specified items except some others. /// </summary> public bool ParseExcept(LexicalState state, LexicalItem main, LexicalItem exception) { int index = state.Position; if (!main.Parse(state)) return false; string parsed = state.Get(main.Key, index); LexicalState check = new LexicalState(parsed); if (exception.Parse(check)) { if (check.IsEndOfData) { state.Reset(index); return false; } } state.AddBack(Key, index); return true; }
/// <summary> /// Tries to parse a batch of similar lexical items with specified delimiter. /// </summary> public bool ParseMany(LexicalState state, LexicalItem part, LexicalItem delimiter) { int index = state.Position; if (!part.Parse(state)) return false; while (true) { int lastIndex = state.Position; if (delimiter != null) { if (!delimiter.Parse(state)) break; } if (!part.Parse(state)) { state.Reset(lastIndex); break; } } state.AddBack(Key, index); return true; }
protected SpecificIdentifier(params LexicalItem[] items) { m_check = new ParseAll(items); }
/// <summary> /// Initializes a new instance. /// </summary> public LexicalTerminal(LexicalItem item) { m_item = item; }
/// <summary> /// Tries to parses specified data string into lexical item. /// </summary> protected abstract bool Parse(LexicalItem item, string data);
protected SpecificAtKeyword(params LexicalItem[] items) { m_check = new ParseAll(items); }