Changeset 4d1d43


Ignore:
Timestamp:
Aug 5, 2010, 10:46:06 AM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
668e28
Parents:
9d5803
Message:

Improved formula parsing to include more complex expressions

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Formula.cpp

    r9d5803 r4d1d43  
    5555
    5656void Formula::fromString(const std::string &formula) throw(ParseError){
     57  // make this transactional, in case an error is thrown
     58  Formula res;
     59  string::const_iterator begin = formula.begin();
     60  string::const_iterator end = formula.end();
     61  res.parseFromString(begin,end,static_cast<char>(0));
     62  (*this)=res;
     63}
     64
     65int Formula::parseMaybeNumber(string::const_iterator &it,string::const_iterator &end) throw(ParseError){
     66  static const range<char> Numbers = makeRange('0',static_cast<char>('9'+1));
     67  int count = 0;
     68  while(it!=end && Numbers.isInRange(*it))
     69    count = (count*10) + ((*it++)-Numbers.first);
     70  // one is implicit
     71  count = (count!=0)?count:1;
     72  return count;
     73}
     74
     75void Formula::parseFromString(string::const_iterator &it,string::const_iterator &end,char delimiter) throw(ParseError){
    5776  // some constants needed for parsing... Assumes ASCII, change if other encodings are used
    5877  static const range<char> CapitalLetters = makeRange('A',static_cast<char>('Z'+1));
    5978  static const range<char> SmallLetters = makeRange('a',static_cast<char>('z'+1));
    60   static const range<char> Numbers = makeRange('0',static_cast<char>('9'+1));
     79  map<char,char> delimiters;
     80  delimiters['('] = ')';
     81  delimiters['['] = ']';
    6182  // clean the formula
    6283  clear();
    63   string::const_iterator end = formula.end(); // will be used frequently
    64   for(string::const_iterator it=formula.begin();it!=end;){
     84  for(/*send from above*/;it!=end && *it!=delimiter;/*updated in loop*/){
     85    // we might have a sub formula
     86    if(delimiters.count(*it)){
     87      Formula sub;
     88      char nextdelim=delimiters[*it];
     89      sub.parseFromString(++it,end,nextdelim);
     90      int count = parseMaybeNumber(++it,end);
     91      addFormula(sub,count);
     92      continue;
     93    }
    6594    string shorthand;
    6695    // Atom names start with a capital letter
     
    71100    while(it!=end && SmallLetters.isInRange(*it))
    72101      shorthand+=(*it++);
    73     // now we can count the occurences
    74     int count = 0;
    75     while(it!=end && Numbers.isInRange(*it))
    76       count = (count*10) + ((*it++)-Numbers.first);
    77     // one is implicit
    78     count = (count!=0)?count:1;
     102    int count = parseMaybeNumber(it,end);
    79103    // test if the shorthand exists
    80104    if(!World::getInstance().getPeriode()->FindElement(shorthand))
     
    82106    // done, we can get the next one
    83107    addElements(shorthand,count);
     108  }
     109  if(it==end && delimiter!=0){
     110    throw(ParseError(__FILE__,__LINE__));
    84111  }
    85112}
  • src/Formula.hpp

    r9d5803 r4d1d43  
    100100
    101101private:
     102  void parseFromString(std::string::const_iterator&,std::string::const_iterator&,char) throw(ParseError);
     103  int parseMaybeNumber(std::string::const_iterator &it,std::string::const_iterator &end) throw(ParseError);
    102104  // this contains all counts of elements in the formula
    103105  // the size of the actual structure might be used in comparisons
Note: See TracChangeset for help on using the changeset viewer.