| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Definition::Constructor::Compiler | # | ||
| 3 | # | Compiler for Constructor Control Sequences | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Core::Definition::Constructor::Compiler; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Global; | ||
| 16 | use LaTeXML::Common::Object; | ||
| 17 | use LaTeXML::Common::Error; | ||
| 18 | use LaTeXML::Common::XML; | ||
| 19 | use Scalar::Util qw(refaddr); | ||
| 20 | |||
| 21 | # We recognize several special operators: | ||
| 22 | # # #number|name accesses an argument to or property of the whatsit | ||
| 23 | # ? ( ) ?test(if)(else) a conditional, test | ||
| 24 | # & , &func(arg,...) replaces by result of function call | ||
| 25 | # < > <qname attr...> generates xml tag | ||
| 26 | # ^ ^ pattern floats to where pattern would be allowed | ||
| 27 | # Each of these can be used literally, if DOUBLED (ie. ## ) | ||
| 28 | # (except ^ ??? ^^ means something special!) | ||
| 29 | |||
| 30 | # These recognize the beginnings of value expressions, conditionals, .. | ||
| 31 | my $VALUE_RE = "(\\#(?!\\#)|\\&[\\w\\:]*\\()"; # [CONSTANT] | ||
| 32 | my $COND_RE = "\\?$VALUE_RE"; # [CONSTANT] | ||
| 33 | # Attempt to follow XML Spec, Appendix B | ||
| 34 | # QName (element tags, attribute names); Could this also allow expressions? | ||
| 35 | my $QNAME_RE = "((?:\\p{Ll}|\\p{Lu}|\\p{Lo}|\\p{Lt}|\\p{Nl}|_|:)" # [CONSTANT] | ||
| 36 | . "(?:\\p{Ll}|\\p{Lu}|\\p{Lo}|\\p{Lt}|\\p{Nl}|_|:|\\p{M}|\\p{Lm}|\\p{Nd}|\\.|\\-)*)"; | ||
| 37 | # The special characters | ||
| 38 | my $SPECIALS = "#?&\\"; | ||
| 39 | # Quoted special characters (or semi-special) | ||
| 40 | my $QUOTED_SPECIALS = "\\\\\\#|\\\\\\?|\\\\\\(|\\\\\\)|\\\\\\&|\\\\\\,|\\\\\\<|\\\\\\>|\\\\\\\\|\\\\\\%" | ||
| 41 | # or special cases: doubled #, & | ||
| 42 | . "|\\#\\#|\\&"; | ||
| 43 | # Unquote the above | ||
| 44 | sub unquote { | ||
| 45 | my ($string) = @_; | ||
| 46 | $string =~ s/\\(\#|\?|\(|\)|\&|\,|\<|\>|\\|%)/$1/gs; | ||
| 47 | $string =~ s/\#\#/\#/gs; | ||
| 48 | $string =~ s/\&/\&/gs; | ||
| 49 | return $string; } | ||
| 50 | |||
| 51 | sub compileConstructor { | ||
| 52 | my ($constructor) = @_; | ||
| 53 | my $replacement = $$constructor{replacement}; | ||
| 54 | return sub { } unless $replacement; | ||
| 55 | my $cs = $constructor->getCS; | ||
| 56 | my $name = $cs->getCSName; | ||
| 57 | my $nargs = $constructor->getNumArgs; | ||
| 58 | local $LaTeXML::Core::Definition::Constructor::CONSTRUCTOR = $constructor; | ||
| 59 | local $LaTeXML::Core::Definition::Constructor::NAME = $name; | ||
| 60 | local $LaTeXML::Core::Definition::Constructor::NARGS = $nargs; | ||
| 61 | $name =~ s/\W//g; | ||
| 62 | my $uid = refaddr $constructor; | ||
| 63 | $name = "LaTeXML::Package::Pool::constructor_" . $name . '_' . $uid; | ||
| 64 | my $floats = ($replacement =~ s/^(\^+)\s*//) && $1; # Grab float marker. | ||
| 65 | my ($body, $code, $result); | ||
| 66 | eval { | ||
| 67 | local $LaTeXML::IGNORE_ERRORS = 1; | ||
| 68 | $body = translate_constructor($replacement, $floats); | ||
| 69 | # Compile the constructor pattern into an anonymous sub that will construct the requested XML. | ||
| 70 | $code = | ||
| 71 | # Put the function in the Pool package, so that functions defined there can be used within" | ||
| 72 | # And, also that these definitions get cleaned up by the Daemon. | ||
| 73 | " package LaTeXML::Package::Pool;\n" | ||
| 74 | . "sub $name {\n" | ||
| 75 | . "my(" . join(', ', '$document', (map { "\$arg$_" } 1 .. $nargs), '%prop') . ")=\@_;\n" | ||
| 76 | . ($floats ? "my \$savenode;\n" : '') | ||
| 77 | . $body | ||
| 78 | . ($floats ? "\$document->setNode(\$savenode) if defined \$savenode;\n" : '') | ||
| 79 | . "}\n" | ||
| 80 | . "1;\n"; | ||
| 81 | ## no critic | ||
| 82 | $result = eval $code; | ||
| 83 | ## use critic | ||
| 84 | }; | ||
| 85 | |||
| 86 | if (!defined $result) { | ||
| 87 | # We can use die in the following code, which will get caught & wrapped "informatively" | ||
| 88 | my $msg = $@; | ||
| 89 | Error('misdefined', $cs, $LaTeXML::Core::Definition::Constructor::CONSTRUCTOR, | ||
| 90 | "Compilation of constructor '" . ToString($cs) . "' failed ", | ||
| 91 | $replacement, $msg); | ||
| 92 | my $stuff = slashify($replacement); | ||
| 93 | $result = sub { $_[0]->makeError('constructor_fail', $stuff); }; | ||
| 94 | return $result; } | ||
| 95 | return \&$name; } | ||
| 96 | |||
| 97 | sub translate_constructor { | ||
| 98 | my ($constructor, $float) = @_; | ||
| 99 | my $code = ''; | ||
| 100 | local $_ = $constructor; | ||
| 101 | while ($_) { | ||
| 102 | # ?test(ifclause)(elseclause) | ||
| 103 | if (/^$COND_RE/so) { | ||
| 104 | my ($bool, $if, $else) = parse_conditional(); | ||
| 105 | $code .= "if($bool){\n" . translate_constructor($if) . "}\n" | ||
| 106 | . ($else ? "else{\n" . translate_constructor($else) . "}\n" : ''); } | ||
| 107 | # Processing instruction: <?name a=v ...?> | ||
| 108 | elsif (s|^\s*<\?$QNAME_RE||so) { | ||
| 109 | my ($pi, $av) = ($1, translate_avpairs()); | ||
| 110 | $code .= "\$document->insertPI('$pi'" . ($av ? ", $av" : '') . ");\n"; | ||
| 111 | die "Missing '?>' at '$_'\n" unless s|^\s*\?>||; } | ||
| 112 | # Open tag: <name a=v ...> or .../> (for empty element) | ||
| 113 | elsif (s|^\s*<$QNAME_RE||so) { | ||
| 114 | my ($tag, $av) = ($1, translate_avpairs()); | ||
| 115 | if ($float) { | ||
| 116 | my $floattype = length($float); | ||
| 117 | if ($floattype == 1) { | ||
| 118 | $code .= "\$savenode=\$document->floatToElement('$tag');\n"; } | ||
| 119 | elsif ($floattype == 2) { | ||
| 120 | $code .= "\$savenode=\$document->floatToElement('$tag',1);\n"; } | ||
| 121 | $float = undef; } | ||
| 122 | $code .= "\$document->openElement('$tag'" . ($av ? ", $av" : '') . ");\n"; | ||
| 123 | $code .= "\$document->closeElement('$tag');\n" if s|^/||; # Empty element. | ||
| 124 | die "Missing '>' at '$_'\n" unless s|^>||; } | ||
| 125 | # Close tag: </name> | ||
| 126 | elsif (s|^</$QNAME_RE\s*>||so) { | ||
| 127 | $code .= "\$document->closeElement('$1');\n"; } | ||
| 128 | # Substitutable value: argument, property... | ||
| 129 | elsif (/^$VALUE_RE/o) { | ||
| 130 | $code .= "\$document->absorb(" . translate_value() . ",\%prop);\n"; } | ||
| 131 | # Attribute: a='v'; assigns in current node? [May conflict with random text!?!] | ||
| 132 | # FISHY!!! | ||
| 133 | elsif (s|^$QNAME_RE\s*=\s*||so) { | ||
| 134 | my $key = $1; | ||
| 135 | my $value = translate_string(); | ||
| 136 | if (defined $value) { | ||
| 137 | if ($float) { | ||
| 138 | $code .= "\$savenode=\$document->floatToAttribute('$key');\n"; | ||
| 139 | $float = undef; } | ||
| 140 | $code .= "\$document->setAttribute(\$document->getElement,'$key'," . $value . ");\n"; } | ||
| 141 | else { # attr value didn't match value pattern? treat whole thing as random text! | ||
| 142 | $code .= "\$document->absorb('" . slashify($key) . "=',\%prop);\n"; } } | ||
| 143 | # Else random text | ||
| 144 | elsif (s/^((?:$QUOTED_SPECIALS|[^\Q$SPECIALS<\E])+)//so) { | ||
| 145 | $code .= "\$document->absorb('" . slashify(unquote($1)) . "',\%prop);\n"; } | ||
| 146 | else { | ||
| 147 | die "Unrecognized at '$_'\n"; } } | ||
| 148 | return $code; } | ||
| 149 | |||
| 150 | sub slashify { | ||
| 151 | my ($string) = @_; | ||
| 152 | $string =~ s/\\/\\\\/g; | ||
| 153 | $string =~ s/'/\\'/g; # slashify ', since we're wrapping in '' | ||
| 154 | return $string; } | ||
| 155 | |||
| 156 | # parse a conditional in a constructor | ||
| 157 | # Conditionals are of the form ?value(...)(...), or ?value(...), | ||
| 158 | # (the else clause may be omitted) | ||
| 159 | # Return the translated (condition, if clause, else clause) | ||
| 160 | # where the if and else clauses are the strings encountered; NOT yet translated! | ||
| 161 | # Note: Signals an error if the pattern can't be matched. | ||
| 162 | use Text::Balanced; | ||
| 163 | |||
| 164 | sub parse_conditional { | ||
| 165 | s/^\?//; # Remove leading "?" | ||
| 166 | my $bool = 'ToString(' . translate_value("(") . ')'; | ||
| 167 | if (my $if = Text::Balanced::extract_bracketed($_, '()')) { | ||
| 168 | $if =~ s/^\(//; $if =~ s/\)$//; | ||
| 169 | my $else = Text::Balanced::extract_bracketed($_, '()'); | ||
| 170 | $else =~ s/^\(// if $else; $else =~ s/\)$// if $else; | ||
| 171 | return ($bool, $if, $else); } | ||
| 172 | else { | ||
| 173 | die "Missing if clause at '$_'\n"; } } | ||
| 174 | |||
| 175 | # Parse a substitutable value from the constructor (in $_) | ||
| 176 | # Recognizes the #1, #prop, and also &function(args,...) | ||
| 177 | # Note: signals an error if no recognizable value was found! | ||
| 178 | sub translate_value { | ||
| 179 | my ($exclude) = @_; | ||
| 180 | $exclude = '' unless defined $exclude; | ||
| 181 | my $value; | ||
| 182 | if (s/^\&([\w\:]*)\(//) { # Recognize a function call, w/args | ||
| 183 | my $fcn = $1; | ||
| 184 | my @args = (); | ||
| 185 | while (!/^\s*\)/) { | ||
| 186 | my $arg = (/^\s*[\'\"]/ ? translate_string() : translate_value(",)")); | ||
| 187 | push(@args, $arg); | ||
| 188 | last unless s/^\s*\,\s*//; } | ||
| 189 | die "Missing ')' in &$fcn(...) at '$_'\n" unless s/\)//; | ||
| 190 | $value = "$fcn(" . join(',', @args) . ")"; } | ||
| 191 | elsif (s/^\#(\d+)//) { # Recognize an explicit #1 for whatsit args | ||
| 192 | my $n = $1; | ||
| 193 | if (($n < 1) || ($n > $LaTeXML::Core::Definition::Constructor::NARGS)) { | ||
| 194 | die "Illegal argument number $n at '$_'\n"; } | ||
| 195 | else { | ||
| 196 | $value = "\$arg$n" } } | ||
| 197 | elsif (s/^\#([\w\-_]+)//) { $value = "\$prop{'$1'}"; } # Recognize #prop for whatsit properties | ||
| 198 | elsif (s/^((?:$QUOTED_SPECIALS|[^\Q$SPECIALS$exclude\E])+)//s) { | ||
| 199 | $value = "'" . slashify(unquote($1)) . "'"; } | ||
| 200 | else { die "Missing value at '$_'\n"; } | ||
| 201 | return $value; } | ||
| 202 | |||
| 203 | # Parse a delimited string from the constructor (in $_), | ||
| 204 | # for example, an attribute value. Can contain substitutions (above), as if interpolated. | ||
| 205 | # The result is a string, or undef if no quotes are found. | ||
| 206 | # NOTE: UNLESS there is ONLY one substituted value, then return the value object. | ||
| 207 | # This is (hopefully) temporary to handle font objects as attributes. | ||
| 208 | # The DOM holds the font objects, rather than strings, | ||
| 209 | # to resolve relative fonts on output. | ||
| 210 | sub translate_string { | ||
| 211 | my @values = (); | ||
| 212 | if (s/^\s*([\'\"])//) { | ||
| 213 | my $quote = $1; | ||
| 214 | while ($_ && !s/^$quote//) { | ||
| 215 | if (/^$COND_RE/o) { # inline conditional; branches should be values | ||
| 216 | my ($bool, $if, $else) = parse_conditional(); | ||
| 217 | my $code = "($bool ?"; | ||
| 218 | { local $_ = $if; $code .= translate_value(); } | ||
| 219 | $code .= ":"; | ||
| 220 | if ($else) { local $_ = $else; $code .= translate_value(); } | ||
| 221 | else { $code .= "''"; } | ||
| 222 | $code .= ")"; | ||
| 223 | push(@values, $code); } | ||
| 224 | elsif (/^$VALUE_RE/o) { | ||
| 225 | push(@values, translate_value($quote)); } | ||
| 226 | elsif (s/^((?:$QUOTED_SPECIALS|[^\Q$SPECIALS$quote\E])+)//s) { | ||
| 227 | push(@values, "'" . slashify(unquote($1)) . "'"); } | ||
| 228 | else { | ||
| 229 | die "Unrecognized at '$_'\n"; } } } | ||
| 230 | if (!@values) { return; } | ||
| 231 | elsif (@values == 1) { return $values[0]; } | ||
| 232 | else { return join('.', (map { (/^\'/ ? $_ : " ToString($_)") } @values)); } } | ||
| 233 | |||
| 234 | # Parse a set of attribute value pairs from a constructor pattern, | ||
| 235 | # substituting argument and property values from the whatsit. | ||
| 236 | # Special cases: | ||
| 237 | # hashes %[value] the value is expected to return a hash! | ||
| 238 | # Conditions are allowed; the branches should also be pairs or hashes | ||
| 239 | # It is acceptable to match NO pairs, returning an empty bit of code. | ||
| 240 | sub translate_avpairs { | ||
| 241 | my @avs = (); | ||
| 242 | s|^\s*||; | ||
| 243 | while ($_) { | ||
| 244 | if (/^$COND_RE/o) { # inline conditional; branches can be pairs or hashes | ||
| 245 | my ($bool, $if, $else) = parse_conditional(); | ||
| 246 | my $code = "($bool ? ("; | ||
| 247 | { local $_ = $if; $code .= translate_avpairs(); } | ||
| 248 | $code .= ") : ("; | ||
| 249 | { local $_ = $else; $code .= translate_avpairs() if $else; } | ||
| 250 | $code .= "))"; | ||
| 251 | push(@avs, $code); } | ||
| 252 | elsif (/^%$VALUE_RE/) { # Hash? Assume the value can be turned into a hash! | ||
| 253 | s/^%//; # Eat the "%" | ||
| 254 | push(@avs, '%{' . translate_value() . '}'); } | ||
| 255 | elsif (s|^$QNAME_RE\s*=\s*||o) { | ||
| 256 | my ($key, $value) = ($1, translate_string()); | ||
| 257 | # Is it ok to assign undef to the attribute if no string? | ||
| 258 | push(@avs, "'$key'=>$value"); } # if defined $value; } | ||
| 259 | else { | ||
| 260 | last; } | ||
| 261 | s|^\s*||; } | ||
| 262 | return join(', ', @avs); } | ||
| 263 | |||
| 264 | #=============================================================================== | ||
| 265 | 1; |