Debian Codegraph @

extensions/Math/src/WikiTexVC/parser.pegjs

raw
1 /** PEGjs lexer/parser */
2 {
3 $this->tu = TexUtil::getInstance();
4
5 # get reference of the options for usage in functions.
6
7
8 # get reference of the options for usage in functions.
9 $this->options = ParserUtil::createOptions($options);
10
11 }
12 // first rule is the start production.
13 start
14 = _ t:tex_expr
15 {
16 assert($t instanceof TexArray);
17 return $t;
18 }
19
20 // the PEG grammar doesn't automatically ignore whitespace when tokenizing.
21 // so we add `_` productions in appropriate places to eat whitespace.
22 // Lexer rules (which are capitalized) are expected to always eat
23 // *trailing* whitespace. Leading whitespace is taken care of in the `start`
24 // rule above.
25 _
26 = [ \t\n\r]*
27
28 /////////////////////////////////////////////////////////////
29 // PARSER
30 //----------------------------------------------------------
31
32 tex_expr
33 = e:expr EOF
34 { return $e; }
35 / e1:ne_expr name:FUN_INFIX e2:ne_expr EOF
36 { return new TexArray( new Infix($name, $e1, $e2)); }
37
38 expr
39 = ne_expr
40 / ""
41 { return new TexArray(); }
42
43 ne_expr
44 = h:lit_aq t:expr
45 { return $t->unshift($h); }
46 / h:litsq_aq t:expr
47 { return $t->unshift($h); }
48 / d:DECLh e:expr
49 { return new TexArray(new Declh($d->getFname(), $e)); }
50 litsq_aq
51 = litsq_fq
52 / litsq_dq
53 / litsq_uq
54 / litsq_zq
55 litsq_fq
56 = l1:litsq_dq SUP l2:lit
57 { return new FQ($l1->getBase(), $l1->getDown(), $l2); }
58 / l1:litsq_uq SUB l2:lit
59 { return new FQ($l1->getBase(), $l2, $l1->getUp()); }
60 litsq_uq
61 = base:litsq_zq SUP upi:lit
62 { return new UQ($base, $upi); }
63 litsq_dq
64 = base:litsq_zq SUB downi:lit
65 { return new DQ($base, $downi); }
66 litsq_zq
67 = SQ_CLOSE
68 { return new Literal( "]"); }
69 expr_nosqc
70 = l:lit_aq e:expr_nosqc
71 { return $e ->unshift( $l ); }
72 / "" /* */
73 { return new TexArray(); }
74 lit_aq
75 = lit_fq
76 / lit_dq
77 / lit_uq
78 / lit_dqn
79 / lit_uqn
80 / lit
81
82 lit_fq
83 = l1:lit_dq SUP l2:lit
84 { return new FQ($l1->getBase(), $l1->getDown(), $l2); }
85 / l1:lit_uq SUB l2:lit
86 { return new FQ ($l1->getBase(), $l2, $l1->getUp()); }
87 / l1:lit_dqn SUP l2:lit
88 { return new FQ(new TexArray(), $l1->getDown(), $l2); }
89
90 lit_uq
91 = base:lit SUP upi:lit
92 { return new UQ($base, $upi); }
93 lit_dq
94 = base:lit SUB downi:lit
95 { return new DQ($base, $downi); }
96 lit_uqn
97 = SUP l:lit
98 { return new UQ(new TexArray(),$l); }
99 lit_dqn
100 = SUB l:lit
101 { return new DQ(new TexArray(),$l); }
102
103
104 left
105 = LEFTI d:DELIMITER
106 { return $d; }
107 / LEFTI SQ_CLOSE
108 { return "]"; }
109 right
110 = RIGHTI d:DELIMITER
111 { return $d; }
112 / RIGHTI SQ_CLOSE
113 { return "]"; }
114 lit
115 = r:LITERAL { return new Literal($r); }
116 / f:generic_func &{ return $this->tu->latex_function_names($f); } _
117 c:( "(" / "[" / "\\{" / "" { return " ";}) _
118 { return new TexArray( new Literal( $f ) , new Literal( $c ) ) ; }
119 // quasi-literal; this is from Texutil.find(...) but the result is not
120 // guaranteed to be Tex.LITERAL(...)
121 / f:generic_func &{ return $this->tu->nullary_macro_aliase($f); } _ // from Texutil.find(...)
122 {
123 $parser = new Parser();
124 $ast = $parser->parse($this->tu->nullary_macro_aliase($f), $this->options);
125 assert($ast instanceof TexArray && $ast->getLength() === 1);
126 return $ast->first();
127 }
128 / f:generic_func &{ return $this->tu->deprecated_nullary_macro_aliase($f); } _ // from Texutil.find(...)
129 {
130 $parser = new Parser();
131 $ast = $parser->parse($this->tu->deprecated_nullary_macro_aliase($f), $this->options);
132 assert($ast instanceof TexArray && $ast->getLength() === 1);
133 if ($this->options['oldtexvc']){
134 return $ast->first();
135 } else {
136 throw new SyntaxError("Deprecation: Alias no longer supported.", [], $this->text(), $this->offset(),
137 $this->line(), $this->column());
138 }
139 }
140 / f:generic_func &{ return $this->tu->mediawiki_function_names($f); } _
141 {
142 if(is_array($f)) {
143 // This is an unexpected case, but covers the ambiguity of slice in javascript.
144 $fProcessed = implode(array_slice($f, 1));
145 } else {
146 $fProcessed = substr($f,1);
147 }
148 return new Fun1nb( '\\operatorname', new Literal( $fProcessed ) );
149 }
150 / r:DELIMITER { return new Literal($r); }
151 / b:BIG r:DELIMITER { return new Big($b, $r); }
152 / b:BIG SQ_CLOSE { return new Big($b, "]"); }
153 / l:left e:expr r:right {return new Lr($l, $r, $e); }
154 / name:FUN_AR1opt e:expr_nosqc SQ_CLOSE l:lit /* must be before FUN_AR1 */
155 { return new Fun2sq($name, $e->setCurly(), $l); }
156 / name:FUN_AR1 l:lit { return new Fun1($name, $l); }
157 / name:FUN_AR1nb l:lit { return new Fun1nb($name, $l); }
158 / name:FUN_MHCHEM l:chem_lit { return new Mhchem($name, $l); }
159 / name:FUN_AR2 l1:lit l2:lit { return new Fun2($name, $l1, $l2); }
160 / name:FUN_AR4_MHCHEM_TEXIFIED l1:lit l2:lit l3:lit l4:lit { return new Fun4($name, $l1, $l2, $l3, $l4); }
161 / name:FUN_AR2nb l1:lit l2:lit { return new Fun2nb($name, $l1, $l2); }
162 / BOX
163 / CURLY_OPEN e:expr CURLY_CLOSE
164 { return $e->setCurly(); }
165 / CURLY_OPEN e1:ne_expr name:FUN_INFIX e2:ne_expr CURLY_CLOSE
166 { return new Infix($name, $e1, $e2); }
167 / BEGIN_MATRIX m:(array/matrix) END_MATRIX
168 { return $m->setTop( 'matrix' ); }
169 / BEGIN_PMATRIX m:(array/matrix) END_PMATRIX
170 { return $m->setTop( 'pmatrix' ); }
171 / BEGIN_BMATRIX m:(array/matrix) END_BMATRIX
172 { return $m->setTop( 'bmatrix' ); }
173 / BEGIN_BBMATRIX m:(array/matrix) END_BBMATRIX
174 { return $m->setTop( 'Bmatrix' ); }
175 / BEGIN_VMATRIX m:(array/matrix) END_VMATRIX
176 { return $m->setTop( 'vmatrix' ); }
177 / BEGIN_VVMATRIX m:(array/matrix) END_VVMATRIX
178 { return $m->setTop( 'Vmatrix' ); }
179 / BEGIN_ARRAY opt_pos m:array END_ARRAY
180 { return $m->setTop( 'array' ); }
181 / BEGIN_ALIGN opt_pos m:matrix END_ALIGN
182 { return $m->setTop( 'aligned' ); }
183 / BEGIN_ALIGNED opt_pos m:matrix END_ALIGNED // parse what we emit
184 { return $m->setTop( 'aligned' ); }
185 / BEGIN_ALIGNAT m:alignat END_ALIGNAT
186 { return $m->setTop( 'alignedat' ); }
187 / BEGIN_ALIGNEDAT m:alignat END_ALIGNEDAT // parse what we emit
188 { return $m->setTop( 'alignedat' ); }
189 / BEGIN_SMALLMATRIX m:(array/matrix) END_SMALLMATRIX
190 { return $m->setTop( 'smallmatrix' ); }
191 / BEGIN_CASES m:matrix END_CASES
192 { return $m->setTop( 'cases' ); }
193 / "\\begin{" alpha+ "}" /* better error messages for unknown environments */
194 { throw new SyntaxError("Illegal TeX function", [], $this->text(), $this->offset(),
195 $this->line(), $this->column()); }
196 / f:generic_func &{ return !$this->tu->getAllFunctionsAt($f); }
197 { throw new SyntaxError("Illegal TeX function", [], $f, $this->offset(), $this->line(), $this->column()); }
198
199
200 // "array" requires mandatory column specification
201 array
202 = cs:column_spec m:matrix
203 { return $m->setColumnSpecs( $cs ); }
204
205 // "alignat" requires mandatory # of columns
206 alignat
207 = as:alignat_spec m:matrix
208 { return $m->setColumnSpecs( $as ); }
209
210 // "matrix" does not require column specification
211 matrix
212 = l:line_start tail:( r:NEXT_ROW m:matrix { return [$m,$r]; } )?
213 { if ($tail === null) { return new Matrix( 'matrix', new TexArray( $l ) ); }
214 return new Matrix( 'matrix', $tail[0]->unshift($l), $tail[1] ); }
215 line_start
216 = f:HLINE l:line_start
217 {
218 if ($l->first() === null ) {
219 $l->push(new TexArray());
220 }
221 $l->first()->unshift(new Literal($f . " ")); return $l;}
222 / line
223 line
224 = e:expr tail:( NEXT_CELL l:line { return $l; } )?
225 {
226 if ($tail === null) { return new TexArray( $e ) ; }
227 return $tail->unshift($e);
228 }
229
230 column_spec
231 = CURLY_OPEN cs:(one_col+ { return $this->text(); }) CURLY_CLOSE
232 { return TexArray::newCurly(new Literal($cs)); }
233
234 one_col
235 = [lrc] _
236 / "p" CURLY_OPEN boxchars+ CURLY_CLOSE
237 / "*" CURLY_OPEN [0-9]+ _ CURLY_CLOSE
238 ( one_col
239 / CURLY_OPEN one_col+ CURLY_CLOSE
240 )
241 / "||" _
242 / "|" _
243 / "@" _ CURLY_OPEN boxchars+ CURLY_CLOSE
244
245 alignat_spec
246 = CURLY_OPEN num:([0-9]+ { return $this->text(); }) _ CURLY_CLOSE
247 { return TexArray::newCurly(new Literal($num)); }
248
249 opt_pos
250 = "[" _ [tcb] _ "]" _
251 / "" /* empty */
252
253 /////////////////////////////////////////////////////////////
254 // MHCHEM grammar rules
255 //----------------------------------------------------------
256
257
258 chem_lit
259 = CURLY_OPEN e:chem_sentence CURLY_CLOSE { return $e->setCurly(); }
260
261 chem_sentence =
262 _ p:chem_phrase " " s:chem_sentence { return new TexArray($p,new TexArray(new Literal(" "),$s)); } /
263 _ p:chem_phrase _ { return new TexArray($p,new TexArray()); }
264
265 chem_phrase =
266 m:"(^)" { return new Literal($m); } /
267 m:chem_word n:CHEM_SINGLE_MACRO { return new ChemWord($m, new Literal($n)); }/
268 m:chem_word { return $m; } /
269 m:CHEM_SINGLE_MACRO { return new Literal($m); } /
270 m:"^" { return new Literal($m); }
271
272 chem_word =
273 m:chem_char n:chem_word_nt { return new ChemWord($m, $n); } /
274 m:CHEM_SINGLE_MACRO n:chem_char_nl o:chem_word_nt { return new ChemWord(new ChemWord(new Literal($m), $n), $o); }
275
276 chem_word_nt = m:chem_word { return $m; } /
277 "" { return new Literal(""); }
278
279 chem_char =
280 m:chem_char_nl { return $m;} /
281 c:CHEM_LETTER { return new Literal($c); }
282
283 chem_char_nl =
284 m:chem_script { return $m;} /
285 CURLY_OPEN c:chem_text CURLY_CLOSE { return TexArray::newCurly($c); } /
286 BEGIN_MATH c:expr END_MATH { return new Dollar($c); }/
287 name:CHEM_BONDI l:chem_bond { return new Fun1($name, $l); } /
288 m:chem_macro { return $m; } /
289 c:CHEM_NONLETTER { return new Literal($c); }
290
291 chem_bond
292 = CURLY_OPEN e:CHEM_BOND_TYPE CURLY_CLOSE { return TexArray::newCurly(new Literal($e)); }
293
294 chem_script =
295 a:CHEM_SUPERSUB b:CHEM_SCRIPT_FOLLOW { return new ChemWord(new Literal($a), new Literal($b)); } /
296 a:CHEM_SUPERSUB b:chem_lit { return new ChemWord(new Literal($a), $b); } /
297 a:CHEM_SUPERSUB BEGIN_MATH b:expr END_MATH { return new ChemWord(new Literal($a), new Dollar($b)); }
298
299 // TODO \color is a not documented feature of mhchem for MathJax, at the moment named colors are accepted
300 chem_macro =
301 name:CHEM_MACRO_2PU l1:chem_lit "_" l2:chem_lit { return new ChemFun2u($name, $l1, $l2); }/ //return new Fun1nb($name, $l);
302 name:CHEM_MACRO_2PC l1:CHEM_COLOR l2:chem_lit { return new Fun2($name, $l1, $l2); } /
303 name:CHEM_MACRO_2P l1:chem_lit l2:chem_lit { return new Fun2($name, $l1, $l2); } /
304 name:CHEM_MACRO_1P l:chem_lit { return new Fun1($name, $l); }
305
306 chem_text = cs:boxchars+ { return new Literal(join('',$cs)); }
307 CHEM_COLOR = "{" _ name:alpha+ _ "}" _ { return new Literal(join('',$name)); }
308
309 /////////////////////////////////////////////////////////////
310 // LEXER
311 //----------------------------------------------------------
312 //space = [ \t\n\r]
313 alpha = [a-zA-Z]
314 literal_id = [a-zA-Z]
315 literal_mn = [0-9]
316 literal_uf_lt = [,:;?!\']
317 delimiter_uf_lt = [().]
318 literal_uf_op = [-+*=]
319 delimiter_uf_op = [\/|]
320 boxchars // match only valid UTF-16 sequences
321 = [-0-9a-zA-Z+*,=():\/;?.!'` \[\]\[\u0080-\ud7ff\]\[\ue000-\uffff\]]
322
323 BOX
324 = b:generic_func &{ return $this->tu->box_functions($b); } _ "{" cs:boxchars+ "}" _
325 { return new Box($b, join('', $cs)); }
326
327 LITERAL
328 = c:( literal_id / literal_mn / literal_uf_lt / "-" / literal_uf_op ) _
329 { return $c; }
330 / f:generic_func &{ return $this->tu->nullary_macro($f); } _ // from Texutil.find(...)
331 { return $f . " "; }
332 / f:generic_func &{ return $this->options['usemathrm'] && $this->tu->nullary_macro_in_mbox($f); } _ // from Texutil.find(...)
333 { return "\\mathrm {" . $f . "} "; }
334 / mathrm:generic_func &{ return $this->options['usemathrm'] && $mathrm === "\\mathrm"; } _
335 "{" f:generic_func &{ return $this->options['usemathrm'] && $this->tu->nullary_macro_in_mbox($f); } _ "}" _
336 /* make sure we can parse what we emit */
337 { return $this->options['usemathrm'] ? "\\mathrm {" . $f . "} " : false;}
338 / f:generic_func &{ return $this->tu->nullary_macro_in_mbox($f); } _ // from Texutil.find(...)
339 { return "\\mbox{" . $f . "} "; }
340 / mbox:generic_func &{ return $mbox === "\\mbox"; } _
341 "{" f:generic_func &{ return $this->tu->nullary_macro_in_mbox($f); } _ "}" _
342 /* make sure we can parse what we emit */
343 { return "\\mbox{" . $f . "} "; }
344 / f:(COLOR / DEFINECOLOR)
345 { return $f; }
346 / "\\" c:[, ;!_#%$&] _
347 { return "\\" . $c; }
348 / c:[><~] _
349 { return $c; }
350 / c:[%$] _
351 { if($this->options['oldtexvc']) {
352 return "\\" . $c; /* escape dangerous chars */
353 } else {
354 throw new SyntaxError("Deprecation: % and $ need to be escaped.", [], $this->text(), $this->offset(),
355 $this->line(), $this->column());
356 }}
357
358 DELIMITER
359 = c:( delimiter_uf_lt / delimiter_uf_op / "[" ) _
360 { return $c; }
361 / "\\" c:[{}|] _
362 { return "\\" . $c; }
363 / f:generic_func &{ return $this->tu->other_delimiters1($f); } _ // from Texutil.find()
364 { return $f . " "; }
365 / f:generic_func &{ return $this->tu->other_delimiters2($f); } _ // from Texutil.find()
366 {
367 $parser = new Parser();
368 $p = $parser->parse($this->tu->other_delimiters2($f), $this->options);
369 # assert.ok(p instanceof TexArray && p.length === 1);
370 assert($p instanceof TexArray && count($p->getArgs()) === 1);
371
372 # assert.ok(p.first() instanceof Literal);
373 assert($p->first() instanceof Literal);
374
375 return $p->first()->getArg();
376 }
377
378 FUN_AR1nb
379 = f:generic_func &{ return $this->tu->fun_ar1nb($f); } _ { return $f; }
380
381 FUN_AR1opt
382 = f:generic_func &{ return $this->tu->fun_ar1opt($f); } _ "[" _ { return $f; }
383
384 NEXT_CELL
385 = "&" _
386
387 LATEX_LENGTH
388 = s:LATEX_SIGN? n:LATEX_NUMBER u:LATEX_UNIT { return new LengthSpec($s, $n, $u); }
389
390 LATEX_SIGN
391 = [+-]
392
393 LATEX_NUMBER
394 = literal_mn+ "."? literal_mn*
395 / "." literal_mn+
396
397 // from http://latexref.xyz/Units-of-length.html
398 LATEX_UNIT
399 = "pt"
400 / "pc"
401 / "in"
402 / "bp"
403 / "cm"
404 / "mm"
405 / "dd"
406 / "cc"
407 / "sp"
408 / "em"
409 / "ex"
410 / "mu"
411
412 / "nd"
413 / "nc"
414
415 NEXT_ROW
416 = ("\\\\" s:("[" l:LATEX_LENGTH "]" { return $l; })? _ {return $s; })
417
418 BEGIN
419 = "\\begin" _
420 END
421 = "\\end" _
422
423 BEGIN_MATRIX
424 = BEGIN "{matrix}" _
425 END_MATRIX
426 = END "{matrix}" _
427 BEGIN_PMATRIX
428 = BEGIN "{pmatrix}" _
429 END_PMATRIX
430 = END "{pmatrix}" _
431 BEGIN_BMATRIX
432 = BEGIN "{bmatrix}" _
433 END_BMATRIX
434 = END "{bmatrix}" _
435 BEGIN_BBMATRIX
436 = BEGIN "{Bmatrix}" _
437 END_BBMATRIX
438 = END "{Bmatrix}" _
439 BEGIN_VMATRIX
440 = BEGIN "{vmatrix}" _
441 END_VMATRIX
442 = END "{vmatrix}" _
443 BEGIN_VVMATRIX
444 = BEGIN "{Vmatrix}" _
445 END_VVMATRIX
446 = END "{Vmatrix}" _
447 BEGIN_ARRAY
448 = BEGIN "{array}" _
449 END_ARRAY
450 = END "{array}" _
451 BEGIN_ALIGN
452 = BEGIN "{align}" _
453 END_ALIGN
454 = END "{align}" _
455 BEGIN_ALIGNED
456 = BEGIN "{aligned}" _
457 END_ALIGNED
458 = END "{aligned}" _
459 BEGIN_ALIGNAT
460 = BEGIN "{alignat}" _
461 END_ALIGNAT
462 = END "{alignat}" _
463 BEGIN_ALIGNEDAT
464 = BEGIN "{alignedat}" _
465 END_ALIGNEDAT
466 = END "{alignedat}" _
467 BEGIN_SMALLMATRIX
468 = BEGIN "{smallmatrix}" _
469 END_SMALLMATRIX
470 = END "{smallmatrix}" _
471 BEGIN_CASES
472 = BEGIN "{cases}" _
473 END_CASES
474 = END "{cases}" _
475
476 SQ_CLOSE
477 = "]" _
478 CURLY_OPEN
479 = "{" _
480 CURLY_CLOSE
481 = "}" _
482 SUP
483 = "^" _
484 SUB
485 = "_" _
486
487 // This is from Texutil.find in texvc
488 generic_func
489 = "\\" alpha+ { return $this->text(); }
490
491 BIG
492 = f:generic_func &{ return $this->tu->big_literals($f); } _
493 { return $f; }
494
495 FUN_AR1
496 = f:generic_func &{ return $this->tu->fun_ar1($f); } _
497 { return $f; }
498 / f:generic_func &{ return $this->options['oldmhchem'] && $this->tu->fun_mhchem($f);} _
499 { return $f; }
500 / f:generic_func &{ return $this->tu->other_fun_ar1($f); } _
501 { if ($this->options['oldtexvc']) {
502 return $this->tu->other_fun_ar1($f);
503 } else {
504 throw new SyntaxError("Deprecation: \\Bbb and \\bold are not allowed in math mode.", [],
505 $this->text(), $this->offset(), $this->line(),$this->column());
506 }
507 }
508
509 FUN_MHCHEM
510 = f:generic_func &{ return $this->tu->fun_mhchem($f); } _
511 { return $f; }
512
513 FUN_AR2
514 = f:generic_func &{ return $this->tu->fun_ar2($f); } _
515 { return $f; }
516
517 FUN_AR4_MHCHEM_TEXIFIED
518 = f:generic_func &{ return $this->tu->fun_ar4($f) && $this->tu->mhchemtexified_required($f); } _
519 { return $f; }
520
521 FUN_INFIX
522 = f:generic_func &{ return $this->tu->fun_infix($f); } _
523 { return $f; }
524
525 DECLh
526 = f:generic_func &{ return $this->tu->declh_function($f); } _
527 { return new Declh($f, new TexArray()); }
528
529 FUN_AR2nb
530 = f:generic_func &{ return $this->tu->fun_ar2nb($f); } _
531 { return $f; }
532
533 LEFTI
534 = f:generic_func &{ return $this->tu->left_function($f); } _
535
536 RIGHTI
537 = f:generic_func &{ return $this->tu->right_function($f); } _
538
539 HLINE
540 = f:generic_func &{ return $this->tu->hline_function($f); } _
541 { return $f; }
542
543 COLOR
544 = f:generic_func &{ return $this->tu->color_function($f); } _ cs:COLOR_SPEC
545 { return $f . " " . $cs; }
546
547 DEFINECOLOR
548 = f:generic_func &{ return $this->tu->definecolor_function($f); } _
549 "{" _ name:alpha+ _ "}" _ "{" _
550 a:( "named"i _ "}" _ cs:COLOR_SPEC_NAMED { return "{named}" . $cs; }
551 / "gray"i _ "}" _ cs:COLOR_SPEC_GRAY { return "{gray}" . $cs; }
552 / "rgb" _ "}" _ cs:COLOR_SPEC_rgb { return "{rgb}" . $cs; }
553 // Note that we actually convert RGB format to rgb format here.
554 / "RGB" _ "}" _ cs:COLOR_SPEC_RGBI { return "{rgb}" . $cs; }
555 / "cmyk"i _ "}" _ cs:COLOR_SPEC_CMYK { return "{cmyk}" . $cs; } )
556 { return $f . " {" . join('',$name) . "}" . $a; }
557
558 COLOR_SPEC
559 = COLOR_SPEC_NAMED
560 / "[" _ "named"i _ "]" _ cs:COLOR_SPEC_NAMED
561 { return "[named]" . $cs; }
562 / "[" _ "gray"i _ "]" _ cs:COLOR_SPEC_GRAY
563 { return "[gray]" . $cs; }
564 / "[" _ "rgb" _ "]" _ cs:COLOR_SPEC_rgb
565 { return "[rgb]" . $cs; }
566 / "[" _ "RGB" _ "]" _ cs:COLOR_SPEC_RGBI
567 // Note that we actually convert RGB format to rgb format here.
568 { return "[rgb]" . $cs; }
569 / "[" _ "cmyk"i _ "]" _ cs:COLOR_SPEC_CMYK
570 { return "[cmyk]" . $cs; }
571
572 COLOR_SPEC_NAMED
573 = "{" _ name:alpha+ _ "}" _
574 { return "{" . join('', $name) . "}"; }
575 COLOR_SPEC_GRAY
576 = "{" _ k:CNUM + "}"
577 { $s = is_array($k) ? $k[0] : $k;
578 return "{" . $s . "}";}
579 COLOR_SPEC_rgb
580 = "{" _ r:CNUM "," _ g:CNUM "," _ b:CNUM "}" _
581 { return "{" . $r . "," . $g . "," . $b . "}"; }
582 COLOR_SPEC_RGBI
583 = "{" _ r:CNUM255 "," _ g:CNUM255 "," _ b:CNUM255 "}" _
584 // Note that we normalize the values to [0,1] here.
585 { return "{" . $r . "," . $g . "," . $b . "}"; }
586 COLOR_SPEC_CMYK
587 = "{" _ c:CNUM "," _ m:CNUM "," _ y:CNUM "," _ k:CNUM "}" _
588 { return "{" . $c . "," . $m . "," . $y . "," . $k . "}"; }
589
590 // An integer in [0, 255] => normalize it to [0,1]
591 CNUM255
592 = n:$( "0" / ([1-9] ([0-9] [0-9]?)? ) ) &{ return intval($n, 10) <= 255; } _
593 { return $n / 255; }
594
595 // A floating-point number in [0, 1]
596 CNUM
597 = n:$( "0"? "." [0-9]+ ) _
598 { return $n; }
599 / n:$( [01] "."? ) _
600 { return $n; }
601
602
603 // MHCHEM LEXER RULES
604 CHEM_SINGLE_MACRO
605 = f:generic_func &{ return $this->tu->mhchem_single_macro($f); } { return $f; }
606 / "\\" c:[, ;!_#%$&] { return "\\" . $c; }
607
608 CHEM_BONDI = f:generic_func &{ return $this->tu->mhchem_bond($f); } _ { return $f; }
609
610 CHEM_MACRO_1P = f:generic_func &{ return $this->tu->mhchem_macro_1p($f); } _ { return $f; }
611
612 CHEM_MACRO_2P = f:generic_func &{ return $this->tu->mhchem_macro_2p($f); } _ { return $f; }
613
614 CHEM_MACRO_2PU = f:generic_func &{ return $this->tu->mhchem_macro_2pu($f); } _ { return $f; }
615
616 CHEM_MACRO_2PC = f:generic_func &{ return $this->tu->mhchem_macro_2pc($f); } _ { return $f; }
617
618 CHEM_SCRIPT_FOLLOW = literal_mn / literal_id / [+-.*']
619
620 CHEM_SUPERSUB = "_" / "^"
621
622 CHEM_BOND_TYPE = "=" / "#" / "~--" / "~-" / "~=" / "~" / "-~-" / "...." / "..." / "<-" / "->" / "-" / "1" / "2" / "3"
623
624
625 // As '$' cannot be used (dangerous char in math mode) to switch from chem mode to math mode
626 // \begin{math} and \end{math} are introduced to do so
627 BEGIN_MATH = BEGIN "{math}" _
628
629 END_MATH = END "{math}" _
630
631 CHEM_LETTER = [a-zA-Z]
632
633 CHEM_NONLETTER =
634 c: "\\{" { return $c; } /
635 c: "\\}" { return $c; } /
636 c: "\\\\" { return $c; } /
637 c:[+-=#().,;/*<>|@&\'\[\]] { return $c; } /
638 c:literal_mn { return $c; } /
639 CURLY_OPEN CURLY_CLOSE { return "{}"; }
640
641 // Missing lexer tokens!
642 FUN_INFIXh = impossible
643 FUN_AR1hl = impossible
644 FUN_AR1hf = impossible
645 FUN_AR2h = impossible
646 impossible = & { return false; }
647
648 // End of file
649 EOF = & { return $this->peg_currPos === $this->input_length; }