From 14e721dda9a38762695ac5435c24818265629b02 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Thu, 13 Dec 2012 14:44:33 +0000 Subject: a top-level API for parsing in the C runtime --- src/runtime/c/pgf/pgf.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'src/runtime/c/pgf/pgf.c') diff --git a/src/runtime/c/pgf/pgf.c b/src/runtime/c/pgf/pgf.c index 6e54193dd..ceeff23bf 100644 --- a/src/runtime/c/pgf/pgf.c +++ b/src/runtime/c/pgf/pgf.c @@ -2,8 +2,12 @@ #include #include #include +#include +#include +#include #include #include +#include #include #include @@ -167,3 +171,73 @@ pgf_print_name(PgfConcr* concr, PgfCId id) name = id; return name; } + +void +pgf_linearize(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err) +{ + GuPool* tmp_pool = gu_local_pool(); + + GuEnum* cts = + pgf_lzr_concretize(concr, expr, tmp_pool); + PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool); + if (!gu_variant_is_null(ctree)) { + pgf_lzr_linearize_simple(concr, ctree, 0, wtr, err); + } + + gu_pool_free(tmp_pool); +} + +GuEnum* +pgf_parse(PgfConcr* concr, PgfCId cat, PgfLexer *lexer, GuPool* pool) +{ + // Begin parsing a sentence of the specified category + PgfParseState* state = + pgf_parser_init_state(concr, cat, 0, pool); + if (state == NULL) { + return NULL; + } + + // Tokenization + GuExn* lex_err = gu_new_exn(NULL, gu_kind(type), pool); + PgfToken tok = pgf_lexer_read_token(lexer, lex_err); + while (!gu_exn_is_raised(lex_err)) { + // feed the token to get a new parse state + state = pgf_parser_next_state(state, tok, pool); + if (state == NULL) { + return NULL; + } + + tok = pgf_lexer_read_token(lexer, lex_err); + } + + // Now begin enumerating the resulting syntax trees + return pgf_parse_result(state, pool); +} + +void +pgf_print_chunks(PgfConcr* concr, PgfCId cat, PgfLexer *lexer, GuPool* pool) +{ + // Begin parsing a sentence of the specified category + PgfParseState* state = + pgf_parser_init_state(concr, cat, 0, pool); + if (state == NULL) { + printf("\n"); + return; + } + + // Tokenization + GuExn* lex_err = gu_new_exn(NULL, gu_kind(type), pool); + PgfToken tok = pgf_lexer_read_token(lexer, lex_err); + while (!gu_exn_is_raised(lex_err)) { + // feed the token to get a new parse state + state = pgf_parser_next_state(state, tok, pool); + if (state == NULL) { + printf("\n"); + return; + } + + tok = pgf_lexer_read_token(lexer, lex_err); + } + + pgf_parse_print_chunks(state); +} -- cgit v1.2.3