]> git.lyx.org Git - lyx.git/blob - src/Lexer.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / Lexer.cpp
1 /**
2  * \file Lexer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Lexer.h"
17
18 #include "debug.h"
19
20 #include "support/convert.h"
21 #include "support/filetools.h"
22 #include "support/gzstream.h"
23 #include "support/lstrings.h"
24 #include "support/lyxalgo.h"
25 #include "support/types.h"
26 #include "support/unicode.h"
27
28 #include <boost/utility.hpp>
29
30 #include <functional>
31 #include <istream>
32 #include <stack>
33 #include <sstream>
34 #include <vector>
35
36
37 namespace lyx {
38
39 using support::compare_ascii_no_case;
40 using support::FileName;
41 using support::getFormatFromContents;
42 using support::isStrDbl;
43 using support::isStrInt;
44 using support::ltrim;
45 using support::makeDisplayPath;
46 using support::prefixIs;
47 using support::split;
48 using support::subst;
49 using support::trim;
50
51 using std::endl;
52 using std::getline;
53 using std::lower_bound;
54 using std::sort;
55 using std::string;
56 using std::ios;
57 using std::istream;
58 using std::ostream;
59
60
61 //////////////////////////////////////////////////////////////////////
62 //
63 // Lexer::Pimpl
64 //
65 //////////////////////////////////////////////////////////////////////
66
67
68 ///
69 class Lexer::Pimpl : boost::noncopyable {
70 public:
71         ///
72         Pimpl(keyword_item * tab, int num);
73         ///
74         std::string const getString() const;
75         ///
76         docstring const getDocString() const;
77         ///
78         void printError(std::string const & message) const;
79         ///
80         void printTable(std::ostream & os);
81         ///
82         void pushTable(keyword_item * tab, int num);
83         ///
84         void popTable();
85         ///
86         bool setFile(support::FileName const & filename);
87         ///
88         void setStream(std::istream & i);
89         ///
90         void setCommentChar(char c);
91         ///
92         bool next(bool esc = false);
93         ///
94         int search_kw(char const * const tag) const;
95         ///
96         int lex();
97         ///
98         bool eatLine();
99         ///
100         bool nextToken();
101         /// test if there is a pushed token or the stream is ok
102         bool inputAvailable();
103         ///
104         void pushToken(std::string const &);
105         /// fb_ is only used to open files, the stream is accessed through is.
106         std::filebuf fb_;
107
108         /// gz_ is only used to open files, the stream is accessed through is.
109         gz::gzstreambuf gz_;
110
111         /// the stream that we use.
112         std::istream is;
113         ///
114         std::string name;
115         ///
116         keyword_item * table;
117         ///
118         int no_items;
119         ///
120         std::string buff;
121         ///
122         int status;
123         ///
124         int lineno;
125         ///
126         std::string pushTok;
127         ///
128         char commentChar;
129 private:
130         ///
131         void verifyTable();
132         ///
133         class pushed_table {
134         public:
135                 ///
136                 pushed_table()
137                         : table_elem(0), table_siz(0) {}
138                 ///
139                 pushed_table(keyword_item * ki, int siz)
140                         : table_elem(ki), table_siz(siz) {}
141                 ///
142                 keyword_item * table_elem;
143                 ///
144                 int table_siz;
145         };
146         ///
147         std::stack<pushed_table> pushed;
148 };
149
150
151
152 namespace {
153
154 class compare_tags
155         : public std::binary_function<keyword_item, keyword_item, bool> {
156 public:
157         // used by lower_bound, sort and sorted
158         bool operator()(keyword_item const & a, keyword_item const & b) const
159         {
160                 // we use the ascii version, because in turkish, 'i'
161                 // is not the lowercase version of 'I', and thus
162                 // turkish locale breaks parsing of tags.
163                 return compare_ascii_no_case(a.tag, b.tag) < 0;
164         }
165 };
166
167 } // end of anon namespace
168
169
170 Lexer::Pimpl::Pimpl(keyword_item * tab, int num)
171         : is(&fb_), table(tab), no_items(num),
172           status(0), lineno(0), commentChar('#')
173 {
174         verifyTable();
175 }
176
177
178 string const Lexer::Pimpl::getString() const
179 {
180         return buff;
181 }
182
183
184 docstring const Lexer::Pimpl::getDocString() const
185 {
186         return from_utf8(buff);
187 }
188
189
190 void Lexer::Pimpl::printError(string const & message) const
191 {
192         string const tmpmsg = subst(message, "$$Token", getString());
193         lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
194                 << " of file " << to_utf8(makeDisplayPath(name)) << ']' << endl;
195 }
196
197
198 void Lexer::Pimpl::printTable(ostream & os)
199 {
200         os << "\nNumber of tags: " << no_items << endl;
201         for (int i= 0; i < no_items; ++i)
202                 os << "table[" << i
203                    << "]:  tag: `" << table[i].tag
204                    << "'  code:" << table[i].code << '\n';
205         os.flush();
206 }
207
208
209 void Lexer::Pimpl::verifyTable()
210 {
211         // Check if the table is sorted and if not, sort it.
212         if (table
213             && !lyx::sorted(table, table + no_items, compare_tags())) {
214                 lyxerr << "The table passed to Lexer is not sorted!\n"
215                        << "Tell the developers to fix it!" << endl;
216                 // We sort it anyway to avoid problems.
217                 lyxerr << "\nUnsorted:" << endl;
218                 printTable(lyxerr);
219
220                 sort(table, table + no_items, compare_tags());
221                 lyxerr << "\nSorted:" << endl;
222                 printTable(lyxerr);
223         }
224 }
225
226
227 void Lexer::Pimpl::pushTable(keyword_item * tab, int num)
228 {
229         pushed_table tmppu(table, no_items);
230         pushed.push(tmppu);
231
232         table = tab;
233         no_items = num;
234
235         verifyTable();
236 }
237
238
239 void Lexer::Pimpl::popTable()
240 {
241         if (pushed.empty()) {
242                 lyxerr << "Lexer error: nothing to pop!" << endl;
243                 return;
244         }
245
246         pushed_table tmp = pushed.top();
247         pushed.pop();
248         table = tmp.table_elem;
249         no_items = tmp.table_siz;
250 }
251
252
253 bool Lexer::Pimpl::setFile(FileName const & filename)
254 {
255         // Check the format of the file.
256         string const format = getFormatFromContents(filename);
257
258         if (format == "gzip" || format == "zip" || format == "compress") {
259                 LYXERR(Debug::LYXLEX) << "lyxlex: compressed" << endl;
260                 // The check only outputs a debug message, because it triggers
261                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
262                 // a fresh new filebuf.  (JMarc)
263                 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
264                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
265                                 "file or stream already set." << endl;
266                 gz_.open(filename.toFilesystemEncoding().c_str(), ios::in);
267                 is.rdbuf(&gz_);
268                 name = filename.absFilename();
269                 lineno = 0;
270                 return gz_.is_open() && is.good();
271         } else {
272                 LYXERR(Debug::LYXLEX) << "lyxlex: UNcompressed" << endl;
273
274                 // The check only outputs a debug message, because it triggers
275                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
276                 // a fresh new filebuf.  (JMarc)
277                 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
278                         LYXERR(Debug::LYXLEX) << "Error in Lexer::setFile: "
279                                 "file or stream already set." << endl;
280                 fb_.open(filename.toFilesystemEncoding().c_str(), ios::in);
281                 is.rdbuf(&fb_);
282                 name = filename.absFilename();
283                 lineno = 0;
284                 return fb_.is_open() && is.good();
285         }
286 }
287
288
289 void Lexer::Pimpl::setStream(istream & i)
290 {
291         if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
292                 LYXERR(Debug::LYXLEX)  << "Error in Lexer::setStream: "
293                         "file or stream already set." << endl;
294         is.rdbuf(i.rdbuf());
295         lineno = 0;
296 }
297
298
299 void Lexer::Pimpl::setCommentChar(char c)
300 {
301         commentChar = c;
302 }
303
304
305 bool Lexer::Pimpl::next(bool esc /* = false */)
306 {
307         if (!pushTok.empty()) {
308                 // There can have been a whole line pushed so
309                 // we extract the first word and leaves the rest
310                 // in pushTok. (Lgb)
311                 if (pushTok[0] == '\\' && pushTok.find(' ') != string::npos) {
312                         buff.clear();
313                         pushTok = split(pushTok, buff, ' ');
314                 } else {
315                         buff = pushTok;
316                         pushTok.clear();
317                 }
318                 status = LEX_TOKEN;
319                 return true;
320         }
321         if (!esc) {
322                 unsigned char c = 0; // getc() returns an int
323                 char cc = 0;
324                 status = 0;
325                 while (is && !status) {
326                         is.get(cc);
327                         c = cc;
328                         if (c == commentChar) {
329                                 // Read rest of line (fast :-)
330 #if 1
331                                 // That is not fast... (Lgb)
332                                 string dummy;
333                                 getline(is, dummy);
334
335                                 LYXERR(Debug::LYXLEX) << "Comment read: `" << c
336                                                       << dummy << '\'' << endl;
337 #else
338                                 // unfortunately ignore is buggy (Lgb)
339                                 is.ignore(100, '\n');
340 #endif
341                                 ++lineno;
342                                 continue;
343                         }
344
345                         if (c == '\"') {
346                                 buff.clear();
347
348                                 do {
349                                         is.get(cc);
350                                         c = cc;
351                                         if (c != '\r')
352                                                 buff.push_back(c);
353                                 } while (c != '\"' && c != '\n' && is);
354
355                                 if (c != '\"') {
356                                         printError("Missing quote");
357                                         if (c == '\n')
358                                                 ++lineno;
359                                 }
360
361                                 buff.resize(buff.size()-1);
362                                 status = LEX_DATA;
363                                 break;
364                         }
365
366                         if (c == ',')
367                                 continue;              /* Skip ','s */
368
369                                 // using relational operators with chars other
370                                 // than == and != is not safe. And if it is done
371                                 // the type _have_ to be unsigned. It usually a
372                                 // lot better to use the functions from cctype
373                         if (c > ' ' && is)  {
374                                 buff.clear();
375
376                                 do {
377                                         buff.push_back(c);
378                                         is.get(cc);
379                                         c = cc;
380                                 } while (c > ' ' && c != ',' && is);
381
382                                 status = LEX_TOKEN;
383                         }
384
385                         if (c == '\r' && is) {
386                                 // The Windows support has lead to the
387                                 // possibility of "\r\n" at the end of
388                                 // a line.  This will stop LyX choking
389                                 // when it expected to find a '\n'
390                                 is.get(cc);
391                                 c = cc;
392                         }
393
394                         if (c == '\n')
395                                 ++lineno;
396
397                 }
398                 if (status)
399                         return true;
400
401                 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
402                 buff.clear();
403                 return false;
404         } else {
405                 unsigned char c = 0; // getc() returns an int
406                 char cc = 0;
407
408                 status = 0;
409                 while (is && !status) {
410                         is.get(cc);
411                         c = cc;
412
413                         // skip ','s
414                         if (c == ',')
415                                 continue;
416
417                         if (c == commentChar) {
418                                 // Read rest of line (fast :-)
419 #if 1
420                                 // That is still not fast... (Lgb)
421                                 string dummy;
422                                 getline(is, dummy);
423
424                                 LYXERR(Debug::LYXLEX) << "Comment read: `" << c
425                                                       << dummy << '\'' << endl;
426 #else
427                                 // but ignore is also still buggy (Lgb)
428                                 // This is fast (Lgb)
429                                 is.ignore(100, '\n');
430 #endif
431                                 ++lineno;
432                                 continue;
433                         }
434
435                         // string
436                         if (c == '\"') {
437                                 buff.clear();
438
439                                 bool escaped = false;
440                                 do {
441                                         escaped = false;
442                                         is.get(cc);
443                                         c = cc;
444                                         if (c == '\r') continue;
445                                         if (c == '\\') {
446                                                 // escape the next char
447                                                 is.get(cc);
448                                                 c = cc;
449                                                 if (c == '\"' || c == '\\')
450                                                         escaped = true;
451                                                 else
452                                                         buff.push_back('\\');
453                                         }
454                                         buff.push_back(c);
455
456                                         if (!escaped && c == '\"')
457                                                 break;
458                                 } while (c != '\n' && is);
459
460                                 if (c != '\"') {
461                                         printError("Missing quote");
462                                         if (c == '\n')
463                                                 ++lineno;
464                                 }
465
466                                 buff.resize(buff.size() -1);
467                                 status = LEX_DATA;
468                                 break;
469                         }
470
471                         if (c > ' ' && is) {
472                                 buff.clear();
473
474                                 do {
475                                         if (c == '\\') {
476                                                 // escape the next char
477                                                 is.get(cc);
478                                                 c = cc;
479                                                 //escaped = true;
480                                         }
481                                         buff.push_back(c);
482                                         is.get(cc);
483                                         c = cc;
484                                 } while (c > ' ' && c != ',' && is);
485
486                                 status = LEX_TOKEN;
487                         }
488                         // new line
489                         if (c == '\n')
490                                 ++lineno;
491                 }
492
493                 if (status)
494                         return true;
495
496                 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
497                 buff.clear();
498                 return false;
499         }
500 }
501
502
503 int Lexer::Pimpl::search_kw(char const * const tag) const
504 {
505         keyword_item search_tag = { tag, 0 };
506         keyword_item * res =
507                 lower_bound(table, table + no_items,
508                             search_tag, compare_tags());
509         // use the compare_ascii_no_case instead of compare_no_case,
510         // because in turkish, 'i' is not the lowercase version of 'I',
511         // and thus turkish locale breaks parsing of tags.
512         if (res != table + no_items
513             && !compare_ascii_no_case(res->tag, tag))
514                 return res->code;
515         return LEX_UNDEF;
516 }
517
518
519 int Lexer::Pimpl::lex()
520 {
521         //NOTE: possible bug.
522         if (next() && status == LEX_TOKEN)
523                 return search_kw(getString().c_str());
524         return status;
525 }
526
527
528 bool Lexer::Pimpl::eatLine()
529 {
530         buff.clear();
531
532         unsigned char c = '\0';
533         char cc = 0;
534         while (is && c != '\n') {
535                 is.get(cc);
536                 c = cc;
537                 //LYXERR(Debug::LYXLEX) << "Lexer::EatLine read char: `"
538                 //                    << c << '\'' << endl;
539                 if (c != '\r')
540                         buff.push_back(c);
541         }
542
543         if (c == '\n') {
544                 ++lineno;
545                 buff.resize(buff.size() - 1);
546                 status = LEX_DATA;
547                 return true;
548         } else if (buff.length() > 0) { // last line
549                 status = LEX_DATA;
550                 return true;
551         } else {
552                 return false;
553         }
554 }
555
556
557 bool Lexer::Pimpl::nextToken()
558 {
559         if (!pushTok.empty()) {
560                 // There can have been a whole line pushed so
561                 // we extract the first word and leaves the rest
562                 // in pushTok. (Lgb)
563                 if (pushTok[0] == '\\' && pushTok.find(' ') != string::npos) {
564                         buff.clear();
565                         pushTok = split(pushTok, buff, ' ');
566                 } else {
567                         buff = pushTok;
568                         pushTok.clear();
569                 }
570                 status = LEX_TOKEN;
571                 return true;
572         }
573
574         status = 0;
575         while (is && !status) {
576                 unsigned char c = 0;
577                 char cc = 0;
578                 is.get(cc);
579                 c = cc;
580                 if (c >= ' ' && is) {
581                         buff.clear();
582
583                         if (c == '\\') { // first char == '\\'
584                                 do {
585                                         buff.push_back(c);
586                                         is.get(cc);
587                                         c = cc;
588                                 } while (c > ' ' && c != '\\' && is);
589                         } else {
590                                 do {
591                                         buff.push_back(c);
592                                         is.get(cc);
593                                         c = cc;
594                                 } while (c >= ' ' && c != '\\' && is);
595                         }
596
597                         if (c == '\\') is.putback(c); // put it back
598                         status = LEX_TOKEN;
599                 }
600
601                 if (c == '\n')
602                         ++lineno;
603
604         }
605         if (status)
606                 return true;
607
608         status = is.eof() ? LEX_FEOF: LEX_UNDEF;
609         buff.clear();
610         return false;
611 }
612
613
614 bool Lexer::Pimpl::inputAvailable()
615 {
616         return is.good();
617 }
618
619
620 void Lexer::Pimpl::pushToken(string const & pt)
621 {
622         pushTok = pt;
623 }
624
625
626
627
628 //////////////////////////////////////////////////////////////////////
629 //
630 // Lexer
631 //
632 //////////////////////////////////////////////////////////////////////
633
634 Lexer::Lexer(keyword_item * tab, int num)
635         : pimpl_(new Pimpl(tab, num))
636 {}
637
638
639 Lexer::~Lexer()
640 {
641         delete pimpl_;
642 }
643
644
645 bool Lexer::isOK() const
646 {
647         return pimpl_->inputAvailable();
648 }
649
650
651 void Lexer::setLineNo(int l)
652 {
653         pimpl_->lineno = l;
654 }
655
656
657 int Lexer::getLineNo() const
658 {
659         return pimpl_->lineno;
660 }
661
662
663 istream & Lexer::getStream()
664 {
665         return pimpl_->is;
666 }
667
668
669 void Lexer::pushTable(keyword_item * tab, int num)
670 {
671         pimpl_->pushTable(tab, num);
672 }
673
674
675 void Lexer::popTable()
676 {
677         pimpl_->popTable();
678 }
679
680
681 void Lexer::printTable(ostream & os)
682 {
683         pimpl_->printTable(os);
684 }
685
686
687 void Lexer::printError(string const & message) const
688 {
689         pimpl_->printError(message);
690 }
691
692
693 bool Lexer::setFile(support::FileName const & filename)
694 {
695         return pimpl_->setFile(filename);
696 }
697
698
699 void Lexer::setStream(istream & i)
700 {
701         pimpl_->setStream(i);
702 }
703
704
705 void Lexer::setCommentChar(char c)
706 {
707         pimpl_->setCommentChar(c);
708 }
709
710 int Lexer::lex()
711 {
712         return pimpl_->lex();
713 }
714
715
716 int Lexer::getInteger() const
717 {
718         lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
719         if (!lastReadOk_) {
720                 pimpl_->printError("integer token missing");
721                 return -1;
722         }
723
724         if (isStrInt(pimpl_->getString()))
725                 return convert<int>(pimpl_->getString());
726
727         lastReadOk_ = false;
728         pimpl_->printError("Bad integer `$$Token'");
729         return -1;
730 }
731
732
733 double Lexer::getFloat() const
734 {
735         // replace comma with dot in case the file was written with
736         // the wrong locale (should be rare, but is easy enough to
737         // avoid).
738         lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
739         if (!lastReadOk_) {
740                 pimpl_->printError("float token missing");
741                 return -1;
742         }
743
744         string const str = subst(pimpl_->getString(), ",", ".");
745         if (isStrDbl(str))
746                 return convert<double>(str);
747
748         lastReadOk_ = false;
749         pimpl_->printError("Bad float `$$Token'");
750         return -1;
751 }
752
753
754 string const Lexer::getString() const
755 {
756         lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
757
758         if (lastReadOk_)
759         return pimpl_->getString();
760
761         return string();
762 }
763
764
765 docstring const Lexer::getDocString() const
766 {
767         lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
768
769         if (lastReadOk_)
770                 return pimpl_->getDocString();
771
772         return docstring();
773 }
774
775
776 // I would prefer to give a tag number instead of an explicit token
777 // here, but it is not possible because Buffer::readDocument uses
778 // explicit tokens (JMarc)
779 string const Lexer::getLongString(string const & endtoken)
780 {
781         string str, prefix;
782         bool firstline = true;
783
784         while (pimpl_->is) { //< eatLine only reads from is, not from pushTok
785                 if (!eatLine())
786                         // blank line in the file being read
787                         continue;
788
789                 string const token = trim(getString(), " \t");
790
791                 LYXERR(Debug::PARSER) << "LongString: `"
792                                       << getString() << '\'' << endl;
793
794                 // We do a case independent comparison, like search_kw does.
795                 if (compare_ascii_no_case(token, endtoken) == 0)
796                         break;
797
798                 string tmpstr = getString();
799                 if (firstline) {
800                         string::size_type i(tmpstr.find_first_not_of(' '));
801                         if (i != string::npos)
802                                 prefix = tmpstr.substr(0, i);
803                         firstline = false;
804                         LYXERR(Debug::PARSER)
805                                 << "Prefix = `" << prefix << "\'" << endl;
806                 }
807
808                 // further lines in long strings may have the same
809                 // whitespace prefix as the first line. Remove it.
810                 if (prefix.length() && prefixIs(tmpstr, prefix)) {
811                         tmpstr.erase(0, prefix.length() - 1);
812                 }
813
814                 str += ltrim(tmpstr, "\t") + '\n';
815         }
816
817         if (!pimpl_->is) {
818                 printError("Long string not ended by `" + endtoken + '\'');
819         }
820
821         return str;
822 }
823
824
825 bool Lexer::getBool() const
826 {
827         if (pimpl_->getString() == "true") {
828                 lastReadOk_ = true;
829                 return true;
830         } else if (pimpl_->getString() != "false") {
831                 pimpl_->printError("Bad boolean `$$Token'. "
832                                    "Use \"false\" or \"true\"");
833                 lastReadOk_ = false;
834         }
835         lastReadOk_ = true;
836         return false;
837 }
838
839
840 bool Lexer::eatLine()
841 {
842         return pimpl_->eatLine();
843 }
844
845
846 bool Lexer::next(bool esc)
847 {
848         return pimpl_->next(esc);
849 }
850
851
852 bool Lexer::nextToken()
853 {
854         return pimpl_->nextToken();
855 }
856
857
858 void Lexer::pushToken(string const & pt)
859 {
860         pimpl_->pushToken(pt);
861 }
862
863
864 Lexer::operator void const *() const
865 {
866         // This behaviour is NOT the same as the std::streams which would
867         // use fail() here. However, our implementation of getString() et al.
868         // can cause the eof() and fail() bits to be set, even though we
869         // haven't tried to read 'em.
870         return lastReadOk_? this : 0;
871 }
872
873
874 bool Lexer::operator!() const
875 {
876         return !lastReadOk_;
877 }
878
879
880 Lexer & Lexer::operator>>(std::string & s)
881 {
882         if (isOK()) {
883                 next();
884                 s = getString();
885         } else {
886                 lastReadOk_ = false;
887         }
888         return *this;
889 }
890
891
892 Lexer & Lexer::operator>>(docstring & s)
893 {
894         if (isOK()) {
895                 next();
896                 s = getDocString();
897         } else {
898                 lastReadOk_ = false;
899         }
900         return *this;
901 }
902
903
904 Lexer & Lexer::operator>>(double & s)
905 {
906         if (isOK()) {
907                 next();
908                 s = getFloat();
909         } else {
910                 lastReadOk_ = false;
911         }
912         return *this;
913 }
914
915
916 Lexer & Lexer::operator>>(int & s)
917 {
918         if (isOK()) {
919                 next();
920                 s = getInteger();
921         } else {
922                 lastReadOk_ = false;
923         }
924         return *this;
925 }
926
927
928 Lexer & Lexer::operator>>(unsigned int & s)
929 {
930         if (isOK()) {
931                 next();
932                 s = getInteger();
933         } else {
934                 lastReadOk_ = false;
935         }
936         return *this;
937 }
938
939
940 Lexer & Lexer::operator>>(bool & s)
941 {
942         if (isOK()) {
943                 next();
944                 s = getBool();
945         } else {
946                 lastReadOk_ = false;
947         }
948         return *this;
949 }
950
951
952 /// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog
953 string const Lexer::quoteString(string const & arg)
954 {
955         std::ostringstream os;
956         os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"';
957         return os.str();
958 }
959
960
961 } // namespace lyx