public InputStream Move(int shift) { var result = new InputStream(initialContent); result.position = position + shift; result.content = initialContent.Substring(result.position); return result; }
public ParsingResult Parse(string expression) { InputStream input = new InputStream(expression); var tokens = grammar.Parse(input); var filteredTokens = grammar.Filter(tokens); var mixedTokens = grammar.ConvertToMixed(filteredTokens); var ast = grammar.CreateAST(mixedTokens); if (mixedTokens.Count != 1) throw new ParserException("Wrong expression."); var optimizedAst = ast.Optimize(); var result = new ParsingResult { Tree = optimizedAst, ParameterNames = Parameters, ParameterExpressions = grammar.ParameterExpressions.Values.ToArray() }; return result; }
public IEnumerable<LexicToken> Parse(InputStream input) { textInfo.Clear(); parameterExpressions.Clear(); // sorting to prevent situation when parameter 'x1' is parsed as parameter 'x' when 'x' is available, too. parameterReader.ParameterNames = parameters.OrderByDescending(s => s.Length); namedConstantReader.Constants = NamedConstants; functionReader.Functions = registeredFunctions; List<LexicToken> tokens = new List<LexicToken>(); int start = 0; do { var copy = input; foreach (var reader in lexicReaders) { LexicToken token = null; if (input.IsEmpty) break; start = input.Position; input = reader.TryRead(input, out token); if (token != null) { TokenInTextInfo info = new TokenInTextInfo { StartIndex = start, Length = input.Position - start, Token = token }; textInfo.Add(info); tokens.Add(token); } } if (input == copy) throw new ParserException(String.Format("Unexpected character '{0}' in position {1}.", input.Content[0], input.Position)); } while (!input.IsEmpty); return tokens; }