public void Parse(string content) { InitMainParsing(); parser.Parse(content); ValueParsingComplete(); KeyParsingComplete(); CategoryParsingComplete(); }
public IEnumerable <SoftLink> GetSoftLinks(string rawText) { var tokens = _tokenParser.Parse(rawText); var links = new List <SoftLink>(); foreach (var t in tokens.Where(p => _cardLinker.CanLink(p))) { var link = _cardLinker.Link(t); links.Add(link); } return(links); }
void TestParser(string input, int count, int[] positions = null, string[] rawValues = null, string[] values = null, Action <IEnumerable <Token> > additionalTest = null) { var tokens = _parser.Parse(input).ToArray(); Assert.AreEqual(count, tokens.Length); if (count == 0) { return; } for (int i = 0; i < tokens.Length; i++) { Test(tokens[i], positions[i], rawValues[i], values[i]); } additionalTest?.Invoke(tokens); }
public void ParserShouldUseWhiteSpaceParserWhenPluggedParserDoesNotSupportWhiteSpace() { ITokenParser parserImp = Mocker.StrictMock <ITokenParser>(); Expect.Call(parserImp.Parse("")).Return(new List <TokenBase>()).Repeat.Once(); Expect.Call(parserImp.SupportsWhiteSpaceParsing).Return(false).Repeat.Once(); Expect.Call(parserImp.SupportsGenericsParsing).Return(true).Repeat.Once(); Mocker.ReplayAll(); CalidusTokenParser parser = new CalidusTokenParser(parserImp, _otherImpl, _otherImpl); parser.Parse(""); Mocker.VerifyAll(); Assert.IsTrue(_otherImpl.WhiteSpaceWasCalled); }
public void ParserShouldCheckWhiteSpaceBeforeCommentsBeforeGenerics() { ITokenParser parserImp = Mocker.StrictMock <ITokenParser>(); using (Mocker.Ordered()) { Expect.Call(parserImp.Parse("")).Return(new List <TokenBase>()).Repeat.Once(); Expect.Call(parserImp.SupportsWhiteSpaceParsing).Return(true).Repeat.Once(); Expect.Call(parserImp.SupportsGenericsParsing).Return(true).Repeat.Once(); } Mocker.ReplayAll(); CalidusTokenParser parser = new CalidusTokenParser(parserImp, _otherImpl, _otherImpl); parser.Parse(""); Mocker.VerifyAll(); }
/// <summary> /// Parses the source into a list of tokens and returns true if it succeeded, otherwise false /// </summary> /// <param name="source">The source to parse</param> /// <param name="source">The tokenlist to put tokens in</param> /// <returns>True if succeeded, otherwise false</returns> public IEnumerable <TokenBase> Parse(String source) { String fixedSource = source.Replace("\r\n", "\n"); IEnumerable <TokenBase> result = _parser.Parse(fixedSource); //check for whitespace support if (!_parser.SupportsWhiteSpaceParsing) { result = _whiteSpaceParser.Parse(fixedSource, result); } //check for generics support if (!_parser.SupportsGenericsParsing) { result = _genericsParser.Parse(result); } //return result return(result); }
public IToken[] ReceiveTokens(DbEnvironment env) { using (var ms = new MemoryStream()) { bool canceled; var buffer = new byte[env.PacketSize]; while (true) { _inner.EnsureReceive(buffer, 0, env.HeaderSize); var length = buffer[2] << 8 | buffer[3]; var bufferStatus = (BufferStatus)buffer[1]; _inner.EnsureReceive(buffer, env.HeaderSize, length - env.HeaderSize); ms.Write(buffer, env.HeaderSize, length - env.HeaderSize); //" If TDS_BUFSTAT_ATTNACK not also TDS_BUFSTAT_EOM, continue reading packets until TDS_BUFSTAT_EOM." canceled = bufferStatus.HasFlag(BufferStatus.TDS_BUFSTAT_ATTNACK) || bufferStatus.HasFlag(BufferStatus.TDS_BUFSTAT_ATTN); if (bufferStatus.HasFlag(BufferStatus.TDS_BUFSTAT_EOM)) { break; } } if (canceled) { Logger.Instance?.WriteLine($"{nameof(RegularSocket)} - received cancel status flag"); return(new IToken[] { new DoneToken { Count = 0, Status = DoneToken.DoneStatus.TDS_DONE_ATTN } }); } ms.Seek(0, SeekOrigin.Begin); LastActive = DateTime.UtcNow; return(_parser.Parse(ms, env)); } }
public ApiDescriptionGroupCollection Generate() { var descriptions = new List <ApiDescription>(); foreach (var method in _functionDataProvider.GetMethods()) { var route = method.TriggerAttribute.Route; var routeParameters = _tokenParser.Parse(route); var apiParameterDescriptions = new List <ApiParameterDescription>(); var apiResponseTypes = new List <ApiResponseType>(); // map body if available var bodyAttributes = method .MethodInfo .GetCustomAttributes(typeof(SwaggerRequestAttribute), false) .Where(x => (x as SwaggerRequestAttribute)?.In == RequestSource.Body) .Select(x => x as SwaggerRequestAttribute); foreach (var parameterMapping in _parameterMappings) { try { apiParameterDescriptions.AddRange(parameterMapping.Map(method.MethodInfo) ?? Enumerable.Empty <ApiParameterDescription>()); } catch (Exception ex) { _logger.LogError(ex, "Error mapping parameters in api explorer"); } } foreach (var responseTypeMapping in _responseMappings) { try { apiResponseTypes.AddRange(responseTypeMapping.Map(method.MethodInfo) ?? Enumerable.Empty <ApiResponseType>()); } catch (Exception ex) { _logger.LogError(ex, "Error mapping parameters in api explorer"); } } foreach (var methodParameter in method.MethodInfo.GetParameters()) { var matchingRouteParameter = routeParameters.SingleOrDefault(param => string.Equals(param, methodParameter.Name, StringComparison.InvariantCultureIgnoreCase)); if (matchingRouteParameter != default && !apiParameterDescriptions.Any(x => string.Equals(x.Name, matchingRouteParameter, StringComparison.InvariantCultureIgnoreCase))) { var isOptional = matchingRouteParameter.EndsWith("?", StringComparison.InvariantCultureIgnoreCase); apiParameterDescriptions.Add(_parameterDescriptionFactory.Create( bindingSource: BindingSource.Path, name: matchingRouteParameter, type: methodParameter.ParameterType, isOptional)); continue; } } foreach (var httpVerb in method.TriggerAttribute.Methods) { var description = new FunctionApiDescription( methodInfo: method.MethodInfo, name: method.Name, parameters: apiParameterDescriptions, verb: httpVerb, route: route, responseTypes: apiResponseTypes); foreach (var bodyAttribute in bodyAttributes) { var requestFormats = GetSupportedFormats(bodyAttribute.Type); foreach (var format in requestFormats) { description.SupportedRequestFormats.Add(format); } } descriptions.Add(description); } } var groups = new ApiDescriptionGroup("default", descriptions); return(new ApiDescriptionGroupCollection(new[] { groups }, version: 1)); }
/// <summary> /// Executa a análise lexa no comando. /// </summary> /// <param name="text">Texto que será analizado.</param> /// <returns></returns> public LexerResult Execute(string text) { var lines = new List <ExpressionLine>(); var expressions = new List <Expression>(); Stack <ExpressionContainer> containersStack = new Stack <ExpressionContainer>(); var containerId = 1; var containers = new List <ExpressionContainer>(); int beginPos = 0; int pos = 0; State state = State.S0; char usingSpecialContainerChar = TokenParser.GetCharacter((int)TokenID.Quote); ExpressionLine lastLine = new ExpressionLine(pos); char current = char.MinValue; int currentToken = (int)TokenID.InvalidExpression; lines.Add(lastLine); bool end = false; while (!end || (pos == text.Length && (state == State.S8 || state == State.S3 || state == State.S2))) { current = pos == text.Length ? text[pos - 1] : text[pos]; currentToken = TokenParser.Parse(current); switch (state) { case State.S0: if (Array.Exists <char>(Configuration.Spaces, f => f == current)) { state = State.S0; if (currentToken == (int)TokenID.Newline) { lastLine.Length = pos - lastLine.BeginPoint; lastLine = new ExpressionLine(pos); lines.Add(lastLine); } else { } beginPos = pos + 1; } else if (Array.Exists <ContainerChars>(Configuration.StringContainers, f => f.Start == current)) { beginPos = pos + 1; usingSpecialContainerChar = Array.Find <ContainerChars>(Configuration.StringContainers, f => f.Start == current).Stop; state = State.S1; } else if (Array.Exists <char>(Configuration.Tabs, f => f == current)) { beginPos = pos; state = State.S2; } else if (Configuration.IsAlpha(text[pos])) { state = State.S3; } else if (char.IsDigit(text[pos])) { state = State.S8; } else if (Array.Exists <ContainerChars>(Configuration.Containers, f => f.Start == current)) { Expression e = new Expression(containersStack.Count > 0 ? containersStack.Peek() : null, pos, lastLine, text[pos]); e.Token = currentToken; var containerChars = Array.Find(Configuration.Containers, f => f.Start == current).Clone(); expressions.Add(e); containersStack.Push(new ExpressionContainer(containerId++, pos + 1, containerChars, lastLine)); beginPos = pos + 1; state = State.S0; } else if (Array.Exists <ContainerChars>(Configuration.Containers, f => f.Stop == current)) { if (containersStack.Count == 0) { throw new LexerException(String.Format("Not open tag for caracter {0}. Line: {1} - Col: {2}.", text[pos], lines.Count, pos - lastLine.BeginPoint)); } else if (containersStack.Peek().ContainerChars.Stop == text[pos]) { var ce = containersStack.Pop(); ce.Length = pos - ce.BeginPoint; containers.Add(ce); Expression e = new Expression(containersStack.Count > 0 ? containersStack.Peek() : null, pos, lastLine, text[pos]); e.Token = currentToken; expressions.Add(e); if (containersStack.Count == 0) { } beginPos = pos + 1; state = State.S0; } else { throw new LexerException(String.Format("Expected caracter {0}. Line: {1} - Col: {2}.", containersStack.Peek().ContainerChars.Stop, lines.Count, pos - lastLine.BeginPoint)); } } else { throw new LexerException(String.Format("Invalid caracter '{0}' in expression context.", text[pos])); } break; case State.S1: if (current == usingSpecialContainerChar) { if (!Configuration.SupportASCIIChar || ((pos > 0 && TokenParser.Parse(text[pos - 1]) != Configuration.ASCIICharPrefix) || (pos > 1 && TokenParser.Parse(text[pos - 2]) == Configuration.ASCIICharPrefix))) { var specialToken = TokenParser.Parse(usingSpecialContainerChar); if (specialToken == (int)TokenID.RBracket || specialToken == (int)TokenID.BSQuote) { Expression e = new Expression(containersStack.Count > 0 ? containersStack.Peek() : null, beginPos, pos - beginPos, lastLine, text, (int)TokenID.Identifier); e.CurrentSpecialContainer = new ContainerChars(text[beginPos - 1], text[pos]); expressions.Add(e); } else { SpecialContainerExpression sce = new SpecialContainerExpression(containersStack.Count > 0 ? containersStack.Peek() : null, beginPos, pos - beginPos, lastLine, text, usingSpecialContainerChar); expressions.Add(sce); sce.ContainerToken = specialToken; } beginPos = pos + 1; state = State.S0; } } break; case State.S2: if (text[pos - 1] == TokenParser.GetCharacter((int)TokenID.Minus) && char.IsDigit(text[pos])) { if (text.Length < 2 || (text.Length > 2 && Array.IndexOf <char>(Configuration.Tabs, text[pos - 2]) >= 0) || (text.Length > 2 && Array.IndexOf <char>(Configuration.Spaces, text[pos - 2]) != -1) || (text.Length > 2 && Configuration.Containers.Any(f => f.Start == text[pos - 2]))) { state = State.S8; continue; } } int joinTokenID; if (!end) { joinTokenID = TokenParser.Parse(text[pos - 1].ToString() + text[pos].ToString()); } else { joinTokenID = TokenParser.Parse(text[pos - 1].ToString()); } if (Array.Exists <int>(Configuration.ExpressionJoinsTokenIDs, f => f == joinTokenID)) { Expression e = new Expression(containersStack.Count > 0 ? containersStack.Peek() : null, beginPos, 2, lastLine, text); e.Token = TokenParser.Parse(e.Text); if (e.Token == (int)TokenID.InvalidExpression) { e.Token = (int)TokenID.InvalidTab; } expressions.Add(e); beginPos = pos + 1; state = State.S0; } else { TabExpression te = new TabExpression(containersStack.Count > 0 ? containersStack.Peek() : null, beginPos, lastLine, text); te.Token = TokenParser.Parse(text[beginPos]); expressions.Add(te); beginPos = pos; pos--; state = State.S0; } break; case State.S3: if (pos == text.Length || !Configuration.IsAlphanumeric(text[pos])) { Expression e = new Expression(containersStack.Count > 0 ? containersStack.Peek() : null, beginPos, pos - beginPos, lastLine, text); if (char.IsDigit(e.Text[0]) || e.Text[0] == '-') { if (e.Text.Length > 2 && (e.Text[1] == 'x' || e.Text[1] == 'X') && System.Text.RegularExpressions.Regex.IsMatch(e.Text.Substring(2), "[0-9]")) { e.Token = (int)TokenID.HexLiteral; } else if (System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^([-]|[0-9])[0-9]*$")) { e.Token = (int)TokenID.IntLiteral; } else if (System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$")) { e.Token = (int)TokenID.RealLiteral; } var aux = _tokenParser.Parse(e.Text.ToLower()); if (aux == (int)TokenID.InvalidExpression) { e.Token = (int)TokenID.Identifier; } else { e.Token = aux; } } else if (_keywords.ContainsKey(e.Text.ToLower())) { e.Token = _keywords[e.Text.ToLower()]; } else { var aux = _tokenParser.Parse(e.Text.ToLower()); if (aux == (int)TokenID.InvalidExpression) { e.Token = (int)TokenID.Identifier; } else { e.Token = aux; } } expressions.Add(e); beginPos = pos; pos--; state = State.S0; } break; case State.S8: if (pos == text.Length || !Configuration.IsNumeric(text[pos])) { Expression e = new Expression(containersStack.Count > 0 ? containersStack.Peek() : null, beginPos, pos - beginPos, lastLine, text); if (e.Text.Length > 2 && (e.Text[1] == 'x' || e.Text[1] == 'X') && System.Text.RegularExpressions.Regex.IsMatch(e.Text.Substring(2), "[0-9]")) { e.Token = (int)TokenID.HexLiteral; } else if (System.Text.RegularExpressions.Regex.IsMatch(e.Text, "[0-9]")) { e.Token = (int)TokenID.IntLiteral; } else if (System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$")) { e.Token = (int)TokenID.RealLiteral; } else { throw new LexerException("Expected number or hexadecimal."); } expressions.Add(e); beginPos = pos; pos--; state = State.S0; } break; } pos++; end = (pos == text.Length); } lastLine.Length = pos - lastLine.BeginPoint; if (state != State.S0) { throw new LexerException("Invalid expression."); } if (containersStack.Count > 0) { throw new LexerException(String.Format("{0} expected. Line: {1} - Col: {2} -- opened in Line: {3} - Col: {4}", containersStack.Peek().ContainerChars.Stop, lines.Count, lastLine.Length, containersStack.Peek().Line.BeginPoint, containersStack.Peek().BeginPoint)); } return(new LexerResult(text, expressions, lines, containers)); }
TokenCollectionChanges Update(string oldValue, string newValue) { GetDetails(oldValue, newValue, out string toParse, out bool inFront, out int position); RemoveAndAdjustPosition(position, inFront, out IEnumerable <Token> removed, out IEnumerable <Token> positionChanged); IEnumerable <Token> newTokens = string.IsNullOrEmpty(toParse) ? new Token[] { } : _parser.Parse(toParse).ToArray(); InsertAndAdjustPosition(newTokens, inFront, position); FirstToken = _tokens.FirstOrDefault(); LastToken = _tokens.LastOrDefault(); return(new TokenCollectionChanges(removed.ToArray(), newTokens.ToArray(), positionChanged.ToArray(), inFront)); }
public List <V> Eval(params string[] text) { IEnumerable <Token <T> > tokens = tokenizer.Tokenize(text); return(parser.Parse(tokens)); }
public virtual Func <TObj, string> BuildLambda() { Formats = Formats ?? new TFormatStore(); PropertyFilter = PropertyFilter ?? (p => true); var instanceExpr = Expression.Parameter(typeof(TObj), "instance"); var strExprs = new List <Expression>(); if (OutputTemplate.IsEmpty()) { strExprs.Add( Expression.Call(instanceExpr, nameof(ToString), null) ); } else { var filteredProps = props.Where(PropertyFilter) .ToArray(); var tokens = parser.Parse(OutputTemplate); foreach (var t in tokens) { var valExpr = default(Expression); var propertyInfo = default(PropertyInfo); if (t.IsLiteral) { valExpr = Expression.Constant(t.Value); } else { if (Formats.ContainsKey(t.Value)) { var callFormatExpr = Expression.Constant(Formats[t.Value]); valExpr = Expression.Invoke(callFormatExpr, instanceExpr); } else if ((propertyInfo = filteredProps.FirstOrDefault(x => x.Name.Equals(t.Value, StringComparison.OrdinalIgnoreCase))) != null) { var propertyExpr = Expression.Property(instanceExpr, propertyInfo); valExpr = propertyInfo.PropertyType == typeof(string) ? (Expression)propertyExpr : Expression.Call(propertyExpr, nameof(ToString), null); } else { valExpr = Expression.Constant(t.Key); } } strExprs.Add(valExpr); } } var body = ExpressionWizard.Spell.StringConcat(strExprs.ToArray()); var lambda = Expression.Lambda <Func <TObj, string> >(body, instanceExpr); var serializerFunc = lambda.CompileFast(); return(serializerFunc); }