StyleSheetParser.h 4.5 KB

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