StyleSheetParser.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORESTYLESHEETPARSER_H
  28. #define ROCKETCORESTYLESHEETPARSER_H
  29. namespace Rocket {
  30. namespace Core {
  31. class PropertyDictionary;
  32. class Stream;
  33. class StyleSheetNode;
  34. /**
  35. Helper class for parsing a style sheet into its memory representation.
  36. @author Lloyd Weehuizen
  37. */
  38. class StyleSheetParser
  39. {
  40. public:
  41. StyleSheetParser();
  42. ~StyleSheetParser();
  43. /// Parses the given stream into the style sheet
  44. /// @param node The root node the stream will be parsed into
  45. /// @param stream The stream to read
  46. /// @return The number of parsed rules, or -1 if an error occured.
  47. int Parse(StyleSheetNode* node, Stream* stream);
  48. /// Parses the given string into the property dictionary
  49. /// @param parsed_properties The properties dictionary the properties will be read into
  50. /// @param properties The properties to parse
  51. /// @return True if the parse was successful, or false if an error occured.
  52. bool ParseProperties(PropertyDictionary& parsed_properties, const String& properties);
  53. private:
  54. // Stream we're parsing from.
  55. Stream* stream;
  56. // Parser memory buffer.
  57. String parse_buffer;
  58. // How far we've read through the buffer.
  59. size_t parse_buffer_pos;
  60. // The name of the file we'r parsing.
  61. String stream_file_name;
  62. // Current line number we're parsing.
  63. size_t line_number;
  64. // Parses properties from the parse buffer into the dictionary
  65. // @param properties The dictionary to store the properties in
  66. bool ReadProperties(PropertyDictionary& properties);
  67. // Import properties into the stylesheet node
  68. // @param node Node to import into
  69. // @param names The names of the nodes
  70. // @param properties The dictionary of properties
  71. // @param rule_specificity The specifity of the rule
  72. bool ImportProperties(StyleSheetNode* node, const String& names, const PropertyDictionary& properties, int rule_specificity);
  73. // Attempts to find one of the given character tokens in the active stream
  74. // If it's found, buffer is filled with all content up until the token
  75. // @param buffer The buffer that receives the content
  76. // @param characters The character tokens to find
  77. // @param remove_token If the token that caused the find to stop should be removed from the stream
  78. bool FindToken(String& buffer, const char* tokens, bool remove_token);
  79. // Attempts to find the next character in the active stream.
  80. // If it's found, buffer is filled with the character
  81. // @param buffer The buffer that receives the character, if read.
  82. bool ReadCharacter(char& buffer);
  83. // Fill the internal parse buffer
  84. bool FillBuffer();
  85. };
  86. }
  87. }
  88. #endif