2 #include "TPushParser.hh"
3 #include "TListener.hh"
5 TPushParser::TPushParser(const TDictionary& d) : dictionary(d), listener(0)
10 TPushParser::TPushParser(const TDictionary& d, TListener& l) : dictionary(d), listener(&l)
19 cursor = doc.create("cursor");
22 doc.root().append(cursor);
25 TPushParser::~TPushParser()
30 TPushParser::PRIME() const
32 const TDictionary::Entry entry = dictionary.find("prime");
33 if (entry.cls == TDictionary::OPERATOR) return entry.value;
38 TPushParser::do_begin()
40 TNode parent = cursor.parent();
41 if (parent.isC() && dictionary.find(parent.nameC()).table)
43 TNode row = doc.create("row");
44 TNode cell = doc.create("cell");
45 TNode g = doc.createG();
53 TNode g = doc.createG(nextId++);
62 TNode parent = cursor.parent();
63 if (parent && parent.isG() && parent.hasId())
65 // normal closing brace for an explicitly open group
69 else if (parent && parent.isG() && parent.parent() && parent.parent().is("cell"))
71 assert(!frames.empty());
72 // closing brace for a structure in which & or \cr have been used
73 TNode row = parent.parent().parent();
74 assert(row && row.is("row"));
75 TNode table = row.parent();
79 // now we have to pop the associated frame in the stack
82 else if (parent && parent.isG() && !parent.hasId() && parent.parent())
84 // closing brace for a right-open macro (like \over)
86 advance(parent.parent());
97 TPushParser::do_shift()
99 TNode parent = cursor.parent();
101 if (parent.is("tex"))
103 TNode math = doc.create("math", nextId++);
104 TNode g = doc.createG();
105 cursor.replace(math);
109 else if (parent.isG() && !parent.hasId() && parent.parent() && parent.parent().is("math"))
113 // there is something before the cursor, hence this is the
114 // closing math shift
115 if (parent.parent()["display"] != "1")
117 // one math shift is enough to close it
122 // we need two closing math shifts
123 parent.parent().append(cursor);
126 else if (parent.parent()["display"] != "1")
128 // there is nothing before the cursor, and the math is not
129 // in display mode, so this must be a double math shift
130 parent.parent()["display"] = "1";
134 parent.parent().append(cursor);
137 else if (parent.is("math"))
143 cerr << "ERROR: math shift" << endl;
148 TPushParser::do_align()
150 TNode parent = cursor.parent();
151 if (parent && parent.isG() && parent.hasId())
153 // alignment tab used for the first time inside a group
154 TNode row = doc.create("row");
155 TNode cell = doc.create("cell");
156 TNode g = doc.createG();
159 g.append(parent.first(), cursor);
161 else if (parent && parent.isG() && parent.parent().is("cell"))
163 // alignment tab used within a cell
164 TNode oldCell = parent.parent();
165 assert(oldCell && oldCell.is("cell"));
166 TNode row = oldCell.parent();
167 assert(row && row.is("row"));
168 TNode cell = doc.create("cell");
169 if (oldCell.next()) oldCell.next().insert(cell);
170 else row.append(cell);
171 TNode g = doc.createG();
177 cerr << "alignment tab used outside matrix" << endl;
182 TPushParser::do_eol()
184 //if (cursor.parent()) cursor.remove();
188 TPushParser::do_parameter(const std::string& p)
194 TPushParser::do_subscript()
196 TNode parent = cursor.parent();
199 TNode prev = cursor.prev();
202 TNode elem = doc.create("sb", nextId++);
203 TNode g = doc.createG();
204 cursor.replace(elem);
210 TNode elem = doc.create("sb", nextId++);
216 else if (parent.isSb() && cursor == parent[1])
218 if (parent["under"] == "1") cerr << "already under" << endl;
219 else parent["under"] = "1";
224 TPushParser::do_superscript()
226 TNode parent = cursor.parent();
229 TNode prev = cursor.prev();
232 TNode elem = doc.create("sp", nextId++);
233 TNode g = doc.createG();
234 cursor.replace(elem);
240 TNode elem = doc.create("sp", nextId++);
246 else if (parent.isSp() && cursor == parent[1])
248 if (parent["over"] == "1") cerr << "already over" << endl;
249 else parent["over"] = "1";
254 TPushParser::do_space(const std::string&)
256 // ? may be used to distinguish tokens in some mode?
260 TPushParser::do_letter(const std::string& s)
262 //TNode parent = cursor.parent();
263 TNode elem = doc.createI(s, nextId++);
264 cursor.replace(elem);
269 TPushParser::do_digit(const std::string& s)
271 TNode parent = cursor.parent();
272 TNode prev = cursor.prev();
273 if (prev && parent.isG() && prev.is("n"))
275 TNode elem = doc.createN(prev.value() + s, nextId++);
280 TNode elem = doc.createN(s, nextId++);
281 cursor.replace(elem);
287 TPushParser::isPrimes(const TNode& node) const
290 return node.isG() && node.last() && node.last().is("o") && node.last()["val"] == PRIME();
294 TPushParser::do_apostrophe()
296 if (cursor.parent() && cursor.parent().isG())
298 if (TNode prev = cursor.prev())
300 if (prev.isSp() && prev[1] && isPrimes(prev[1]))
301 prev[1].append(doc.createO(PRIME(), nextId++));
302 else if (prev.isSb() && prev[0] &&
303 prev[0].isSp() && prev[0][1] &&
304 isPrimes(prev[0][1]))
305 prev[0][1].append(doc.createO(PRIME(), nextId++));
308 TNode elem = doc.create("sp");
309 TNode g = doc.createG();
313 g.append(doc.createO(PRIME(), nextId++));
328 TPushParser::do_other(const std::string& s)
336 cout << "TPushParser::do_other " << s << endl;
337 cout << "DOCUMENT: " << static_cast<GdomeNode*>(cursor.element().get_ownerDocument()) << endl;
338 TNode elem = doc.createT("o", s, nextId++);
339 cursor.replace(elem);
346 TPushParser::do_active(const std::string&)
352 TPushParser::do_comment()
360 TNode parent = cursor.parent();
361 if (parent && parent.isG() &&
362 parent.parent() && parent.parent().is("cell") &&
363 parent.parent().parent() && parent.parent().parent().is("row"))
365 TNode oldRow = parent.parent().parent();
367 TNode table = oldRow.parent();
369 TNode row = doc.create("row");
370 TNode cell = doc.create("cell");
371 TNode g = doc.createG();
372 if (oldRow.next()) oldRow.next().insert(row);
373 else table.append(row);
381 TPushParser::do_control(const std::string& name)
383 if (name == "cr") do_cr();
386 TNode parent = cursor.parent();
387 const TDictionary::Entry& entry = dictionary.find(name);
390 case TDictionary::IDENTIFIER:
392 TNode t = doc.createI(entry.value, nextId++);
398 case TDictionary::OPERATOR:
400 TNode t = doc.createO(entry.value, nextId++);
406 case TDictionary::NUMBER:
408 TNode t = doc.createN(entry.value, nextId++);
414 case TDictionary::MACRO:
416 TNode m = doc.createC(name, nextId++);
419 cout << "ecco tutti i token del pattern della entry inserita" << endl;
420 for (unsigned i = 0; i < entry.pattern.size(); i++)
421 cout << entry.pattern[i].value << endl;
423 if (entry.leftOpen && entry.rightOpen)
425 assert(entry.pattern.empty());
426 assert(parent.isG());
427 TNode g1 = doc.createG();
428 g1["left-open"] = "1";
429 g1.append(parent.first(), m);
431 TNode g2 = doc.createG();
434 frames.push(Frame(entry));
436 else if (entry.leftOpen)
438 assert(parent.isG());
439 TNode g = doc.createG();
440 g["left-open"] = "1";
441 g.append(parent.first(), m);
445 else if (entry.rightOpen)
447 assert(entry.pattern.empty());
448 assert(parent.isG());
449 TNode g = doc.createG();
452 frames.push(Frame(entry));
454 else if (!entry.pattern.empty())
458 frames.push(Frame(entry));
461 cout << "do_control: we have a table" << endl;
463 if (entry.paramDelimited(0))
465 cout << "do_control: it'a MACRO with delimited first argument" << endl;
466 TNode g = doc.createG();
475 // error, but we could handle this very easily
476 cerr << "error, but we could handle this easily" << endl;
482 case TDictionary::UNDEFINED:
484 cerr << "ERROR: using undefined macro `" << name << "'" << endl;
485 TNode m = doc.createC(name, nextId++);
497 TPushParser::gdelete_prev()
499 // if in this function, the prev of cursor does exist, also the parent and we want a graphical deleting.
501 assert(cursor.prev());
502 assert(cursor.parent());
504 TNode prev = cursor.prev();
505 if (prev.is("i") || prev.is("o") || prev.is("n"))
507 // the control below is designed to handle the case in which val have more than one unicode character
508 DOM::UCS4String ucs4val(prev.element().getAttribute("val"));
509 if ((ucs4val.length() <= 1) || prev.element().hasAttribute("name"))
512 prev.replace(cursor);
514 if (cursor.parent().isC())
516 // in this case we have removed an element of a MACRO.
517 // we can assert that this element was a non delimited argument
518 assert(!frames.empty());
519 Frame& frame = frames.top();
520 assert(frame.pos > 0);
527 ucs4val.erase(ucs4val.length() - 1, 1);
528 prev.element().setAttribute(DOM::GdomeString("val"), DOM::GdomeString(ucs4val));
530 } // end of if (prev.is("i") || ...)
531 else if (prev.is("sp") || prev.is("sb"))
536 } // end of if (prev.is("sp") || prev.is("sb"))
543 else if (prev.isC() && dictionary.find(prev["name"]).cls != TDictionary::UNDEFINED)
545 const TDictionary::Entry& entry = dictionary.find(prev["name"]);
547 // i start to remove a MACRO. The frame associated to this MACRO was
548 // popped from the stack (i think). So, i re-push the frame in the stack,
549 // but the pos member should be set correctly
550 cout << "gdelete_prev: i have to start to delete a MACRO" << endl;
552 // if the control element is leftOpen and rightOpen, the cursor should be placed after
553 // the last child of the control element's last child
556 cout << "gdelte_prev(): i have to delete a control rightOpen, so i push an element in the stack" << endl;
561 prev.last().append(cursor);
564 else if (entry.leftOpen)
566 cout << "gdelete_prev: i have to delete a control element with leftOpen" << endl;
567 assert(prev.first());
568 assert(prev.first().isG());
569 assert(prev.first() == prev.last());
570 TNode g = prev.first();
572 prev.replace(g.first(), TNode());
574 else if (!entry.pattern.empty())
576 // we have to start removing a MACRO which accepts arguments.
577 // a MACRO without child does not exist
579 cout << "gdelete_prev: i have to remove a MACRO with argument" << endl;
581 assert(prev.size() >= 1);
583 if (prev.last().isG() && !prev.last().hasId())
585 // this means that the last child of the MACRO is a phantom group,
586 // which in turn means that it is a delimited argument
587 // so we have to ideally remove this delimiter
589 prev.last().append(cursor);
590 // i have to push a frame with a correct value of pos
592 assert(entry.previousParam(entry.pattern.size()) != entry.pattern.size());
593 unsigned sequence_length = entry.pattern.size() - entry.previousParam(entry.pattern.size()) - 1;
594 frame.pos = entry.pattern.size() - sequence_length - 1;
596 cout << "gdelete_prev: i have inserted a frame, it's pos is: " << frames.top().pos << endl;
600 // in this case, the last child of the MACRO is
601 // an argument which is NOT delimited, so i try to
603 cout << "gdelete_prev: i try to remove the last argumet of the MACRO" << endl;
605 if (entry.table == 1 && prev.last().is("row"))
607 // in this case should be appended to the group associated to
608 // the last cell of the last row of the table
609 assert(prev.last().last().is("cell") && prev.last().last().first().isG());
610 prev.last().last().first().append(cursor);
612 else prev.append(cursor);
615 frame.pos = entry.pattern.size();
618 if (cursor.prev()) gdelete_prev();
624 else if (dictionary.find(prev["name"]).cls == TDictionary::UNDEFINED)
626 // The user wants to delete an undefined element.
627 // I suppose that he (or she) wants to delete it all
629 prev.replace(cursor);
639 TPushParser::rgreplace_futher(void)
641 // this function MUST only be invoked, when the cursor
642 // is the only child of a group with id. This function
643 // replace the group with the cursor. But if the new parent
644 // is a group with id and the cursor is the only child of the
645 // group, the new parent is replaced...and so on.
646 // r stands for recursive, g stands for graphical
647 assert(cursor.parent());
648 assert(cursor.parent().isG() && cursor.parent().hasId());
650 TNode parent = cursor.parent();
652 while (parent.isG() && parent.hasId() && (parent.first() == cursor))
654 parent.replace(cursor);
655 parent = cursor.parent();
660 // in this case we have removed a MACRO's child.
661 // I can assert that this MACRO accepts arguments.
662 assert(!frames.empty());
663 Frame& frame = frames.top();
664 assert(frame.pos > 0);
670 TPushParser::do_gdelete()
672 // this function operates a graphical deleting
674 //if (!frames.empty())
675 // cout << "do_gdelete: c'e' un frame aperto e il suo pos vale: " << frames.top().pos << endl;
677 TNode parent = cursor.parent();
679 // if no parent, do nothing
685 TNode prev = cursor.prev();
688 // i try to delete the preceding element
691 if ((parent.first() == cursor) && parent.isG() && parent.hasId())
695 else // no previous node is present
697 // if here, we are in a gruop whose only child is the cursor.
701 // the parent is a phantom group
702 assert(parent.parent());
703 if (!parent.parent().is("math"))
705 TNode gfuther = parent.parent();
707 // if the grand futher is a group with Id, it should be removed,
708 // but i don't know if it will never occur...
709 if (gfuther.isG() && gfuther.hasId())
712 parent.replace(cursor);
714 // re-start the process
717 else if (gfuther.isC())
719 // the grand futher is a control element: since the parent is a phantom group,
720 // the TML tree should be in a inconsistent state (once removed the parent).
722 // being here means that there is a frame in the stack
723 assert(!frames.empty());
724 cout << "do_gdelete: i have to remove a phantom group whuch is a child of a MACRO" << endl;
725 Frame& frame = frames.top();
726 if (frame.entry.leftOpen && frame.entry.rightOpen)
728 // in this case, the cursor is in the second and last child
729 // of the MACRO. We can assert that the grand futher has two
730 // children. which are both phantom group
731 cout << "do_gdelete: the MACRO is leftOpen and rigthOpen" << endl;
732 assert(gfuther.size() == 2);
733 assert((gfuther.last() == parent) && (gfuther.first().isG() && !gfuther.first().hasId()));
734 assert(frame.pos == 0);
736 TNode ggfuther = gfuther.parent();
740 // i have to replace the gfuther with the elements of its first child
741 gfuther.replace(gfuther.first().first(), TNode());
742 cout << "do_gdelete: i have removed the control element, and replaced it with its first child" << endl;
743 ggfuther.append(cursor);
744 cout << "do_gdelete: cursor appended to the grand grand futher" << endl;
746 // now we have the situation preceding the insertion of the MACRO leftOpen and rightOpen
747 // this MACRO no longer exists.
750 else if (frame.entry.rightOpen)
752 // the user has inserted a MACRO rightOpen. Since the cursor is the
753 // only child of the MACRO, the user want to remove it.
754 // We can assert that cursor's parent is the only child of the MACRO
755 cout << "do_gdelete: the MACRO is rightOpen only" << endl;
756 assert(gfuther.size() == 1);
757 assert(frame.pos == 0); // i think this assert has no sense
761 gfuther.replace(cursor);
763 // now we have the situation preceding the MACRO rightOpen, so i have to pop the frame
766 else if (frame.entry.leftOpen)
768 // it' s an unpredicted situation
769 cout << "it's a bad situation, maybe handlable, but unpredicted" << endl;
771 else if (!frame.entry.pattern.empty())
773 // the MACRO (the cursor's grand futher) accepts arguments.
774 // we have to control if the cursor's uncle does exist.
778 // in this case, we can assert that frame in the stack has
779 // pos greater than 0
780 assert(frame.pos > 0);
782 // cursor's uncle does exist. we have to control
783 // its nature (is it a phantom group?)
784 TNode uncle = parent.prev();
785 if (uncle.isG() && !uncle.hasId())
787 // the cursor's uncle is a phantom group, so it was a
788 // delimited argument of the MACRO and the corrisponding sequence of
789 // delimeters is inserted. So, the action of deleting means
790 // removing this sequence
791 assert(frame.pos > 1);
792 unsigned sequence_length = frame.pos - frame.entry.previousParam(frame.pos) - 1;
793 assert(frame.entry.previousParam(frame.pos) != frame.entry.pattern.size());
794 assert(sequence_length);
795 // sequence_length is the length of the delimiters sequence which separates
796 // the current parameter and the previous parameter
797 frame.pos = frame.pos - sequence_length - 1;
800 uncle.append(cursor);
804 // the uncle was a NOT delimited argument. So i try to
807 parent.replace(cursor);
809 parent = cursor.parent(); // i update the parent (it should be the MACRO)
810 assert(parent.isC());
818 // cursor's parent is the only child of the MACRO, which accepts arguments
819 // i can assert that frame.pos == 0.
820 // In this case i can replace the MACRO with the cursor
821 assert(frame.pos == 0);
825 gfuther.replace(cursor);
832 else if (gfuther.is("cell"))
834 // being here means that there is a frame in the stack
835 // associated to the "table"
836 cout << "do_gdelete: i have to delete a cell" << endl;
837 assert(!frames.empty());
838 assert(frames.top().pos == 1);
839 assert(frames.top().entry.table == 1);
840 // a cell MUST be child of row element, which in turn MUST be child of an element
841 // havin attribute table.
842 assert(gfuther.parent() && gfuther.parent().is("row") && gfuther.parent().parent());
843 TNode row = gfuther.parent();
845 // in this case the cell has no element, so the user wants to delete this cell.
846 TNode prev_cell = gfuther.prev();
851 // now the cell no longer exists
855 // i this case, the cell is the only cell in the row.
856 // So, i assume that the user wants to delete the entire row.
857 TNode table = row.parent();
858 TNode prev_row = row.prev();
862 // the row was the only child of the table.
863 // I think have to delete the entire table
864 assert(table.parent());
865 TNode parent_table = table.parent();
868 parent_table.append(cursor);
872 // there are other rows (one at least)
873 assert(prev_row.is("row"));
874 assert(prev_row.last());
875 TNode last_cell = prev_row.last();
876 assert(last_cell.is("cell"));
877 assert(last_cell.size() == 1);
878 assert(last_cell.first().isG() && !last_cell.first().hasId());
879 last_cell.first().append(cursor);
884 // being here means that there is a previous cell,
885 // so we append the cursor to group.
886 assert(prev_cell.size() == 1);
887 assert(prev_cell.first().isG() && !prev_cell.first().hasId());
888 prev_cell.first().append(cursor);
892 else // the grand futher is math
894 // nothing to do...i think
895 assert(frames.empty());
900 // the parent is a group with id and has no elements other than cursor
901 // so we replace it with the cursor.
904 // i have to re-start the process, because it' a graphical delete
909 else if (parent.isC())
911 // being here means that there is an open frame for the control element
912 // and this element is closed at either side (no leftOpen no rightOpen)
913 // and the MACRO was waiting for a non delimited argument, so
914 // we can assert that frame.entry.pattern.size() >= 1
915 assert(!frames.empty());
916 Frame& frame = frames.top();
918 assert(frame.entry.pattern.size() >= 1);
920 cout << "do_gdelete: frames.top().pos = " << frames.top().pos << endl;
922 TNode prev = cursor.prev();
926 // in this case we can replace the MACRO with the cursor
927 // and pop the stack and we can assert that frame.pos == 0
928 assert(frame.pos == 0);
929 cursor.remove(); // it should not be necessary, but i'm not shure
930 parent.replace(cursor);
935 // in this case the cursor has a preceding element
936 // and there are differnt things based on the nature
937 // of the prev: if it's a phantom group do something,
938 // else do something else
939 if (prev.isG() && !prev.hasId())
941 // in this case we have to append the cursor
942 // to the prev and decrement frame.pos of the length of
943 // delimiters sequence that delimitates the preceding argument.
944 // So we ideally remove this sequence
945 assert(frame.pos > 1);
948 assert(frame.entry.previousParam(frame.pos) != frame.entry.pattern.size());
949 unsigned sequence_length = frame.pos - frame.entry.previousParam(frame.pos) - 1;
950 assert(sequence_length);
951 frame.pos = frame.pos - sequence_length - 1;
955 // the prev is an non delimited argument, so we try to
961 else if (parent.is("sp") || parent.is("sb"))
963 // being here means that a prev MUST exist
964 // and that there is only an element preceding the cursor.
965 // The sp's (or sb's) parent MUST NOT be a MACRO
967 assert(parent.size() == 2);
968 assert(parent.parent() && !parent.parent().isC());
970 TNode prev = cursor.prev();
972 if (prev.isG() /*&& !prev.hasId()*/ && (prev.size() == 0))
975 parent.replace(cursor);
977 // now, cursor should be the only parent's child
978 assert(cursor.parent().size() == 1);
980 if (cursor.parent().isG() && cursor.parent().hasId()) rgreplace_futher();
984 assert(prev.hasId());
985 parent.replace(prev);
986 prev.parent().append(cursor);
987 // now prev should have a preceding element
988 assert(cursor.parent().size() > 1);
993 // not handled: no control for tables, ...
998 // the cursor has no parent!!!
1000 // emit an error? and if we want to emit an error, in which way?
1006 TPushParser::process(const TToken& token)
1008 switch (token.category)
1010 case TToken::BEGIN: do_begin(); break;
1011 case TToken::END: do_end(); break;
1012 case TToken::SHIFT: do_shift(); break;
1013 case TToken::ALIGN: do_align(); break;
1014 case TToken::EOL: do_eol(); break;
1015 case TToken::PARAMETER: do_parameter(token.value); break;
1016 case TToken::SUPERSCRIPT: do_superscript(); break;
1017 case TToken::SUBSCRIPT: do_subscript(); break;
1018 case TToken::SPACE: do_space(token.value); break;
1019 case TToken::LETTER: do_letter(token.value); break;
1020 case TToken::DIGIT: do_digit(token.value); break;
1021 case TToken::OTHER: do_other(token.value); break;
1022 case TToken::ACTIVE: do_active(token.value); break;
1023 case TToken::COMMENT: do_comment(); break;
1024 case TToken::CONTROL: do_control(token.value); break;
1025 case TToken::GDELETE: do_gdelete(); break;
1030 TPushParser::push(const TToken& token)
1032 cerr << "TPushParser::push " << token.value << " (cat: " << token.category << ")" << endl;
1034 if (token.category == TToken::GDELETE)
1036 cout << "push: i have to process a token with category member = GDELETE" << endl;
1039 else if ((doc.root().first() && doc.root().first().is("math")) || token.category == TToken::SHIFT)
1042 TNode parent = cursor.parent();
1043 // If the cursor has no parent then it is detached from the editing
1044 // tree, which means this token will be ignored
1047 // If the parent is a phantom group and the grand-parent is a
1048 // control sequence, there are two cases:
1049 // a. we are parsing a delimited argument of a entry
1050 // b. we are parsing a side of a right- or left-open entry
1051 if (parent.isG() && !parent.hasId() && parent.parent().isC())
1053 // There must be an open frame, for the grand-parent is a control sequence
1054 assert(!frames.empty());
1055 Frame& frame = frames.top();
1056 if (!frame.entry.pattern.empty())
1058 // The entry pattern is not empty. By our conventions this means
1059 // the entry cannot be open at either end, hence we are parsing
1060 // a delimited argument
1061 assert(frame.pos + 1 < frame.entry.pattern.size());
1062 assert(frame.entry.pattern[frame.pos + 1].category != TToken::PARAMETER);
1063 if (frame.entry.pattern[frame.pos + 1] == token)
1065 // The token matches with a delimiter of the argument,
1066 // so we increment the frame.pos
1069 if (frame.entry.lastDelimiter(frame.pos))
1071 // this delimiter is the last one for the argumet,
1072 // so the argument is completed
1076 if (frame.pos == frame.entry.pattern.size())
1078 // This token has completed the entry
1080 advance(parent.parent());
1082 else if (frame.entry.paramDelimited(frame.pos))
1084 // For the next is a delimited argument we have to place
1085 // a suitable phantom group with the cursor inside
1086 TNode g = doc.createG();
1087 parent.parent().append(g);
1091 parent.parent().append(cursor);
1096 // Delimiter mismatch.
1097 if (frame.entry.pattern[frame.pos].category != TToken::PARAMETER)
1099 // in this case, there is a sequence of delimiters that delimitates
1100 // the argument, and the user correctly inserted a portion of this
1101 // sequence, but has inserted a wrong delimiter.
1102 // Here, there are some possibilities:
1103 // - ignore the token, and wait for the correct delimiter
1104 // - ignore the token, wait for the correct delimiter and emit an error
1105 // If we want to emit an error, we shlould implement a class, that handle
1107 // At the moment, the error is printed to screen
1108 cout << "push: it's not the correct delimiter...you have to type " << frame.entry.pattern[frame.pos + 1].value << endl;
1112 // in this case, the sequence of delimiters is composed of one
1113 // delimiter. It means that we have to process the token
1120 // The entry pattern is empty, hence we are parsing a right-open
1121 // entry. What happens if we actually are in the left side?
1122 // This could happen only when re-editing an entered expression
1124 assert(frame.entry.rightOpen);
1128 else if (parent.isC())
1130 // We are parsing a non-delimited argument entry
1132 Frame& frame = frames.top();
1133 assert(frame.pos < frame.entry.pattern.size());
1135 cout << "push: there is a frame with pos " << frame.pos << endl;
1136 if (frame.entry.pattern[frame.pos].category == TToken::PARAMETER)
1138 // As by the TeX parsing rules of undelimited parameters,
1139 // empty spaces are ignored
1140 if (token.category != TToken::SPACE)
1142 // We need to increase the frame position here, becase inside
1143 // process the function advance will be called. At that point
1144 // it will be important for the parser to know that the entry
1145 // has been completed in order to place the cursor correctly
1146 // in the next position
1151 else if (frame.entry.pattern[frame.pos] == token)
1153 // The token has been accepted
1155 if (frame.pos < frame.entry.pattern.size() &&
1156 frame.entry.paramDelimited(frame.pos))
1158 // If the next is a delimited argument we have to place
1159 // the phantom group with the cursor inside
1160 TNode g = doc.createG();
1169 // There is a mismatch. Emit an error and ignore the token?
1170 cerr << "ERROR: token ignored: " << token.value << " (cat: " << token.category << ")" << endl;
1178 cout << "ignored token" << endl;
1181 if (listener) listener->callback(doc); //it shoul be repristened if you remove the comment in the else above
1183 } // this end corresponds to the if ((doc.root().first() && doc.root().first().is("math")) || token.category == TToken::SHIFT)
1186 // there is no math element
1187 // In this case, i think we have to emit an error
1188 cout << "push: ignored token...you have to enter in math mode...insert $" << endl;
1192 //if (listener) listener->callback(doc);
1194 if (frames.empty()) cout << "stack vuoto" << endl;
1195 else cout << "stack non vuoto" << endl;
1199 TPushParser::advance(const TNode& node)
1202 TNode parent = node.parent();
1204 ; // nothing to do, the cursor is not in the document any more
1205 else if (parent.isG())
1207 TNode next = node.next();
1208 if (next) next.insert(cursor);
1209 else parent.append(cursor);
1211 else if (parent.isC())
1217 Frame& frame = frames.top();
1218 if (frame.pos == frame.entry.pattern.size())
1224 parent.append(cursor);
1227 else if (parent.is("math"))
1234 TPushParser::setCursor(const std::string& c)
1237 if (listener) listener->callback(doc);