Debian Codegraph @

scripts/generate-html5-entities

raw
1 #!/usr/bin/env ruby
2 # frozen_string_literal: true
3
4 # Copyright 2018 Stephen Checkoway
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # The output of this script should be compiled with ragel 6.10 which is the
19 # current stable version. Version 7 changes some stuff but what, precisely,
20 # isn't documented yet.
21
22 require "psych"
23 require "open-uri"
24
25 ENTITIES_URL = "https://html.spec.whatwg.org/entities.json"
26 ACCEPTABLE_CODEPOINTS_LENGTHS = [1, 2]
27
28 # Modify the json to remove the characters item because it contains surrogate
29 # pairs that Psych errors on.
30 entities = URI.parse(ENTITIES_URL).read
31 entities.gsub!(/, "characters"[^}]*/, "")
32 entities = Psych.safe_load(entities)
33
34 without_semicolon = {}
35 with_semicolon = {}
36 entities.each do |key, val|
37 if key.end_with?(";")
38 with_semicolon[key[1..-1]] = val["codepoints"]
39 else
40 without_semicolon[key[1..-1]] = val["codepoints"]
41 end
42 end
43
44 without_semicolon.each do |entity, codepoints|
45 unless ACCEPTABLE_CODEPOINTS_LENGTHS.include?(codepoints.length)
46 puts "#{entity} has the wrong number of codepoints"
47 end
48 with = "#{entity};"
49 unless with_semicolon.key?(with)
50 puts("#{entity} but not #{with}")
51 next
52 end
53 if codepoints != with_semicolon[with]
54 puts("#{entity} and #{with} have different codepoints!")
55 end
56 end
57
58 puts(<<~EOF)
59 // Generated from <#{ENTITIES_URL}> by entities.rb
60 #include "char_ref.h"
61 %%{
62 machine named_char_ref;
63 named_char_ref := |*
64 EOF
65 with_semicolon.each do |entity, codepoints|
66 without = entity.chop
67 pattern = if without_semicolon.key?(without)
68 "'#{without}' . ';'?"
69 else
70 "'#{entity}'"
71 end
72 code = codepoints.map.with_index { |cp, idx| format("output[%d]=%#04x", idx, cp) }
73 puts(" #{pattern} => {#{code.join("; ")}; fbreak;};")
74 end
75 puts(<<~EOF)
76 *|;
77 write data noerror nofinal noentry noprefix;
78 }%%
79
80 size_t match_named_char_ref (
81 const char *str,
82 size_t size,
83 int output[2]
84 ) {
85 int cs;
86 int act;
87 const char *p = str;
88 const char *pe = str + size;
89 const char *const eof = pe;
90 const char *ts;
91 const char *te;
92 output[0] = output[1] = kGumboNoChar;
93 %% write init;
94 %% write exec;
95 (void)ts;
96 (void)act;
97 size = p - str;
98 return cs >= %%{ write first_final; }%%? size:0;
99 }
100 EOF