private void ParseArgument(MapLexer lexer, MapParserContext context, MappedDataExpression expression) { // We should have an identifier... // TODO - literals var ident = CompoundIdentifier.Parse(lexer); // The identifier is either an attribute or a model... if (ident.HasPrefix) { expression.Arguments.Add(new MappedDataArgument(context.Resolve(ident))); } else { // A model...look it up and add it... var modelId = ident.Parts.First(); var model = context.Models.FirstOrDefault(x => x.Id == modelId); if (model == null) { throw new ParserException(ident, $"Model '{modelId}' not found."); } expression.Arguments.Add(new MappedDataArgument(model)); } }
public MappedDataExpression Parse(MapLexer lexer, MapParserContext context) { var expression = new MappedDataExpression(); // Regardless, we should always start with an identifier. This means that functions // are not keywords. lexer.VerifyToken(TokenKind.Identifier); // If the identifier is a function name, parse a function call... if (functions.Contains(lexer.Token.Text)) { ParseFunctionCall(lexer, context, expression); } else { // We don't have a function call, it must be an identifier. Parse it... var ident = CompoundIdentifier.Parse(lexer); var resolved = context.Resolve(ident); expression.Arguments.Add(new MappedDataArgument(resolved)); } return(expression); }
private void ParseFunctionCall(MapLexer lexer, MapParserContext context, MappedDataExpression expression) { // We're sitting on the function name... expression.FunctionName = lexer.Consume(TokenKind.Identifier); // Start of the argument list lexer.Consume(TokenKind.LeftParen); // Parse an argument while (true) { ParseArgument(lexer, context, expression); if (lexer.Token.Kind != TokenKind.Comma) { break; } lexer.Consume(TokenKind.Comma); } // End of the argument list lexer.Consume(TokenKind.RightParen); }
private bool BuildExampleExpression(StringBuilder builder, MappedDataExpression expression, string exampleId) { var found = false; var isFunction = !string.IsNullOrEmpty(expression.FunctionName); if (isFunction) { builder.Append(expression.FunctionName); builder.Append("("); } bool first = true; foreach (var arg in expression.Arguments) { if (first) { first = false; } else { builder.Append(", "); } if (arg.Model != null) { builder.Append(arg.Model.Id); } else if (arg.Attribute != null) { if (arg.Attribute.Examples.ContainsKey(exampleId)) { if (isFunction) { builder.Append("\""); } builder.Append(arg.Attribute.Examples[exampleId]); if (isFunction) { builder.Append("\""); } found = true; } else { builder.Append("NIL"); } } else { builder.Append("[unhandled-argument]"); } } if (isFunction) { builder.Append(")"); } return(found); }