lang_2023/include/parse_tree.hpp

66 lines
1.2 KiB
C++
Raw Normal View History

2023-03-26 15:20:53 +03:00
#pragma once
#include <string>
// for clangd
#include "tree_sitter/api.h"
namespace parser {
class ParseTree {
public:
class Node {
public:
std::string GetType();
std::pair<size_t, size_t> GetStartPoint();
std::pair<size_t, size_t> GetEndPoint();
std::string GetAsSExpression();
2023-03-28 12:05:20 +03:00
std::string GetValue(); // from source
2023-03-26 15:20:53 +03:00
bool IsNull();
bool IsNamed();
bool IsMissing();
bool IsExtra(); // comments, etc.
bool HasError();
Node NthChild(size_t n);
size_t ChildCount();
Node NthNamedChild(size_t n);
size_t NamedChildCount();
2023-03-28 12:05:20 +03:00
Node ChildByFieldName(const std::string& name);
2023-03-26 15:20:53 +03:00
// ?? use field id instaed of name ??
// ?? node equality check needed ??
private:
TSNode node_;
};
2023-03-28 12:05:20 +03:00
class Cursor { // ?? needed ??
2023-03-26 15:20:53 +03:00
public:
Cursor(const Node& node);
void ResetTo(const Node& node);
Node GetCurrentNode();
std::string GetCurrentNodeName();
bool GoToParent();
bool GoToNextSibling();
bool GoToFirstChild();
private:
TSTreeCursor cursor_;
};
ParseTree(const std::string& input);
2023-03-28 12:05:20 +03:00
Node GetRoot() const;
2023-03-26 15:20:53 +03:00
private:
TSTree* tree_;
2023-03-28 12:05:20 +03:00
std::string source; // for token value extraction
2023-03-26 15:20:53 +03:00
};
} // namespace parser