parse_vrml.C

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright  2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #include <archon/util/parse_values.H>
00021 #include <archon/util/unicode.H>
00022 #include <archon/util/lexer.H>
00023 #include <archon/util/lr_parser_simple.H>
00024 #include <archon/util/lr_parser_methods.H>
00025 
00026 #include <archon/x3d/server/field_type.H>
00027 #include <archon/x3d/server/parse_vrml.H>
00028 
00029 namespace Archon
00030 {
00031   namespace X3D
00032   {
00033     namespace VRML
00034     {
00060       struct Parser
00061       {
00062         struct Context: Lexer::Context, LrParserBase::Context, X3D::Parser
00063         {
00064           Lexer *lexer;
00065           Uri baseUri;
00066           ProgressTracker *progressTracker;
00067 
00068           string printQuote(ustring s)
00069           {
00070             return Unicode::encodeUtf8(Text::escapeNonprintable(s));
00071           }
00072 
00073           string printQuote(string utf8encoded)
00074           {
00075             return printQuote(Unicode::decodeUtf8(utf8encoded));
00076           }
00077 
00078           void warning(string m)
00079           {
00080             if(logger)
00081               logger->log(baseUri.getFile() + ":" +
00082                           Text::toString(lexer->getLineNumber()) + ": " + m);
00083           }
00084 
00085           void fieldValueWarning(string);
00086 
00087 
00088           // Lexer actions
00089 
00090           void lexerError()
00091           {
00092             warning("Syntax error. Unexpected character " +
00093                     printQuote(lexer->getText()) + " (ignored).");
00094           }
00095 
00096           ustring lexer_string()
00097           {
00098             const ustring lexeme = lexer->getText();
00099             ustring s;
00100             s.reserve(lexeme.size());
00101             bool esc = false;
00102             for(unsigned i=1; i<lexeme.size()-1; ++i)
00103             {
00104               const uchar &c = lexeme[i];
00105               if(esc||lexeme[i] != '\\')
00106               {
00107                 s+=c;
00108                 esc = false;
00109               }
00110               else esc = true;
00111             }
00112             return s;
00113           }
00114 
00120           int lexer_decInt()
00121           {
00122             const ustring lexeme = lexer->getText();
00123             bool neg = false;
00124             ustring::const_iterator i = lexeme.begin();
00125             if(*i == '+') ++i;
00126             else if(*i == '-') ++i, neg = true;
00127             unsigned val = 0;
00128             do
00129             {
00130               val *= 10;
00131               val += *i - '0';
00132               ++i;
00133             }
00134             while(i != lexeme.end());
00135             const int ival = *static_cast<int *>(&val);
00136             return neg ? -ival : ival;
00137           }
00138 
00144           int lexer_hexInt()
00145           {
00146             const ustring lexeme = lexer->getText();
00147             bool neg = false;
00148             ustring::const_iterator i = lexeme.begin();
00149             if(*i == '+') ++i;
00150             else if(*i == '-') ++i, neg = true;
00151             i+=2;
00152             unsigned val = 0;
00153             do
00154             {
00155               val *= 16;
00156               val += *i - (*i >= 'a' ? 'a' : *i >= 'A' ? 'A' : '0');
00157               ++i;
00158             }
00159             while(i != lexeme.end());
00160             const int ival = *static_cast<int *>(&val);
00161             return neg ? -ival : ival;
00162           }
00163 
00164           double lexer_float()
00165           {
00166             const string s = Unicode::encodeUtf8(lexer->getText());
00167             return ParseValues::parseDouble(s.c_str());
00168           }
00169 
00170           ustring lexer_id()
00171           {
00172             return lexer->getText();
00173           }
00174 
00175 
00176           // Parser actions
00177 
00178           void parserError()
00179           {
00180             if(lexer->getType() < 0)
00181               warning("Syntax error. Unexpected end of input.");
00182             else
00183               warning("Syntax error. Unexpected symbol " +
00184                       printQuote(lexer->getText()) + ".");
00185           }
00186 
00187           string typeName(RefAny v)
00188           {
00189             return
00190               dynamic_cast<const RefObject<bool> *  >(v.get()) ? "boolean value" :
00191               dynamic_cast<const RefObject<int> *   >(v.get()) ? "integer value" :
00192               dynamic_cast<const RefObject<double> *>(v.get()) ? "floating point value" :
00193               dynamic_cast<const RefObject<string> *>(v.get()) ? "string value" :
00194               dynamic_cast<const Node *             >(v.get()) ? "node value" : "<unknown>";
00195           }
00196 
00197           bool extract(const RefAny &s, bool &t)
00198           {
00199             if(const RefObject<bool> *v =
00200                dynamic_cast<const RefObject<bool> *>(s.get()))
00201               t = v->value;
00202             else
00203             {
00204               fieldValueWarning("Need boolean value, found " + typeName(s));
00205               return false;
00206             }
00207             return true;
00208           }
00209 
00210           bool extract(const RefAny &s, string &t)
00211           {
00212             if(const RefObject<ustring> *v =
00213                dynamic_cast<const RefObject<ustring> *>(s.get()))
00214               t = Unicode::encodeUtf8(v->value);
00215             else
00216             {
00217               t = "";
00218               return false;
00219             }
00220             return true;
00221           }
00222 
00223           bool extract(const RefAny &s, int &t)
00224           {
00225             if(const RefObject<int> *v =
00226                dynamic_cast<const RefObject<int> *>(s.get()))
00227               t = v->value;
00228             else if(const RefObject<double> *v =
00229                     dynamic_cast<const RefObject<double> *>(s.get()))
00230             {
00231               t = (int)round(v->value);
00232               fieldValueWarning("Need integer value, found " + typeName(s) + " (rounded)");
00233             }
00234             else
00235             {
00236               fieldValueWarning("Need integer value, found " + typeName(s));
00237               return false;
00238             }
00239             return true;
00240           }
00241 
00242           bool extract(const RefAny &s, double &t)
00243           {
00244             if(const RefObject<int> *v =
00245                dynamic_cast<const RefObject<int> *>(s.get()))
00246               t = v->value;
00247             else if(const RefObject<double> *v =
00248                     dynamic_cast<const RefObject<double> *>(s.get()))
00249               t = v->value;
00250             else
00251             {
00252               fieldValueWarning("Need floating point value, found " + typeName(s));
00253               return false;
00254             }
00255             return true;
00256           }
00257 
00263           bool extract(const RefAny &s, Uri &t)
00264           {
00265             if(const RefObject<ustring> *v =
00266                dynamic_cast<const RefObject<ustring> *>(s.get()))
00267             {
00268               try
00269               {
00270                 t = Uri(Unicode::encodeUtf8(v->value), baseUri);
00271               }
00272               catch(Uri::SyntaxException &e)
00273               {
00274                 fieldValueWarning(Uri::explain(e));
00275                 return false;
00276               }
00277             }
00278             else
00279             {
00280               fieldValueWarning("Need string value, found " + typeName(s));
00281               return false;
00282             }
00283             return true;
00284           }
00285 
00286           bool extract(const RefAny &s, Time &t)
00287           {
00288             if(const RefObject<int> *v =
00289                dynamic_cast<const RefObject<int> *>(s.get()))
00290               t = Time((double)v->value);
00291             else if(const RefObject<double> *v =
00292                     dynamic_cast<const RefObject<double> *>(s.get()))
00293               t = Time(v->value);
00294             else
00295             {
00296               fieldValueWarning("Need floating point value, found " + typeName(s));
00297               return false;
00298             }
00299             return true;
00300           }
00301 
00302           bool extract(const RefAny &s, Ref<Node> &t)
00303           {
00304             if(!s)
00305               t = 0;
00306             else if(const Node *v =
00307                     dynamic_cast<const Node *>(s.get()))
00308               t = const_cast<Node *>(v);
00309             else
00310             {
00311               fieldValueWarning("Need node value, found " + typeName(s));
00312               return false;
00313             }
00314             return true;
00315           }
00316 
00317           struct FieldAssemblerBase
00318           {
00324             virtual bool add(Context *, RefAny) = 0;
00325 
00326             /*
00327              * \return 0 if the composite value is invalid, otherwise a
00328              * pointer to the value.
00329              */
00330             virtual void *ref(Context *) = 0;
00331           };
00332 
00336           template<typename T>
00337           struct FieldAssembler: FieldAssemblerBase
00338           {
00339             T value;
00340             int pos;
00341             bool invalid;
00342             bool excess;
00343 
00344             FieldAssembler(): pos(0), invalid(false), excess(false) {}
00345 
00346             bool add(Context *c, const RefAny &v)
00347             {
00348               if(pos == 1);
00349               {
00350                 if(!excess)
00351                   c->fieldValueWarning("Too many components. (excess ignored)");
00352                 excess = true;
00353                 return true;
00354               }
00355               if(!c->extract(v, value)) invalid = true;
00356               ++pos;
00357               return pos == 1;
00358             }
00359 
00360             void *ref(Context *c)
00361             {
00362               if(invalid) return 0;
00363               if(pos == 1) return &value;
00364               c->fieldValueWarning("Components missing (value ignored)");
00365               return 0;
00366             }
00367           };
00368 
00369           struct FieldAssembler<Vector2>: FieldAssemblerBase
00370           {
00371             Vector2 value;
00372             int pos;
00373             bool invalid;
00374             bool excess;
00375 
00376             FieldAssembler(): pos(0), invalid(false), excess(false) {}
00377 
00378             bool add(Context *c, const RefAny &v)
00379             {
00380               if(pos == 2)
00381               {
00382                 if(!excess)
00383                   c->fieldValueWarning("Too many components. (excess ignored)");
00384                 excess = true;
00385                 return true;
00386               }
00387               if(!c->extract(v, pos == 0 ? value.x : value.y)) invalid = true;
00388               ++pos;
00389               return pos == 2;
00390             }
00391 
00392             void *ref(Context *c)
00393             {
00394               if(invalid) return 0;
00395               if(pos == 2) return &value;
00396               c->fieldValueWarning("Components missing (vector ignored)");
00397               return 0;
00398             }
00399           };
00400 
00401           struct FieldAssembler<Vector3>: FieldAssemblerBase
00402           {
00403             Vector3 value;
00404             int pos;
00405             bool invalid;
00406             bool excess;
00407 
00408             FieldAssembler(): pos(0), invalid(false), excess(false) {}
00409 
00410             bool add(Context *c, const RefAny &v)
00411             {
00412               if(pos == 3)
00413               {
00414                 if(!excess)
00415                   c->fieldValueWarning("Too many components. (excess ignored)");
00416                 excess = true;
00417                 return true;
00418               }
00419               if(!c->extract(v, pos == 0 ? value.x : pos == 1 ? value.y : value.z))
00420                 invalid = true;
00421               ++pos;
00422               return pos == 3;
00423             }
00424 
00425             void *ref(Context *c)
00426             {
00427               if(invalid) return 0;
00428               if(pos == 3) return &value;
00429               c->fieldValueWarning("Components missing (vector ignored)");
00430               return 0;
00431             }
00432           };
00433 
00434           struct FieldAssembler<AxisRot>: FieldAssemblerBase
00435           {
00436             AxisRot value;
00437             int pos;
00438             bool invalid;
00439             bool excess;
00440 
00441             FieldAssembler(): pos(0), invalid(false), excess(false) {}
00442 
00443             bool add(Context *c, const RefAny &v)
00444             {
00445               if(pos == 4)
00446               {
00447                 if(!excess)
00448                   c->fieldValueWarning("Too many components. (excess ignored)");
00449                 excess = true;
00450                 return true;
00451               }
00452               if(!c->extract(v,
00453                              pos == 0 ? value.normalizedAxis.x :
00454                              pos == 1 ? value.normalizedAxis.y :
00455                              pos == 2 ? value.normalizedAxis.z :
00456                              value.angle))
00457                 invalid = true;
00458               ++pos;
00459               return pos == 4;
00460             }
00461 
00462             void *ref(Context *c)
00463             {
00464               if(invalid) return 0;
00465               if(pos == 4) return &value;
00466               c->fieldValueWarning("Components missing (rotation ignored)");
00467               return 0;
00468             }
00469           };
00470 
00471           struct FieldAssembler<Image>: FieldAssemblerBase
00472           {
00473             int width;
00474             int height;
00475             int components;
00476             Image value;
00477             long pos;
00478             bool invalid;
00479             bool excess;
00480             long size;
00481 
00482             FieldAssembler(): pos(-3), invalid(false), excess(false) {}
00483             virtual ~FieldAssembler() {}
00484 
00485             int add(Context *c, const RefAny &v)
00486             {
00487               if(pos == -3)
00488               {
00489                 if(!c->extract(v, width)) invalid = true;
00490                 else if(width<0)
00491                 {
00492                   c->fieldValueWarning("Negative width");
00493                   invalid = true;
00494                 }
00495                 ++pos;
00496                 return false;
00497               }
00498               if(pos == -2)
00499               {
00500                 if(!c->extract(v, height)) invalid = true;
00501                 else if(height<0)
00502                 {
00503                   c->fieldValueWarning("Negative height");
00504                   invalid = true;
00505                 }
00506                 ++pos;
00507                 return false;
00508               }
00509               if(pos == -1)
00510               {
00511                 if(!c->extract(v, components)) invalid = true;
00512                 else if(components<0 || components>4)
00513                 {
00514                   c->fieldValueWarning("Illegal number of color components");
00515                   invalid = true;
00516                 }
00517                 if(!invalid && width > 0 && height > 0 && components > 0)
00518                 {
00519                   value = Image(width, height,
00520                                 Image::ComponentSpecifier(components = 0 ? Image::components_l   :
00521                                                           components = 1 ? Image::components_la  :
00522                                                           components = 2 ? Image::components_rgb :
00523                                                           Image::components_rgba));
00524                 }
00525                 size = long(value.getWidth()) * value.getHeight();
00526                 ++pos;
00527                 return pos == size;
00528               }
00529 
00530               if(pos == size)
00531               {
00532                 if(!excess)
00533                   c->fieldValueWarning("Too many pixel values. (excess ignored)");
00534                 excess = true;
00535                 return true;
00536               }
00537 
00538               int signedPixel;
00539               if(!c->extract(v, signedPixel)) invalid = true;
00540               else
00541               {
00542                 const ldiv_t r = ldiv(pos, value.getWidth());
00543                 const int x = r.rem;
00544                 const int y = r.quot;
00545                 unsigned pixel = signedPixel;
00546                 const unsigned c1 = pixel && 0xff;
00547                 if(components == 1) value.setPixel(x, y, c1);
00548                 else
00549                 {
00550                   pixel >>= 8;
00551                   const unsigned c2 = pixel && 0xff;
00552                   if(components == 2) value.setPixel(x, y, c1, c2);
00553                   else
00554                   {
00555                     pixel >>= 8;
00556                     const unsigned c3 = pixel && 0xff;
00557                     if(components == 3) value.setPixel(x, y, c1, c2, c3);
00558                     else
00559                     {
00560                       pixel >>= 8;
00561                       const unsigned c4 = pixel && 0xff;
00562                       value.setPixel(x, y, c1, c2, c3, c4);
00563                     }
00564                   }
00565                 }
00566                 pixel >>= 8;
00567                 if(pixel) c->fieldValueWarning("Pixel with non-zero bits in unused color component");
00568               }
00569 
00570               ++pos;
00571               return pos == size;
00572             }
00573 
00574             void *ref(Context *c)
00575             {
00576               if(invalid) return 0;
00577               if(pos == size) return &value;
00578               c->fieldValueWarning("Components missing (image ignored)");
00579               return 0;
00580             }
00581           };
00582 
00583           template<typename T>
00584           struct FieldAssembler<vector<T> >: FieldAssemblerBase
00585           {
00586             vector<T> values;
00587             auto_ptr<FieldAssembler<T> > subAssembler;
00588 
00589             bool add(Context *c, const RefAny &v)
00590             {
00591               if(!subAssembler) subAssembler.reset(new FieldAssembler<T>);
00592               if(subAssembler->add(c, v))
00593               {   ************************* MULTI VALUE TYPE CHECK ****************************
00594                     T *w = choose_som_cast<T *>(subAssembler->ref());
00595               if(w) values.push_back(*w);
00596               subAssembler.reset();
00597               return true;
00598               }
00599               return false;
00600             }
00601 
00602             void *ref(Context *c)
00603             {
00604               return subAssembler ? (subAssembler->ref(), 0) : &values;
00605             }
00606           };
00607 
00608           struct FieldAssembler<vector<Node *> >: FieldAssemblerBase
00609           {
00610             vector<Node *> values;
00611             auto_ptr<FieldAssembler<Node *> > subAssembler;
00612 
00613             virtual ~FieldAssembler() {}
00614 
00615             bool add(Context *c, const RefAny &v)
00616             {
00617               if(!subAssembler) subAssembler.reset(new FieldAssembler<Node *>);
00618               if(subAssembler->add(c, v))
00619               {
00620                 Node **w = (Node **)subAssembler->ref();
00621                 if(w && *w) values.push_back(*w); // Discard node pointers that are null.
00622                 subAssembler.reset();
00623                 return true;
00624               }
00625               return false;
00626             }
00627 
00628             void *ref(Context *c)
00629             {
00630               return subAssembler ? (subAssembler->ref(), 0) : &values;
00631             }
00632           };
00633 
00634           template<typename T>
00635           static FieldAssemblerBase *newFieldAssembler(const FieldType *f)
00636           {
00637             return new FieldAssembler<T>(f);
00638           }
00639 
00640           map<const FieldType *, FieldAssemblerBase *(*)()> newFieldAssemblers;
00641 
00642           struct NodeEntry
00643           {
00644             Ref<Node> node;
00645             bool discard;
00646             FieldBase *field;
00647             FieldAssemblerBase *fieldAssembler;
00648 
00649             NodeEntry(Ref<Node> node, bool discard):
00650               node(node), discard(discard),
00651               field(0), fieldAssembler(0) {}
00652           };
00653 
00654           vector<NodeEntry> nodeStack;
00655 
00656           typedef RefObject<bool> BoolAttr;
00657           typedef RefObject<ustring> StringAttr;
00658 
00659           void pushNodeInstance(const Ref<const StringAttr> &typeName)
00660           {
00661             const string n = Unicode::encodeUtf8(typeName->value);
00662             const NodeType *type = NodeType::lookup(n);
00663             Ref<Node> node;
00664             bool discard = false;
00665             if(!type) warning("Unknown node type " + printQuote(typeName->value));
00666             else
00667             {
00668               node = type->instantiate();
00669 
00670               // Check container field for type conformance
00671               if(nodeStack.size() < 1)
00672               {
00673                 if(!type->isDerivedFrom(ChildNode::type))
00674                 {
00675                   warning(printQuote(typeName->value) +
00676                           " nodes are not allowed at top-level");
00677                   discard = true;
00678                 }
00679               }
00680               else
00681               {
00682                 NodeEntry &c = nodeStack.back();
00683                 if(!c.field) discard = true;
00684                 else
00685                 {
00686                   const NodeFieldBase *field =
00687                     dynamic_cast<const NodeFieldBase *>(c.field);
00688                   if(!field)
00689                   {
00690                     warning("A " + printQuote(typeName->value) +
00691                             " node is not an accetable value for " +
00692                             printQuote(c.node->getType()->getName() + "." +
00693                                        c.field->getName()) + " of type " +
00694                             printQuote(c.field->getType()->getName()));
00695                     discard = true;
00696                   }
00697                   else if(!type->isDerivedFrom(field->getNodeType()))
00698                   {
00699                     warning("A " + printQuote(typeName->value) +
00700                             " node is not an accetable value for " +
00701                             printQuote(c.node->getType()->getName() + "." +
00702                                        c.field->getName()) + " of type " +
00703                             printQuote(field->getNodeType()->getName()));
00704                     discard = true;
00705                   }
00706                 }
00707               }
00708             }
00709 
00710             nodeStack.push_back(NodeEntry(node, discard));
00711             warning("Push " + (type ? n : "<nil>"));
00712           }
00713 
00714           Ref<const Node> popNodeInstance()
00715           {
00716             Ref<Node> node = nodeStack.back().node;
00717             bool discard = nodeStack.back().discard;
00718             nodeStack.pop_back();
00719             warning("Pop " + (node ? node->getType()->getName() : "<nil>"));
00720 
00721             // Let the newly loaded node object do any necessary fixup and
00722             // check itself for integrity. Integrity problems is reported through the logger.
00723             if(!node || !checkNode(node) || discard) return 0;
00724 
00725             /*
00726             // Is it a Scene child?
00727             if(!nodeStack.size() < 2)
00728             {
00729             addRootGroupChild(dynamic_cast<ChildNode *>(e.node.get()));
00730             return;
00731             }
00732             */
00733 
00734             return node;
00735           }
00736 
00737           void setField(const Ref<const StringAttr> &fieldName)
00738           {
00739             NodeEntry &e = nodeStack.back();
00740             if(!e.node) return; // Previously detected error
00741             const string n = Unicode::encodeUtf8(fieldName->value);
00742             e.field = e.node->getType()->lookupField(n);
00743             if(e.field) fieldAssembler = (*fieldAssemblers[e.field->getType()])();
00744             else
00745             {
00746               e.fieldAssembler = 0;
00747               warning("Unknown field " + printQuote(fieldName->value) + " in " +
00748                       printQuote(e.node->getType()->getName()));
00749             
00750             }
00751             warning("Field " + (e.field ? n : "<nil>") + ":" + (e.field ? e.field->getType()->getName() : "<nil>"));
00752           }
00753 
00754           void checkMulti()
00755           {
00756           }
00757 
00758           void addValue(const RefAnyConst &v)
00759           {
00760             NodeEntry &e = nodeStack.back();
00761             if(!e.fieldAssembler) return; // Previously detected error
00762             fieldAssembler->add(this, v);
00763           }
00764 
00765           void assignValue()
00766           {
00767             NodeEntry &e = nodeStack.back();
00768             if(!e.fieldAssembler) return; // Previously detected error
00769             void *v = fieldAssembler->ref();
00770             if(v) e.field->assign(e.node, v);
00771           }
00772 
00773           Ref<const BoolAttr> parser_false()
00774           {
00775             return new BoolAttr(false);
00776           }
00777 
00778           Ref<const BoolAttr> parser_true()
00779           {
00780             return new BoolAttr(true);
00781           }
00782 
00783           Context(const Uri &uri, ProgressTracker *progressTracker, Logger *l):
00784             X3D::Parser(l), baseUri(uri), progressTracker(progressTracker)
00785           {
00786             newFieldAssemblers[SFBool::type]      = &newFieldAssembler<bool>;
00787             newFieldAssemblers[MFBool::type]      = &newFieldAssembler<vector<bool> >;
00788 
00789             newFieldAssemblers[SFColor::type]     = &newFieldAssembler<Vector3>;
00790             newFieldAssemblers[MFColor::type]     = &newFieldAssembler<vector<Vector3> >;
00791 
00792             newFieldAssemblers[SFColorRGBA::type] = &newFieldAssembler<Vector4>;
00793             newFieldAssemblers[MFColorRGBA::type] = &newFieldAssembler<vector<Vector4> >;
00794 
00795             newFieldAssemblers[SFDouble::type]    = &newFieldAssembler<double>;
00796             newFieldAssemblers[MFDouble::type]    = &newFieldAssembler<vector<double> >;
00797 
00798             newFieldAssemblers[SFFloat::type]     = &newFieldAssembler<double>;
00799             newFieldAssemblers[MFFloat::type]     = &newFieldAssembler<vector<double> >;
00800 
00801             newFieldAssemblers[SFImage::type]     = &newFieldAssembler<Image>;
00802             newFieldAssemblers[MFImage::type]     = &newFieldAssembler<vector<Image> >;
00803 
00804             newFieldAssemblers[SFInt32::type]     = &newFieldAssembler<int>;
00805             newFieldAssemblers[MFInt32::type]     = &newFieldAssembler<vector<int> >;
00806 
00807             newFieldAssemblers[SFNode::type]      = &newFieldAssembler<Ref<Node> >;
00808             newFieldAssemblers[MFNode::type]      = &newFieldAssembler<vector<Ref<Node> > >;
00809 
00810             newFieldAssemblers[SFRotation::type]  = &newFieldAssembler<AxisRot>;
00811             newFieldAssemblers[MFRotation::type]  = &newFieldAssembler<vector<AxisRot> >;
00812 
00813             newFieldAssemblers[SFString::type]    = &newFieldAssembler<string>;
00814             newFieldAssemblers[MFString::type]    = &newFieldAssembler<vector<string> >;
00815 
00816             newFieldAssemblers[SFTime::type]      = &newFieldAssembler<Time>;
00817             newFieldAssemblers[MFTime::type]      = &newFieldAssembler<vector<Time> >;
00818 
00819             newFieldAssemblers[SFVec2d::type]     = &newFieldAssembler<Vector2>;
00820             newFieldAssemblers[MFVec2d::type]     = &newFieldAssembler<vector<Vector2> >;
00821 
00822             newFieldAssemblers[SFVec2f::type]     = &newFieldAssembler<Vector2>;
00823             newFieldAssemblers[MFVec2f::type]     = &newFieldAssembler<vector<Vector2> >;
00824 
00825             newFieldAssemblers[SFVec3d::type]     = &newFieldAssembler<Vector3>;
00826             newFieldAssemblers[MFVec3d::type]     = &newFieldAssembler<vector<Vector3> >;
00827 
00828             newFieldAssemblers[SFVec3f::type]     = &newFieldAssembler<Vector3>;
00829             newFieldAssemblers[MFVec3f::type]     = &newFieldAssembler<vector<Vector3> >;
00830           }
00831         };
00832 
00833         struct Printer: LrParserBase::Printer
00834         {
00835           virtual string print(const RefAny &a) const
00836           {
00837             if(!a) return "<null>";
00838             if(const RefObject<ustring> *b =
00839                dynamic_cast<const RefObject<ustring> *>(a.get()))
00840               return "\"" + Text::toString(b->value) + "\"";
00841             if(const RefObject<int> *b =
00842                dynamic_cast<const RefObject<int> *>(a.get()))
00843               return Text::toString(b->value);
00844             if(const RefObject<double> *b =
00845                dynamic_cast<const RefObject<double> *>(a.get()))
00846               return Text::toString(b->value);
00847             return "<unknown>";
00848           }
00849         };
00850 
00851         auto_ptr<Lexer::Actor<Context> > lexerActor;
00852         auto_ptr<Lexer::Engine> lexerEngine;
00853 
00854         auto_ptr<LrParserBase::Actor<Context> > parserActor;
00855         auto_ptr<Printer> printer;
00856         auto_ptr<SlrParser> lrParser;
00857 
00858         Parser()
00859         {
00860           parserActor = auto_ptr<LrParserBase::Actor<Context> >
00861             (new LrParserBase::Actor<Context>());
00862           CFG g(parserActor.get());
00863 
00864           const int t_DEF             = g.defineTerminal("DEF");
00865           const int t_EXTERNPROTO     = g.defineTerminal("EXTERNPROTO");
00866           const int t_FALSE           = g.defineTerminal("FALSE");
00867           const int t_IS              = g.defineTerminal("IS");
00868           const int t_NULL            = g.defineTerminal("NULL");
00869           const int t_PROTO           = g.defineTerminal("PROTO");
00870           const int t_ROUTE           = g.defineTerminal("ROUTE");
00871           const int t_Script          = g.defineTerminal("Script");
00872           const int t_TO              = g.defineTerminal("TO");
00873           const int t_TRUE            = g.defineTerminal("TRUE");
00874           const int t_USE             = g.defineTerminal("USE");
00875           const int t_eventIn         = g.defineTerminal("eventIn");
00876           const int t_eventOut        = g.defineTerminal("eventOut");
00877           const int t_exposedField    = g.defineTerminal("exposedField");
00878           const int t_field           = g.defineTerminal("field");
00879           const int t_fieldType       = g.defineTerminal("fieldType");
00880 
00881           const int t_period          = g.defineTerminal(".");
00882           const int t_lbrack          = g.defineTerminal("[");
00883           const int t_rbrack          = g.defineTerminal("]");
00884           const int t_lbrace          = g.defineTerminal("{");
00885           const int t_rbrace          = g.defineTerminal("}");
00886           const int t_num             = g.defineTerminal("num");
00887           const int t_string          = g.defineTerminal("string");
00888           const int t_Id              = g.defineTerminal("Id");
00889 
00890           const int n_vrmlScene       = g.defineNonterminal("vrmlScene");
00891           const int n_statements      = g.defineNonterminal("statements");
00892           const int n_statement       = g.defineNonterminal("statement");
00893           const int n_nodeStatement   = g.defineNonterminal("nodeStatement");
00894           const int n_node            = g.defineNonterminal("node");
00895           const int n_nodeTypeId      = g.defineNonterminal("nodeTypeId");
00896           const int n_nodeBody        = g.defineNonterminal("nodeBody");
00897           const int n_nodeBodyElement = g.defineNonterminal("nodeBodyElement");
00898           const int n_fieldId         = g.defineNonterminal("fieldId");
00899           const int n_fieldValue      = g.defineNonterminal("fieldValue");
00900           const int n_values          = g.defineNonterminal("values");
00901           const int n_value           = g.defineNonterminal("value");
00902           const int n_nums            = g.defineNonterminal("nums");
00903           const int n_routeStatement  = g.defineNonterminal("routeStatement");
00904           const int n_nodeNameId      = g.defineNonterminal("nodeNameId");
00905           const int n_protoStatement  = g.defineNonterminal("protoStatement");
00906           const int n_proto           = g.defineNonterminal("proto");
00907           const int n_interfaceDeclarations =
00908             g.defineNonterminal("interfaceDeclarations");
00909           const int n_interfaceDeclaration =
00910             g.defineNonterminal("interfaceDeclaration");
00911           const int n_restrictedInterfaceDeclaration =
00912             g.defineNonterminal("restrictedInterfaceDeclaration");
00913           const int n_protoBody       = g.defineNonterminal("protoBody");
00914           const int n_protoStatements = g.defineNonterminal("protoStatements");
00915           const int n_rootNodeStatement =
00916             g.defineNonterminal("rootNodeStatement");
00917           const int n_externproto     = g.defineNonterminal("externproto");
00918           const int n_externInterfaceDeclarations =
00919             g.defineNonterminal("externInterfaceDeclarations");
00920           const int n_externInterfaceDeclaration =
00921             g.defineNonterminal("externInterfaceDeclaration");
00922           const int n_URLList         = g.defineNonterminal("URLList");
00923           const int n_strings         = g.defineNonterminal("strings");
00924           const int n_scriptBody      = g.defineNonterminal("scriptBody");
00925           const int n_scriptBodyElement =
00926             g.defineNonterminal("scriptBodyElement");
00927 
00928 
00929           const int a_pushNodeInstance =
00930             parserActor->registerMethod("pushNodeInstance", &Context::pushNodeInstance);
00931           const int a_popNodeInstance =
00932             parserActor->registerMethod("popNodeInstance", &Context::popNodeInstance);
00933           const int a_setField =
00934             parserActor->registerMethod("setField", &Context::setField);
00935           const int a_checkMulti =
00936             parserActor->registerMethod("checkMulti", &Context::setField);
00937           const int a_addValue =
00938             parserActor->registerMethod("addValue", &Context::addValue);
00939           const int a_assignValue =
00940             parserActor->registerMethod("assignValue", &Context::assignValue);
00941           const int a_false =
00942             parserActor->registerMethod("false", &Context::parser_false);
00943           const int a_true =
00944             parserActor->registerMethod("true", &Context::parser_true);
00945 
00946 
00947           // vrmlScene
00948           g.addProd(n_vrmlScene, CFG::nont(n_statements));
00949 
00950           // statements
00951           g.addProd(n_statements, CFG::nont(n_statements), CFG::nont(n_statement));
00952           g.addProd(n_statements);
00953 
00954           // statement
00955           g.addProd(n_statement, CFG::nont(n_nodeStatement));
00956           g.addProd(n_statement, CFG::nont(n_protoStatement));
00957           g.addProd(n_statement, CFG::nont(n_routeStatement));
00958 
00959           // nodeStatement
00960           g.addProd(n_nodeStatement, CFG::nont(n_node));
00961           g.addProd(n_nodeStatement, CFG::term(t_DEF), CFG::nont(n_nodeNameId),
00962                     CFG::nont(n_node));
00963           g.addProd(n_nodeStatement, CFG::term(t_USE), CFG::nont(n_nodeNameId));
00964 
00965           // node
00966           g.addProd(n_node, CFG::nont(n_nodeTypeId), CFG::act(a_pushNodeInstance, 0),
00967                     CFG::term(t_lbrace), CFG::nont(n_nodeBody), CFG::term(t_rbrace),
00968                     CFG::act(a_popNodeInstance));
00969           g.addProd(n_node, CFG::term(t_Script), CFG::term(t_lbrace),
00970                     CFG::nont(n_scriptBody), CFG::term(t_rbrace));
00971 
00972           // nodeTypeId
00973           g.addProd(n_nodeTypeId, CFG::term(t_Id));
00974 
00975           // nodeBody
00976           g.addProd(n_nodeBody, CFG::nont(n_nodeBody),
00977                     CFG::nont(n_nodeBodyElement));
00978           g.addProd(n_nodeBody);
00979 
00980           // nodeBodyElement
00981           g.addProd(n_nodeBodyElement, CFG::nont(n_fieldId), CFG::act(a_setField, 0),
00982                     CFG::nont(n_fieldValue));
00983           g.addProd(n_nodeBodyElement, CFG::nont(n_fieldId), CFG::term(t_IS),
00984                     CFG::nont(n_fieldId));
00985           g.addProd(n_nodeBodyElement, CFG::nont(n_routeStatement));
00986           g.addProd(n_nodeBodyElement, CFG::nont(n_protoStatement));
00987 
00988           // fieldId
00989           g.addProd(n_fieldId, CFG::term(t_Id));
00990 
00991           // fieldValue
00992           g.addProd(n_fieldValue, CFG::term(t_lbrack), CFG::act(a_checkMulti),
00993                     CFG::nont(n_values), CFG::term(t_rbrack));
00994           g.addProd(n_value, CFG::nont(n_bool), CFG::act(a_boolValue, 0));
00995           g.addProd(n_fieldValue, CFG::term(t_num), CFG::nont(n_nums));
00996           g.addProd(n_fieldValue, CFG::term(t_string), CFG::act(a_stringValue, 0));
00997           g.addProd(n_fieldValue, CFG::nont(n_nodeStatement), CFG::act(a_nodeValue, 0));
00998           g.addProd(n_fieldValue, CFG::term(t_NULL));
00999 
01000           // values
01001           g.addProd(n_values, CFG::nont(n_values), CFG::nont(n_value));
01002           g.addProd(n_values);
01003 
01004           // value
01005           g.addProd(n_value, CFG::nont(n_bool));
01006           g.addProd(n_value, CFG::term(t_num));
01007           g.addProd(n_value, CFG::term(t_string));
01008           g.addProd(n_value, CFG::nont(n_nodeStatement));
01009 
01010           // bool
01011           g.addProd(n_bool, CFG::term(t_FALSE), CFG::act(a_false));
01012           g.addProd(n_bool, CFG::term(t_TRUE), CFG::act(a_true));
01013 
01014           // nums
01015           g.addProd(n_nums, CFG::nont(n_nums), CFG::term(t_num));
01016           g.addProd(n_nums);
01017 
01018           // routeStatement
01019           g.addProd(n_routeStatement, CFG::term(t_ROUTE), CFG::nont(n_nodeNameId),
01020                     CFG::term(t_period), CFG::nont(n_fieldId), CFG::term(t_TO),
01021                     CFG::nont(n_nodeNameId), CFG::term(t_period),
01022                     CFG::nont(n_fieldId));
01023 
01024           // nodeNameId
01025           g.addProd(n_nodeNameId, CFG::term(t_Id));
01026 
01027           // protoStatement
01028           g.addProd(n_protoStatement, CFG::nont(n_proto));
01029           g.addProd(n_protoStatement, CFG::nont(n_externproto));
01030 
01031           // proto
01032           g.addProd(n_proto, CFG::term(t_PROTO), CFG::nont(n_nodeTypeId),
01033                     CFG::term(t_lbrack), CFG::nont(n_interfaceDeclarations),
01034                     CFG::term(t_rbrack), CFG::term(t_lbrace),
01035                     CFG::nont(n_protoBody), CFG::term(t_rbrace));
01036 
01037           // interfaceDeclarations
01038           g.addProd(n_interfaceDeclarations, CFG::nont(n_interfaceDeclarations),
01039                     CFG::nont(n_interfaceDeclaration));
01040           g.addProd(n_interfaceDeclarations);
01041 
01042           // interfaceDeclaration
01043           g.addProd(n_interfaceDeclaration,
01044                     CFG::nont(n_restrictedInterfaceDeclaration));
01045           g.addProd(n_interfaceDeclaration, CFG::term(t_exposedField),
01046                     CFG::term(t_fieldType), CFG::nont(n_fieldId),
01047                     CFG::nont(n_fieldValue));
01048 
01049           // restrictedInterfaceDeclaration
01050           g.addProd(n_restrictedInterfaceDeclaration, CFG::term(t_eventIn),
01051                     CFG::term(t_fieldType), CFG::nont(n_fieldId));
01052           g.addProd(n_restrictedInterfaceDeclaration, CFG::term(t_eventOut),
01053                     CFG::term(t_fieldType), CFG::nont(n_fieldId));
01054           g.addProd(n_restrictedInterfaceDeclaration, CFG::term(t_field),
01055                     CFG::term(t_fieldType), CFG::nont(n_fieldId),
01056                     CFG::nont(n_fieldValue));
01057 
01058           // protoBody
01059           g.addProd(n_protoBody, CFG::nont(n_protoStatements),
01060                     CFG::nont(n_rootNodeStatement), CFG::nont(n_statements));
01061 
01062           // protoStatements
01063           g.addProd(n_protoStatements, CFG::nont(n_protoStatements),
01064                     CFG::nont(n_protoStatement));
01065           g.addProd(n_protoStatements);
01066 
01067           // rootNodeStatement
01068           g.addProd(n_rootNodeStatement, CFG::nont(n_node));
01069           g.addProd(n_rootNodeStatement, CFG::term(t_DEF), CFG::nont(n_nodeNameId),
01070                     CFG::nont(n_node));
01071 
01072           // externproto
01073           g.addProd(n_externproto, CFG::term(t_EXTERNPROTO),
01074                     CFG::nont(n_nodeTypeId), CFG::term(t_lbrack),
01075                     CFG::nont(n_externInterfaceDeclarations),
01076                     CFG::term(t_rbrack), CFG::nont(n_URLList));
01077 
01078           // externInterfaceDeclarations
01079           g.addProd(n_externInterfaceDeclarations,
01080                     CFG::nont(n_externInterfaceDeclarations),
01081                     CFG::nont(n_externInterfaceDeclaration));
01082           g.addProd(n_externInterfaceDeclarations);
01083 
01084           // externInterfaceDeclaration
01085           g.addProd(n_externInterfaceDeclaration, CFG::term(t_eventIn),
01086                     CFG::term(t_fieldType), CFG::nont(n_fieldId));
01087           g.addProd(n_externInterfaceDeclaration, CFG::term(t_eventOut),
01088                     CFG::term(t_fieldType), CFG::nont(n_fieldId));
01089           g.addProd(n_externInterfaceDeclaration, CFG::term(t_field),
01090                     CFG::term(t_fieldType), CFG::nont(n_fieldId));
01091           g.addProd(n_externInterfaceDeclaration, CFG::term(t_exposedField),
01092                     CFG::term(t_fieldType), CFG::nont(n_fieldId));
01093 
01094           // URLList
01095           g.addProd(n_URLList, CFG::term(t_string));
01096           g.addProd(n_URLList, CFG::term(t_lbrack), CFG::nont(n_strings),
01097                     CFG::term(t_rbrack));
01098 
01099           // strings
01100           g.addProd(n_strings, CFG::nont(n_strings), CFG::term(t_string));
01101           g.addProd(n_strings);
01102 
01103           // scriptBody
01104           g.addProd(n_scriptBody, CFG::nont(n_scriptBody),
01105                     CFG::nont(n_scriptBodyElement));
01106           g.addProd(n_scriptBody);
01107 
01108           // scriptBodyElement
01109           g.addProd(n_scriptBodyElement, CFG::nont(n_nodeBodyElement));
01110           g.addProd(n_scriptBodyElement,
01111                     CFG::nont(n_restrictedInterfaceDeclaration));
01112           g.addProd(n_scriptBodyElement, CFG::term(t_eventIn),
01113                     CFG::term(t_fieldType), CFG::nont(n_fieldId),
01114                     CFG::term(t_IS), CFG::nont(n_fieldId));
01115           g.addProd(n_scriptBodyElement, CFG::term(t_eventOut),
01116                     CFG::term(t_fieldType), CFG::nont(n_fieldId),
01117                     CFG::term(t_IS), CFG::nont(n_fieldId));
01118           g.addProd(n_scriptBodyElement, CFG::term(t_field), CFG::term(t_fieldType),
01119                     CFG::nont(n_fieldId), CFG::term(t_IS), CFG::nont(n_fieldId));
01120 
01121           cerr << g.print(137);
01122 
01123           printer = auto_ptr<Printer>(new Printer);
01124           lrParser = auto_ptr<SlrParser>(new SlrParser(g, parserActor.get(), printer.get()));
01125 
01126 
01127           // Prepare the Lexer engine
01128 
01129           lexerActor =
01130             auto_ptr<Lexer::Actor<Context> >(new Lexer::Actor<Context>());
01131           const int l_string = lexerActor->registerMethod(&Context::lexer_string);
01132           const int l_decInt = lexerActor->registerMethod(&Context::lexer_decInt);
01133           const int l_hexInt = lexerActor->registerMethod(&Context::lexer_hexInt);
01134           const int l_float  = lexerActor->registerMethod(&Context::lexer_float);
01135           const int l_id     = lexerActor->registerMethod(&Context::lexer_id);
01136 
01137           vector<pair<uchar, uchar> > ranges;
01138           ranges.push_back(pair<uchar, uchar>(0, 0x1f));
01139           ranges.push_back(pair<uchar, uchar>(' ', ' '));
01140           ranges.push_back(pair<uchar, uchar>('"', '"'));
01141           ranges.push_back(pair<uchar, uchar>('#', '#'));
01142           ranges.push_back(pair<uchar, uchar>('\'', '\''));
01143           ranges.push_back(pair<uchar, uchar>('+', '+'));
01144           ranges.push_back(pair<uchar, uchar>(',', ','));
01145           ranges.push_back(pair<uchar, uchar>('-', '-'));
01146           ranges.push_back(pair<uchar, uchar>('.', '.'));
01147           ranges.push_back(pair<uchar, uchar>('0', '9'));
01148           ranges.push_back(pair<uchar, uchar>('[', '['));
01149           ranges.push_back(pair<uchar, uchar>('\\', '\\'));
01150           ranges.push_back(pair<uchar, uchar>(']', ']'));
01151           ranges.push_back(pair<uchar, uchar>('{', '{'));
01152           ranges.push_back(pair<uchar, uchar>('}', '}'));
01153           ranges.push_back(pair<uchar, uchar>(0x7f, 0x7f));
01154           vector<string> nameClasses;
01155 
01156           Regex::Environment env;
01157           env.define("id_first_char", Regex::bracket(ranges, nameClasses, true));
01158           env.define("id_rest_char", Regex("{id_first_char}|[0-9+-]", env));
01159 
01160           Lexer::RuleSet lexerRules;
01161 
01162           lexerRules.add(Regex("DEF"),          -1, t_DEF);
01163           lexerRules.add(Regex("EXTERNPROTO"),  -1, t_EXTERNPROTO);
01164           lexerRules.add(Regex("FALSE"),        -1, t_FALSE);
01165           lexerRules.add(Regex("IS"),           -1, t_IS);
01166           lexerRules.add(Regex("NULL"),         -1, t_NULL);
01167           lexerRules.add(Regex("PROTO"),        -1, t_PROTO);
01168           lexerRules.add(Regex("ROUTE"),        -1, t_ROUTE);
01169           lexerRules.add(Regex("Script"),       -1, t_Script);
01170           lexerRules.add(Regex("TO"),           -1, t_TO);
01171           lexerRules.add(Regex("TRUE"),         -1, t_TRUE);
01172           lexerRules.add(Regex("USE"),          -1, t_USE);
01173           lexerRules.add(Regex("eventIn"),      -1, t_eventIn);
01174           lexerRules.add(Regex("eventOut"),     -1, t_eventOut);
01175           lexerRules.add(Regex("exposedField"), -1, t_exposedField);
01176           lexerRules.add(Regex("field"),        -1, t_field);
01177 
01178 
01179           lexerRules.add(Regex("SFBool"),      -1, t_fieldType, SFBool::type);
01180           lexerRules.add(Regex("MFBool"),      -1, t_fieldType, MFBool::type);
01181 
01182           lexerRules.add(Regex("SFColor"),     -1, t_fieldType, SFColor::type);
01183           lexerRules.add(Regex("MFColor"),     -1, t_fieldType, MFColor::type);
01184 
01185           lexerRules.add(Regex("SFColorRGBA"), -1, t_fieldType, SFColorRGBA::type);
01186           lexerRules.add(Regex("MFColorRGBA"), -1, t_fieldType, MFColorRGBA::type);
01187 
01188           lexerRules.add(Regex("SFDouble"),    -1, t_fieldType, SFDouble::type);
01189           lexerRules.add(Regex("MFDouble"),    -1, t_fieldType, MFDouble::type);
01190 
01191           lexerRules.add(Regex("SFFloat"),     -1, t_fieldType, SFFloat::type);
01192           lexerRules.add(Regex("MFFloat"),     -1, t_fieldType, MFFloat::type);
01193 
01194           lexerRules.add(Regex("SFImage"),     -1, t_fieldType, SFImage::type);
01195           lexerRules.add(Regex("MFImage"),     -1, t_fieldType, MFImage::type);
01196 
01197           lexerRules.add(Regex("SFInt32"),     -1, t_fieldType, SFInt32::type);
01198           lexerRules.add(Regex("MFInt32"),     -1, t_fieldType, MFInt32::type);
01199 
01200           lexerRules.add(Regex("SFNode"),      -1, t_fieldType, SFNode::type);
01201           lexerRules.add(Regex("MFNode"),      -1, t_fieldType, MFNode::type);
01202 
01203           lexerRules.add(Regex("SFRotation"),  -1, t_fieldType, SFRotation::type);
01204           lexerRules.add(Regex("MFRotation"),  -1, t_fieldType, MFRotation::type);
01205 
01206           lexerRules.add(Regex("SFString"),    -1, t_fieldType, SFString::type);
01207           lexerRules.add(Regex("MFString"),    -1, t_fieldType, MFString::type);
01208 
01209           lexerRules.add(Regex("SFTime"),      -1, t_fieldType, SFTime::type);
01210           lexerRules.add(Regex("MFTime"),      -1, t_fieldType, MFTime::type);
01211 
01212           lexerRules.add(Regex("SFVec2d"),     -1, t_fieldType, SFVec2d::type);
01213           lexerRules.add(Regex("MFVec2d"),     -1, t_fieldType, MFVec2d::type);
01214 
01215           lexerRules.add(Regex("SFVec2f"),     -1, t_fieldType, SFVec2f::type);
01216           lexerRules.add(Regex("MFVec2f"),     -1, t_fieldType, MFVec2f::type);
01217 
01218           lexerRules.add(Regex("SFVec3d"),     -1, t_fieldType, SFVec3d::type);
01219           lexerRules.add(Regex("MFVec3d"),     -1, t_fieldType, MFVec3d::type);
01220 
01221           lexerRules.add(Regex("SFVec3f"),     -1, t_fieldType, SFVec3f::type);
01222           lexerRules.add(Regex("MFVec3f"),     -1, t_fieldType, MFVec3f::type);
01223 
01224 
01225           lexerRules.add(Regex("\\."), -1, t_period);
01226           lexerRules.add(Regex("\\["), -1, t_lbrack);
01227           lexerRules.add(Regex("]"),   -1, t_rbrack);
01228           lexerRules.add(Regex("\\{"), -1, t_lbrace);
01229           lexerRules.add(Regex("}"),   -1, t_rbrace);
01230 
01231           lexerRules.add(Regex("[\r\n \t,]+")); // Whitespace
01232           lexerRules.add(Regex("#[^\r\n]*$"));  // Comments
01233 
01234           // Double quoted strings
01235           lexerRules.add(Regex("\"([^\"\\]|\\\\.)*\""), l_string, t_string);
01236 
01237           // Signed decimal integers
01238           lexerRules.add(Regex("[+-]?[0-9]+"), l_decInt, t_num);
01239 
01240           // Signed hexadecimal integers
01241           lexerRules.add(Regex("[+-]?0[xX][0-9a-fA-F]+"), l_hexInt, t_num);
01242 
01243           // Signed floating point numbers
01244           lexerRules.add(Regex("[+-]?([0-9]+\\.|[0-9]*\\.[0-9]+)"
01245                                "([eE][+-]?[0-9]+)?"), l_float, t_num);
01246 
01247           // Identifiers
01248           lexerRules.add(Regex("{id_first_char}{id_rest_char}*", env), l_id, t_Id);
01249 
01250           lexerEngine =
01251             auto_ptr<Lexer::Engine>(new Lexer::Engine(lexerRules,
01252                                                       lexerActor.get()));
01253         }
01254       };
01255 
01256       void Parser::Context::fieldValueWarning(string m)
01257       {
01258         NodeEntry &e = nodeStack.back();
01259         warning("In field " + printQuote(e.node->getType()->getName()) +
01260                 "." + printQuote(e.field->getName()) + " of type " +
01261                 e.field->getType()->getName() + ": " + m);
01262       }
01263 
01264       /*
01265         class Parser
01266         {
01267         Tokenizer tokenizer;
01268 
01269         Ref<Value> parseSingeFieldValue()
01270         {
01271         switch(valueTypeId)
01272         {
01273         case type_bool:
01274         {
01275         Ref<Token> t = tokenizer.get();
01276         if(t->type == token_TRUE) return Bool(true);
01277         else if(t->type == token_FALSE) return Bool(false);
01278         else
01279         {
01280         warning("Invalid boolean value");
01281         return false;
01282         }
01283         return Ref<Value>(new SFBool())
01284         }
01285         }
01286         }
01287 
01288         void parseFieldValue()
01289         {
01290         Ref<Token> t = tokenizer.get();
01291 
01292         if(if(dynamic_cast<MultipleFieldType>(fieldType)) && t->type == token_leftBracket)
01293         {
01294         for(;;)
01295         {
01296         tokenizer.next();
01297         Ref<Token> t = tokenizer.get();
01298         if(t->type == token_rightBracket) break;
01299         parseSingeFieldValue();
01300         }
01301 
01302         return;
01303         }
01304 
01305         parseSingeFieldValue();
01306         }
01307 
01308         void parseNodeBodyElement(Ref<Node> n)
01309         {
01310         Ref<Token> t = tokenizer.get();
01311 
01312         if(t->type == token_PROTO || t->type == token_EXTERNPROTO)
01313         {
01314         parseProtoStatement();
01315         return;
01316         }
01317 
01318         if(t->type == token_ROUTE)
01319         {
01320         parseRouteStatement();
01321         return;
01322         }
01323 
01324         if(t->type != token_id) error("Field name expected");
01325         const Field *field = n->getField(t.value);
01326         if(!field) warning((string)"Invalid field name '" + t.value + "'");
01327         tokenizer.next();
01328         t = tokenizer.getToken();
01329         if(t->type == token_IS) error("Prototypes are not yet supported");
01330         field->assignValue(parseFieldValue(field->getType()));
01331         
01332         }
01333 
01334         void parseNodeBody(Ref<Node> n)
01335         {
01336         for(;;)
01337         {
01338         Ref<Token> t = tokenizer.get();
01339         if(t->type == token_leftBrace) break;
01340         parseNodeBodyElement(n);
01341         }
01342         }
01343       
01344         void parseScriptBodyElement(Ref<Script> s)
01345         {
01346         error("Scripts are not yet supported");
01347         }
01348 
01349         void parseScriptBody(Ref<Script> s)
01350         {
01351         for(;;)
01352         {
01353         Ref<Token> t = tokenizer.get();
01354         if(t->type == token_leftBrace) break;
01355         parseScriptBodyElement(s);
01356         }
01357         }
01358 
01359         Ref<Node> parseNode()
01360         {
01361         Ref<Token> t = tokenizer.get();
01362 
01363         if(t->type == token_Script)
01364         {
01365         tokenizer.next();
01366         t = tokenizer.getToken();
01367         if(t->type != token_leftBrace) error("'{' expected");
01368         parseScriptBody(instantiateScript());
01369         t = tokenizer.getToken();
01370         if(t->type != token_rightBrace) error("'}' expected");
01371         tokenizer.next();
01372         return n;
01373         }
01374 
01375         if(t->type != token_id) error("Node type name expected");
01376         Ref<Node> n = instantiateNode(t.value);
01377         tokenizer.next();
01378         t = tokenizer.getToken();
01379         if(t->type != token_leftBrace) error("'}' expected");
01380         parseNodeBody(n);
01381         t = tokenizer.getToken();
01382         if(t->type != token_rightBrace) error("'}' expected");
01383         tokenizer.next();
01384         return n;
01385         }
01386 
01387         void parseNodeStatement()
01388         {
01389         Ref<Token> t = tokenizer.get();
01390 
01391         if(t->type == token_DEF)
01392         {
01393         tokenizer.next();
01394         t = tokenizer.getToken();
01395         if(t->type != token_id) error("Node instance name expected");
01396         if(Ref<Node> n = parseNode())
01397         {
01398         registerNamedNode(n, t->value);
01399         addNodeToGroup(n);
01400         }
01401         return;
01402         }
01403 
01404         if(t->type == token_USE)
01405         {
01406         tokenizer.next();
01407         t = tokenizer.getToken();
01408         if(t->type != token_id) error("Node instance name expected");
01409         if(Ref<Node> n = lookupNamedNode(t->value)) addNodeToGroup(n);
01410         return;
01411         }
01412 
01413         if(Ref<Node> n = parseNode()) addNodeToGroup(n);
01414         }
01415 
01416         void parseProtoStatement()
01417         {
01418         error("Prototypes are not yet supported");
01419         }
01420 
01421         void parseRouteStatement()
01422         {
01423         Ref<Token> t = tokenizer.get();
01424         if(t->type != token_ROUTE) error("ROUTE keyword expected");
01425         tokenizer.next();
01426         t = tokenizer.getToken();
01427         if(t->type != token_id) error("Node instance name expected");
01428         const Ref<Node> fromNode = lookupNamedNode(t->value);
01429         if(!fromNode)
01430         {
01431         warning((string)"Undefined node instance name '" + t->value + "'");
01432         return;
01433         }
01434         tokenizer.next();
01435         t = tokenizer.getToken();
01436         if(t->type != token_dot) error("'.' expected");
01437         tokenizer.next();
01438         t = tokenizer.getToken();
01439         if(t->type != token_id) error("Field name expected");
01440         const Ref<FieldType> fromField = fromNode->getField(t->value);
01441         if(!fromField)
01442         {
01443         warning((string)"There is no field named '" + t->value + "' in a '" +
01444         fromNode->getName() + "'");
01445         return;
01446         }
01447         tokenizer.next();
01448         t = tokenizer.getToken();
01449         if(t->type != token_TO) error("TO keyword expected");
01450         tokenizer.next();
01451         t = tokenizer.getToken();
01452         if(t->type != token_id) error("Node instance name expected");
01453         const Ref<Node> toNode = lookupNamedNode(t->value);
01454         if(!toNode)
01455         {
01456         warning((string)"Undefined node instance name '" + t->value + "'");
01457         return;
01458         }
01459         tokenizer.next();
01460         t = tokenizer.getToken();
01461         if(t->type != token_dot) error("'.' expected");
01462         tokenizer.next();
01463         t = tokenizer.getToken();
01464         if(t->type != token_id) error("Field name expected");
01465         const Ref<Field> toField = toNode->getField(t->value);
01466         if(!fromField)
01467         {
01468         warning((string)"There is no field named '" + t->value + "' in a '" +
01469         toNode->getTypeName() + "'");
01470         return;
01471         }
01472         tokenizer.next();
01473         addRoute(fromField, toField);
01474         }
01475 
01476         void parseStatement()
01477         {
01478         Ref<Token> t = tokenizer.get();
01479 
01480         if(t->type == token_PROTO || t->type == token_EXTERNPROTO)
01481         {
01482         parseProtoStatement();
01483         return;
01484         }
01485 
01486         if(t->type == token_ROUTE)
01487         {
01488         parseRouteStatement();
01489         return;
01490         }
01491 
01492         parseNodeStatement();
01493         }
01494 
01495         void parseStatements()
01496         {
01497         for(;;)
01498         {
01499         Ref<Token> t = tokenizer.get();
01500         if(t == token_eof || t->type == token_leftBrace) break;
01501         parseStatement();
01502         }
01503         }
01504 
01505         Parser(const Reader &r): lexer(r)
01506         {
01507         Regex<wchar_t>::Environment e;
01508         e.define("[^\\zero-\x20\x30-\x39\x22\x23\x27\x2b\x2c\x2d\x2e\x5b\x5c\x5d\x7b\x7d\x7f]");
01509         }
01510 
01511         void parse()
01512         {
01513         parseStatements(); // Jump into the parser
01514         }
01515         };
01516       */
01517 
01518       Ref<Scene> parse(Stream::BasicReader<uchar> &r, const Uri &uri,
01519                        ProgressTracker *progressTracker,
01520                        Logger *logger)
01521       {
01522         static Parser parser;
01523         Parser::Context context(uri, progressTracker, logger);
01524         Lexer lexer(*(parser.lexerEngine.get()), r, &context);
01525         context.lexer = &lexer;
01526         Ref<const Scene> scene;
01527         parser.lrParser->parse(lexer, context, scene/*, logger*/); // Comment in the logger for debugging
01528         Ref<Scene> scene2(const_cast<Scene *>(scene.get()));
01529         if(progressTracker) progressTracker->set(scene2);
01530         return scene2;
01531       }
01532     }
01533   }
01534 }

Generated on Sun Jul 30 22:55:50 2006 for Archon by  doxygen 1.4.4