private State DoRepetition(State state, List <Result> results, int min, int max, ParseMethod method) { State start = state; int count = 0; while (count <= max) { State temp = method(state, results); if (temp.Parsed && temp.Index > state.Index) { state = temp; ++count; } else { state = new State(state.Index, true, ErrorSet.Combine(state.Errors, temp.Errors)); break; } } if (count < min || count > max) { state = new State(start.Index, false, ErrorSet.Combine(start.Errors, state.Errors)); } return(state); }
private State DoParseRange(State state, List <Result> results, bool inverted, string chars, string ranges, UnicodeCategory[] categories, string label) { char ch = m_input[state.Index]; bool matched = chars.IndexOf(ch) >= 0; for (int i = 0; i < ranges.Length && !matched; i += 2) { matched = ranges[i] <= ch && ch <= ranges[i + 1]; } for (int i = 0; categories != null && i < categories.Length && !matched; ++i) { matched = char.GetUnicodeCategory(ch) == categories[i]; } if (inverted) { matched = !matched && ch != '\x0'; } if (matched) { results.Add(new Result(this, state.Index, 1, m_input, default(string))); return(new State(state.Index + 1, true, state.Errors)); } return(new State(state.Index, false, ErrorSet.Combine(state.Errors, new ErrorSet(state.Index, label)))); }
// Value := '(' Expression ')' S private State DoParseValue5Rule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, "(")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "Expression")); }, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, ")")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "S")); }); if (_state.Parsed) { Expression value = results.Count > 0 ? results[0].Value : default(Expression); value = results[1].Value; _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } else { string expected = null; expected = "parenthesized expression"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
// Space := [ \t\r\n] private State DoParseSpaceRule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoParseRange(_state, results, false, " \t\r\n", string.Empty, null, "[ \t\r\n]"); if (_state.Parsed) { Expression value = results.Count > 0 ? results[0].Value : default(Expression); _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } else { string expected = null; expected = "whitespace"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
// OrExpression := AndExpression ('or' SS AndExpression)+ private State DoParseOrExpression1Rule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoParse(s, r, "AndExpression")); }, delegate(State s, List <Result> r) { return(DoRepetition(s, r, 1, 2147483647, delegate(State s2, List <Result> r2) { return DoSequence(s2, r2, delegate(State s3, List <Result> r3) { return DoParseLiteral(s3, r3, "or"); }, delegate(State s3, List <Result> r3) { return DoParse(s3, r3, "SS"); }, delegate(State s3, List <Result> r3) { return DoParse(s3, r3, "AndExpression"); }); })); }); if (_state.Parsed) { Predicate value = results.Count > 0 ? results[0].Value : default(Predicate); value = new OrPredicate(from e in results where e.Value != null select e.Value); _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } else { string expected; expected = "or expression"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
// Variable := VariablePrefix VariableSuffix* S private State DoParseVariableRule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoParse(s, r, "VariablePrefix")); }, delegate(State s, List <Result> r) { return(DoRepetition(s, r, 0, 2147483647, delegate(State s2, List <Result> r2) { return DoParse(s2, r2, "VariableSuffix"); })); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "S")); }); if (_state.Parsed) { Predicate value = results.Count > 0 ? results[0].Value : default(Predicate); string text = m_input.Substring(_start.Index, _state.Index - _start.Index); value = new VariablePredicate(text.Trim()); if (text != null) { _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } } else { string expected; expected = "variable"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
// Literal := 'false' SS private State DoParseLiteral3Rule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, "false")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "SS")); }); if (_state.Parsed) { Predicate value = results.Count > 0 ? results[0].Value : default(Predicate); value = new BoolPredicate(false); _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } else { string expected; expected = "boolean literal"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
// PrimaryExpression := 'excluded' SS '(' S Literal ')' S private State DoParsePrimaryExpression3Rule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, "excluded")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "SS")); }, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, "(")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "S")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "Literal")); }, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, ")")); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "S")); }); if (_state.Parsed) { Predicate value = results.Count > 0 ? results[0].Value : default(Predicate); value = new ExcludedPredicate(results[2].Value); _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } else { string expected; expected = "excluded function"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
private State DoParseLiteral(State state, List <Result> results, string literal) { State result; if (string.Compare(m_input, state.Index, literal, 0, literal.Length) == 0) { results.Add(new Result(this, state.Index, literal.Length, m_input, default(string))); result = new State(state.Index + literal.Length, true, state.Errors); } else { result = new State(state.Index, false, ErrorSet.Combine(state.Errors, new ErrorSet(state.Index, literal))); } return(result); }
// Value := [0-9]+ '.' [0-9]+ (('e' / 'E') [0-9]+)? S private State DoParseValue1Rule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoRepetition(s, r, 1, 2147483647, delegate(State s2, List <Result> r2) { return DoParseRange(s2, r2, false, string.Empty, "09", null, "[0-9]"); })); }, delegate(State s, List <Result> r) { return(DoParseLiteral(s, r, ".")); }, delegate(State s, List <Result> r) { return(DoRepetition(s, r, 1, 2147483647, delegate(State s2, List <Result> r2) { return DoParseRange(s2, r2, false, string.Empty, "09", null, "[0-9]"); })); }, delegate(State s, List <Result> r) { return(DoRepetition(s, r, 0, 1, delegate(State s2, List <Result> r2) { return DoSequence(s2, r2, delegate(State s3, List <Result> r3) { return DoChoice(s3, r3, delegate(State s4, List <Result> r4) { return DoParseLiteral(s4, r4, "e"); }, delegate(State s4, List <Result> r4) { return DoParseLiteral(s4, r4, "E"); }); }, delegate(State s3, List <Result> r3) { return DoRepetition(s3, r3, 1, 2147483647, delegate(State s4, List <Result> r4) { return DoParseRange(s4, r4, false, string.Empty, "09", null, "[0-9]"); }); }); })); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "S")); }); if (_state.Parsed) { Expression value = results.Count > 0 ? results[0].Value : default(Expression); string text = m_input.Substring(_start.Index, _state.Index - _start.Index); value = new FloatExpression(text.Trim()); if (text != null) { _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } } else { string expected = null; expected = "number"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }
private State DoParseLiteral(State state, List <Result> results, string literal) { int j = state.Index; for (int i = 0; i < literal.Length; ++i) { if (char.ToLower(m_input[j + i]) != literal[i]) { return(new State(state.Index, false, ErrorSet.Combine(state.Errors, new ErrorSet(state.Index, literal)))); } } int k = j + literal.Length; results.Add(new Result(this, j, literal.Length, m_input, default(List <string>))); state = new State(k, true, state.Errors); return(state); }
private State DoSequence(State state, List <Result> results, params ParseMethod[] methods) { State start = state; int startResult = results.Count; foreach (ParseMethod method in methods) { State temp = method(state, results); if (temp.Parsed) { state = temp; } else { state = new State(start.Index, false, ErrorSet.Combine(start.Errors, temp.Errors)); results.RemoveRange(startResult, results.Count - startResult); break; } } return(state); }
// Identifier := [$a-zA-Z] [a-zA-Z0-9]* S private State DoParseIdentifierRule(State _state, List <Result> _outResults) { State _start = _state; List <Result> results = new List <Result>(); _state = DoSequence(_state, results, delegate(State s, List <Result> r) { return(DoParseRange(s, r, false, "$", "azAZ", null, "[$a-zA-Z]")); }, delegate(State s, List <Result> r) { return(DoRepetition(s, r, 0, 2147483647, delegate(State s2, List <Result> r2) { return DoParseRange(s2, r2, false, string.Empty, "azAZ09", null, "[a-zA-Z0-9]"); })); }, delegate(State s, List <Result> r) { return(DoParse(s, r, "S")); }); if (_state.Parsed) { Expression value = results.Count > 0 ? results[0].Value : default(Expression); string text = m_input.Substring(_start.Index, _state.Index - _start.Index); value = new VariableExpression(text.Trim()); if (text != null) { _outResults.Add(new Result(this, _start.Index, _state.Index - _start.Index, m_input, value)); } } else { string expected = null; expected = "variable"; if (expected != null) { _state = new State(_start.Index, false, ErrorSet.Combine(_start.Errors, new ErrorSet(_state.Errors.Index, expected))); } } return(_state); }