mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-25 13:07:13 +00:00
debug
This commit is contained in:
parent
582ad5668e
commit
0d62ae0814
29 changed files with 99479 additions and 1166 deletions
|
|
@ -526,6 +526,7 @@ struct AnnotatedType : public Node {
|
|||
};
|
||||
|
||||
using TypeParameter = std::variant<
|
||||
std::unique_ptr<TypeExpression>,
|
||||
std::unique_ptr<ParametrizedType>,
|
||||
std::unique_ptr<Expression>>;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
// for clangd
|
||||
#include "tree_sitter/api.h"
|
||||
|
||||
extern "C" const TSLanguage* tree_sitter_LANG();
|
||||
|
||||
namespace parser {
|
||||
|
||||
|
|
@ -12,31 +13,76 @@ 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();
|
||||
Node() = default;
|
||||
Node(const TSNode &node, const std::string* source) : node_(node), source_(source) {}
|
||||
|
||||
std::string GetValue(); // from source
|
||||
std::string GetType() {
|
||||
return ts_node_type(node_);
|
||||
}
|
||||
|
||||
bool IsNull();
|
||||
bool IsNamed();
|
||||
bool IsMissing();
|
||||
bool IsExtra(); // comments, etc.
|
||||
bool HasError();
|
||||
std::pair<size_t, size_t> GetStartPoint() {
|
||||
TSPoint point = ts_node_start_point(node_);
|
||||
return {point.row, point.column};
|
||||
}
|
||||
|
||||
Node NthChild(size_t n);
|
||||
size_t ChildCount();
|
||||
std::pair<size_t, size_t> GetEndPoint() {
|
||||
TSPoint point = ts_node_end_point(node_);
|
||||
return {point.row, point.column};
|
||||
}
|
||||
|
||||
Node NthNamedChild(size_t n);
|
||||
size_t NamedChildCount();
|
||||
std::string GetAsSExpression() {
|
||||
return ts_node_string(node_);
|
||||
}
|
||||
|
||||
Node ChildByFieldName(const std::string& name);
|
||||
std::string GetValue() { // from source
|
||||
size_t start = ts_node_start_byte(node_);
|
||||
size_t end = ts_node_end_byte(node_);
|
||||
return source_->substr(start, end - start); // TODO check
|
||||
}
|
||||
|
||||
bool IsNull() {
|
||||
return ts_node_is_null(node_);
|
||||
}
|
||||
|
||||
bool IsNamed() {
|
||||
return ts_node_is_named(node_);
|
||||
}
|
||||
|
||||
bool IsMissing() {
|
||||
return ts_node_is_missing(node_);
|
||||
}
|
||||
|
||||
bool IsExtra() { // comments, etc.
|
||||
return ts_node_is_extra(node_);
|
||||
}
|
||||
|
||||
bool HasError() {
|
||||
return ts_node_has_error(node_);
|
||||
}
|
||||
|
||||
Node NthChild(size_t n) {
|
||||
return Node(ts_node_child(node_, n), source_);
|
||||
}
|
||||
size_t ChildCount() {
|
||||
return ts_node_child_count(node_);
|
||||
}
|
||||
|
||||
Node NthNamedChild(size_t n) {
|
||||
return Node(ts_node_named_child(node_, n), source_);
|
||||
}
|
||||
size_t NamedChildCount() {
|
||||
return ts_node_named_child_count(node_);
|
||||
}
|
||||
|
||||
Node ChildByFieldName(const std::string& name) {
|
||||
return Node(ts_node_child_by_field_name(node_, name.c_str(), name.size()), source_);
|
||||
}
|
||||
|
||||
// ?? use field id instaed of name ??
|
||||
// ?? node equality check needed ??
|
||||
private:
|
||||
TSNode node_;
|
||||
const std::string* source_ = nullptr;
|
||||
};
|
||||
class Cursor { // ?? needed ??
|
||||
public:
|
||||
|
|
@ -54,12 +100,24 @@ public:
|
|||
TSTreeCursor cursor_;
|
||||
};
|
||||
|
||||
ParseTree(const std::string& input);
|
||||
ParseTree(const std::string& source) : source_(source) {
|
||||
TSParser* parser = ts_parser_new();
|
||||
ts_parser_set_language(parser, tree_sitter_LANG());
|
||||
|
||||
tree_ = ts_parser_parse_string(
|
||||
parser,
|
||||
NULL,
|
||||
source_.c_str(),
|
||||
source_.size());
|
||||
}
|
||||
|
||||
Node GetRoot() const {
|
||||
return Node(ts_tree_root_node(tree_), &source_);
|
||||
}
|
||||
|
||||
Node GetRoot() const;
|
||||
private:
|
||||
TSTree* tree_;
|
||||
std::string source; // for token value extraction
|
||||
std::string source_; // for token value extraction
|
||||
};
|
||||
|
||||
} // namespace parser
|
||||
|
|
|
|||
224
include/parser.h
Normal file
224
include/parser.h
Normal file
|
|
@ -0,0 +1,224 @@
|
|||
#ifndef TREE_SITTER_PARSER_H_
|
||||
#define TREE_SITTER_PARSER_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ts_builtin_sym_error ((TSSymbol)-1)
|
||||
#define ts_builtin_sym_end 0
|
||||
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
|
||||
|
||||
typedef uint16_t TSStateId;
|
||||
|
||||
#ifndef TREE_SITTER_API_H_
|
||||
typedef uint16_t TSSymbol;
|
||||
typedef uint16_t TSFieldId;
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
TSFieldId field_id;
|
||||
uint8_t child_index;
|
||||
bool inherited;
|
||||
} TSFieldMapEntry;
|
||||
|
||||
typedef struct {
|
||||
uint16_t index;
|
||||
uint16_t length;
|
||||
} TSFieldMapSlice;
|
||||
|
||||
typedef struct {
|
||||
bool visible;
|
||||
bool named;
|
||||
bool supertype;
|
||||
} TSSymbolMetadata;
|
||||
|
||||
typedef struct TSLexer TSLexer;
|
||||
|
||||
struct TSLexer {
|
||||
int32_t lookahead;
|
||||
TSSymbol result_symbol;
|
||||
void (*advance)(TSLexer *, bool);
|
||||
void (*mark_end)(TSLexer *);
|
||||
uint32_t (*get_column)(TSLexer *);
|
||||
bool (*is_at_included_range_start)(const TSLexer *);
|
||||
bool (*eof)(const TSLexer *);
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TSParseActionTypeShift,
|
||||
TSParseActionTypeReduce,
|
||||
TSParseActionTypeAccept,
|
||||
TSParseActionTypeRecover,
|
||||
} TSParseActionType;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t type;
|
||||
TSStateId state;
|
||||
bool extra;
|
||||
bool repetition;
|
||||
} shift;
|
||||
struct {
|
||||
uint8_t type;
|
||||
uint8_t child_count;
|
||||
TSSymbol symbol;
|
||||
int16_t dynamic_precedence;
|
||||
uint16_t production_id;
|
||||
} reduce;
|
||||
uint8_t type;
|
||||
} TSParseAction;
|
||||
|
||||
typedef struct {
|
||||
uint16_t lex_state;
|
||||
uint16_t external_lex_state;
|
||||
} TSLexMode;
|
||||
|
||||
typedef union {
|
||||
TSParseAction action;
|
||||
struct {
|
||||
uint8_t count;
|
||||
bool reusable;
|
||||
} entry;
|
||||
} TSParseActionEntry;
|
||||
|
||||
struct TSLanguage {
|
||||
uint32_t version;
|
||||
uint32_t symbol_count;
|
||||
uint32_t alias_count;
|
||||
uint32_t token_count;
|
||||
uint32_t external_token_count;
|
||||
uint32_t state_count;
|
||||
uint32_t large_state_count;
|
||||
uint32_t production_id_count;
|
||||
uint32_t field_count;
|
||||
uint16_t max_alias_sequence_length;
|
||||
const uint16_t *parse_table;
|
||||
const uint16_t *small_parse_table;
|
||||
const uint32_t *small_parse_table_map;
|
||||
const TSParseActionEntry *parse_actions;
|
||||
const char * const *symbol_names;
|
||||
const char * const *field_names;
|
||||
const TSFieldMapSlice *field_map_slices;
|
||||
const TSFieldMapEntry *field_map_entries;
|
||||
const TSSymbolMetadata *symbol_metadata;
|
||||
const TSSymbol *public_symbol_map;
|
||||
const uint16_t *alias_map;
|
||||
const TSSymbol *alias_sequences;
|
||||
const TSLexMode *lex_modes;
|
||||
bool (*lex_fn)(TSLexer *, TSStateId);
|
||||
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
|
||||
TSSymbol keyword_capture_token;
|
||||
struct {
|
||||
const bool *states;
|
||||
const TSSymbol *symbol_map;
|
||||
void *(*create)(void);
|
||||
void (*destroy)(void *);
|
||||
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
|
||||
unsigned (*serialize)(void *, char *);
|
||||
void (*deserialize)(void *, const char *, unsigned);
|
||||
} external_scanner;
|
||||
const TSStateId *primary_state_ids;
|
||||
};
|
||||
|
||||
/*
|
||||
* Lexer Macros
|
||||
*/
|
||||
|
||||
#define START_LEXER() \
|
||||
bool result = false; \
|
||||
bool skip = false; \
|
||||
bool eof = false; \
|
||||
int32_t lookahead; \
|
||||
goto start; \
|
||||
next_state: \
|
||||
lexer->advance(lexer, skip); \
|
||||
start: \
|
||||
skip = false; \
|
||||
lookahead = lexer->lookahead;
|
||||
|
||||
#define ADVANCE(state_value) \
|
||||
{ \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define SKIP(state_value) \
|
||||
{ \
|
||||
skip = true; \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ACCEPT_TOKEN(symbol_value) \
|
||||
result = true; \
|
||||
lexer->result_symbol = symbol_value; \
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
#define END_STATE() return result;
|
||||
|
||||
/*
|
||||
* Parse Table Macros
|
||||
*/
|
||||
|
||||
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
|
||||
|
||||
#define STATE(id) id
|
||||
|
||||
#define ACTIONS(id) id
|
||||
|
||||
#define SHIFT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = state_value \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_REPEAT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = state_value, \
|
||||
.repetition = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_EXTRA() \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.extra = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define REDUCE(symbol_val, child_count_val, ...) \
|
||||
{{ \
|
||||
.reduce = { \
|
||||
.type = TSParseActionTypeReduce, \
|
||||
.symbol = symbol_val, \
|
||||
.child_count = child_count_val, \
|
||||
__VA_ARGS__ \
|
||||
}, \
|
||||
}}
|
||||
|
||||
#define RECOVER() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeRecover \
|
||||
}}
|
||||
|
||||
#define ACCEPT_INPUT() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeAccept \
|
||||
}}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_PARSER_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue