Start building AST in parser
This commit is contained in:
parent
5813816179
commit
614e497ac4
12 changed files with 262 additions and 50 deletions
34
src/runtime/document.c
Normal file
34
src/runtime/document.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include "document.h"
|
||||
|
||||
struct TSDocument {
|
||||
TSDocumentParseFn *parse_fn;
|
||||
const char **symbol_names;
|
||||
const char *text;
|
||||
TSTree *tree;
|
||||
};
|
||||
|
||||
TSDocument * TSDocumentMake() {
|
||||
TSDocument *result = malloc(sizeof(TSDocument));
|
||||
return result;
|
||||
}
|
||||
|
||||
void TSDocumentSetUp(TSDocument *document, TSDocumentParseFn fn, const char **symbol_names) {
|
||||
document->parse_fn = fn;
|
||||
document->symbol_names = symbol_names;
|
||||
}
|
||||
|
||||
void TSDocumentSetText(TSDocument *document, const char *text) {
|
||||
document->text = text;
|
||||
document->tree = document->parse_fn(document->text);
|
||||
}
|
||||
|
||||
TSTree * TSDocumentTree(const TSDocument *document) {
|
||||
return document->tree;
|
||||
}
|
||||
|
||||
char * TSDocumentToString(const TSDocument *document) {
|
||||
if (document->tree)
|
||||
return TSTreeToString(document->tree, document->symbol_names);
|
||||
else
|
||||
return "#<null tree>";
|
||||
}
|
||||
|
|
@ -3,12 +3,17 @@
|
|||
|
||||
static int INITIAL_STACK_SIZE = 100;
|
||||
|
||||
struct TSStackEntry {
|
||||
TSState state;
|
||||
TSTree *node;
|
||||
};
|
||||
|
||||
TSParser TSParserMake(const char *input) {
|
||||
TSParser result = {
|
||||
.tree = TSTreeMake(),
|
||||
.tree = NULL,
|
||||
.input = input,
|
||||
.position = 0,
|
||||
.lookahead_sym = 0,
|
||||
.lookahead_node = NULL,
|
||||
.lex_state = 0,
|
||||
.stack = calloc(INITIAL_STACK_SIZE, sizeof(TSStackEntry)),
|
||||
.stack_size = 0,
|
||||
|
|
@ -19,14 +24,21 @@ TSParser TSParserMake(const char *input) {
|
|||
void TSParserShift(TSParser *parser, TSState parse_state) {
|
||||
TSStackEntry *entry = (parser->stack + parser->stack_size);
|
||||
entry->state = parse_state;
|
||||
entry->symbol = parser->lookahead_sym;
|
||||
parser->lookahead_sym = -1;
|
||||
entry->node = parser->lookahead_node;
|
||||
parser->lookahead_node = NULL;
|
||||
parser->stack_size++;
|
||||
}
|
||||
|
||||
void TSParserReduce(TSParser *parser, TSSymbol symbol, int child_count) {
|
||||
parser->lookahead_sym = symbol;
|
||||
parser->stack_size -= child_count;
|
||||
|
||||
TSTree **children = malloc(child_count * sizeof(TSTree *));
|
||||
for (int i = 0; i < child_count; i++) {
|
||||
size_t j = parser->stack_size + i;
|
||||
children[i] = parser->stack[j].node;
|
||||
}
|
||||
|
||||
parser->lookahead_node = TSTreeMake(symbol, child_count, children);
|
||||
}
|
||||
|
||||
void TSParserError(TSParser *parser) {
|
||||
|
|
@ -42,12 +54,13 @@ char TSParserLookaheadChar(const TSParser *parser) {
|
|||
return parser->input[parser->position];
|
||||
}
|
||||
|
||||
TSSymbol TSParserLookaheadSym(const TSParser *parser) {
|
||||
return parser->lookahead_sym;
|
||||
long TSParserLookaheadSym(const TSParser *parser) {
|
||||
TSTree *node = parser->lookahead_node;
|
||||
return node ? node->value : -1;
|
||||
}
|
||||
|
||||
void TSParserSetLookaheadSym(TSParser *parser, TSSymbol symbol) {
|
||||
parser->lookahead_sym = symbol;
|
||||
parser->lookahead_node = TSTreeMake(symbol, 0, NULL);
|
||||
}
|
||||
|
||||
TSState TSParserParseState(const TSParser *parser) {
|
||||
|
|
@ -61,3 +74,8 @@ TSState TSParserLexState(const TSParser *parser) {
|
|||
void TSParserSetLexState(TSParser *parser, TSState lex_state) {
|
||||
parser->lex_state = lex_state;
|
||||
}
|
||||
|
||||
void TSParserAcceptInput(TSParser *parser) {
|
||||
parser->tree = parser->stack[parser->stack_size - 1].node;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,58 @@
|
|||
#include "document.h"
|
||||
#include "tree.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
TSTree TSTreeMake() {
|
||||
TSTree result = {
|
||||
};
|
||||
TSTree * TSTreeMake(TSSymbol value, size_t child_count, TSTree **children) {
|
||||
TSTree *result = malloc(sizeof(TSTree));
|
||||
result->value = value;
|
||||
result->child_count = child_count;
|
||||
result->children = children;
|
||||
for (int i = 0; i < child_count; i++)
|
||||
TSTreeRetain(children[i]);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void TSTreeRetain(TSTree *tree) {
|
||||
tree->ref_count++;
|
||||
}
|
||||
|
||||
void TSTreeRelease(TSTree *tree) {
|
||||
tree->ref_count--;
|
||||
if (tree->ref_count == 0) {
|
||||
for (int i = 0; i < tree->child_count; i++)
|
||||
TSTreeRelease(tree->children[i]);
|
||||
free(tree);
|
||||
}
|
||||
}
|
||||
|
||||
int TSTreeEquals(const TSTree *node1, const TSTree *node2) {
|
||||
if (node1->value != node2->value) return 0;
|
||||
if (node1->child_count != node2->child_count) return 0;
|
||||
for (int i = 0; i < node1->child_count; i++) {
|
||||
TSTree *child1 = node1->children[i];
|
||||
TSTree *child2 = node2->children[i];
|
||||
if (!TSTreeEquals(child1, child2)) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
char * TSTreeWriteToString(const TSTree *tree, const char **symbol_names, char *string) {
|
||||
char *result = string;
|
||||
const char *name = symbol_names[tree->value];
|
||||
sprintf(result, "(%s", name);
|
||||
result += strlen(name) + 1;
|
||||
for (int i = 0; i < tree->child_count; i++) {
|
||||
result[0] = ' ';
|
||||
result++;
|
||||
result = TSTreeWriteToString(tree->children[i], symbol_names, result);
|
||||
}
|
||||
result[0] = ')';
|
||||
result++;
|
||||
return result;
|
||||
}
|
||||
|
||||
char * TSTreeToString(const TSTree *tree, const char **symbol_names) {
|
||||
char *string = calloc(100, sizeof(char));
|
||||
TSTreeWriteToString(tree, symbol_names, string);
|
||||
return string;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue