Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of LibCSS.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
  6.  */
  7.  
  8. #ifndef css_css__parse_language_h_
  9. #define css_css__parse_language_h_
  10.  
  11. #include <parserutils/utils/stack.h>
  12. #include <parserutils/utils/vector.h>
  13.  
  14. #include <libcss/functypes.h>
  15. #include <libcss/types.h>
  16.  
  17. #include "lex/lex.h"
  18. #include "parse/parse.h"
  19. #include "parse/propstrings.h"
  20.  
  21. /**
  22.  * CSS namespace mapping
  23.  */
  24. typedef struct css_namespace {
  25.         lwc_string *prefix;             /**< Namespace prefix */
  26.         lwc_string *uri;                /**< Namespace URI */
  27. } css_namespace;
  28.  
  29. /**
  30.  * Context for a CSS language parser
  31.  */
  32. typedef struct css_language {
  33.         css_stylesheet *sheet;          /**< The stylesheet to parse for */
  34.  
  35. #define STACK_CHUNK 32
  36.         parserutils_stack *context;     /**< Context stack */
  37.  
  38.         enum {
  39.                 CHARSET_PERMITTED,
  40.                 IMPORT_PERMITTED,
  41.                 NAMESPACE_PERMITTED,
  42.                 HAD_RULE
  43.         } state;                        /**< State flag, for at-rule handling */
  44.  
  45.         /** Interned strings */
  46.         lwc_string **strings;
  47.  
  48.         lwc_string *default_namespace;  /**< Default namespace URI */
  49.         css_namespace *namespaces;      /**< Array of namespace mappings */
  50.         uint32_t num_namespaces;        /**< Number of namespace mappings */
  51.  
  52.         css_allocator_fn alloc;         /**< Memory (de)allocation function */
  53.         void *pw;                       /**< Client's private data */
  54. } css_language;
  55.  
  56. css_error css__language_create(css_stylesheet *sheet, css_parser *parser,
  57.                 css_allocator_fn alloc, void *pw, void **language);
  58. css_error css__language_destroy(css_language *language);
  59.  
  60. /******************************************************************************
  61.  * Helper functions                                                           *
  62.  ******************************************************************************/
  63.  
  64. /**
  65.  * Consume all leading whitespace tokens
  66.  *
  67.  * \param vector  The vector to consume from
  68.  * \param ctx     The vector's context
  69.  */
  70. static inline void consumeWhitespace(const parserutils_vector *vector, int *ctx)
  71. {
  72.         const css_token *token = NULL;
  73.  
  74.         while ((token = parserutils_vector_peek(vector, *ctx)) != NULL &&
  75.                         token->type == CSS_TOKEN_S)
  76.                 parserutils_vector_iterate(vector, ctx);
  77. }
  78.  
  79. /**
  80.  * Determine if a token is a character
  81.  *
  82.  * \param token  The token to consider
  83.  * \param c      The character to match (lowercase ASCII only)
  84.  * \return True if the token matches, false otherwise
  85.  */
  86. static inline bool tokenIsChar(const css_token *token, uint8_t c)
  87. {
  88.         bool result = false;
  89.  
  90.         if (token != NULL && token->type == CSS_TOKEN_CHAR &&
  91.                         lwc_string_length(token->idata) == 1) {
  92.                 char d = lwc_string_data(token->idata)[0];
  93.  
  94.                 /* Ensure lowercase comparison */
  95.                 if ('A' <= d && d <= 'Z')
  96.                         d += 'a' - 'A';
  97.  
  98.                 result = (d == c);
  99.         }
  100.  
  101.         return result;
  102. }
  103.  
  104. #endif
  105.  
  106.