| 1 |
/** PEGjs lexer/parser */ |
| 2 |
/** |
| 3 |
* |
| 4 |
* This implements the intent grammar from W3C (as of 15.08.23) from: |
| 5 |
* https://www.w3.org/TR/mathml4/#mixing_intent_grammar |
| 6 |
* |
| 7 |
* intent := concept-or-literal | number | reference | application | structure |
| 8 |
* concept-or-literal := NCName |
| 9 |
* number := '-'? digit+ ( '.' digit+ )? |
| 10 |
* reference := '$' NCName |
| 11 |
* structure := ':' ('common' | 'structure' | 'chemistry' | 'equations' ) |
| 12 |
* application := intent hint? '(' arguments? ')' |
| 13 |
* arguments := intent ( ',' intent )* |
| 14 |
* hint := '@' ( 'prefix' | 'infix' | 'postfix' | 'function' | 'silent' ) |
| 15 |
* |
| 16 |
* Added 'structure'-macros intents like in W3C Examples to the grammar |
| 17 |
* https://github.com/w3c/mathml-docs/blob/main/intent-examples/examples.html |
| 18 |
* |
| 19 |
* The applications condition is implemented a bit differently, repetitive arguments in parenthesis are |
| 20 |
* allowed i.e f(a)(b), this enables checking for applications condition without infinite loop |
| 21 |
* warning by PEG interpreter. |
| 22 |
* |
| 23 |
* Also added hints like "decimal-comma", "thousands-comma" for number |
| 24 |
* |
| 25 |
* author: Johannes Stegmüller |
| 26 |
**/ |
| 27 |
|
| 28 |
start = application / intent |
| 29 |
intent = concept_or_literal / number / reference / structure |
| 30 |
concept_or_literal = NCName { |
| 31 |
return $this->text(); |
| 32 |
} |
| 33 |
number = sign:("-"?) |
| 34 |
intPart:digits |
| 35 |
fracPart:("." digits)? |
| 36 |
hint:hint? |
| 37 |
{ |
| 38 |
$intPartS = implode($intPart ?? []); |
| 39 |
$fracPartS = implode($fracPart[1] ?? []); |
| 40 |
$value = $sign ?? ""; |
| 41 |
$value .= $intPartS; |
| 42 |
$value .= isset($fracPart) ? ("." . $fracPartS ) : ""; |
| 43 |
return ["type" => "number", "value" => $value, "hint"=>$hint]; |
| 44 |
} |
| 45 |
digits = digit+ |
| 46 |
digit = [0-9] |
| 47 |
reference = "$" NCName { |
| 48 |
return substr($this->text(),1); |
| 49 |
} |
| 50 |
structure = ":" ("common" / "structure" / "chemistry" /"matrix" / "equations" / "chemical-element" / "chemical-formula" / "chemical-equation") |
| 51 |
{ |
| 52 |
return substr($this->text(),1); |
| 53 |
} |
| 54 |
application = intent:intent hint:hint? args:("(" WS? arguments:arguments? WS? ")")+ |
| 55 |
{ |
| 56 |
$argsF = []; |
| 57 |
foreach ($args as $arg) { |
| 58 |
$argsF[] = $arg[2]; |
| 59 |
} |
| 60 |
$returnObj = [ |
| 61 |
"type" => "application", |
| 62 |
"intent" => $intent, |
| 63 |
"hint" => isset($hint) ? $hint : null, |
| 64 |
"arguments" => $argsF |
| 65 |
]; |
| 66 |
return $returnObj; |
| 67 |
} |
| 68 |
arguments = WS? first:start WS? rest:("," WS? start WS?)* |
| 69 |
{ |
| 70 |
$args = [$first]; |
| 71 |
foreach ($rest as $arg) { |
| 72 |
$args[] = $arg[2]; |
| 73 |
} |
| 74 |
return $args; |
| 75 |
} |
| 76 |
hint = ("@"/":") hintType:("prefix" / "infix" / "postfix" / "function" / "silent" / "decimal-comma" /"thousands-comma") |
| 77 |
{ |
| 78 |
return $hintType; |
| 79 |
} |
| 80 |
NCName = !":" [a-zA-Z_] [a-zA-Z0-9._-]* { return $this->text(); } |
| 81 |
WS = " " {return " "; } |