From 732ef5e9787e618ead3a18f9f6aa2ed1f03f1670 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 14 Oct 2020 17:44:38 -0600 Subject: throw early draft of parser into the world --- tests/__init__.py | 0 tests/test_parsing.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_parsing.py (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_parsing.py b/tests/test_parsing.py new file mode 100644 index 0000000..91787a6 --- /dev/null +++ b/tests/test_parsing.py @@ -0,0 +1,75 @@ +import unittest + +from crowbar_reference_compiler import parse_header, parse_implementation, scan + + +class TestParsing(unittest.TestCase): + def test_basic(self): + print(parse_header(scan("int x();"))) + + def test_scdoc_str(self): + # adapted from https://git.sr.ht/~sircmpwn/scdoc/tree/master/include/str.h + print(parse_header(scan(r""" +include "stdint.h"; + +struct str { + char *str; + typedef size_t len; + typedef size_t size; +}; + +struct str *str_create(); +void str_free(struct str *str); +void str_reset(struct str *str); +int str_append_ch(struct str *str, typedef uint32_t ch); +"""))) + # adapted from https://git.sr.ht/~sircmpwn/scdoc/tree/master/src/string.c + print(parse_implementation(scan(r""" +include "stdlib.h"; +include "stdint.h"; +include "str.h"; +include "unicode.h"; + +int ensure_capacity(struct str *str, typedef size_t len) { + if (len + 1 >= str->size) { + char *new = realloc(str->str, str->size * 2); + if (!new) { + return 0; + } + str->str = new; + str->size *= 2; + } + return 1; +} + +struct str *str_create() { + struct str *str = calloc(1, sizeof(struct str)); + str->str = malloc(16); + str->size = 16; + str->len = 0; + str->str[0] = '\0'; + return str; +} + +void str_free(struct str *str) { + if (!str) { + return; + } + free(str->str); + free(str); +} + +int str_append_ch(struct str *str, typedef uint32_t ch) { + int size = utf8_chsize(ch); + if (size <= 0) { + return -1; + } + if (!ensure_capacity(str, str->len + size)) { + return -1; + } + utf8_encode(&str->str[str->len], ch); + str->len += size; + str->str[str->len] = '\0'; + return size; +} +"""))) -- cgit v1.2.3