From d373243e9dd954b1cd1d4867a2b98658d6d77004 Mon Sep 17 00:00:00 2001 From: belerico Date: Sun, 19 Apr 2020 18:46:06 +0200 Subject: [PATCH] Add latex matrix parsing --- py_asciimath/grammar/latex_grammar.py | 2 ++ py_asciimath/transformer/transformer.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/py_asciimath/grammar/latex_grammar.py b/py_asciimath/grammar/latex_grammar.py index d3a8b24..637da14 100644 --- a/py_asciimath/grammar/latex_grammar.py +++ b/py_asciimath/grammar/latex_grammar.py @@ -22,6 +22,7 @@ | s "_" s "^" s -> exp_under_super s: _l start? _r -> exp_par | "\\left" (_l | /\./ | /\\vert/) start? "\\right" (_r | /\./ | /\\vert/) -> exp_par + | "\\begin{{matrix}}" row_mat (/\\\\/ row_mat?)* "\\end{{matrix}}" -> exp_mat | _u "{{" start "}}" -> exp_unary | _b "{{" start "}}" "{{" start "}}" -> exp_binary | _latex1 -> symbol @@ -30,6 +31,7 @@ !_c: /d[A-Za-z]/ | NUMBER | LETTER + !row_mat: start ("&" start?)* -> row_mat !_l: {} // left parenthesis !_r: {} // right parenthesis !_b: {} // binary functions diff --git a/py_asciimath/transformer/transformer.py b/py_asciimath/transformer/transformer.py index 42611cd..489180c 100644 --- a/py_asciimath/transformer/transformer.py +++ b/py_asciimath/transformer/transformer.py @@ -437,3 +437,21 @@ def const(self, items): @log def q_str(self, items): return items + + @log + def exp_mat(self, items): + s = "" + for i in items: + if i == "\\\\": + i = "," + s = s + i + return "[" + s + "]" + + @log + def row_mat(self, items): + s = "" + for i in items: + if i == "&": + i = "," + s = s + i + return "[" + s + "]"