cataract 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +7 -0
  2. data/.clang-tidy +30 -0
  3. data/.github/workflows/ci-macos.yml +12 -0
  4. data/.github/workflows/ci.yml +77 -0
  5. data/.github/workflows/test.yml +76 -0
  6. data/.gitignore +45 -0
  7. data/.overcommit.yml +38 -0
  8. data/.rubocop.yml +83 -0
  9. data/BENCHMARKS.md +201 -0
  10. data/CHANGELOG.md +1 -0
  11. data/Gemfile +27 -0
  12. data/LICENSE +21 -0
  13. data/RAGEL_MIGRATION.md +60 -0
  14. data/README.md +292 -0
  15. data/Rakefile +209 -0
  16. data/benchmarks/benchmark_harness.rb +193 -0
  17. data/benchmarks/benchmark_merging.rb +121 -0
  18. data/benchmarks/benchmark_optimization_comparison.rb +168 -0
  19. data/benchmarks/benchmark_parsing.rb +153 -0
  20. data/benchmarks/benchmark_ragel_removal.rb +56 -0
  21. data/benchmarks/benchmark_runner.rb +70 -0
  22. data/benchmarks/benchmark_serialization.rb +180 -0
  23. data/benchmarks/benchmark_shorthand.rb +109 -0
  24. data/benchmarks/benchmark_shorthand_expansion.rb +176 -0
  25. data/benchmarks/benchmark_specificity.rb +124 -0
  26. data/benchmarks/benchmark_string_allocation.rb +151 -0
  27. data/benchmarks/benchmark_stylesheet_to_s.rb +62 -0
  28. data/benchmarks/benchmark_to_s_cached.rb +55 -0
  29. data/benchmarks/benchmark_value_splitter.rb +54 -0
  30. data/benchmarks/benchmark_yjit.rb +158 -0
  31. data/benchmarks/benchmark_yjit_workers.rb +61 -0
  32. data/benchmarks/profile_to_s.rb +23 -0
  33. data/benchmarks/speedup_calculator.rb +83 -0
  34. data/benchmarks/system_metadata.rb +81 -0
  35. data/benchmarks/templates/benchmarks.md.erb +221 -0
  36. data/benchmarks/yjit_tests.rb +141 -0
  37. data/cataract.gemspec +34 -0
  38. data/cliff.toml +92 -0
  39. data/examples/color_conversion_visual_test/color_conversion_test.html +3603 -0
  40. data/examples/color_conversion_visual_test/generate.rb +202 -0
  41. data/examples/color_conversion_visual_test/template.html.erb +259 -0
  42. data/examples/css_analyzer/analyzer.rb +164 -0
  43. data/examples/css_analyzer/analyzers/base.rb +33 -0
  44. data/examples/css_analyzer/analyzers/colors.rb +133 -0
  45. data/examples/css_analyzer/analyzers/important.rb +88 -0
  46. data/examples/css_analyzer/analyzers/properties.rb +61 -0
  47. data/examples/css_analyzer/analyzers/specificity.rb +68 -0
  48. data/examples/css_analyzer/templates/report.html.erb +575 -0
  49. data/examples/css_analyzer.rb +69 -0
  50. data/examples/github_analysis.html +5343 -0
  51. data/ext/cataract/cataract.c +1086 -0
  52. data/ext/cataract/cataract.h +174 -0
  53. data/ext/cataract/css_parser.c +1435 -0
  54. data/ext/cataract/extconf.rb +48 -0
  55. data/ext/cataract/import_scanner.c +174 -0
  56. data/ext/cataract/merge.c +973 -0
  57. data/ext/cataract/shorthand_expander.c +902 -0
  58. data/ext/cataract/specificity.c +213 -0
  59. data/ext/cataract/value_splitter.c +116 -0
  60. data/ext/cataract_color/cataract_color.c +16 -0
  61. data/ext/cataract_color/color_conversion.c +1687 -0
  62. data/ext/cataract_color/color_conversion.h +136 -0
  63. data/ext/cataract_color/color_conversion_lab.c +571 -0
  64. data/ext/cataract_color/color_conversion_named.c +259 -0
  65. data/ext/cataract_color/color_conversion_oklab.c +547 -0
  66. data/ext/cataract_color/extconf.rb +23 -0
  67. data/ext/cataract_old/cataract.c +393 -0
  68. data/ext/cataract_old/cataract.h +250 -0
  69. data/ext/cataract_old/css_parser.c +933 -0
  70. data/ext/cataract_old/extconf.rb +67 -0
  71. data/ext/cataract_old/import_scanner.c +174 -0
  72. data/ext/cataract_old/merge.c +776 -0
  73. data/ext/cataract_old/shorthand_expander.c +902 -0
  74. data/ext/cataract_old/specificity.c +213 -0
  75. data/ext/cataract_old/stylesheet.c +290 -0
  76. data/ext/cataract_old/value_splitter.c +116 -0
  77. data/lib/cataract/at_rule.rb +97 -0
  78. data/lib/cataract/color_conversion.rb +18 -0
  79. data/lib/cataract/declarations.rb +332 -0
  80. data/lib/cataract/import_resolver.rb +210 -0
  81. data/lib/cataract/rule.rb +131 -0
  82. data/lib/cataract/stylesheet.rb +716 -0
  83. data/lib/cataract/stylesheet_scope.rb +257 -0
  84. data/lib/cataract/version.rb +5 -0
  85. data/lib/cataract.rb +107 -0
  86. data/lib/tasks/gem.rake +158 -0
  87. data/scripts/fuzzer/run.rb +828 -0
  88. data/scripts/fuzzer/worker.rb +99 -0
  89. data/scripts/generate_benchmarks_md.rb +155 -0
  90. metadata +135 -0
@@ -0,0 +1,174 @@
1
+ #ifndef CATARACT_NEW_H
2
+ #define CATARACT_NEW_H
3
+
4
+ #include <ruby.h>
5
+ #include <ruby/encoding.h>
6
+
7
+ // ============================================================================
8
+ // Global struct class references
9
+ // ============================================================================
10
+
11
+ extern VALUE cRule;
12
+ extern VALUE cDeclaration;
13
+ extern VALUE cAtRule;
14
+ extern VALUE cStylesheet;
15
+
16
+ // Error class references
17
+ extern VALUE eCataractError;
18
+ extern VALUE eDepthError;
19
+ extern VALUE eSizeError;
20
+
21
+ // ============================================================================
22
+ // Struct field indices
23
+ // ============================================================================
24
+
25
+ // Rule struct field indices (id, selector, declarations, specificity, parent_rule_id, nesting_style)
26
+ #define RULE_ID 0
27
+ #define RULE_SELECTOR 1
28
+ #define RULE_DECLARATIONS 2
29
+ #define RULE_SPECIFICITY 3
30
+ #define RULE_PARENT_RULE_ID 4
31
+ #define RULE_NESTING_STYLE 5
32
+
33
+ // Nesting style constants
34
+ #define NESTING_STYLE_IMPLICIT 0 // .parent { .child { } } - no &
35
+ #define NESTING_STYLE_EXPLICIT 1 // .parent { &.child { } } - has &
36
+
37
+ // Named constants for parse_css_recursive() call clarity
38
+ // (Makes call sites self-documenting)
39
+ #define NO_PARENT_MEDIA Qnil
40
+ #define NO_PARENT_SELECTOR Qnil
41
+ #define NO_PARENT_RULE_ID Qnil
42
+
43
+ // Declaration struct field indices (property, value, important)
44
+ #define DECL_PROPERTY 0
45
+ #define DECL_VALUE 1
46
+ #define DECL_IMPORTANT 2
47
+
48
+ // AtRule struct field indices (id, selector, content, specificity)
49
+ // Matches Rule interface for duck-typing
50
+ #define AT_RULE_ID 0
51
+ #define AT_RULE_SELECTOR 1
52
+ #define AT_RULE_CONTENT 2
53
+ #define AT_RULE_SPECIFICITY 3
54
+
55
+ // ============================================================================
56
+ // Macros
57
+ // ============================================================================
58
+
59
+ // Whitespace detection
60
+ #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
61
+
62
+ // Debug output (disabled by default)
63
+ // #define CATARACT_DEBUG 1
64
+
65
+ #ifdef CATARACT_DEBUG
66
+ #define DEBUG_PRINTF(...) printf(__VA_ARGS__)
67
+ #else
68
+ #define DEBUG_PRINTF(...) ((void)0)
69
+ #endif
70
+
71
+ // Trim leading whitespace - modifies start pointer
72
+ static inline void trim_leading(const char **start, const char *end) {
73
+ while (*start < end && IS_WHITESPACE(**start)) {
74
+ (*start)++;
75
+ }
76
+ }
77
+
78
+ // Trim trailing whitespace - modifies end pointer
79
+ static inline void trim_trailing(const char *start, const char **end) {
80
+ while (*end > start && IS_WHITESPACE(*(*end - 1))) {
81
+ (*end)--;
82
+ }
83
+ }
84
+
85
+ // Strip whitespace from both ends and return new string
86
+ static inline VALUE strip_string(const char *str, long len) {
87
+ const char *start = str;
88
+ const char *end = str + len;
89
+ trim_leading(&start, end);
90
+ trim_trailing(start, &end);
91
+ return rb_str_new(start, end - start);
92
+ }
93
+
94
+ // US-ASCII string literal creation
95
+ // Only for compile-time string literals - uses sizeof() for length
96
+ // For runtime char*, use rb_usascii_str_new(ptr, len) directly
97
+ #define USASCII_STR(str) rb_usascii_str_new((str), sizeof(str) - 1)
98
+
99
+ // UTF-8 string literal creation
100
+ // Only for compile-time string literals - uses sizeof() for length
101
+ // For runtime char*, use rb_utf8_str_new(ptr, len) directly
102
+ #define UTF8_STR(str) rb_utf8_str_new((str), sizeof(str) - 1)
103
+
104
+ // String allocation macros (from old cataract.h)
105
+ #ifndef DISABLE_STR_BUF_OPTIMIZATION
106
+ #define STR_NEW_WITH_CAPACITY(capacity) rb_str_buf_new(capacity)
107
+ #define STR_NEW_CSTR(str) rb_str_new_cstr(str)
108
+ #else
109
+ #define STR_NEW_WITH_CAPACITY(capacity) rb_str_new_cstr("")
110
+ #define STR_NEW_CSTR(str) rb_str_new_cstr(str)
111
+ #endif
112
+
113
+ // Safety limits
114
+ #ifndef MAX_PARSE_DEPTH
115
+ #define MAX_PARSE_DEPTH 10 // Max recursion depth for nested @media/@supports blocks and CSS nesting
116
+ #endif
117
+
118
+ #ifndef MAX_PROPERTY_NAME_LENGTH
119
+ #define MAX_PROPERTY_NAME_LENGTH 256 // Max length of CSS property name
120
+ #endif
121
+
122
+ #ifndef MAX_PROPERTY_VALUE_LENGTH
123
+ #define MAX_PROPERTY_VALUE_LENGTH 32768 // Max length of CSS property value (32KB)
124
+ #endif
125
+
126
+ #ifndef MAX_MEDIA_QUERIES
127
+ #define MAX_MEDIA_QUERIES 1000 // Prevent symbol table exhaustion
128
+ #endif
129
+
130
+ // ============================================================================
131
+ // Function declarations
132
+ // ============================================================================
133
+
134
+ // CSS parser (css_parser_new.c)
135
+ VALUE parse_css_new(VALUE self, VALUE css_string);
136
+ VALUE parse_css_new_impl(VALUE css_string, int rule_id_offset);
137
+ VALUE parse_media_types(VALUE self, VALUE media_query_sym);
138
+
139
+ // Merge (merge_new.c)
140
+ VALUE cataract_merge_new(VALUE self, VALUE rules_array);
141
+ void init_merge_constants(void);
142
+
143
+ // Specificity (specificity.c)
144
+ VALUE calculate_specificity(VALUE self, VALUE selector);
145
+
146
+ // Import scanner (import_scanner.c)
147
+ VALUE extract_imports(VALUE self, VALUE css_string);
148
+
149
+ // Shorthand expander (shorthand_expander_new.c)
150
+ VALUE cataract_split_value(VALUE self, VALUE value);
151
+ VALUE cataract_expand_margin(VALUE self, VALUE value);
152
+ VALUE cataract_expand_padding(VALUE self, VALUE value);
153
+ VALUE cataract_expand_border(VALUE self, VALUE value);
154
+ VALUE cataract_expand_border_color(VALUE self, VALUE value);
155
+ VALUE cataract_expand_border_style(VALUE self, VALUE value);
156
+ VALUE cataract_expand_border_width(VALUE self, VALUE value);
157
+ VALUE cataract_expand_border_side(VALUE self, VALUE side, VALUE value);
158
+ VALUE cataract_expand_font(VALUE self, VALUE value);
159
+ VALUE cataract_expand_list_style(VALUE self, VALUE value);
160
+ VALUE cataract_expand_background(VALUE self, VALUE value);
161
+ VALUE cataract_create_margin_shorthand(VALUE self, VALUE properties);
162
+ VALUE cataract_create_padding_shorthand(VALUE self, VALUE properties);
163
+ VALUE cataract_create_border_width_shorthand(VALUE self, VALUE properties);
164
+ VALUE cataract_create_border_style_shorthand(VALUE self, VALUE properties);
165
+ VALUE cataract_create_border_color_shorthand(VALUE self, VALUE properties);
166
+ VALUE cataract_create_border_shorthand(VALUE self, VALUE properties);
167
+ VALUE cataract_create_font_shorthand(VALUE self, VALUE properties);
168
+ VALUE cataract_create_list_style_shorthand(VALUE self, VALUE properties);
169
+ VALUE cataract_create_background_shorthand(VALUE self, VALUE properties);
170
+
171
+ // Helper (from css_parser_new.c)
172
+ VALUE lowercase_property(VALUE property_str);
173
+
174
+ #endif // CATARACT_NEW_H