MCPcopy Create free account
hub / github.com/GraphLite-AI/GraphLite / array_expression

Function array_expression

graphlite/src/ast/parser.rs:1928–1992  ·  view source on GitHub ↗

Parse array expression: [expr1, expr2, ...] Returns a Literal::Vector if all elements are numeric, otherwise fails This allows arrays to be used as arguments to functions that accept vector inputs

(tokens: &[Token])

Source from the content-addressed store, hash-verified

1926/// Returns a Literal::Vector if all elements are numeric, otherwise fails
1927/// This allows arrays to be used as arguments to functions that accept vector inputs
1928fn array_expression(tokens: &[Token]) -> IResult<&[Token], Literal> {
1929 // Parse [expr1, expr2, ...]
1930 let (tokens, _) = expect_token(Token::LeftBracket)(tokens)?;
1931
1932 // Check for empty array
1933 if matches!(tokens.first(), Some(Token::RightBracket)) {
1934 let (tokens, _) = expect_token(Token::RightBracket)(tokens)?;
1935 return Ok((tokens, Literal::Vector(vec![])));
1936 }
1937
1938 // Parse expressions
1939 let (tokens, expressions) = expression_list(tokens)?;
1940 let (tokens, _) = expect_token(Token::RightBracket)(tokens)?;
1941
1942 // For now, we'll convert all expressions to a simple numeric vector
1943 // In the future, we might want to support more complex array types
1944 // For AI functions, we typically expect numeric arrays
1945 // Removed unused values vector since we now use literals directly
1946
1947 // Try to convert to a Literal list
1948 let mut literals = Vec::new();
1949 let mut all_numeric = true;
1950 let mut numeric_values = Vec::new();
1951
1952 for expr in expressions {
1953 // Try to extract a literal value from the expression
1954 match expr {
1955 Expression::Literal(literal) => match &literal {
1956 Literal::Integer(n) => {
1957 numeric_values.push(*n as f64);
1958 literals.push(literal);
1959 }
1960 Literal::Float(f) => {
1961 numeric_values.push(*f);
1962 literals.push(literal);
1963 }
1964 _ => {
1965 all_numeric = false;
1966 literals.push(literal);
1967 }
1968 },
1969 _ => {
1970 // For complex expressions, we can't evaluate them at parse time
1971 // This is problematic, so we'll return an error for now
1972 return Err(nom::Err::Error(nom::error::Error::new(
1973 tokens,
1974 nom::error::ErrorKind::Tag,
1975 )));
1976 }
1977 }
1978 }
1979
1980 // If all elements are numeric, return a Vector for backwards compatibility
1981 // Otherwise return a generic List
1982 if all_numeric
1983 && !literals.is_empty()
1984 && literals
1985 .iter()

Callers

nothing calls this directly

Calls 5

expect_tokenFunction · 0.85
expression_listFunction · 0.85
ErrorEnum · 0.85
is_emptyMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected