Start building AST in parser
This commit is contained in:
parent
5813816179
commit
614e497ac4
12 changed files with 262 additions and 50 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue