From a324baae03e4018c2c1aa8e4692505f8fcbe7172 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Wed, 30 Jul 2008 20:05:15 +0000 Subject: Implement the first-stage parser. Things missing: 1) Recovery after parse errors 2) Event emission (the locations of DEBUG_EVENTS are about right for this) 3) A second-stage parser 4) Any kind of testsuite svn path=/trunk/libcss/; revision=4825 --- test/INDEX | 1 + test/Makefile | 2 +- test/data/css/INDEX | 1 + test/data/css/blocks.css | 10 ++++++ test/parse.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 test/data/css/blocks.css create mode 100644 test/parse.c (limited to 'test') diff --git a/test/INDEX b/test/INDEX index b35be5c..fcf08f5 100644 --- a/test/INDEX +++ b/test/INDEX @@ -6,6 +6,7 @@ libcss Library initialisation/finalisation csdetect Character set detection csdetect lex Lexing css lex-auto Automated lexer tests lex +parse Parsing css # Regression tests diff --git a/test/Makefile b/test/Makefile index e2a97c6..a8363f4 100644 --- a/test/Makefile +++ b/test/Makefile @@ -35,7 +35,7 @@ d := $(DIR) CFLAGS := $(CFLAGS) -I$(TOP)/src/ -I$(d) # Tests -TESTS_$(d) := csdetect lex lex-auto libcss +TESTS_$(d) := csdetect lex lex-auto libcss parse TESTS_$(d) := $(TESTS_$(d)) # Items for top-level makefile to use diff --git a/test/data/css/INDEX b/test/data/css/INDEX index 531d4a5..4d46ba2 100644 --- a/test/data/css/INDEX +++ b/test/data/css/INDEX @@ -4,3 +4,4 @@ simple.css Reasonably simple CSS file (semantically invalid) allzengarden.css All CSS Zen Garden stylesheets concatenated +blocks.css Basic blocks and at-rule syntax diff --git a/test/data/css/blocks.css b/test/data/css/blocks.css new file mode 100644 index 0000000..9ecd720 --- /dev/null +++ b/test/data/css/blocks.css @@ -0,0 +1,10 @@ +@charset "UTF-8"; + +@import "simple.css"; + +@media screen +{ + body { background-color: green; } +} + + diff --git a/test/parse.c b/test/parse.c new file mode 100644 index 0000000..f24aa76 --- /dev/null +++ b/test/parse.c @@ -0,0 +1,79 @@ +#include +#include + +#include + +#include "charset/detect.h" +#include "utils/utils.h" + +#include "parse/parse.h" + +#include "testutils.h" + +static void *myrealloc(void *ptr, size_t len, void *pw) +{ + UNUSED(pw); + + return realloc(ptr, len); +} + +int main(int argc, char **argv) +{ + css_parser *parser; + FILE *fp; + size_t len, origlen; +#define CHUNK_SIZE (4096) + uint8_t buf[CHUNK_SIZE]; + css_error error; + + if (argc != 3) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + /* Initialise library */ + assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK); + + parser = css_parser_create((css_stylesheet *) 10, + "UTF-8", CSS_CHARSET_DICTATED, myrealloc, NULL); + assert(parser != NULL); + + fp = fopen(argv[2], "rb"); + if (fp == NULL) { + printf("Failed opening %s\n", argv[2]); + return 1; + } + + fseek(fp, 0, SEEK_END); + origlen = len = ftell(fp); + fseek(fp, 0, SEEK_SET); + + while (len >= CHUNK_SIZE) { + fread(buf, 1, CHUNK_SIZE, fp); + + error = css_parser_parse_chunk(parser, buf, CHUNK_SIZE); + assert(error == CSS_OK || error == CSS_NEEDDATA); + } + + if (len > 0) { + fread(buf, 1, len, fp); + + error = css_parser_parse_chunk(parser, buf, len); + assert(error == CSS_OK || error == CSS_NEEDDATA); + + len = 0; + } + + fclose(fp); + + assert(css_parser_completed(parser) == CSS_OK); + + css_parser_destroy(parser); + + assert(css_finalise(myrealloc, NULL) == CSS_OK); + + printf("PASS\n"); + + return 0; +} + -- cgit v1.2.3