Revision | c5724a598d0e0e376b567fcadc9ead0999225881 (tree) |
---|---|
Time | 2023-04-03 02:44:20 |
Author | dyknon <dyknon@user...> |
Commiter | dyknon |
Remove unneeded "Grammar" file
@@ -4,9 +4,6 @@ use warnings; | ||
4 | 4 | use strict; |
5 | 5 | use feature qw(postderef); |
6 | 6 | use Carp; |
7 | -use Text::Hjson::Grammar; | |
8 | - | |
9 | -our $grammar = "Text::Hjson::Grammar"; | |
10 | 7 | |
11 | 8 | our $ws = qr/[\t\r\n ]/; |
12 | 9 | our $inline_ws = qr/[\t\r ]/; |
@@ -102,57 +99,6 @@ sub decode_quoted_string($$){ | ||
102 | 99 | $str |
103 | 100 | } |
104 | 101 | |
105 | -sub consume($$$$){ | |
106 | - my ($self, $what, $in, $at) = @_; | |
107 | - croak "unknown grammar: $what" if(!$grammar->can($what)); | |
108 | - my %g = $grammar->$what(); | |
109 | - my $act = sub{scalar ($g{act} // sub{})->($self, @_)}; | |
110 | - if($g{re}){ | |
111 | - pos($in) = $at; | |
112 | - if($in =~ /\G$g{re}/g){ | |
113 | - ( | |
114 | - pos($in), | |
115 | - $act->([@{^CAPTURE}]) | |
116 | - ) | |
117 | - }else{ | |
118 | - () | |
119 | - } | |
120 | - }elsif($g{seq}){ | |
121 | - my @got; | |
122 | - for($g{seq}->@*){ | |
123 | - ($at, my $read) = $self->consume($_, $in, $at); | |
124 | - return () if(!defined $at); | |
125 | - push @got, $read; | |
126 | - } | |
127 | - ($at, $act->(@got)) | |
128 | - }elsif($g{any}){ | |
129 | - my ($i, $bt, $read); | |
130 | - for $i(0 .. $g{any}->$#*){ | |
131 | - ($bt, $read) = $self->consume($g{any}[$i], $in, $at); | |
132 | - if(defined $bt){ | |
133 | - return ($bt, $act->($i, $read)); | |
134 | - } | |
135 | - } | |
136 | - () | |
137 | - }elsif($g{many}){ | |
138 | - my $bt; | |
139 | - my @got; | |
140 | - while(1){ | |
141 | - ($bt, my $read) = $self->consume($g{many}, $in, $at); | |
142 | - last if(!defined $bt); | |
143 | - die "infinite loop detected in $what" if($at == $bt); | |
144 | - $at = $bt; | |
145 | - push @got, $read; | |
146 | - } | |
147 | - ($at, $act->(@got)) | |
148 | - }elsif($g{opt}){ | |
149 | - my ($bt, $read) = $self->consume($g{opt}, $in, $at); | |
150 | - ($bt//$at, $act->($read)) | |
151 | - }else{ | |
152 | - die "broken grammar"; | |
153 | - } | |
154 | -} | |
155 | - | |
156 | 102 | sub consume_value($$$){ |
157 | 103 | my ($self, $in, $at) = @_; |
158 | 104 | my $s = substr($in, $at, 1); |
@@ -1,181 +0,0 @@ | ||
1 | -package Text::Hjson::Grammar; | |
2 | - | |
3 | -use warnings; | |
4 | -use strict; | |
5 | -use feature qw(postderef); | |
6 | - | |
7 | -our $ws = qr/[\t\r\n ]/; | |
8 | -our $inline_ws = qr/[\t\r ]/; | |
9 | -our $inline = qr/$inline_ws|[^\0-\x20]/; | |
10 | -our $any = qr/$ws|[^\0-\x20]/; | |
11 | -our $line_comment = qr/(?:#|\/\/)$inline*+/; | |
12 | -our $block_comment = qr/\/\*(?:[^*]|\*(?!\/))*+\*\//; | |
13 | -our $comment = qr/$line_comment|$block_comment/; | |
14 | -our $wsc = qr/(?:$ws|$comment)*+/; | |
15 | -our $litend = qr/(?![ \t]*+[^\0-\x20#,\/\[\]{}])/; | |
16 | -our $separator = qr/$wsc,|(?:$inline_ws|$comment)*+\n/; | |
17 | -our $nonpunct = qr/[^\0-\x20,:\[\]{}]/; | |
18 | - | |
19 | -sub literal{ | |
20 | - re => qr/$wsc(true|false|null)$litend/, | |
21 | - act => sub{ | |
22 | - my ($self, $match) = @_; | |
23 | - { | |
24 | - true => $self->true, | |
25 | - false => $self->false, | |
26 | - null => $self->null, | |
27 | - }->{$match->[0]} | |
28 | - } | |
29 | -} | |
30 | -sub separator{re => qr/$separator/} | |
31 | -sub optional_sep{re => qr/$separator?/} | |
32 | -# XXX: not docuemnted, but tests say quoteless string | |
33 | -# starting with quote is not allowed. | |
34 | -sub quoteless_guard{re => qr/$wsc(?!["'])/} | |
35 | -sub value{ | |
36 | - any => [qw(literal object array number string)], | |
37 | - act => sub{$_[2]} | |
38 | -} | |
39 | -sub root{ | |
40 | - any => [qw(object_member_arr value)], | |
41 | - act => sub{$_[2]} | |
42 | -} | |
43 | -sub object{ | |
44 | - seq => [qw(begin_object object_inner end_object)], | |
45 | - act => sub{$_[2]} | |
46 | -} | |
47 | -sub begin_object{re => qr/$wsc\{/} | |
48 | -sub end_object{re => qr/$wsc\}/} | |
49 | -sub object_inner{ | |
50 | - opt => "object_member_arr", | |
51 | - act => sub{ | |
52 | - my ($self, $match) = @_; | |
53 | - $match // $self->object | |
54 | - } | |
55 | -} | |
56 | -sub object_member_arr{ | |
57 | - seq => [qw(object_member object_members optional_sep)], | |
58 | - act => sub{ | |
59 | - my ($self, $first, $left) = @_; | |
60 | - $self->object($first, @$left) | |
61 | - } | |
62 | -} | |
63 | -sub object_member{ | |
64 | - seq => [qw(object_key object_key_sep value)], | |
65 | - act => sub{ | |
66 | - my ($self, $key, $sep, $val) = @_; | |
67 | - [$key, $val] | |
68 | - } | |
69 | -} | |
70 | -sub object_members{ | |
71 | - many => "separated_object_member", | |
72 | - act => sub{shift; [@_]} | |
73 | -} | |
74 | -sub separated_object_member{ | |
75 | - seq => [qw(separator object_member)], | |
76 | - act => sub{$_[2]} | |
77 | -} | |
78 | -sub object_key{ | |
79 | - any => [qw(string_quoted object_key_noquote)], | |
80 | - act => sub{$_[2]} | |
81 | -} | |
82 | -sub object_key_noquote{ | |
83 | - seq => [qw(quoteless_guard object_key_noquote_body)], | |
84 | - act => sub{$_[2]} | |
85 | -} | |
86 | -sub object_key_noquote_body{ | |
87 | - re => qr/($nonpunct+)/, | |
88 | - act => sub{$_[1][0]} | |
89 | -} | |
90 | -sub wsc{re => qr/$wsc/} | |
91 | -sub object_key_sep{re => qr/$wsc:/} | |
92 | -sub array{ | |
93 | - seq => [qw(begin_array array_inner end_array)], | |
94 | - act => sub{$_[2]} | |
95 | -} | |
96 | -sub begin_array{re => qr/$wsc\[/} | |
97 | -sub end_array{re => qr/$wsc\]/} | |
98 | -sub array_inner{ | |
99 | - opt => "array_member_arr", | |
100 | - act => sub{ | |
101 | - my ($self, $match) = @_; | |
102 | - $match // $self->array; | |
103 | - } | |
104 | -} | |
105 | -sub array_member_arr{ | |
106 | - seq => [qw(value array_members optional_sep)], | |
107 | - act => sub{ | |
108 | - my ($self, $first, $left) = @_; | |
109 | - $self->array($first, @$left) | |
110 | - } | |
111 | -} | |
112 | -sub array_members{ | |
113 | - many => "separated_array_member", | |
114 | - act => sub{shift; [@_]} | |
115 | -} | |
116 | -sub separated_array_member{ | |
117 | - seq => [qw(separator value)], | |
118 | - act => sub{$_[2]} | |
119 | -} | |
120 | -sub number{ | |
121 | - re => qr/$wsc(-)?+((?:0|[1-9][0-9]*+)(?:\.[0-9]++)?+)(?:[eE]([-+]?+[0-9]++))?+$litend/, | |
122 | - act => sub{ | |
123 | - my ($self, $match) = @_; | |
124 | - my $v = $match->[1] * 10**($match->[2] // 0); | |
125 | - $match->[0] ? -$v : $v | |
126 | - } | |
127 | -} | |
128 | -sub string{ | |
129 | - any => [qw(string_multiline string_quoted string_noquote)], | |
130 | - act => sub{$_[2]} | |
131 | -} | |
132 | -# XXX: not in RFC but quote by ' is allowed. | |
133 | -sub string_quoted{ | |
134 | - re => qr/$wsc(["'])((?:(?!\g{1})[^\0-\x1f\\]|\\.)*+)\g{1}/, | |
135 | - act => sub{ | |
136 | - my ($self, $match) = @_; | |
137 | - $self->string($self->decode_quoted_string($match->[1])) | |
138 | - #$match->[1] =~ s{\\(u(.{4})|.)}{ | |
139 | - # { | |
140 | - # '"' => '"', | |
141 | - # "'" => "'", | |
142 | - # "\\" => "\\", | |
143 | - # "/" => "/", | |
144 | - # "b" => "\b", | |
145 | - # "f" => "\f", | |
146 | - # "n" => "\n", | |
147 | - # "r" => "\r", | |
148 | - # "t" => "\t", | |
149 | - # }->{$1} // chr(hex($2)) | |
150 | - #}ger; | |
151 | - } | |
152 | -} | |
153 | -sub string_noquote{ | |
154 | - seq => [qw(quoteless_guard string_noquote_body)], | |
155 | - act => sub{$_[2]} | |
156 | -} | |
157 | -sub string_noquote_body{ | |
158 | - re => qr/(${nonpunct}(?:\t|[^\0-\x1f])*)/, | |
159 | - act => sub{ | |
160 | - my ($self, $match) = @_; | |
161 | - $match->[0] =~ s/$inline_ws*+\z//r; | |
162 | - } | |
163 | -} | |
164 | -sub string_multiline{ | |
165 | - re => qr/($wsc)'''((?:(?!''')$any)*+)'''/, | |
166 | - act => sub{ | |
167 | - my ($self, $match) = @_; | |
168 | - my $indent = length($match->[0] =~ s/.*\n//r); | |
169 | - my $s = $match->[1] =~ s/\r//r; | |
170 | - | |
171 | - # XXX: What is "column" | |
172 | - $s =~ s/(?<=\n)${inline_ws}{0,$indent}//g; | |
173 | - # XXX: RFC says: Whitespace on the first line is ignored. | |
174 | - # my understanding: first line is ignored if it contains only ws. | |
175 | - $s =~ s/^$inline_ws*\n//; | |
176 | - | |
177 | - $s =~ s/\n\z//r | |
178 | - } | |
179 | -} | |
180 | - | |
181 | -1; |