3 #include "TPushParser.hh"
4 #include "AMathMLFactory.hh"
6 TPushParser::TPushParser(ALogger& l, const TDictionary& d) : APushParser(l), dictionary(d)
11 TPushParser::TPushParser(ALogger& l, AMathMLFactory& f, const TDictionary& d) : APushParser(l, f), dictionary(d)
16 TPushParser::~TPushParser()
23 cursor = doc.create("cursor");
24 cursor["visible"] = "1";
33 if (cursor.parent()) cursor.remove();
35 doc.root().append(cursor);
36 if (factory) factory->documentModified(doc);
40 TPushParser::PRIME() const
42 const TDictionary::Entry entry = dictionary.find("prime");
43 if (entry.cls == TDictionary::OPERATOR) return entry.value;
48 TPushParser::do_begin()
50 TNode parent = cursor.parent();
51 if (parent.isC() && dictionary.find(parent.nameC()).table)
53 TNode row = doc.create("row");
54 TNode cell = doc.create("cell");
55 TNode g = doc.createG();
63 TNode g = doc.createG(nextId++);
71 TPushParser::correctBrace()
73 // this method MUST be invoked when the cursor is child of a
74 // phantom group, which in turn is the last rightOpen MACRO's child.
75 // The only way to exit from a rightOpen MACRO is opening a group before
76 // inserting the MACRO and, once the MACRO is completely inserted, closing
78 // This method return true if the condition above is true. False, otherwise.
79 assert(cursor.parent() && cursor.parent().isG() && !cursor.parent().hasId());
80 TNode parent = cursor.parent();
81 assert(parent.parent() && parent.parent().isC());
82 assert(!frames.empty());
83 Frame& frame = frames.top();
84 assert(frame.entry.rightOpen);
85 assert(parent.parent().last() == parent);
87 TNode c = parent.parent();
90 TNode node = c.parent();
93 if (node.isG() && node.hasId())
95 // in this case, the rightOpen MACRO is a child of a group with id.
96 // So, the '}' is correct
102 // the MACRO is a phantom group's child. We have to control why we
103 // have this phantom group
104 TNode nodeParent = node.parent();
105 if (nodeParent && nodeParent.isC())
107 // we have to control the nature of this MACRO
108 const TDictionary::Entry& entry = dictionary.find(nodeParent.nameC());
109 if (entry.rightOpen && node == nodeParent.last())
111 // in this case we have to re-iterate the process
112 node = nodeParent.parent();
120 // at the moment we assume that a MACRO cannot be child of an element other than a group
130 TPushParser::do_end()
132 TNode parent = cursor.parent();
133 if (parent && parent.isG() && parent.hasId())
135 // normal closing brace for an explicitly open group
140 else if (parent && parent.isG() && parent.parent() && parent.parent().is("cell"))
142 assert(!frames.empty());
143 // closing brace for a structure in which & or \cr have been used
144 TNode row = parent.parent().parent();
145 assert(row && row.is("row"));
146 assert(row.parent());
150 else if (parent && parent.isG() && !parent.hasId() && parent.parent() && !parent.parent().is("math"))
152 // In this case, we have to control the cursor's grand parent.
153 TNode gparent = parent.parent();
155 if (gparent.isC() && gparent.last() == parent)
157 // a frame MUST be in the stack
158 assert(!frames.empty());
160 // we have to control the nature of this macro
161 if (frames.top().entry.rightOpen)
163 // in this case, the '}' character is the proper way to exit from the phantom group, and
164 // in particular, this character means that the user wants to exit from the MACRO.
165 // A rightOpen MACRO MUST be descendant of a group with Id. This '}' is the closing brace of this
166 // group. So, we have to control if this group exists. This groyp could exist, but this MACRO could
167 // be another MACRO's child, so we have to control this last MACRO recursively. This recurive control
168 // is done by the correctBrace method.
171 // the '}' is not correct
172 logger.warning("nothing to close");
184 logger.warning("closing brace ignored");
190 // at the moment, a phantom group with the cursor inside can be a MACRO's child or a cell's child, and these cases
191 // are handled in other blocks of code.
192 logger.error("do_end: strange TML tree");
198 // In this case, there is a redundant '}', so we can ignore it and
200 logger.warning("There is so no corresponding'{'");
207 TPushParser::do_shift()
209 TNode parent = cursor.parent();
211 if (parent.is("tex"))
213 TNode math = doc.create("math", nextId++);
214 TNode g = doc.createG();
215 cursor.replace(math);
220 else if (parent.isG() && !parent.hasId() && parent.parent() && parent.parent().is("math"))
224 // there is something before the cursor, hence this is the
225 // closing math shift
226 if (parent.parent()["display"] != "1")
228 // one math shift is enough to close it
234 // we need two closing math shifts
235 //cursor.remove(); ??
236 parent.parent().append(cursor);
240 else if (parent.parent()["display"] != "1")
242 // there is nothing before the cursor, and the math is not
243 // in display mode, so this must be a double math shift
244 parent.parent()["display"] = "1";
249 parent.parent().append(cursor);
253 else if (parent.is("math"))
260 logger.warning("parser: math shift");
266 TPushParser::do_align()
268 TNode parent = cursor.parent();
269 if (parent && parent.isG() && parent.hasId())
271 // alignment tab used for the first time inside a group
272 TNode row = doc.create("row");
273 TNode cell = doc.create("cell");
274 TNode g = doc.createG();
277 g.append(parent.first(), cursor);
280 else if (parent && parent.isG() && parent.parent().is("cell"))
282 // alignment tab used within a cell
283 TNode oldCell = parent.parent();
284 assert(oldCell && oldCell.is("cell"));
285 TNode row = oldCell.parent();
286 assert(row && row.is("row"));
287 TNode cell = doc.create("cell");
288 if (oldCell.next()) oldCell.next().insert(cell);
289 else row.append(cell);
290 TNode g = doc.createG();
297 logger.warning("alignment tab used outside matrix");
303 TPushParser::do_eol()
305 //if (cursor.parent()) cursor.remove();
306 logger.warning("token ignored");
311 TPushParser::do_parameter(const std::string& p)
313 logger.warning("token ignored");
318 TPushParser::do_subscript()
320 TNode parent = cursor.parent();
323 TNode prev = cursor.prev();
326 TNode elem = doc.create("sb", nextId++);
327 TNode g = doc.createG();
328 cursor.replace(elem);
335 TNode elem = doc.create("sb", nextId++);
342 else if (parent.isSb() && cursor == parent[1])
344 if (parent["under"] == "1")
346 logger.warning("already under");
351 parent["under"] = "1";
357 logger.warning("token ignored");
363 TPushParser::do_superscript()
365 TNode parent = cursor.parent();
368 TNode prev = cursor.prev();
371 TNode elem = doc.create("sp", nextId++);
372 TNode g = doc.createG();
373 cursor.replace(elem);
380 TNode elem = doc.create("sp", nextId++);
387 else if (parent.isSp() && cursor == parent[1])
389 if (parent["over"] == "1")
391 logger.warning("token ignored: already over");
396 parent["over"] = "1";
402 logger.warning("token ignored");
408 TPushParser::do_space(const std::string&)
414 TPushParser::do_letter(const std::string& s)
416 //TNode parent = cursor.parent();
417 TNode elem = doc.createI(s, nextId++);
418 cursor.replace(elem);
424 TPushParser::do_digit(const std::string& s)
426 TNode elem = doc.createN(s, nextId++);
427 cursor.replace(elem);
433 TPushParser::isPrimes(const TNode& node) const
436 return node.isG() && node.last() && node.last().is("o") && node.last()["val"] == PRIME();
440 TPushParser::do_apostrophe()
442 if (cursor.parent() && cursor.parent().isG())
444 if (TNode prev = cursor.prev())
446 if (prev.isSp() && prev[1] && isPrimes(prev[1]))
448 prev[1].append(doc.createO(PRIME(), nextId++));
451 else if (prev.isSb() && prev[0] &&
452 prev[0].isSp() && prev[0][1] &&
453 isPrimes(prev[0][1]))
455 prev[0][1].append(doc.createO(PRIME(), nextId++));
460 TNode elem = doc.create("sp");
461 TNode g = doc.createG();
465 g.append(doc.createO(PRIME(), nextId++));
472 logger.warning("parser: you have to type an identifier before ''");
478 logger.warning("token ignored: you have to be in a group");
484 TPushParser::do_other(const std::string& s)
489 return do_apostrophe();
492 /*cout << "TPushParser::do_other " << s << endl;
493 cout << "DOCUMENT: " << static_cast<GdomeNode*>(cursor.element().get_ownerDocument()) << endl;*/
494 TNode elem = doc.createT("o", s, nextId++);
495 cursor.replace(elem);
503 TPushParser::do_active(const std::string&)
506 logger.warning("token ignorde");
511 TPushParser::do_comment()
520 TNode parent = cursor.parent();
521 if (parent && parent.isG() &&
522 parent.parent() && parent.parent().is("cell") &&
523 parent.parent().parent() && parent.parent().parent().is("row"))
525 TNode oldRow = parent.parent().parent();
527 TNode table = oldRow.parent();
529 TNode row = doc.create("row");
530 TNode cell = doc.create("cell");
531 TNode g = doc.createG();
532 if (oldRow.next()) oldRow.next().insert(row);
533 else table.append(row);
541 // at the moment, \cr can only be used inside a table
542 logger.warning("token ignored: cr used outside a table");
548 TPushParser::do_control(const std::string& name)
550 if (name == "cr") return do_cr();
553 TNode parent = cursor.parent();
554 const TDictionary::Entry& entry = dictionary.find(name);
557 case TDictionary::IDENTIFIER:
559 TNode t = doc.createI(entry.value, nextId++);
566 case TDictionary::OPERATOR:
568 TNode t = doc.createO(entry.value, nextId++);
575 case TDictionary::NUMBER:
577 TNode t = doc.createN(entry.value, nextId++);
584 case TDictionary::MACRO:
588 TNode m = doc.createC(name, nextId++);
590 if (entry.leftOpen && entry.rightOpen)
592 assert(entry.pattern.empty());
593 assert(parent.isG());
594 TNode g1 = doc.createG();
595 g1["left-open"] = "1";
596 g1.append(parent.first(), m);
598 TNode g2 = doc.createG();
601 frames.push(Frame(entry));
603 else if (entry.leftOpen)
605 assert(parent.isG());
606 TNode g = doc.createG();
607 g["left-open"] = "1";
608 g.append(parent.first(), m);
612 else if (entry.rightOpen)
614 assert(entry.pattern.empty());
615 assert(parent.isG());
616 TNode g = doc.createG();
619 frames.push(Frame(entry));
621 else if (!entry.pattern.empty())
623 frames.push(Frame(entry));
624 if (entry.paramDelimited(0))
626 TNode g = doc.createG();
635 // it's an empty macro
640 else if (!entry.pattern.size() && !entry.rightOpen && !entry.leftOpen)
642 // a macro with no arguments and no right open and no left open, can be child of anything
643 TNode m = doc.createC(name, nextId++);
650 // a macro with arguments or a rightOpen or leftOpen macro must be a group's child
651 logger.warning("token ignored: this macro should be in a group");
656 case TDictionary::UNDEFINED:
658 logger.warning("parser: using undefined macro " + name);
659 TNode m = doc.createC(name, nextId++);
668 logger.warning("token ignored");
676 TPushParser::drop_prev_token(bool special)
678 assert(cursor.prev());
679 assert(cursor.parent());
680 TNode prev = cursor.prev();
681 assert(prev.is("i") || prev.is("o") || prev.is("n"));
683 DOM::UCS4String ucs4val = prev.element().getAttribute("val");
684 bool macro = prev.element().hasAttribute("name");
685 std::string utf8name;
686 if (macro) utf8name = prev.element().getAttribute("name");
689 prev.replace(cursor);
691 if (cursor.parent().isC())
693 // in this case we have removed an element of a MACRO.
694 // we can assert that this element was a non delimited argument
695 assert(!frames.empty());
696 Frame& frame = frames.top();
697 assert(frame.pos > 0);
701 if ((ucs4val.length() > 1) && !special)
705 // in this case we can return the content of ucs4val, but we have
706 // to convert it in a utf8
707 DOM::GdomeString gdsval(ucs4val);
708 std::string utf8val(gdsval);
709 utf8val = utf8val.erase(utf8val.length() - 1, 1);
710 return std::string(utf8val);
714 // in this case, the content of val could be in unicode,
715 // but we have attribute name, which doesn't contain character not representable
717 return "\\" + utf8name.erase(utf8name.length() - 1, 1);
720 else if ((ucs4val.length() <= 1) && macro && special) return "\\" + utf8name.erase(utf8name.length() - 1, 1);
725 TPushParser::drop_prev_script(bool special)
727 // this method deletes an sp or an sb preceding the cursor
728 assert(cursor.prev());
729 assert(cursor.parent());
730 TNode prev = cursor.prev();
731 assert(prev.is("sp") || prev.is("sb"));
734 // we can invoke the drop_prev, because a sp (sb) MUST have two children
735 // but we cannot invoke do_drop_script because it assumes when called, the first
736 // child has been removed yet.
737 if (cursor.prev().isG() && !prev.hasId())
739 // in this case, the user has inserted the a sequence of '.
740 // Hence, we force a normal deleting, because the behaviour must be the same
741 // for the two kind of deleting
742 return drop_prev(false);
744 else return drop_prev(special);
748 TPushParser::drop_prev_group(bool special)
750 assert(cursor.prev() && cursor.prev().isG());
751 TNode parent = cursor.parent();
752 TNode prev = cursor.prev();
756 if (parent.isC() && prev.hasId())
758 assert(!frames.empty());
762 if (special) return "";
765 // a group could have no children, so the drop_prev is not appropriate
766 // so, this method is not equivalent to the one above
767 return do_drop(special);
772 TPushParser::drop_prev_macro(bool special)
774 assert(cursor.parent());
775 assert(cursor.prev());
776 TNode prev = cursor.prev();
779 std::string macro_name = prev.nameC();
781 TNode parent = cursor.parent();
783 const TDictionary::Entry& entry = dictionary.find(prev["name"]);
785 if (!entry.defined())
787 // We can assume that the user wants to completely delete the undefined macro,
788 // but we can also handle this case as we handle tokens. At the moment, we delete the
791 prev.replace(cursor);
792 if (cursor.parent().isC())
794 // we have removed a macro's child
795 assert(!frames.empty());
798 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
803 // we start to remove a MACRO. Different actions must be taken, based on the nature
804 // of the MACRO. In some cases, we can't remove the MACRO immediately, in other
805 // cases it's correct. In the first set of cases, we have to update the stack, pushing
806 // a frame in it with a correct value of pos, in the
807 // second one, we must not push a frame in the stack
811 // In this fragment of code we also handle the leftOpen && rightOpen MACRO.
812 // if the control element is rightOpen, the cursor should be placed after
813 // the last child of the control element's last child, and than, we try to remove something.
814 // A frame MUST be pushed in the stack, because we dont' know if the following actions
815 // will completely remove the MACRO.
816 frames.push(Frame(entry));
818 // Since the MACRO is rightOpen, the last child of the MACRO must be a phantom group
819 assert(prev.last().isG() && !prev.last().hasId());
822 prev.last().append(cursor);
824 if (special) return "";
827 // the drop_prev is not appropriate, because the last child of the MACRO could have no children
828 return do_drop_phantom_group(special);
831 else if (entry.leftOpen)
833 // the leftOpen MACRO MUST have one and only one child, which MUST be a phantom group
834 // In this case, we do not have to push a frame in the stack, because we remove the
835 // MACRO immediately, substituting it with the content of the phantom group.
836 // We could remove the last child of the phantom group, but
837 // it's not clear if it's the correct behavior of the graphical deleting.
838 // At the moment, to give a standard behaviour, we remove the last element.
839 assert(prev.first());
840 assert(prev.first().isG());
841 assert(prev.first() == prev.last());
843 TNode g = prev.first();
846 // in this case, the phantom group has at least one child, so we can call the
849 prev.replace(g.first(), TNode());
850 parent.append(cursor);
851 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
852 else return do_drop(special);
856 // otherwise, the phantom group has no children, so we remove it, also the MACRO.
859 prev.replace(cursor);
860 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
863 // Once removed this empty macro, we could try to remove something else.
864 // This would be justified by the fact that, generally, an empty macro gives no visual information
866 return do_drop(special); // special is false
870 else if (!entry.pattern.empty())
872 // we have to start to remove a MACRO which accepts arguments.
873 // If the MACRO accepts arguments, the MACRO has at least one child
874 assert(prev.size() >= 1);
876 // Differnt actions must be taken, based on the nature of the last child
877 // of the MACRO. We have to distinguish the case in which it's a delimited argument,
878 // frome the one in which it's a not delimited argument.
879 if (prev.last().isG() && !prev.last().hasId())
883 // in this case, we have to start removing the last delimiter
884 frames.push(Frame(entry, entry.pattern.size() - 2));
887 prev.last().append(cursor);
889 std::string last_del = entry.pattern[entry.pattern.size() - 1].value;
891 return "\\" + last_del.erase(last_del.length() - 1, 1);
895 // the last argument of the MACRO is a delimited argumet. We ideally remove
896 // the sequence of delimiters
898 prev.last().append(cursor);
899 // we have to push a frame with a correct value of pos
900 assert(entry.previousParam(entry.pattern.size()) != entry.pattern.size());
902 unsigned sequence_length = entry.pattern.size() - entry.previousParam(entry.pattern.size()) - 1;
903 unsigned p = entry.pattern.size() - sequence_length - 1;
904 // now, p is the correct value of pos, and we can push the frame.
905 frames.push(Frame(entry, p));
907 // To give a standard behaviour to the graphical deleting, we remove the last
908 // element of the macro. Since we are in a phantom group, we can invoke the
909 // do_drop_phantom_group(special).
910 return do_drop_phantom_group(special);
915 // in this case, the last child of the MACRO is not a delimited argument, so we try
916 // to remove it, but we have to take differnt actions if the MACRO is a table with rows or not.
918 if (entry.table == 1 && prev.last().is("row"))
920 // in this case the cursor has to be appended to the group associated to
921 // the last cell of the last row of the table
922 assert(prev.last().last().is("cell") && prev.last().last().first().isG());
923 prev.last().last().first().append(cursor);
925 // we have to push a frame in the stack. Since tables has a pattern size = 1, we have to
926 // set pos at 0, because appending the cursor to the last cell means that this argument
927 // is not whole inserted.
928 // We don't call frames.push(Frame(entry)), because it incoditionaly set pos at 0. The
929 // following line is more general
930 frames.push(Frame(entry, entry.pattern.size() - 1));
933 // to type a table with rows and cells, the user had typed a
934 // "{", and to exit from it, the user had inserted a "}".
935 // Since we are in a special deleting, we just idealy remove the "}"
938 else return do_drop_phantom_group(special);
942 // we push a frame in the stack with a correct value of member pos.
943 // This correct value is the size of the pattern - 1, because we have been started to delete
944 // a MACRO. It means that all of the MACRO's arguments have been inserted, but
945 frames.push(Frame(entry, entry.pattern.size()));
947 return drop_prev(special);
950 } // end of the else of the if (prev.last().isG() && !prev.last().hasId())
952 } // end of if (!entry.pattern.empty())
955 // if we are here, the MACRO preceding the cursor, is !(rightOpen || leftOpen),
956 // and has no pattern. It means that it has no children.
957 // We can replace it with the cursor
958 assert(prev.size() == 0);
960 prev.replace(cursor);
961 if (cursor.parent().isC())
963 // we have removed an empty macro, which was a non delimited argument of a macro.
964 // We have to decrement pos
965 assert(!frames.empty());
969 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
972 // now we could start to remove something else. This behaviour would be justified by the
973 // fact that, generally, an empty MACRO gives no visual information about it.
974 // To adopt this behaviour, just remove the comment to the following instruction
975 // return do_drop(special);
977 } // end of defined MACRO
982 TPushParser::drop_prev(bool special)
984 // if in this function, the prev of cursor does exist, also the parent and we want a graphical deleting.
986 assert(cursor.prev());
987 assert(cursor.parent());
989 TNode prev = cursor.prev();
991 if (prev.is("i") || prev.is("o") || prev.is("n"))
993 return drop_prev_token(special);
995 else if (prev.isSp() || prev.isSb())
997 return drop_prev_script(special);
1001 return drop_prev_group(special);
1003 else if (prev.isC())
1005 // here, we also treat the case in which the MACRO is a table
1006 return drop_prev_macro(special);
1010 // not handled. Future cases...
1017 TPushParser::rgreplace_father()
1019 // this method MUST only be invoked, when the cursor
1020 // is the only child of a group with id. This function
1021 // replaces the group with the cursor. But if the new parent
1022 // is a group with id and the cursor is the only child of the
1023 // group, the new parent is replaced...and so on.
1024 // r stands for recursive, g stands for graphical.
1025 assert(cursor.parent());
1026 assert(cursor.parent().isG() && cursor.parent().hasId());
1028 TNode parent = cursor.parent();
1030 while (parent.isG() && parent.hasId() && (parent.first() == cursor))
1032 parent.replace(cursor);
1033 parent = cursor.parent();
1038 TPushParser::do_drop_script(bool special)
1040 // If we are here, the cursor is child of a script (sp or sb) and
1041 // this means that a prev does exist and that there is one and only one
1042 // element preceding the cursor. The sp's (or sb's) parent
1043 // MUST NOT be a MACRO.
1044 // The element preceding the cursor is the base of the script.
1046 assert(cursor.parent() && (cursor.parent().isSp() || cursor.parent().isSb()));
1047 TNode parent = cursor.parent();
1049 assert(parent.size() == 2);
1050 assert(parent.parent() && !parent.parent().isC());
1052 TNode prev = cursor.prev();
1054 if (prev.isG() /*&& !prev.hasId()*/ && (prev.size() == 0))
1056 // in this case, the script's base is a group with no elements, so
1057 // we have to remove the entire MACRO, replacing it with the cursor.
1058 // This situation occurs when the user had typed something like this
1065 if (special && prev.hasId())
1067 // in this case, the user has typed: ...{}^
1068 // hence we idealy remove the ^
1069 parent.replace(prev);
1070 prev.parent().append(cursor);
1073 else if (!prev.hasId())
1075 // we idealy remove the ^, but the phantom group
1076 // has to be removed, also
1078 parent.replace(cursor);
1084 parent.replace(cursor);
1086 // since the script had no children, we can try to remove something else.
1087 // Since we don't know who is cursor's parent, and who is cursor's preceding
1088 // element, we invoke the do_drop()
1089 return do_drop(special);
1092 * * the following istructions have sense, only if we remove the preceding one.
1094 // if the new parent is a group with Id and the cursor is the only
1095 // element of this group, we have to remove the group. These controls are made
1096 // in the method rgreplace_father().
1097 if (cursor.parent().isG() && cursor.parent().hasId()) rgreplace_father();
1103 // in this case, the prev has to replace the script.
1104 parent.replace(prev);
1105 prev.parent().append(cursor);
1106 // now prev have a preceding element
1107 assert(cursor.parent().size() > 1);
1109 if (special) return "";
1112 // to give a standard behaviour, we try to remove the element, which was
1113 // the script's base.
1114 return do_drop(special);
1118 } // end of method do_drop_script
1121 TPushParser::do_drop_macro(bool special)
1123 // If we are here, the cursor is a child of a MACRO and this means
1124 // that there is an open frame for the control element
1125 // and this element is closed at either side (no leftOpen no rightOpen)
1126 // and the MACRO is waiting for a not delimited argument, so
1127 // we can assert that frame.entry.pattern.size() >= 1
1128 assert(cursor.parent() && cursor.parent().isC());
1129 TNode parent = cursor.parent();
1131 // this string is useful iff we have a special deleting
1132 std::string macro_name = parent.nameC();
1134 assert(!frames.empty());
1135 Frame& frame = frames.top();
1136 assert(frame.entry.pattern.size() >= 1);
1138 // we have to take different actions, based on if a preceding element exists
1140 TNode prev = cursor.prev();
1143 // in this case, a prev does not exist, so the actions of deleting means
1144 // that we have to remove the MACRO. So we have to pop the stack.
1145 assert(frame.pos == 0);
1147 parent.replace(cursor);
1150 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
1153 // Since the macro hadn't no children and this is a graphical deleting, we try
1154 // to remove something else
1155 return do_drop(special);
1158 * the following block of code has sense only if we remove the preceding instruction
1160 // if the new parent is a group with Id, and has no elements other than the
1161 // cursor, we can remove it, because it' a graphical deleting
1162 if (cursor.parent() && cursor.parent().isG() && cursor.parent().hasId())
1164 else if (cursor.parent().isC())
1166 // We have assumed that a MACRO cannot be a MACRO's child.
1167 // At the moment, this assumption is valid, but in a future
1168 // it might be false.
1169 assert(!frames.empty());
1170 Frame& frame = frames.top();
1171 assert(frame.pos > 0);
1179 // a prev does exist, we have to control if it's a delimited argument or not.
1180 if (prev.isG() && !prev.hasId())
1182 // in this case, prev is a delimited argument, so we have
1183 // to ideally remove the sequence of delimiters
1184 Frame& frame = frames.top();
1185 assert(frame.pos > 1);
1187 prev.append(cursor);
1188 assert(frame.entry.previousParam(frame.pos) != frame.entry.pattern.size());
1192 // in this case we have to start removing the last delimimeter.
1193 // It means that we return in a situation where the user has not entirely
1194 // inserted the delimited argument. So, we have to decrement frame.pos of
1195 // two units: the delimiter and the actual argument
1196 std::string last_del = frame.entry.pattern[frame.pos - 1].value;
1197 frame.pos = frame.pos - 2;
1198 return "\\" + last_del.erase(last_del.length() - 1, 1);
1202 // these 3 lines of code update the member pos.
1203 unsigned sequence_length = frame.pos - frame.entry.previousParam(frame.pos) - 1;
1204 assert(sequence_length);
1205 frame.pos = frame.pos - sequence_length - 1;
1207 // since it's a graphical deleting, we have to remove the current preceding element.
1208 // We don't invoke the drop_prev(), because a do_drop_phantom_group is more general.
1209 return do_drop_phantom_group(special);
1214 // the prev is not a delimited argument, so we have to try to remove it.
1215 // We "try", because the prev might be something that
1216 // a simple delete cannot remove completely
1217 return drop_prev(special);
1224 TPushParser::do_drop_groupId(bool special)
1226 // if we are here, the cursor's parent is a group with Id
1227 assert(cursor.parent() && cursor.parent().isG() && cursor.parent().hasId());
1228 TNode parent = cursor.parent();
1230 // we have to take different actions based on if the cursor has a preceding
1232 TNode prev = cursor.prev();
1235 // the cursor has a preceding element, so we try to remove it
1236 if (special) return drop_prev(special);
1239 std::string str = drop_prev(special);
1241 // We control if the group has to be removed, because the cursor
1242 // might be the only element of the group.
1243 // But we have to be careful, because drop_prev could change the TML tree
1244 // more than we think...parent could no longer exist!
1245 parent = cursor.parent();
1246 if ((parent.first() == cursor) && parent.isG() && parent.hasId())
1254 // the cursor has no preceding elements, so we have to remove the
1258 parent.replace(cursor);
1264 // we have to re-start the process, because it' a graphical deleting
1265 return do_drop(special);
1269 } // end of method do_drop_groupId()
1272 TPushParser::do_drop_phantom_group(bool special)
1274 // if we are here, the cursor MUST be a child of a
1276 assert(cursor.parent() && cursor.parent().isG() && !cursor.parent().hasId());
1278 TNode parent = cursor.parent();
1280 // now we have to control if the cursor has a preceding element or not
1281 TNode prev = cursor.prev();
1284 if (parent.parent() && parent.parent().isC())
1286 // there is a frame in the stack
1287 assert(!frames.empty());
1288 if (frames.top().entry.pattern.size())
1290 Frame& frame = frames.top();
1293 // we are in a delimited argument. If the user has inserted a proper subset of the
1294 // delimiters'sequence, we start to remove the previous delimiter. Start to remove
1295 // a delimiter means that that delimiter must be removed from the count of inserted delimiters.
1296 // It means that we have to decrement the member pos.
1297 if (frame.entry.pattern[frame.pos].category != TToken::PARAMETER)
1299 std::string del = frame.entry.pattern[frame.pos].value;
1301 return "\\" + del.erase(del.length() - 1, 1);
1306 // we are in a delimited argument. If the user has inserted a proper subset of the delimiters'sequence,
1307 // we have to remove the portion the user has inserted.
1308 while (frame.entry.pattern[frame.pos].category != TToken::PARAMETER) frame.pos--;
1313 // the cursor has a preceding element, so we try to remove it
1314 std::string str = drop_prev(special);
1316 if (special) return str;
1319 // now we have to control the parent, to handle the case of primes. But we have returned from a drop_prev(), which
1320 // could change the TML tree. So not asssuming that cursor's parent is unchanged is convenient.
1321 parent = cursor.parent();
1322 if (parent.isG() && !parent.hasId() && (parent.size() == 1) && parent.parent().isSp())
1324 // in this case the drop_prev has removed the only element preceding the cursor.
1325 // Since the phantom group is an sp's child, the user has removed all \' in the
1327 // Now we have some possibilities:
1328 // - we can replace the phantom group with the cursor, giving the user the chance to insert a new
1330 // - we can remove the phantom group and the sp element, recreating the state before the user inserted the first
1332 // At the moment we implement the second one.
1333 assert(parent.parent().size() == 2);
1334 TNode gparent = parent.parent();
1335 TNode base = gparent.first();
1338 gparent.replace(base);
1339 // now base's parent is no more gparent
1340 base.parent().append(cursor);
1344 else if (parent.isG() && !parent.hasId() && parent.parent().isSp())
1346 // in this case we have to place the cursor after the sp element
1348 assert(parent.parent().parent());
1349 parent.parent().parent().append(cursor);
1357 // in this case the cursor is the only element of the phantom group,
1358 // so we have to remove it. But, a phantom group has a special role,
1359 // so we have to control the grand father of the cursor.
1360 TNode gfather = parent.parent();
1363 // If here, the TML tree is in an inconsistent state
1364 logger.error("TML tree in a inconsistent state");
1367 else if (gfather.isC())
1369 // in this case the phantom group is child of a MACRO.
1370 // We have to control the nature of this MACRO.
1371 assert(!frames.empty());
1372 Frame& frame = frames.top();
1374 // this variable is useful in a special deleting
1375 std::string macro_name = gfather.nameC();
1377 if (frame.entry.leftOpen && frame.entry.rightOpen)
1379 // in this case, the cursor'parent is in the second and last child
1380 // of the MACRO. We can assert that the grand father has two
1381 // children, which are both phantom groups
1382 assert(gfather.size() == 2);
1383 assert((gfather.last() == parent) && (gfather.first().isG() && !gfather.first().hasId()));
1384 assert(frame.pos == 0);
1386 TNode ggfather = gfather.parent();
1390 // we have to replace the gfather with the elements of its first child, but this group may have no
1392 if (gfather.first().size())
1394 gfather.replace(gfather.first().first(), TNode());
1395 ggfather.append(cursor);
1399 // in this case, the MACRO has to be replaced with the cursor
1400 gfather.first().remove();
1401 gfather.replace(cursor);
1403 // now we have the situation preceding the insertion of the leftOpen and rightOpen MACRO.
1404 // this MACRO no longer exists.
1407 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
1410 // to give a standard behaviour to the graphical deleting, we call the do_drop.
1411 return do_drop(special);
1414 else if (frame.entry.rightOpen)
1416 // the user has inserted a rightOpen MACRO, and now, this MACRO has no children (excluding the
1417 // phantom group), so we remove the MACRO.
1418 // We can assert that cursor's parent is the only child of the MACRO
1419 assert(gfather.size() == 1);
1420 assert(frame.pos == 0);
1423 gfather.replace(cursor);
1425 // now we have the situation preceding the rightOpen MACRO, so we have to pop the frame
1428 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
1431 // to give a standard behaviour to the graphical deleting, we call the do_drop.
1432 return do_drop(special);
1436 else if (frame.entry.leftOpen)
1438 // this situation will never occur.
1439 logger.error("the parser has generated a wrong TML tree");
1442 else if (!frame.entry.pattern.empty())
1444 // the MACRO accepts arguments, and the phantom group in which
1445 // the cursor is, rappresents a delimited argument.
1446 // We have to control if the cursor's parent has a preceding element,
1448 TNode uncle = parent.prev();
1451 // the parent is the only element of the MACRO.
1452 // we can assert that frame.pos == 0.
1453 // In this case we can replace the MACRO with the cursor
1454 assert(frame.pos == 0);
1457 gfather.replace(cursor);
1460 if (special) return "\\" + macro_name.erase(macro_name.length() - 1, 1);
1463 // once we have replaced the empty macro with the cursor, we can remove
1465 return do_drop(special);
1470 // the parent has a preceding element. Now we have
1471 // to control if the uncle is a delimited argument or not.
1472 if (uncle.isG() && !uncle.hasId())
1474 // cursor's uncle is a delimited argument
1477 uncle.append(cursor);
1480 // we have to start removing the last delimiter of the delimited
1482 std::string last_del = frame.entry.pattern[frame.pos - 1].value;
1483 frame.pos = frame.pos - 2;
1484 return "\\" + last_del.erase(last_del.length() - 1, 1);
1488 // the uncle is a delimited argument. So we have to ideally
1489 // remove the sequence of delimiters.
1490 assert(frame.pos > 1);
1491 unsigned sequence_length = frame.pos - frame.entry.previousParam(frame.pos) - 1;
1492 assert(frame.entry.previousParam(frame.pos) != frame.entry.pattern.size());
1493 assert(sequence_length);
1494 // sequence_length is the length of the delimiters sequence which separates
1495 // the current parameter and the previous parameter
1496 frame.pos = frame.pos - sequence_length - 1;
1498 // once removed the sequnce of delimiters, we can start to remove the actual
1499 // parameter. We can call the do_drop_phantom_group() because a delimited argument
1500 // is always a phantom group's child
1501 return do_drop_phantom_group(special);
1506 // the uncle is a not delimited argument, so we try to remove it.
1508 parent.replace(cursor);
1509 parent = cursor.parent(); // we update the parent (it should be the MACRO)
1510 assert(parent.isC());
1512 // now we try to remove the uncle (now it' the preceding element)
1513 return drop_prev(special);
1515 } // this is the else's end, that handles the case in which an uncle exists
1516 } // end of if (!frame.entry.pattern.empty())
1519 // the entry has no arguments, is not rightOpen and is not leftOpen.
1520 logger.error("TML tree in a strange state");
1523 } // end of if (gfather.isC())
1524 else if (gfather.is("cell"))
1526 // A table is a control sequence, so there is a frame in the stack
1527 assert(!frames.empty());
1528 assert(frames.top().pos == 0);
1529 assert(frames.top().entry.table == 1);
1531 // a cell MUST be a row's child, which in turn is a table's child
1532 assert(gfather.parent() && gfather.parent().is("row") && gfather.parent().parent());
1534 // this variable is useful to handle the special deleting
1535 std::string table_name = gfather.parent().parent().nameC();
1537 TNode row = gfather.parent();
1539 // in this case the cell has no element, so the user wants to delete this cell.
1540 TNode prev_cell = gfather.prev();
1544 // now the cell no longer exists
1548 // in this case, the cell was the only cell in the row.
1549 // So, we assume that the user wants to delete the entire row.
1550 TNode table = row.parent();
1551 TNode prev_row = row.prev();
1558 // Since there was a cell (and a row), the user has typed a "{" to
1559 // We ideally remove this character.
1560 table.append(cursor);
1565 // the row was the only child of the table.
1566 // so we have to delete the entire table
1567 assert(table.parent());
1568 TNode parent_table = table.parent();
1571 parent_table.append(cursor);
1577 // there are other rows (one or more)
1578 assert(prev_row.is("row"));
1579 assert(prev_row.last());
1580 TNode last_cell = prev_row.last();
1581 assert(last_cell.is("cell"));
1582 assert(last_cell.size() == 1);
1583 assert(last_cell.first().isG() && !last_cell.first().hasId());
1584 last_cell.first().append(cursor);
1585 // Since cells and rows are separated by spaces and CRs
1586 // (and the user can see this spaces and CRs), a special deleting
1587 // is equivalent to a normal deleting
1590 } // end of if (!prev_cell)
1593 // being here means that there is a previous cell,
1594 // so we append the cursor to group.
1595 assert(prev_cell.size() == 1);
1596 assert(prev_cell.first().isG() && !prev_cell.first().hasId());
1597 prev_cell.first().append(cursor);
1600 } // end of if (gfather.is("cell"))
1601 else if (gfather.isSp())
1603 // we cannot be here because a phantom group can be a Sp child only
1604 // in two cases. If the user has typed somethong like:
1606 // the cursor is not phantom group's child.
1607 // If the user has typed somethong like
1609 // In this case the sequence of ' is placed in a phantom group,
1610 // which becomes the exponent of the script. But, the cursor is
1611 // always outside the phantom group
1612 logger.error("TML tree in a strange state");
1615 else if (gfather.is("math"))
1617 // in this case we ignore the user's will of deleting
1618 // but we could also decide to remove the math mode.
1619 logger.warning("Parser: nothing to delete");
1624 // cursor's grand father is undefined
1625 logger.error("parser: TML tree is in a unknown state");
1628 } // end of the else of the if (prev)
1634 TPushParser::do_drop(bool special)
1636 // we have to handle the case in wich the cursor has a parent or not
1637 if (!cursor.parent())
1639 // it's not a good situation...at the moment we do not take actions
1640 logger.error("TML tree not well structured");
1645 // a parent exists. We have to take differnt actions, based on the nature of
1647 TNode parent = cursor.parent();
1648 if (parent.is("math"))
1650 // we ca do two thing...we can remove the math mode (it implies controlling the display attribute), we can do nothing
1651 // At the moment, the user's will of deleting is simply ignored
1652 logger.warning("nothing to delete");
1655 else if (parent.isG())
1657 // the cursor's parent is a group. We have to control if it's a phantom group or not
1660 return do_drop_groupId(special);
1664 return do_drop_phantom_group(special);
1666 } // end of parent is group
1667 else if (parent.isC())
1669 return do_drop_macro(special);
1670 } // end of parent is a MACRO
1671 else if (parent.isSp() || parent.isSb())
1673 return do_drop_script(special);
1674 } // end of parent is sp or sb
1675 } // end of the else which consider the case in which parent exists
1677 } // end of method do_drop
1680 TPushParser::process(const TToken& token)
1682 switch (token.category)
1684 case TToken::BEGIN: return do_begin(); break;
1685 case TToken::END: return do_end(); break;
1686 case TToken::SHIFT: return do_shift(); break;
1687 case TToken::ALIGN: return do_align(); break;
1688 case TToken::EOL: return do_eol(); break;
1689 case TToken::PARAMETER: return do_parameter(token.value); break;
1690 case TToken::SUPERSCRIPT: return do_superscript(); break;
1691 case TToken::SUBSCRIPT: return do_subscript(); break;
1692 case TToken::SPACE: return do_space(token.value); break;
1693 case TToken::LETTER: return do_letter(token.value); break;
1694 case TToken::DIGIT: return do_digit(token.value); break;
1695 case TToken::OTHER: return do_other(token.value); break;
1696 case TToken::ACTIVE: return do_active(token.value); break;
1697 case TToken::COMMENT: return do_comment(); break;
1698 case TToken::CONTROL: return do_control(token.value); break;
1703 TPushParser::push(const TToken& token)
1705 TNode parent = cursor.parent();
1706 // If the cursor has no parent then it is detached from the editing
1707 // tree, which means this token will be ignored
1710 // If the parent is a phantom group and the grand-parent is a
1711 // control sequence, there are two cases:
1712 // a. we are parsing a delimited argument of a entry
1713 // b. we are parsing a side of a right- or left-open entry
1714 if (parent.isG() && !parent.hasId() && parent.parent().isC())
1716 // There must be an open frame, for the grand-parent is a control sequence
1717 assert(!frames.empty());
1718 Frame& frame = frames.top();
1719 if (!frame.entry.pattern.empty())
1721 // The entry pattern is not empty. By our conventions this means
1722 // the entry cannot be open at either end, hence we are parsing
1723 // a delimited argument
1724 assert(frame.pos + 1 < frame.entry.pattern.size());
1725 assert(frame.entry.pattern[frame.pos + 1].category != TToken::PARAMETER);
1726 if (frame.entry.pattern[frame.pos + 1] == token)
1728 // The token matches with a delimiter of the argument,
1729 // hence we increment the frame.pos
1732 if (frame.entry.lastDelimiter(frame.pos))
1734 // this delimiter is the last one for the argumet,
1735 // so the argument is completed
1742 // Delimiter mismatch.
1743 if (frame.entry.pattern[frame.pos].category != TToken::PARAMETER)
1745 // in this case, there is a sequence of delimiters that delimitates
1746 // the argument, and the user has correctly inserted a portion of this
1747 // sequence, but now has inserted a wrong delimiter.
1748 // Here, there are some possibilities:
1749 // - ignore the token, and wait for the correct delimiter
1750 // - ignore the token, wait for the correct delimiter and emit an error
1751 // At the moment, we implement the second possibily
1752 logger.warning("parser: it's not the correct delimiter...you have to type " + frame.entry.pattern[frame.pos + 1].value);
1756 // in this case, the sequence of delimiters is composed of one
1757 // delimiter. It means that we have to process the token
1764 // The entry pattern is empty, hence we are parsing a right-open
1765 // entry. What happens if we actually are in the left side?
1766 // This could happen only when re-editing an entered expression
1768 assert(frame.entry.rightOpen);
1772 else if (parent.isC())
1774 // We are parsing a non-delimited argument entry
1776 Frame& frame = frames.top();
1777 assert(frame.pos < frame.entry.pattern.size());
1779 if (frame.entry.pattern[frame.pos].category == TToken::PARAMETER)
1781 // As by the TeX parsing rules of undelimited parameters,
1782 // empty spaces are ignored
1783 if (token.category != TToken::SPACE) process(token);
1785 else if (frame.entry.pattern[frame.pos] == token)
1787 // The token has been accepted
1789 if (frame.pos < frame.entry.pattern.size() &&
1790 frame.entry.paramDelimited(frame.pos))
1792 // If the next is a delimited argument we have to place
1793 // the phantom group with the cursor inside
1794 TNode g = doc.createG();
1806 // There is a mismatch. Emit an error and ignore the token?
1807 logger.warning("parser: token ignored: " + token.value);
1814 logger.warning("ignored token");
1817 if (factory && doc.dirtyNode() && !frozen()) factory->documentModified(doc);
1821 TPushParser::drop(bool special)
1823 std::string str = do_drop(special);
1824 if (factory && doc.dirtyNode() && !frozen()) factory->documentModified(doc);
1829 TPushParser::advance(const TNode& node)
1836 logger.error("wrong TML tree");
1838 else if (node.parent().isG())
1840 TNode next = node.next();
1841 if (next) next.insert(cursor);
1842 else node.parent().append(cursor);
1844 else if (node.parent().isC())
1846 assert(!frames.empty());
1847 if ((frames.top().pos + 1 == frames.top().entry.pattern.size()) || (frames.top().entry.pattern.empty()))
1849 // we are here when we have a right open macro, or the inserted element is the last one
1850 if (frames.top().entry.rightOpen)
1852 // we have to remove the frame from the stack
1854 advance(node.parent().parent());
1859 advance(node.parent());
1862 else if (frames.top().entry.paramDelimited(frames.top().pos + 1))
1864 // the next argument is delimited, so we have to create a phantom group
1865 TNode g = doc.createG();
1867 node.parent().append(g);
1872 // the next argumet is not delimited, so we have to append the cursor
1874 node.parent().append(cursor);
1878 else advance(node.parent());
1882 TPushParser::setCursorHint(const std::string& c)
1884 if (cursor["val"] != c)
1887 if (factory && doc.dirtyNode() && !frozen()) factory->documentModified(doc);
1892 TPushParser::hideCursor()
1894 if (hiddenCursor++ == 0)
1896 cursor["visible"] = "0";
1897 if (factory && doc.dirtyNode() && !frozen()) factory->documentModified(doc);
1905 TPushParser::showCursor()
1907 if (hiddenCursor > 0 && --hiddenCursor == 0)
1909 cursor["visible"] = "1";
1910 if (factory && doc.dirtyNode() && !frozen()) factory->documentModified(doc);
1920 if (APushParser::thaw() && factory && doc.dirtyNode())
1922 factory->documentModified(doc);