small parser fixes

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6326 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2003-03-03 17:49:26 +00:00
parent ca4efc12c2
commit b366633564
3 changed files with 95 additions and 25 deletions

View File

@ -989,6 +989,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
else if (t.cs() == "begin") { else if (t.cs() == "begin") {
string const name = getArg('{', '}'); string const name = getArg('{', '}');
skipSpaces();
if (name == "array" || name == "subarray") { if (name == "array" || name == "subarray") {
string const valign = parse_verbatim_option() + 'c'; string const valign = parse_verbatim_option() + 'c';
@ -1001,7 +1002,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
string const valign = parse_verbatim_option() + 'c'; string const valign = parse_verbatim_option() + 'c';
string const halign = parse_verbatim_item(); string const halign = parse_verbatim_item();
cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign))); cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
parse2(cell->back(), FLAG_END, mode, false); parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
} }
else if (name == "split" || name == "cases" || else if (name == "split" || name == "cases" ||

View File

@ -10,6 +10,7 @@
#include <cctype> #include <cctype>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <map>
#include <stack> #include <stack>
#include <string> #include <string>
#include <vector> #include <vector>
@ -28,6 +29,7 @@ using std::ios;
using std::ifstream; using std::ifstream;
using std::istream; using std::istream;
using std::istringstream; using std::istringstream;
using std::map;
using std::ostream; using std::ostream;
using std::ostringstream; using std::ostringstream;
using std::stack; using std::stack;
@ -37,7 +39,7 @@ using std::vector;
namespace { namespace {
void parse(Parser & p, ostream & os, unsigned flags, mode_type mode); void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode);
char const OPEN = '<'; char const OPEN = '<';
char const CLOSE = '>'; char const CLOSE = '>';
@ -135,7 +137,7 @@ string cap(string s)
} }
string const trim(string const & a, char const * p = " ") string const trim(string const & a, char const * p = " \t\n\r")
{ {
// lyx::Assert(p); // lyx::Assert(p);
@ -153,7 +155,7 @@ string const trim(string const & a, char const * p = " ")
} }
void split(string const & s, vector<string> & result, char delim) void split(string const & s, vector<string> & result, char delim = ',')
{ {
istringstream is(s); istringstream is(s);
string t; string t;
@ -162,6 +164,22 @@ void split(string const & s, vector<string> & result, char delim)
} }
// splits "x=z, y=b" into a map
map<string, string> split_map(string const & s)
{
map<string, string> res;
vector<string> v;
split(s, v);
for (size_t i = 0; i < v.size(); ++i) {
size_t const pos = v[i].find('=');
string const index = v[i].substr(0, pos);
string const value = v[i].substr(pos + 1, string::npos);
res[trim(index)] = trim(value);
}
return res;
}
string join(vector<string> const & input, char delim) string join(vector<string> const & input, char delim)
{ {
ostringstream os; ostringstream os;
@ -415,7 +433,7 @@ void end_preamble(ostream & os)
} }
void parse(Parser & p, ostream & os, unsigned flags, mode_type mode) void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode)
{ {
while (p.good()) { while (p.good()) {
Token const & t = p.getToken(); Token const & t = p.getToken();
@ -533,39 +551,39 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
} }
else if (t.cat() == catBegin) { else if (t.cat() == catBegin) {
if (mode == MATH_MODE) if (mode == TEXT_MODE)
os << '{';
else
handle_tex(os, "{"); handle_tex(os, "{");
else
os << '{';
} }
else if (t.cat() == catEnd) { else if (t.cat() == catEnd) {
if (flags & FLAG_BRACE_LAST) if (flags & FLAG_BRACE_LAST)
return; return;
if (mode == MATH_MODE) if (mode == TEXT_MODE)
os << '}';
else
handle_tex(os, "}"); handle_tex(os, "}");
else
os << '}';
} }
else if (t.cat() == catAlign) { else if (t.cat() == catAlign) {
if (mode == MATH_MODE) if (mode == TEXT_MODE)
os << t.character();
else
os << TAB; os << TAB;
else
os << t.character();
} }
else if (t.cs() == "tabularnewline") { else if (t.cs() == "tabularnewline") {
if (mode == MATH_MODE) if (mode == TEXT_MODE)
os << t.asInput();
else
os << LINE; os << LINE;
else
os << t.asInput();
} }
else if (t.cs() == "\\" && mode == MATH_MODE) else if (t.cs() == "\\" && mode == MATH_MODE)
os << t.asInput(); os << t.asInput();
else if (t.cs() == "\\" && curr_env() == "tabular") else if (t.cs() == "\\" && mode == TEXT_MODE && curr_env() == "tabular")
os << LINE; os << LINE;
else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) { else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
@ -612,6 +630,11 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
string const body = p.verbatimItem(); string const body = p.verbatimItem();
// only non-lyxspecific stuff // only non-lyxspecific stuff
if (name != "\\noun " && name != "\\tabularnewline ") { if (name != "\\noun " && name != "\\tabularnewline ") {
ostringstream ss;
ss << '\\' << t.cs() << '{' << name << '}'
<< opts << '{' << body << "}\n";
handle_tex(os, ss.str());
/*
ostream & out = in_preamble ? h_preamble : os; ostream & out = in_preamble ? h_preamble : os;
if (!in_preamble) if (!in_preamble)
begin_inset(os, "FormulaMacro\n"); begin_inset(os, "FormulaMacro\n");
@ -619,6 +642,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
<< opts << "{" << body << "}\n"; << opts << "{" << body << "}\n";
if (!in_preamble) if (!in_preamble)
end_inset(os); end_inset(os);
*/
} }
} }
@ -658,6 +682,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
active_environments.push(name); active_environments.push(name);
if (name == "document") { if (name == "document") {
end_preamble(os); end_preamble(os);
os << "\n\n\\layout Standard\n\n";
parse(p, os, FLAG_END, mode); parse(p, os, FLAG_END, mode);
} else if (name == "abstract") { } else if (name == "abstract") {
handle_par(os); handle_par(os);
@ -669,15 +694,18 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
os << "\\end{" << name << "}"; os << "\\end{" << name << "}";
end_inset(os); end_inset(os);
} else if (name == "tabular") { } else if (name == "tabular") {
if (mode == TEXT_MODE) if (mode == TEXT_MODE)
handle_tabular(p, os, mode); handle_tabular(p, os, mode);
else { else {
os << "\\begin{" << name << "}"; os << "\\begin{" << name << "}";
parse(p, os, FLAG_END, MATHTEXT_MODE); parse(p, os, FLAG_END, MATHTEXT_MODE);
os << "\\end{" << name << "}"; os << "\\end{" << name << "}";
} }
} else if (name == "table") { } else if (name == "table" || name == "figure") {
begin_inset(os, "Float table\n"); string opts = p.getOpt();
begin_inset(os, "Float " + name + "\n");
if (opts.size())
os << "placement " << opts << '\n';
os << "wide false\n" os << "wide false\n"
<< "collapsed false\n" << "collapsed false\n"
<< "\n" << "\n"
@ -688,7 +716,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
p.verbatimItem(); // swallow next arg p.verbatimItem(); // swallow next arg
parse(p, os, FLAG_END, mode); parse(p, os, FLAG_END, mode);
os << "\n\\layout Standard\n\n"; os << "\n\\layout Standard\n\n";
} else if (mode == MATH_MODE) { } else if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
os << "\\begin{" << name << "}"; os << "\\begin{" << name << "}";
parse(p, os, FLAG_END, mode); parse(p, os, FLAG_END, mode);
os << "\\end{" << name << "}"; os << "\\end{" << name << "}";
@ -823,6 +851,19 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
} }
os << "\n\n\\layout " << cap(name) << "\n\n"; os << "\n\n\\layout " << cap(name) << "\n\n";
parse(p, os, FLAG_ITEM, mode); parse(p, os, FLAG_ITEM, mode);
os << "\n\n\\layout Standard\n\n";
}
else if (t.cs() == "includegraphics") {
map<string, string> opts = split_map(p.getArg('[', ']'));
string name = p.verbatimItem();
begin_inset(os, "Graphics ");
os << "\n\tfilename " << name << '\n';
if (opts.find("width") != opts.end())
os << "\twidth " << opts["width"] << '\n';
if (opts.find("height") != opts.end())
os << "\theight " << opts["height"] << '\n';
end_inset(os);
} }
else if (t.cs() == "makeindex" || t.cs() == "maketitle") else if (t.cs() == "makeindex" || t.cs() == "maketitle")
@ -883,6 +924,20 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
else if (t.cs() == "&" && mode == TEXT_MODE) else if (t.cs() == "&" && mode == TEXT_MODE)
os << '&'; os << '&';
else if (t.cs() == "\"") {
string const name = p.verbatimItem();
if (name == "a") os << 'ä';
else if (name == "o") os << 'ö';
else if (name == "u") os << 'ü';
else if (name == "A") os << 'Ä';
else if (name == "O") os << 'Ö';
else if (name == "U") os << 'Ü';
else handle_ert(os, "\"{" + name + "}");
}
else if (t.cs() == "ss")
os << "ß";
else if (t.cs() == "input") else if (t.cs() == "input")
handle_tex(os, "\\input{" + p.verbatimItem() + "}\n"); handle_tex(os, "\\input{" + p.verbatimItem() + "}\n");
@ -898,8 +953,11 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
} }
else { else {
if (mode == MATH_MODE) //cerr << "#: " << t << " mode: " << mode << endl;
if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
os << t.asInput(); os << t.asInput();
//cerr << "#: writing: '" << t.asInput() << "'\n";
}
else if (in_preamble) else if (in_preamble)
h_preamble << t.asInput(); h_preamble << t.asInput();
else { else {

View File

@ -49,6 +49,9 @@ void catInit()
theCatcode[13] = catIgnore; theCatcode[13] = catIgnore;
theCatcode['~'] = catActive; theCatcode['~'] = catActive;
theCatcode['%'] = catComment; theCatcode['%'] = catComment;
// This is wrong!
theCatcode['@'] = catLetter;
} }
} }
@ -149,8 +152,15 @@ Token const & Parser::getToken()
void Parser::skipSpaces() void Parser::skipSpaces()
{ {
while (nextToken().cat() == catSpace || nextToken().cat() == catNewline) while (1) {
getToken(); if (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
getToken();
else if (nextToken().cat() == catComment)
while (nextToken().cat() != catNewline)
getToken();
else
break;
}
} }
@ -312,6 +322,7 @@ string Parser::verbatimItem()
{ {
if (!good()) if (!good())
error("stream bad"); error("stream bad");
skipSpaces();
if (nextToken().cat() == catBegin) { if (nextToken().cat() == catBegin) {
Token t = getToken(); // skip brace Token t = getToken(); // skip brace
string res; string res;