diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-11-16 23:15:37 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-11-16 23:15:37 +0100 |
commit | 8441aa3623e1c2fa9982731bc60b65b29451e6b8 (patch) | |
tree | 658bf021dcb8feacd6a8fa82c6b17fb941c9853c /src/mds-kbdc/tree.c | |
parent | mds-kbdc: fix stack bug when parsing mapping statements (diff) | |
download | mds-8441aa3623e1c2fa9982731bc60b65b29451e6b8.tar.gz mds-8441aa3623e1c2fa9982731bc60b65b29451e6b8.tar.bz2 mds-8441aa3623e1c2fa9982731bc60b65b29451e6b8.tar.xz |
print parsed tree, of course there are some errors...
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/mds-kbdc/tree.c | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/src/mds-kbdc/tree.c b/src/mds-kbdc/tree.c index 4f0fec0..9f1085c 100644 --- a/src/mds-kbdc/tree.c +++ b/src/mds-kbdc/tree.c @@ -218,3 +218,254 @@ void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this) free(this); } + + +/** + * Convert the tree to a specialised subtype and + * prints its type and code location + * + * @param LOWERCASE:identifer The name of subtype + * @param NOTATION:const char* The notation for the subtype + */ +#define NODE(LOWERCASE, NOTATION) \ + mds_kbdc_tree_##LOWERCASE##_t* node; \ + node = (mds_kbdc_tree_##LOWERCASE##_t*)this; \ + fprintf(output, "%*.s(\033[01m%s\033[00m", indent, "", NOTATION); \ + fprintf(output, " \033[36m(@ %zu %zu-%zu)\033[00m", \ + node->loc_line, node->loc_start, node->loc_end) + + +/** + * Print a member for `node` which is a subtree + * + * @param MEMBER:identifier The tree structure's member + */ +#define BRANCH(MEMBER) \ + if (node->MEMBER) \ + { \ + fprintf(output, "\n%*.s(.%s\n", indent + 2, "", #MEMBER); \ + mds_kbdc_tree_print_indented(node->MEMBER, output, indent + 4); \ + fprintf(output, "%*.s)", indent + 2, ""); \ + } \ + else \ + fprintf(output, "\n%*.s(.%s \033[35mnil\033[00m)", indent + 2, "", #MEMBER) + + +/** + * End a tree which has at least one member that is a subtree + */ +#define COMPLEX \ + fprintf(output, "\n%*.s)\n", indent, "") + + +/** + * Print a member for `node` which is a string + * + * @param MEMBER:identifier The tree structure's member + */ +#define STRING(MEMBER) \ + if (node->MEMBER) \ + fprintf(output, " ‘\033[32m%s\033[00m’", node->MEMBER); \ + else \ + fprintf(output, " \033[35mnil\033[00m") + + +/** + * Print a member for `node` which is a string, + * and end the tree + * + * @param MEMBER:identifier The tree structure's member + */ +#define SIMPLE(MEMBER) \ + STRING(MEMBER); \ + fprintf(output, ")\n", node->MEMBER) + + +/** + * Print a tree which has only one member, + * and whose member is a string + * + * @param LOWERCASE:identifier See `NODE` + * @param NOTATION:const char* See `NODE` + * @param MEMBER:identifier See `STRING` + */ +#define SIMPLEX(LOWERCASE, NOTATION, MEMBER) \ + { \ + NODE(LOWERCASE, NOTATION); \ + SIMPLE(MEMBER); \ + } \ + break + + +/** + * Print a tree which has exactly two members, + * and whose members is are strings + * + * @param LOWERCASE:identifier See `NODE` + * @param NOTATION:const char* See `NODE` + * @param FIRST:identifier See `STRING`, the first member + * @param LAST:identifier See `STRING`, the second member + */ +#define DUPLEX(LOWERCASE, NOTATION, FIRST, LAST) \ + { \ + NODE(LOWERCASE, NOTATION); \ + STRING(FIRST); \ + SIMPLE(LAST); \ + } \ + break + + +/** + * Print a tree which has exactly one member, + * and whose members is a subtree + * + * @param LOWERCASE:identifier See `NODE` + * @param NOTATION:const char* See `NODE` + * @param MEMBER:identifier See `BRANCH` + */ +#define NESTING(LOWERCASE, NOTATION, MEMBER) \ + { \ + NODE(LOWERCASE, NOTATION); \ + BRANCH(MEMBER); \ + COMPLEX; \ + } \ + break + + +/** + * Print a tree which has exactly two members, + * and whose first member is a string and second + * member is a subtree + * + * @param LOWERCASE:identifier See `NODE` + * @param NOTATION:const char* See `NODE` + * @param NAMER:identifier See `STRING` + * @param MEMBER:identifier See `BRANCH` + */ +#define NAMED_NESTING(LOWERCASE, NOTATION, NAMER, MEMBER) \ + { \ + NODE(LOWERCASE, NOTATION); \ + STRING(NAMER); \ + BRANCH(MEMBER); \ + COMPLEX; \ + } \ + break + + +/** + * Print a tree which has no members + * + * @param NOTATION:const char* See `NODE` + */ +#define NOTHING(NOTATION) \ + fprintf(output, "%*.s(\033[01m%s\033[00m", indent, "", NOTATION); \ + fprintf(output, " \033[36m(@ %zu %zu-%zu)\033[00m", \ + this->loc_line, this->loc_start, this->loc_end); \ + fprintf(output, ")\n"); \ + break + + +/** + * Print a tree into a file + * + * @param this The tree node + * @param output The output file + * @param indent The indent + */ +static void mds_kbdc_tree_print_indented(mds_kbdc_tree_t* restrict this, FILE* output, int indent) +{ + again: + if (this == NULL) + return; + + switch (this->type) + { + /* These have their break built into their macro. */ + case MDS_KBDC_TREE_TYPE_INFORMATION: NESTING(information, "information", inner); + case MDS_KBDC_TREE_TYPE_INFORMATION_LANGUAGE: SIMPLEX(information_language, "language", data); + case MDS_KBDC_TREE_TYPE_INFORMATION_COUNTRY: SIMPLEX(information_country, "country", data); + case MDS_KBDC_TREE_TYPE_INFORMATION_VARIANT: SIMPLEX(information_variant, "variant", data); + case MDS_KBDC_TREE_TYPE_INCLUDE: SIMPLEX(include, "include", filename); + case MDS_KBDC_TREE_TYPE_FUNCTION: NAMED_NESTING(function, "function", name, inner); + case MDS_KBDC_TREE_TYPE_MACRO: NAMED_NESTING(macro, "macro", name, inner); + case MDS_KBDC_TREE_TYPE_ASSUMPTION: NESTING(assumption, "assumption", inner); + case MDS_KBDC_TREE_TYPE_ASSUMPTION_HAVE: NESTING(assumption_have, "have", data); + case MDS_KBDC_TREE_TYPE_ASSUMPTION_HAVE_CHARS: SIMPLEX(assumption_have_chars, "have_chars", chars); + case MDS_KBDC_TREE_TYPE_ASSUMPTION_HAVE_RANGE: DUPLEX(assumption_have_range, "have_range", first, last); + case MDS_KBDC_TREE_TYPE_LET: NAMED_NESTING(let, "let", variable, value); + case MDS_KBDC_TREE_TYPE_ARRAY: NESTING(array, "array", elements); + case MDS_KBDC_TREE_TYPE_KEYS: SIMPLEX(keys, "keys", keys); + case MDS_KBDC_TREE_TYPE_STRING: SIMPLEX(string, "string", string); + case MDS_KBDC_TREE_TYPE_NOTHING: NOTHING("nothing"); + case MDS_KBDC_TREE_TYPE_ALTERNATION: NESTING(alternation, "alternation", inner); + case MDS_KBDC_TREE_TYPE_UNORDERED: NESTING(unordered, "unordered", inner); + case MDS_KBDC_TREE_TYPE_MACRO_CALL: NAMED_NESTING(macro_call, "macro_call", name, arguments); + case MDS_KBDC_TREE_TYPE_RETURN: NOTHING("return"); + case MDS_KBDC_TREE_TYPE_BREAK: NOTHING("break"); + case MDS_KBDC_TREE_TYPE_CONTINUE: NOTHING("continue"); + + case MDS_KBDC_TREE_TYPE_FOR: + { + NODE(for, "for"); + STRING(first); + STRING(last); + fprintf(output, " (.variable"); + STRING(variable); + fprintf(output, ")"); + BRANCH(inner); + COMPLEX; + } + break; + + case MDS_KBDC_TREE_TYPE_IF: + { + NODE(if, "if"); + STRING(condition); + BRANCH(inner); + BRANCH(otherwise); + COMPLEX; + } + break; + + case MDS_KBDC_TREE_TYPE_MAP: + { + NODE(map, "map"); + BRANCH(sequence); + BRANCH(result); + COMPLEX; + } + break; + + default: + abort(); + break; + } + + this = this->next; + goto again; +} + + +/** + * Print a tree into a file + * + * @param this The tree node + * @param output The output file + */ +void mds_kbdc_tree_print(mds_kbdc_tree_t* restrict this, FILE* output) +{ + mds_kbdc_tree_print_indented(this, output, 0); +} + + +#undef NOTHING +#undef NAMED_NESTING +#undef NESTING +#undef DUPLEX +#undef SIMPLEX +#undef SIMPLE +#undef STRING +#undef COMPLEX +#undef BRANCH +#undef NODE + |