use tree_sitter::Node;
pub const SOURCE_FILE: &str = "SurrealQL";
pub const PREDICATE: &str = "Predicate";
pub const SUB_QUERY: &str = "SubQuery";
pub const BLOCK: &str = "Block";
pub const BRACE_OPEN: &str = "BraceOpen";
pub const BRACE_CLOSE: &str = "BraceClose";
pub const DEFINE_STATEMENT: &str = "DefineStatement";
pub const SELECT_STATEMENT: &str = "SelectStatement";
pub const CREATE_STATEMENT: &str = "CreateStatement";
pub const UPDATE_STATEMENT: &str = "UpdateStatement";
pub const UPSERT_STATEMENT: &str = "UpsertStatement";
pub const DELETE_STATEMENT: &str = "DeleteStatement";
pub const RELATE_STATEMENT: &str = "RelateStatement";
pub const INSERT_STATEMENT: &str = "InsertStatement";
pub const LET_STATEMENT: &str = "LetStatement";
pub const FOR_STATEMENT: &str = "ForStatement";
pub const IF_ELSE_STATEMENT: &str = "IfElseStatement";
pub const MODERN: &str = "Modern";
pub const RETURN_STATEMENT: &str = "ReturnStatement";
pub const THROW_STATEMENT: &str = "ThrowStatement";
pub const ON_TABLE_CLAUSE: &str = "OnTableClause";
pub const TYPE_CLAUSE: &str = "TypeClause";
pub const COMMENT_CLAUSE: &str = "CommentClause";
pub const CONTENT_CLAUSE: &str = "ContentClause";
pub const SET_CLAUSE: &str = "SetClause";
pub const RETURN_CLAUSE: &str = "ReturnClause";
pub const WHERE_CLAUSE: &str = "WhereClause";
pub const GROUP_CLAUSE: &str = "GroupClause";
pub const OMIT_CLAUSE: &str = "OmitClause";
pub const ORDER_CLAUSE: &str = "OrderClause";
pub const SPLIT_CLAUSE: &str = "SplitClause";
pub const FETCH_CLAUSE: &str = "FetchClause";
pub const TIMEOUT_CLAUSE: &str = "TimeoutClause";
pub const PARALLEL_CLAUSE: &str = "ParallelClause";
pub const LIMIT_START_COMBO_CLAUSE: &str = "LimitStartComboClause";
pub const WITH_CLAUSE: &str = "WithClause";
pub const EXPLAIN_CLAUSE: &str = "ExplainClause";
pub const VERSION_CLAUSE: &str = "VersionClause";
pub const TEMPFILES_CLAUSE: &str = "TempfilesClause";
pub const WHEN_CLAUSE: &str = "WhenClause";
pub const THEN_CLAUSE: &str = "ThenClause";
pub const FIELDS_COLUMNS_CLAUSE: &str = "FieldsColumnsClause";
pub const UNIQUE_CLAUSE: &str = "UniqueClause";
pub const PERMISSIONS_FOR_CLAUSE: &str = "PermissionsForClause";
pub const PERMISSIONS_BASIC_CLAUSE: &str = "PermissionsBasicClause";
pub const BINARY_EXPRESSION: &str = "BinaryExpression";
pub const PREFIX_EXPRESSION: &str = "PrefixExpression";
pub const PATH: &str = "Path";
pub const SUBSCRIPT: &str = "Subscript";
pub const ARGUMENT_LIST: &str = "ArgumentList";
pub const MIDDLEWARE_CLAUSE: &str = "MiddlewareClause";
pub const ACCESS_DEFINITION: &str = "AccessDefinition";
pub const SCOPE_DEFINITION: &str = "ScopeDefinition";
pub const FUNCTION_CALL: &str = "FunctionCall";
pub const IDIOM_FUNCTION: &str = "IdiomFunction";
pub const SCRIPTING_FUNCTION: &str = "FunctionJs";
pub const JS_FUNCTION_BODY: &str = "JavaScriptBlock";
pub const CLOSURE: &str = "Closure";
pub const RANGE: &str = "Range";
pub const PARAM_DEFINITION: &str = "ParamDefinition";
pub const FIELD_ASSIGNMENT: &str = "FieldAssignment";
pub const OBJECT: &str = "Object";
pub const OBJECT_CONTENT: &str = "ObjectContent";
pub const OBJECT_PROPERTY: &str = "ObjectProperty";
pub const OBJECT_KEY: &str = "ObjectKey";
pub const KEY_NAME: &str = "KeyName";
pub const ARRAY: &str = "Array";
pub const SET: &str = "Set";
pub const POINT: &str = "Point";
pub const RECORD_ID: &str = "RecordId";
pub const RANGE_RECORD_ID: &str = "RangeRecordId";
pub const RECORD_ID_RANGE: &str = "RecordIdRange";
pub const RECORD_TB_IDENT: &str = "RecordTbIdent";
pub const RECORD_ID_IDENT: &str = "RecordIdIdent";
pub const RECORD_ID_STRING: &str = "RecordIdString";
pub const IDENT: &str = "Ident";
pub const IDIOM: &str = "Idiom";
pub const CUSTOM_FUNCTION_NAME: &str = "FunctionName";
pub const BUILTIN_FUNCTION_NAME: &str = "FunctionName";
pub const FUNCTION_NAME: &str = "FunctionName";
pub const VARIABLE_NAME: &str = "VariableName";
pub const FUNCTION_JS: &str = "FunctionJs";
pub const FIELDS: &str = "Fields";
pub const ANY: &str = "Any";
pub const OPTIONAL: &str = "Optional";
pub const LITERAL: &str = "Literal";
pub const NUMBER: &str = "Number";
pub const INT: &str = "Int";
pub const FLOAT: &str = "Float";
pub const DECIMAL: &str = "Decimal";
pub const STRING: &str = "String";
pub const FORMAT_STRING: &str = "FormatString";
pub const REGEX: &str = "Regex";
pub const DURATION: &str = "Duration";
pub const BOOL: &str = "Bool";
pub const NONE: &str = "None";
pub const TYPE: &str = "Type";
pub const TYPE_NAME: &str = "TypeName";
pub const PARAMETERIZED_TYPE: &str = "ParameterizedType";
pub const UNION_TYPE: &str = "UnionType";
pub const LITERAL_TYPE: &str = "LiteralType";
pub const ARRAY_TYPE: &str = "ArrayType";
pub const OBJECT_TYPE: &str = "ObjectType";
pub const OBJECT_TYPE_CONTENT: &str = "ObjectTypeContent";
pub const OBJECT_TYPE_PROPERTY: &str = "ObjectTypeProperty";
pub const TYPE_CAST: &str = "TypeCast";
pub const TYPE_KINDS: &[&str] = &[
TYPE,
TYPE_NAME,
PARAMETERIZED_TYPE,
UNION_TYPE,
LITERAL_TYPE,
ARRAY_TYPE,
OBJECT_TYPE,
];
pub const COLON: &str = "Colon";
pub const PIPE: &str = "Pipe";
pub const LOOKUP_RIGHT: &str = "LookupRight";
pub const OPERATOR: &str = "Operator";
pub const COMMENT: &str = "Comment";
pub const BLOCK_COMMENT: &str = "BlockComment";
pub fn text_of<'a>(source: &'a str, node: Node<'_>) -> Option<&'a str> {
node.utf8_text(source.as_bytes()).ok().map(str::trim)
}
pub fn is_keyword(node: Node<'_>) -> bool {
node.kind() == "Keyword" || node.kind().starts_with("keyword_")
}
pub fn is_kw(node: Node<'_>, source: &str, expected: &str) -> bool {
is_keyword(node)
&& text_of(source, node).is_some_and(|text| text.eq_ignore_ascii_case(expected))
}
pub fn is_define_statement(kind: &str) -> bool {
kind == DEFINE_STATEMENT
}
pub fn find_child<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
let mut cursor = node.walk();
node.named_children(&mut cursor).find(|c| c.kind() == kind)
}
pub fn find_child_any<'tree>(node: Node<'tree>, kinds: &[&str]) -> Option<Node<'tree>> {
let mut cursor = node.walk();
node.named_children(&mut cursor)
.find(|c| kinds.contains(&c.kind()))
}
pub fn named_children<'tree>(node: Node<'tree>) -> Vec<Node<'tree>> {
let mut cursor = node.walk();
node.named_children(&mut cursor).collect()
}
pub fn has_descendant(node: Node<'_>, kind: &str) -> bool {
if node.kind() == kind {
return true;
}
let mut cursor = node.walk();
for child in node.named_children(&mut cursor) {
if has_descendant(child, kind) {
return true;
}
}
false
}
pub fn dotted_name(source: &str, node: Node<'_>) -> Option<String> {
let mut parts = Vec::new();
collect_identifier_parts(source, node, &mut parts);
if parts.is_empty() {
None
} else {
Some(parts.join("."))
}
}
fn collect_identifier_parts(source: &str, node: Node<'_>, parts: &mut Vec<String>) {
if matches!(node.kind(), IDENT | IDIOM)
&& let Some(text) = text_of(source, node)
{
parts.push(text.to_string());
return;
}
let mut cursor = node.walk();
for child in node.named_children(&mut cursor) {
collect_identifier_parts(source, child, parts);
}
}