From 63f60598e030911846c399c3aab45e0cc8ee2993 Mon Sep 17 00:00:00 2001 From: Dodji Seketeli Date: Thu, 12 Feb 2004 22:49:12 +0000 Subject: added this new api entry point to parse comma separated list of property 2004-02-12 Dodji Seketeli * src/cr-declaration.[ch]: (cr_declaration_parse_list_from_buf): added this new api entry point to parse comma separated list of property declarations. * src/cr-parser.c: (cr_parser_parse_expr): dont return an error when we encounter EOF immediately at the end of an expression. (cr_parser_get_tknzr): added this new api entry point to get the parser's underlying tokenizer. * src/cr-statement.h: fixed a comment here. * tests/test4-main.c: wrote a test for the new cr_declaration_parse_list_from_buf() function. --- ChangeLog | 14 +++++++ src/cr-declaration.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/cr-declaration.h | 5 +++ src/cr-parser.c | 29 ++++++++++++- src/cr-parser.h | 3 ++ src/cr-statement.h | 4 +- tests/test4-main.c | 51 +++++++++++++++++++++- 7 files changed, 218 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 37e12b4..e3d8645 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2004-02-12 Dodji Seketeli + + * src/cr-declaration.[ch]: + (cr_declaration_parse_list_from_buf): added this new api entry point + to parse comma separated list of property declarations. + * src/cr-parser.c: + (cr_parser_parse_expr): dont return an error when we encounter EOF immediately + at the end of an expression. + (cr_parser_get_tknzr): added this new api entry point to get + the parser's underlying tokenizer. + * src/cr-statement.h: fixed a comment here. + * tests/test4-main.c: wrote a test for the new + cr_declaration_parse_list_from_buf() function. + 2004-02-11 Dodji Seketeli * src/Makefile.am: make sure headers go in $prefix/include/libcroco/libcroco . diff --git a/src/cr-declaration.c b/src/cr-declaration.c index 096759c..352bebe 100644 --- a/src/cr-declaration.c +++ b/src/cr-declaration.c @@ -216,6 +216,122 @@ cr_declaration_parse_from_buf (CRStatement *a_statement, return result ; } +/** + *Parses a ';' separated list of properties declaration. + *@param a_str the input buffer that contains the list of declaration to + *parse. + *@param a_enc the encoding of a_str + *@return the parsed list of declaration, NULL if parsing failed. + */ +CRDeclaration * +cr_declaration_parse_list_from_buf (const guchar *a_str, enum CREncoding a_enc) +{ + + enum CRStatus status = CR_OK ; + CRTerm *value = NULL ; + GString *property = NULL; + CRDeclaration *result = NULL, *cur_decl = NULL ; + CRParser * parser = NULL ; + CRTknzr *tokenizer = NULL ; + + g_return_val_if_fail (a_str, NULL) ; + + parser = cr_parser_new_from_buf (a_str, + strlen (a_str), + a_enc, FALSE) ; + g_return_val_if_fail (parser, NULL) ; + status = cr_parser_get_tknzr (parser, &tokenizer) ; + if (status != CR_OK || !tokenizer) + { + if (status == CR_OK) + status = CR_ERROR ; + goto cleanup ; + } + status = cr_parser_try_to_skip_spaces_and_comments (parser) ; + if (status != CR_OK) + goto cleanup ; + + status = cr_parser_parse_declaration (parser, &property, + &value) ; + if (status != CR_OK || !property) + { + if (status != CR_OK) + status = CR_ERROR ; + goto cleanup ; + } + result = cr_declaration_new (NULL, property, value) ; + if (result) + { + property = NULL ; + value = NULL ; + } + /*now, go parse the other declarations*/ + for (;;) + { + guint32 c = 0 ; + cr_parser_try_to_skip_spaces_and_comments (parser) ; + status = cr_tknzr_peek_char (tokenizer, &c) ; + if (status != CR_OK) + { + if (status == CR_END_OF_INPUT_ERROR) + status = CR_OK ; + goto cleanup ; + } + if (c == ';') + { + status = cr_tknzr_read_char (tokenizer, &c) ; + } + else + { + break ; + } + cr_parser_try_to_skip_spaces_and_comments (parser) ; + status = cr_parser_parse_declaration (parser, &property, + &value) ; + if (status != CR_OK || !property) + break ; + cur_decl = cr_declaration_new (NULL, property, value) ; + if (cur_decl) + { + result = cr_declaration_append (result, cur_decl) ; + property = NULL ; + value = NULL ; + cur_decl = NULL; + } + else + { + break ; + } + } + + cleanup: + + if (parser) + { + cr_parser_destroy (parser) ; + parser = NULL ; + } + + if (property) + { + g_string_free (property, TRUE) ; + property = NULL ; + } + + if (value) + { + cr_term_destroy (value) ; + value = NULL ; + } + + if (status != CR_OK && result) + { + cr_declaration_destroy (result) ; + result = NULL ; + } + return result ; +} + /** *Appends a new declaration to the current declarations list. diff --git a/src/cr-declaration.h b/src/cr-declaration.h index acc7c8a..c96c551 100644 --- a/src/cr-declaration.h +++ b/src/cr-declaration.h @@ -78,6 +78,11 @@ CRDeclaration * cr_declaration_parse_from_buf (CRStatement *a_statement, const guchar *a_str, enum CREncoding a_enc) ; + +CRDeclaration * +cr_declaration_parse_list_from_buf (const guchar *a_str, + enum CREncoding a_enc) ; + CRDeclaration * cr_declaration_append (CRDeclaration *a_this, CRDeclaration *a_new) ; diff --git a/src/cr-parser.c b/src/cr-parser.c index c580799..6cb5219 100644 --- a/src/cr-parser.c +++ b/src/cr-parser.c @@ -3682,10 +3682,12 @@ cr_parser_parse_expr (CRParser *a_this, CRTerm **a_expr) { if (status == CR_END_OF_INPUT_ERROR) { + /* if (!nb_terms) { goto error ; - } + } + */ status = CR_OK ; break ; } @@ -5124,7 +5126,32 @@ cr_parser_set_tknzr (CRParser *a_this, CRTknzr *a_tknzr) return CR_OK ; } +/** + *Getter of the parser's underlying tokenizer + *@param a_this the current instance of #CRParser + *@param a_tknzr out parameter. The returned tokenizer + *@return CR_OK upon succesful completion, an error code + *otherwise + */ +enum CRStatus +cr_parser_get_tknzr (CRParser *a_this, CRTknzr **a_tknzr) +{ + g_return_val_if_fail (a_this && PRIVATE (a_this) + && a_tknzr, + CR_BAD_PARAM_ERROR) ; + + *a_tknzr = PRIVATE (a_this)->tknzr ; + return CR_OK ; +} +/** + *Parses a stylesheet from a buffer + *@param a_this the current instance of #CRparser + *@param a_buf the input buffer + *@param a_len the length of the input buffer + *@param a_enc the encoding of the buffer + *@return CR_OK upon successful completion, an error code otherwise. + */ enum CRStatus cr_parser_parse_buf (CRParser *a_this, const guchar *a_buf, gulong a_len, enum CREncoding a_enc) diff --git a/src/cr-parser.h b/src/cr-parser.h index 4dc80cb..9339c9a 100644 --- a/src/cr-parser.h +++ b/src/cr-parser.h @@ -73,6 +73,9 @@ cr_parser_new_from_input (CRInput *a_input) ; enum CRStatus cr_parser_set_tknzr (CRParser *a_this, CRTknzr *a_tknzr) ; +enum CRStatus +cr_parser_get_tknzr (CRParser *a_this, CRTknzr **a_tknzr) ; + enum CRStatus cr_parser_try_to_skip_spaces_and_comments (CRParser *a_this) ; diff --git a/src/cr-statement.h b/src/cr-statement.h index 9f893be..b052c26 100644 --- a/src/cr-statement.h +++ b/src/cr-statement.h @@ -151,9 +151,11 @@ enum CRStatementType *each unknown at-rule will *be of this type. */ + + /**A css at-rule*/ AT_RULE_STMT = 0, - /**A css at-rule*/ + /*A css ruleset*/ RULESET_STMT, /**A css2 import rule*/ diff --git a/tests/test4-main.c b/tests/test4-main.c index 3733ad5..5db1383 100644 --- a/tests/test4-main.c +++ b/tests/test4-main.c @@ -38,6 +38,9 @@ CRDocHandler * gv_test_handler = {0} ; const guchar * gv_decl_buf = "toto: tutu, tata" ; +const guchar * gv_decl_list_buf = +"toto: titi; prop1:val1 ; prop2:val2" ; + const guchar *gv_ruleset_buf = "s1 > s2 {toto: tutu, tata} " ; @@ -154,12 +157,13 @@ test_cr_declaration_parse (void) decl = cr_declaration_parse_from_buf (NULL, gv_decl_buf, CR_UTF_8) ; + if (!decl) + return CR_ERROR ; tmp_str = cr_declaration_to_string (decl, 2) ; if (decl) { cr_declaration_destroy (decl) ; - return CR_OK ; } if (tmp_str) @@ -168,7 +172,44 @@ test_cr_declaration_parse (void) tmp_str = NULL ; } - return CR_ERROR ; + return CR_OK ; +} + +static enum CRStatus +test_cr_declaration_parse_list (void) +{ + GString *str = NULL ; + guchar *tmp_str = NULL ; + CRDeclaration *decl = NULL, *cur_decl = NULL ; + + decl = cr_declaration_parse_list_from_buf (gv_decl_list_buf, + CR_UTF_8) ; + if (!decl) + return CR_ERROR ; + str = g_string_new (NULL) ; + for (cur_decl = decl ; cur_decl ; cur_decl = cur_decl->next) + { + tmp_str = cr_declaration_to_string (cur_decl, 2) ; + if (tmp_str) + { + g_string_append_printf (str, "%s;", tmp_str) ; + g_free (tmp_str) ; + tmp_str = NULL ; + } + + } + if (decl) + { + cr_declaration_destroy (decl) ; + } + + if (str) + { + g_string_free (str, TRUE) ; + str = NULL ; + } + + return CR_OK ; } static enum CRStatus @@ -363,6 +404,12 @@ main (int argc, char ** argv) return 0 ; } + status = test_cr_declaration_parse_list () ; + if (status != CR_OK) + { + g_print ("\nKO\n") ; + return 0 ; + } status = test_cr_statement_ruleset_parse () ; if (status != CR_OK) { -- cgit v1.2.1