diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,1868 +1,1870 @@ 2018-08-30 Andy Buckley + * Update embedded yaml-cpp to v0.6.0. + * Convert xyzMin/Max properties to functions in the Python interfaces. * Return numpy arrays if possible in Python interface array methods. 2018-08-23 Andy Buckley * Release 1.7.2 * Add missing bin(i) method to Python Profile1D interface. 2018-08-22 Andy Buckley * Avoid const-overloading clash for binAt functions. 2018-08-14 Chris Gutschow * Merge Graeme Watt's modifications to yodadiff. 2018-08-14 Andy Buckley * Release 1.7.1 2018-08-08 Andy Buckley * yodadiff now supports quiet (-q) and list (-l) modes. 2018-06-01 Louie Corpe * Point1,2,3D now support multiple error sources for the highest dimension. * Scatter1,2,3D now read in/out with additional columns for the extra error sources, with a variations() method to check with sources are available 2018-05-02 Andy Buckley * StringUtils.h: Replace std::ptr_fun (removed in C++17) with a lambda function. Thanks to Stefan Richter. 2018-04-30 Andy Buckley * Add "-" = stdin/stdout recognition to C++ IO functions. * Convert remaining Python read/write methods to use the C++ IO functions. * Convert Python read/write methods with filename arguments to use the gzip-aware IO functions. 2018-04-27 Andy Buckley * Fix missing lock=false flag initialisation in Histo2D constructor from a Bins vector. * Improve axis locking error messages. 2017-12-21 Andy Buckley * Version 1.7.0 release 2017-12-11 Andy Buckley * Add Nentries printout to yodals -v 2017-09-18 Andy Buckley * Patches for ROOT conversion from Robert Hatcher -- thanks! 2017-09-16 Andy Buckley * Add YODA format version annotation, at version 2, and update YODA reader to use version info and multiline YAML EOF marker. * Write YODA annotations in YAML with a --- YAML break-line. 2017-09-14 Andy Buckley * Enable compressed writing from Python. 2017-09-13 Andy Buckley * Remove UNUSED macro in favour of anonymous args. * Enable zipped writing... but only works from C++ so far. 2017-09-11 Andy Buckley * Add HistoBin2D::area(), and provide a default normto=1.0 argument on the Python Histo2D.normalize() method. 2017-09-04 Andy Buckley * Change license explicitly to GPLv3, cf. MCnet3 agreement. * Parse YODA format AO headers as YAML (restriction to single-line dict entries for now). 2017-08-24 Andy Buckley * Use a slightly enhanced fast numeric parser in ReaderYODA (taken from LHAPDF, originally inspired by Gavin Salam). * Add an UNSCALE spec option to yodascale, to undo ScaledBy effects. 2017-08-19 Andy Buckley * Add optional zlib support via zstr -- massive thanks to Dmitry Kalinkin for the lovely patch! 2017-08-16 Andy Buckley * Fix setVal(i, x) numbered-axis methods on Point2D and Point3D: switch break statements were missing. * Explicitly load all ROOT objects as a list rather than generator. Patch from Dmitry Kalinkin. 2017-07-24 Andy Buckley * Improvements to yodaplot, including two operating modes: the default CMP mode is suitable for plotting histos by path, from raw .yoda files. * Update yoda.plotting functions to treat plot-keys as args and AO annotations via case-insensitive keys. * Add annotationsDict to the Python AO interface. * Add AO as an alias for AnalysisObject. 2017-07-23 Andy Buckley * Add parallel/compatibility yoda1 package to aid eventual transition to YODA v2. 2017-07-22 Andy Buckley * Add x,y,zMins and Maxs to all 1D data types and scatters (and x,yMin/Max to the scatters) -- Python interface only. * Rework some of the yoda.plotting tools, making it a bit more compatible with user-scripted matplotlib. 2017-07-18 Andy Buckley * Add convenience aliases H1D, H2D, P1D, P2D, and S1D, S2D, S3D for the HistoXD, ProfileXD, and ScatterXD classes respectively. 2017-07-08 Andy Buckley * Add xyVals/Errs and other 'bin array property' accessors to the Python Histo1D and Profile1D types: important for connection to matplotlib. 2017-06-28 Andy Buckley * Use Python natsort library to sort yodals output if available. 2017-06-18 Andy Buckley * Version 1.6.7 release. 2017-05-12 Andy Buckley * pyext/yoda/rootcompat.pyx: Fix ordering of TH1 vs. TProfile conversion -- TProfile *is* a TH1, so we have to test for the more specific type-match first. Thanks to Dmitry Kalinkin for the patch. 2017-05-02 Andy Buckley * Add static Reader methods to match the Writer ones. 2017-02-23 Andy Buckley * Fix Histo2D and Profile2D total distribution reading from YODA format. 2017-02-19 Holger Schulz * Convert TH1F to TH1D in root2flat. Much simpler than duplicating the TH1D stuff in pyext. 2016-12-13 Andy Buckley * Version 1.6.6 release. 2016-12-12 Holger Schulz * Bugfixes in Cython bins accessors for Histo2D. 2016-11-17 Leif Lonnblad * Fixed warning messages about the obsoleteness of AIDA so that the scripts actually still work. 2016-09-28 Andy Buckley * Version 1.6.5 release, for the benefit of ROOT fans. * Fix handling of --enable/disable-root configure options. 2016-09-26 David Grellscheid * Improvements to Cython version testing. 2016-09-25 Andy Buckley * Version 1.6.4 release. 2016-09-20 David Grellscheid * Remove aliases for @property functions. They were scheduled for removal anyway, and don't work with Cython >= 24. 2016-09-06 Andy Buckley * Update configure scripts to use newer (Py3-safe) Python testing macros. 2016-08-09 Andy Buckley * Version 1.6.3 release! 2016-07-22 Andy Buckley * Add 'add' modes for scatter combination to yodamerge. * Fix yodamerge scatter averaging to use the first AO. 2016-07-21 Andy Buckley * Add --type-mismatch-mode flag and fallback logic to yodamerge. * Fix yodamerge logic to handle cases where an AO only appears once. 2016-07-19 Andy Buckley * Deprecate flat2yoda script and add warning output to it and the AIDA conversion scripts. * Add a convenience yoda2yoda script. 2016-07-14 Andy Buckley * Try to build PyROOT interface by default, if root-config is found. 2016-07-11 Andy Buckley * Remove accidentally remaining reference to Boost flags in yoda-config. 2016-07-06 Andy Buckley * Version 1.6.2 release! 2016-07-05 Andy Buckley * Pass the toNewScatter3D() scalebyarea flag to the called toScatter3D() functions. 2016-06-06 Andy Buckley * Re-enable the disabled-for-some-reason Scatter1D combineWith Python mappings. 2016-04-28 Andy Buckley * Version 1.6.1 release! * Add a unit test for annotation handling correctness. * Fix numerical precision of string storage of floating-point attributes. * Fix a bug in use of the replacement for lexical_cast. 2016-04-20 Andy Buckley * Version 1.6.0 release! 2016-04-16 Andy Buckley * Extend SFINAE craziness to allow writing of any object (e.g. smart pointer) that can be dereferenced to something that has AnalysisObject as its base class... and also to any container of them! Amazing what you can do with C++11! 2016-04-14 Andy Buckley * Add a few consts to the arguments in Scatter error setting via pairs. * Fix double-writing of minus errors in WriterYODA for Scatter1D and Scatter3D. Thanks to Graeme Watt for the report and fix. 2016-04-12 Andy Buckley * Remove Boost dependency and require C++11 compilation. 2016-04-08 Andy Buckley * Add a --guess-prefix flag to yoda-config, cf. fastjet-config. 2015-12-20 Andy Buckley * Change AO uncomputable division and mkScatter operations to return/set NaN rather than 0 -- behaviour change requires new major version series 1.6. 2016-03-09 Andy Buckley * Version 1.5.9! (oh no, we're out of convenient version number space!!) 2016-03-08 Andy Buckley * Add abs function to eq calculation in yodadiff. 2016-02-29 Andy Buckley * Remove blocking of builds against ROOT6 -- it works fine. 2016-02-16 Andy Buckley * Add a --add option to yodamerge, for simple histo stacking. Thanks to Chris Gutschow for the patch, although my spidey sense is tingling... 2015-12-21 Andy Buckley * Version 1.5.8! * Add a rebinning unit test, pytest-rebin. * Add optional range arguments to rebinBy methods, allowing block rebinnings to be applied only within a range of (original) bin indices. * Add missing root.py submodule file. Oops! 2015-12-20 Andy Buckley * Convert linspace to use multiplication rather than repeated addition to construct edge values, reducing precision errors. Thanks to Holger Schulz for the suggestion. 2015-12-15 Andy Buckley * Add xEdges() methods to Axis1D and the Histo1D and Profile1D that use it. The returned edge lists are finite only, i.e. they do not contain the +-inf values on the ends of the internal BinSearcher edges. 2015-12-13 Andy Buckley * Version 1.5.7! * Extend batch-adding in ReaderYODA to include Scatter types. * Add a match_aos function to Python, for filtering AO lists/dicts on path patterns and anti-patterns. 2015-12-12 Andy Buckley * Add a flag to yoda2root to change whether the conversion is to 'proper' types or to (more robustly) TGraphAsymmErrors objects. * Fix accidental use of S2D_MODE flag where S1D_MODE should have been used, in yodamerge. Thanks again to Radek Podskubka. * Allow new rebinTo() merging to restrict to a subset of the bin range, merging the outside bins into the overflow distributions. 2015-12-11 Andy Buckley * Add a rebinTo() method on Axis1D, allowing rebinning to a new given set of bin edges. Add an explicitly named rebinBy(), and overloaded rebin() aliases for both, and pass through to Histo1D and Profile1D APIs. Plus other internal tweaks to binning functionality... anticipating/fuelling the fundamental rewrite. 2015-12-10 Andy Buckley * Improve ReaderYODA to use temporary bin containers, to minimise calling sort when adding bins to histos. A quick test suggests this has sped up big file reading by a factor of 30 or so!!! * Add missing addBins() operators (only in C++ so far) to Histo and Profile classes. * Fix Counter::numEntries to return an unsigned long rather than double. Thanks to Radek Podskubka for the bug discovery and detective work. * Improve sortedvector to insert new elements into the sorted position, rather than resorting the whole vector. This should be a bit more efficient, but I think the asymptotic complexity is the same. Might help a bit with reading big data files. 2015-12-04 Andy Buckley * Add yoda.HAS_ROOT_SUPPORT flag, for API user convenience. 2015-11-22 Andy Buckley * Version 1.5.6! * Make AO path setting and retrieval prepend a leading slash if it is missing (unless the path is completely empty). 2015-11-21 Andy Buckley * Add root2yoda conversion script. * Deprecating yoda.to_root() in favour of yoda.root module, which contains to_root and to_yoda functions, as well as a ROOT file walking function. 2015-11-17 Andy Buckley * Map ROOT-to-YODA (as scatter) functions to Python. Phew. * Map new to-ROOT functions, including TGraph ones, to Python. 2015-11-16 Andy Buckley * ROOTCnv.h: Add toScatter3D ROOT->YODA, and toNew* YODA->ROOT conversion routines. * ROOTCnv.h: Fix bug in toTH2D(const Histo2D& h) as used with ROOT6. Thanks to Tim Martin. 2015-11-05 Andy Buckley * Fix double-dealloc in new Point class hierarchy Python mapping. 2015-10-23 Andy Buckley * Make the version() function inline, and the numerical constants static. * Change the default plotting backend to MPL rather than the much slower PGF. 2015-10-09 Andy Buckley * Reinstate __getitem__ special methods for Scatters in Python. * Provide dim() methods/attributes for the Point and Bin base classes. * Rename set*Err to set*Errs for the asymmetric variants. Plural aliases are also provided for the symm case. 2015-10-08 Andy Buckley * Pass std::pairs by reference in Point*D error setting functions. * Add Point base class with generic accessors to Point*D properties via an integer axis ID argument. 2015-10-07 Andy Buckley * Version 1.5.5 release. * Counter.pyx: Typo fix in sumW mapping. * yodamerge: Re-add checking for non-emptiness before merging, in case the empty ones are missing a ScaledBy attribute. Won't normally apply to Profiles, since they don't usually get normalised, but we might as well include them in the vetoing since empty histos don't contribute to the merging. Added a command-line option to disable this heuristic since in very strange situations a null sumW does not mean no fills. 2015-10-06 Andy Buckley * Version 1.5.4 release. * ReaderYODA: Typo fixes in Counter filling of sumW and Scatter3D reader state flag. * yodamerge: add merging heuristics for Scatter1D and Scatter3D (needs unification) 2015-10-05 Andy Buckley * yodamerge: add a fix for empty-in-all-runs histo merging; thanks to Daniel Rauch. 2015-10-04 Andy Buckley * Adding dim() function and corresponding Python attribute to AnalysisObject. * Map Counter arithmetic operations into Python. * Map Counter mkScatter() into Python (as bound method). * Add a YODA::version() function, mapped into Python and used to set the yoda.__version__ variable. 2015-10-01 Andy Buckley * Expose the yoda.plot() Python function in a way that doesn't automatically induce a dependence on matplotlib. 2015-09-30 Andy Buckley * Fix yodals to work with Counters. 2015-09-23 Andy Buckley * Version 1.5.3 release. * Update Boost version requirement to 1.48, due to use of type_traits/has_dereference, and add a check for that feature's header. 2015-09-19 Andy Buckley * Further improvements to handling leading _multiple_ # marks on YODA format BEGIN lines. 2015-09-11 Andy Buckley * Version 1.5.2 release. * Tolerate leading # symbols without separating whitespace on BEGIN lines in YODA format parsing. * Further improvements to handling LowStatsErrors in YODA format writing. * Fix shadowed variables that made ReaderYODA unhappy. 2015-09-03 Andy Buckley * Version 1.5.1 release. * Fix bugs in Python wrapper for Point3D. 2015-08-28 Peter Richardson * Catch LowStatsError when writing multiple histograms so only the histogram with the problem is not written 2015-08-28 Andy Buckley * Version 1.5.0 release. 2015-08-24 Andy Buckley * Improve protection of efficiency calculation against the weird world of general weighted events. 2015-08-17 Andy Buckley * Add some protection against calling matplotlib's legend() method if there are no valid labels to display, to suppress an MPL warning message when using yoda.plot(). 2015-08-12 Andy Buckley * Fix cut & paste typo, and add LowStatsError catching in Profile division. 2015-08-11 Andy Buckley * Replace old Spirit-based ReaderYODA with the new hand-rolled one. * Adding filling of Histo1D, Profile1D, Histo2D and Profile2D in new ReaderYODA. More hacking of Axis and Histo/Profile interfaces... needs clean-up, and infinite binning implementation. 2015-08-07 Andy Buckley * Convert the ReaderFLAT parser to also use a simple hand-written parser rather than Spirit. 2015-08-04 Andy Buckley * Add methods for Counter, Axis and Histo1D internal state access/setting, mainly for new persistency. NEEDS PRE-RELEASE TESTING!!! 2015-07-30 Andy Buckley * Remove # markers from YODA format BEGIN/END output. The parser will continue to accept them. 2015-07-29 Andy Buckley * Add SFINAE trait magic to restrict write(RANGE) functions to accepting iterables. Also generalising to allow either container-of-objects or container-of-pointers args by providing a writeBody(AO*) function to complement writeBody(AO&). Based on a patch from Lukas Heinrich. * Add configuration of output streams to throw exceptions on bad/fail state (based on patch from Lukas Heinrich). 2015-07-01 Andy Buckley * 1.4.0 release. 2015-06-30 Andy Buckley * More tweaks to yodamerge: adding control of S2D merging strategy, and now performing weighted normalized histo merges without reference to an absolute normalization. * Change yodamerge norm-detection heuristic to just look for a ScaledBy attribute rather than fuzzily compare norms. 2015-06-26 Andy Buckley * Removing add, subtract, and divide functions and operators on Scatter types, and re-implementing Histo and Profile divide functions explicitly rather than via mkScatter. Also removed from the Python interface. The combine() methods remain. 2015-06-24 Andy Buckley * Adding workaround versions of binAt to all the Python histo types (for some reason the direct mapping that works for bin(i) produces a compile error for binAt(x) :-/ 2015-06-23 Andy Buckley * Renaming, tidying, completing, etc. the Python-mapped methods on Bin1D and Bin2D. * Rename Python Histo2D mean, variance, etc. pair-returning methods to xyMean, xyVariance, etc., to distinguish from Profile2D mean, variance, etc. * Add Python mappings of all the methods below. * Add full set of {x,y}{Mean,Variance,StdDev,StdErr,RMS} to 1D and 2D binned distributions. * Add optional includeoverflows=True argument to all binned AO numEntries and effNumEntries. * Fix type of numEntries to always be unsigned long. 2015-06-18 Andy Buckley * Fix typos in Point3D Python mapping (accidentally trying to get the ptr via _Point2D rather than _Point3D). 2015-06-13 Andy Buckley * Adding an AnalysisObject::name() method, to return the last part of the path. Mapped into a Python property. * Adding an optional usestddev argument to mkScatter for profile types, so the error bars can represent distribution width rather than uncertainty on the mean. Mapped to Python. 2015-06-08 Andy Buckley * Adding unpatterns arguments to Python read functions, and auto-conversion from single strings and re.compile()d strs. 2015-06-04 Andy Buckley * Add binAt(x,y) and binIndexAt(iglobal) Python methods for 2D histos. Still want a way to get and pass a pair of bin indices, I think. * Adding includeoverflows optional args for Histo1D (eff)numEntries. * Adding Rename Histo1D integral() methods as integral(), integralRange(), and integralTo(), and mapping to Python. 2015-06-02 Andy Buckley * Adding missing binAt and binIndexAt methods to Histo1D and Profile1D, plus other minor Python mapping tweaks. * Add a regex pattern match optional argument to the IO.read() Python functions, for pre-emptive filtering. 2015-03-27 Andy Buckley * Fix a harmless possibility to raise an FPE exception in the BinSearcher. Thanks to Leif Lonnblad for the discovery, debug and patch! 2015-03-19 Andy Buckley * Bump version for 1.3.1 release. 2015-03-06 Andy Buckley * Adding usefocus optional argument to some mkScatter functions, plus the Python bindings. * Cleaning up some Python mappings of 2D histogram bin classes. * Removing mappings of bin-level fill and scale operations in Python. * Fix formatting and error handling in Python Bin and Dbn __repr__ methods. * Add a -i/--in-place option pair on yodascale. 2015-02-05 Andy Buckley * Convert script matching options to use re search rather than match. * Adding matching options and verbose option to yodals. 2015-01-27 Andy Buckley * Improvements and additions to ROOTCnv.h routines, particularly to TProfile creation: thanks to Roman Lysak for advice. 2015-01-16 Andy Buckley * Add convenience YODA/YODA.h header. 2015-01-15 Andy Buckley * yodascale now uses PointMatcher and can normalize or multiply to abs values or ref histos/bin ranges. 2015-01-05 Andy Buckley * Adding yoda.matcher Python sub-package with PointMatcher functionality. To be used in Professor 2.0 and in yodascale. * Adding 'scat2' type to yodahist. * Add match/unmatch args to all conversion scripts, via a new Python yoda.script_helpers function. * Script updates, improved docstrings, and improved tab completion. 2014-12-10 Andy Buckley * Add a yoda.plotting sub-module, based on matplotlib. 2014-12-03 Andy Buckley * Small build improvements: cleaning test1.root from the yoda2root test, and adding a make target & flag file for mktemplates in pyext/yoda to make sure that it only gets run once. 2014-11-25 Andy Buckley * Handle overflow filling in binned types without invoking an exception. * Change inRange to have non-fuzzy comparison behaviour. 2014-11-11 Andy Buckley * Improving/adding __div__ functions in Python for all binned types. * Add std:: prefix to isinf() calls in BinSearcher.h. 2014-09-30 Andy Buckley * 1.3.0 release! * Use numEntries() rather than effNumEntries() when checking consistency of inputs to efficiency() calculations -- the effNumEntries of a set can be smaller than that of a strict subset, surprisingly! 2014-09-17 Andy Buckley * Small improvements to yodahist and yodaplot behaviours/UIs. * Adding setX/Y/Z(val, err) methods to Point3D. * Add an efficiency method for 2D histos. * Hide fill and fillBin methods from Python mappings of bin types. 2014-09-01 Andy Buckley * YODA 1.2.1 release! 2014-08-29 Andy Buckley * Hide non-const access to bin objects from histogram users: avoids potential for inconsistency between total dbns and in-range bins. * Bug in BinSearcher fixed by Peter Richardson: constructor arguments were passed in the wrong order when constructing a LinEstimator in cases where log binning wouldn't be allowed. 2014-08-26 Andy Buckley * Add protection against / characters in histo names in yoda2root (thanks to Will Bell for the report and suggested patch). 2014-08-17 Andy Buckley * Add +=, -=, *-, /=, ++ and -- operators to Counter, along with an (implicit) constructor from a double -- all for user convenience so Counter can be used in lieu of a simple number. 2014-08-15 Andy Buckley * YODA 1.2.0 release! * Permit +-inf values to be filled into histograms; NaN fills will still explicitly throw an exception. * Add unit tests for Counter, Scatter1D, and Scatter2D, including persistency. * Adding YODA and FLAT format I/O for Scatter1D and Counter (as far as currently possible -- FLAT Counter can't be read due to a #item ambiguity, just like the one between the YODA format Point3D and ProfileBin1D). To be continued... 2014-08-14 Andy Buckley * Python mappings for Dbn0D and Counter, and other improvements. * Adding val() and err() methods to Counter, and errW() and relErrW() to all DbnXD types. * Adding Scatter1D and Point1D types, with conversion from Counter supported. Both Counter and Scatter1D still need to be supported by YODA persistency. 2014-08-11 Andy Buckley * Add ROOT version checking to configure. Thanks to Michael Grosse for the report/request. 2014-08-05 Andy Buckley * Remove all methods not specific about the axis to which they refer, e.g. Histo1D::mean -> xMean. Also remove all related aliases (a nightmare to maintain) and low/highEdge and midpoint functions: use the proper xMin/Max/Mid from now on. This is a significant compatibility breaking API change (and the decision was not taken lightly) so will require a 2nd digit version change. * Lots of adding xMin/Max etc. functions to C++ and Python bin/histo classes. * Compiler pickiness fixes in BinSearcher. * Improvement to linspace, avoiding fuzzyEquals and again making sure that the end value is exact. 2014-07-23 Andy Buckley * Fix to logspace: make sure that start and end values are exact, not the result of exp(log(x)). * Clean-up, minor improvements, and adding a test for BinSearcher and friends. 2014-07-19 Andy Buckley * Various consistency improvements and minor bugfixes to Python mapping utils and Dbn and Bin objects. * Fix Axis2D::reset, which was resetting the total dbn and outflows, but not the bins!!! Thanks to Ewen Gillies for the report. 2014-07-18 Andy Buckley * Add scaleX,Y,Z and scaleXYZ to Point and Scatter classes, and deprecate less explicit/consistent Scatter2D/3D.scale method. 2014-07-17 Andy Buckley * yodascale now writes out rescaled histograms and profiles rather than scatters. * A few more improvements on Point2D/3D, adding x,y,zMin/Max function mappings. * Add first version of a yodascale script, based on code from Simone Amoroso. 2014-07-16 Andy Buckley * More Scatter and 2D histo interface improvement. * Remove 'return *this' from Scatter2D/3D add and combine methods. * Add unit test checks for 1D and 2D mkScatter functions. * Improve Scatter2D/3D C++ and Python interfaces. * Add Scatter3D Python mapping. 2014-07-15 Andy Buckley * Add auto-parsing of yes/no/on/off/true/false as bools in the Python ao.annotation() function. * Add parsing of yodaplot styles from command line args and analysis object annotations. 2014-07-12 Andy Buckley * Fix infinite recursions in Python wrappers for Point2D and Point3D, and make the Python Point3D interface more standard. * Add yodaplot script for basic plotting, using pgfplots as a backend. 2014-07-10 Andy Buckley * Add mkScatter(Scatter2D) and mkScatter(Scatter3D) functions and Python mappings: this allows all AOs to be used as args to mkScatter(...) without needing to check if they already are scatters. 2014-07-02 Andy Buckley * Set y value and/or error to 0 in mkScatter(Histo1D) if an exception is thrown when calculating the appropriate values. Need an optional param to control this error handling behaviour between set-zero and skip-bin? 2014-07-01 Andy Buckley * Add exception translation to the mkScatter functions. * Add -m/-M match/unmatch options to yodacnv -- useful for filtering histogram file contents in a YODA->YODA conversion. 2014-06-24 Andy Buckley * Don't complain about merge assumptions if there is only one object with that path name to be 'merged' 2014-06-17 Andy Buckley * Adding explicit int cast in Python wrapping of numEntries functions. 2014-06-13 Andy Buckley * Adding yodals script to list data file contents. 2014-06-11 David Grellscheid * pyext/yoda/Makefile.am: 'make distcheck' and out-of-source builds should work now. 2014-06-10 Andy Buckley * Fix use of the install command for bash completion installation on Macs. 2014-06-06 Andy Buckley * YODA 1.1.0 release. Middle version number change to reflect API changes w.r.t 1.0.6. * Adding unit tests against ref data for yodamerge in make check. 2014-06-04 Andy Buckley * Fix silly typos in yodamerge which somehow made it past "make check" testing :-( 2014-06-02 Andy Buckley * YODA 1.0.7 release. DO NOT USE: prefer 1.1.0, above. 2014-05-30 Andy Buckley * Removing 'foreach' macro definition and using raw BOOST_FOREACH instead until C++11 is allowed. * Adding pytest-p1d and pytest-p2d tests, and FLAT writing/reading in pytests. * Tweaking WriterFLAT and adding ReaderFLAT functionality for 2D histos and profiles. 2014-05-30 Holger Schulz * Some basic (FLAT) write-out capability for 2D histos 2014-05-29 Andy Buckley * Improvements to the yodahist script, including support for 2D histograms and input files. * Adding bin edges constructors for all 1D and 2D histos in Python. 2014-05-22 Andy Buckley * Improvements to Profile2D and Point2D interfaces in Python. * Add a single-file at a time yodacnv multi-format converter script (thanks to Andrii Verbytskyi for the suggestion). 2014-05-19 Andy Buckley * Typo fixes in Profile2D YODA-format parsing: 2D histo tests now pass! * Adding Python tests for Histo2D and Profile2D. * Adding a YODA/Predicates.h header and using it in Axis2D bin edge construction. * Enabling Profile2D writing in various Writers, and a bit of IO code tidying. 2014-05-17 Andy Buckley * Disable writing out of Histo2D and Profile2D outflows for now, while they are redesigned, and get 2D I/O working for the in-range part. 2014-05-14 Andy Buckley * Mapping the divide and efficiency functions into the Python interface as class methods, including the __div__ special function. 2014-05-13 Andy Buckley * Add the AnalysisObject::type() method back in Python (even though type(ao) is more Pythonic, this may be useful) 2014-05-06 Andy Buckley * Add Profile2D YODA format writing, note need for Scatter format change, add sumXY storage to both 2D histo types. 2014-05-03 Andy Buckley * Adding YODA reader functionality for Histo2D and Profile2D, but without outflows support yet. 2014-04-25 Andy Buckley * Adding simple command line yodahist script for quick 1D histogramming from plain text files, with weight support. More development to come! * Mapping linspace, logspace and a few stat functions into Python. 2014-04-24 Andy Buckley * Fixes, script installation, and detailed numerical comparisons in yodadiff. 2014-04-17 Andy Buckley * Change AnalysisObject::annotations to return the list of annotation keys rather than the map, since the previous behaviour mapped very badly into Python. 2014-04-16 Andy Buckley * Add special case handling for 2-arg use of x2y scripts where the second arg is -, for stdin. This will be treated as writing out to stdout, not converting two files one of which is stdin. 2014-04-15 Andy Buckley * Adding a more portable version of getline to be used in the YODA file parsing to avoid falling over on DOS-produced input files. 2014-04-14 Andy Buckley * Adding the namespace protection workaround for Boost described at http://www.boost.org/doc/libs/1_55_0/doc/html/foreach.html 2014-04-13 Andy Buckley * Adding an assumed-equal-run-size, ratio-like merging heuristic for Scatter2Ds to yodamerge, and renaming the --normalize-all flag to --assume-normalized. * Adding and installing a pkg-config data file for YODA. * Rationalising (and fixing?) the yodamerge logic re. user & normalization scalings, and making way for a Scatter2D merging heuristic. 2014-03-10 Andy Buckley * YODA 1.0.6 release. 2014-03-06 Andy Buckley * Improvements to AnalysisObject annotation handling in Python. * Adding rescaling arguments to yodamerge (and scale function to Python Scatter2D). * Better documentation and consistency of Histo and Profile Python wrappers, and removing some inappropriate attributes. * Adding clone() and newclone() functions to all analysis object classes. 2014-02-28 Andy Buckley * Cython mapping improvements, esp. adding numPoints/numBins functions and better Scatter2D __repr__. * Adding mapping of the mkScatter functions into Python (as methods on Histo1D and Profile1D rather than the original free functions, at least for now: some Python type-identifying boilerplate is needed to make a single mkScatter function work in Python) 2014-02-27 Leif Lönnblad * Minor modifications to BinSearcher to avoid NaN's. The NaN's were treated correctly before, but better to avoid them all together. 2014-02-27 Andy Buckley * Adding the generated pyext/yoda/rootcompat.cpp to the tarball and sorting out the ROOT/Cython interface conditionals a bit more sanely. Thanks to Oldrich Kepka for the bug report. * Protecting yodamerge against input histograms with zero integrals (thanks to Christian Bierlich for the bug report). 2014-02-14 Frank Siegert * Fix race condition with mktemplates. 2014-02-12 David Grellscheid * Fix bug in mktemplates code (thanks to Christian Johnson for the bug report). 2014-02-09 Andy Buckley * Adding explicit include/generated dir creation to Python extension build (thanks to Christian Johnson for the bug report). 2014-02-06 Andy Buckley * 1.0.5 release! 2014-02-05 Andy Buckley * Adding patches to ReaderFLAT and ReaderYODA use of Boost Spirit which reduce the Boost version requirement from 1.47 -> 1.41. Thanks to Andrii Verbytskyi for the patch. * Protect against invalid prefix value if the --prefix configure option is unused. 2014-02-04 Andy Buckley * Adding copy assignment operators where missing, based on an implementation in AnalysisObject which only copies rvalue paths and titles if they are non-null. 2014-02-03 Andy Buckley * Improving (i.e. increasing) bin edge overlap tolerance: 1e-10 relative was too tight. 2014-01-31 Andy Buckley * Adding x/yMid etc. methods on Bin1D and 2D, and more related Python API improvements. 2014-01-28 Andy Buckley * Adding missing fillBin methods to 1D and 2D Histo/Profile Python classes. * Fixed yodamerge default output file name treatment. * Avoid computing an unrecoverable error in Histo1D.__repr__ * Clean-ups and API improvements in Python IO functions. * Adding more sumW,W2 and (eff)NumEntries attrs to Python objects. 2013-12-17 Andy Buckley * Improved argument handling for x2y scripts. 2013-11-16 Andy Buckley * Fix to build the Cython rootcompt extension .cpp on request. 2013-11-14 Andy Buckley * Adding flags for the C++11 or C++0x standard if supported, cf. Rivet. 2013-10-24 Andy Buckley * YODA 1.0.4 release. * Supporting zsh completion via bash completion compatibility. 2013-10-21 Andy Buckley * Removing unused internal iterator typedefs from Writer functions. 2013-10-18 Andy Buckley * Adding a yodaenv.sh sourceable script to help with environment setup. * Remove Scatters from being handled by yodamerge by blocking the __add__ method fallback. 2013-10-09 Andy Buckley * Improvements to yoda-config and command-line completion, for the Rivet 2.0.0 release. 2013-10-09 Andy Buckley * Version 1.0.3 release. 2013-10-04 Andy Buckley * Cython mapping improvements. * Adding some improved heuristics and a --normalize-all option to yodamerge. Frank S is now happy again ;-) 2013-10-01 Andy Buckley * Adding operator +, -, +=, -= Python mappings wherever possible for Histo1/2D, Profile1/2D, and Scatter2D. 2013-09-26 Andy Buckley * Cython is no longer needed by tarball users. 2013-09-25 Andy Buckley * Unset path of returned histogram if those of the args to add() and subtract() are difference. 2013-09-24 Andy Buckley * Python mapping improvements. 2013-09-23 Andy Buckley * Add the -avoid-version flag to libtool. * Adding more add and subtract special methods in Python. 2013-09-22 Andy Buckley * mkScatter schanged to use histo midpoints rather than focuses by default for the point x value. 2013-08-14 Andy Buckley * Version 1.0.2. * Some exception message improvements and improving the protection of cosmetic mean calculations in WriterYODA. 2013-07-12 Andy Buckley * Adding ROOT detection in configure and otherwise updating Dave's rootcompat module so that it'll compile. There might be an inconvenient ROOT version dependency in the signature of one of the PyROOT API functions that is used as a shim :-( 2013-06-17 Andy Buckley * Adding yoda.m4 from James Robinson. 2013-06-06 Hendrik Hoeth * Improve "==" operator in Axis1D and Axis2D 2013-06-06 Andy Buckley * Adding fillBin() methods to all 1D and 2D histos, and noting that Bin types need a back-link to their axis to maintain consistency. * Release of version 1.0.1 2013-06-05 Andy Buckley * Change the divide(Scatter, Scatter) behaviours to use the midpoint of the num/denom bins rather than mean of foci for the output point position (and hence errors, too). * Adding a toIntegralEfficiencyHisto function. * Adding another Histo1D::integral() function, this time from 0 -> i, maybe including the underflow. 2013-06-04 Andy Buckley * Updating the Cython version requirement to 0.18 2013-06-03 Andy Buckley * Adding relErr functions to 1D and 2D histo and profile bins, and being careful about div by zero. * Improvements in error treatment in division (better handling of zeros). * Renaming merge-histos to yodamerge and installing it (and improving the usage string a bit). 2013-05-31 Andy Buckley * Adding the Counter type, and ability to output it from the YODA writer. * Adding numEntries and effNumEntries methods to 1D and 2D Histo and Profile classes. * Adding Dbn0D and using it to implement Dbn1D. 2013-05-30 Andy Buckley * Fixing several nasty errors in argument ordering for Point{1,2,3}D construction in Scatter addPoint functions. * Adding abs(...) to the returned Dbn1D::variance(), to avoid problems when negative weights produce negative variance. No, we don't like this either: is there a more correct way? * Fixing the efficiency(Histo1D, Histo1D) implementation, cf. http://root.cern.ch/phpBB3/viewtopic.php?t=3753 * Adding mkScatter(Profile2D) 2013-05-29 Hendrik Hoeth * Adding a reader for FLAT files and a flat2yoda converter 2013-05-27 Andy Buckley * Adding a yoda-completion file for bash. 2013-05-17 Andy Buckley * Mapping HistoBin1D.relErr in Python. * Adding a non-const points() accessor to Scatter2D. 2013-05-13 Andy Buckley * Adding combined value+error setX/Y functions on Point2D. * Adding HistoBin1D::relErr() 2013-04-23 Andy Buckley * Adding Python output handling for single AOs and to be able to use a "-" filename to mean stdout. 2013-04-12 Andy Buckley * Releasing version 1.0.0 -- it seems stable enough. 2013-04-10 Andy Buckley * Being more careful about adding -Wno-* flags to the C++ compiler used to built the Cython extension lib. 2013-03-22 Andy Buckley * Removing the use of svn:external to pull in Boost macros and using a minimal local set instead. * Using the nice Boost-finding macros from https://github.com/tsuna/boost.m4 and tidying up configure.ac 2013-03-15 Andy Buckley * Re-organising the C++ side of the auto-format I/O functions, into a new IO header and separated from the Reader.h and Writer.h. I'm tempted to say that users shouldn't really NEED to ever directly touch the Reader and Writer objects... * Adding auto-format read and write functions. I will probably change the API. Python mappings have been provided, but the string workarounds were too much of a pain with Cython 0.16 so I have updated the Cython version requirement to 0.17 where it is automatic and hence much cleaner. 2013-03-08 Andy Buckley * Making the x2y converter scripts write a copy into the *current* directory if only the input is specified. 2013-03-05 Andy Buckley * Removing Plot entirely from YODA: it was an anomaly only added to make plot file generation easy, but this is now done better via StringIO (in new compare-histos/rivet-cmphistos). * Removing the Plot from Cython... and soon from YODA itself: we'll do this stuff manually and less hackily. * Make Cython automatically add a copy of the original call signature to each function's docstring. 2013-03-04 Andy Buckley * Adding Plot mapping to Cython and improving the AO annotations handling in Python. * Adding PLOT section writing to WriterFLAT (and WriterYODA, although that might be a bad idea...) * Adding aida2yoda and aida2flat converter scripts. 2013-02-02 David Mallows * Adding support for Python >= 2.4 (was Python >= 2.6) * Fixing miscellaneous warnings on GCC 4.1 2013-01-30 Andy Buckley * Adding a points() method to the Python Scatter2D wrapper. * Adding a virtual destructor to Bin. 2012-12-30 Andy Buckley * Adding support for Boost.Range arguments and file format autodetection in Writer. 2012-11-24 Andy Buckley * Bump version to 0.6beta0 * Adding more ROOT converters. Who knows how to make TProfiles from scratch, but Histo1D and Scatter2D are covered, which should be enough to get started with, at least. 2012-11-16 Andy Buckley * Adding yoda-config 2012-11-16 Hendrik Hoeth * Adding WriterFLAT and yoda2flat 2012-11-16 Andy Buckley * Adding YODA/ROOTCnv.h. for data object converter functions. Two (untested) functions added for TH1 -> YODA. * Adding toIntegralHisto(Histo1D&) function. 2012-11-15 Dave Mallows * Commited numerous changes to Axis2D. Axis2D now uses BinSearcher as with Axis1D. 2012-11-15 Andy Buckley * Improving division and efficiency treatments, and allowing arbitrary f(x), f(y), and flip transformations on Scatter2D. 2012-11-14 Andy Buckley * Converting linspace, logspace, and their usage to place the nbins argument first. 2012-08-07 Andy Buckley * Removing unused (beyond 2nd order) sumWXYZ counter from Dbn3D. 2012-08-07 Dave Mallows * Converted Axis1D to use new Utils/BinSearcher. 2012-08-02 Dave Mallows * Heavily refactored Cython bindings * HistoBin1D, ProfileBin1D etc. now inherit from Bin1D[DBN] * Temporarily removed Histo2D, Profile2D and Scatter3D mappings. 2012-07-23 Andy Buckley * Installing scripts from bin dir, and making the yoda2aida interface nicer. * Adding Cython mappings for Dbn3D and Profile2D, and other fixes/improvements. 2012-07-22 Andy Buckley * Adding Cython mappings for Scatter3D and ProfileBin2D. * Fixing more crap code legacy from old 2D plot implementation, this time in Scatter3D. 2012-07-19 Andy Buckley * Adding stdErr for Histo2D + Python mapping, and more Cython improvements. * Adding path/title-only AO constructors and making nice Python constructor for Histo2D. * Cython mapping improvements & additions for Point3D + Scatter3D. * Removing mixed symm/asymm constructors on Point*D & Scatter*D classes. 2012-07-12 Andy Buckley * Reintroducing Profile2D and Scatter3D. * Adding axis locking to Axis2D. * Supporting Histo2D in WriterYODA. 2012-07-02 Andy Buckley * More incremental progress toward a working 2D bin hash mechanism. 2012-05-03 Andy Buckley * Adding nice constructor behaviours to the Histo1D and Profile2D Python interfaces, and adding the mkScatter operation for Profile1D. * Adding more default constructors for analysis objects, to allow member variable and STL container use without pointers. 2012-05-02 Andy Buckley * A much simplified and more robust rewrite of the Axis1D class, just using STL map in place of the hand-written bin edge caching. * Improvements (I hope) to the binary search in Axis1D, and providing an experimental default constructor for Histo1D. 2011-12-08 Hendrik Hoeth * ReaderYODA can now parse Histo1D and Profile1D flat files 2011-12-08 Andy Buckley * Adding a Utils::ndarray object and using it to implement a general Scatter system, with generalised Point and Error to boot. 2011-12-07 Hendrik Hoeth * Lots of cleanup 2011-12-07 Andy Buckley * Mapping the Dbn1D and Dbn2D classes into Python. * Adding an outflows() accessor to Histo2D. * Writing out total dbn lines for Histo1D and Profile1D in the YODA format, and now writing out the 'cross-terms' for Profile1D, too. * Properly adding Dbn1D accessors for Histo1D. * Updating the Cython mappings to provide the totalDbn() methods and add a placeholder mapping for Dbn2D. Completed mappings are needed for Dbn{1,2,3}D and the Profile types. * Adding totalDbn() accessors to data types. 2011-12-06 Andy Buckley * Making Histo1D/2D::scaleW() write a ScaledBy annotation. * Adding annotation-fetching methods with a default return value argument to AnalysisObject. * Adding normalize() methods to Histo1D/2D. * Adding weighted RMS calculating methods to Dbn1D, Dbn2D and Bin1D/2D. 2011-09-03 Dave Mallows * Fixed ReaderAIDA: x-value and low y-error interchanged when filling Scatter2D. * Changed to Cython for Python bindings: Swig bindings were in need of serious amounts of work. Cython should provide a means to provide more Pythonic bindings to YODA. A minimal subset of ReaderAIDA, Scatter2D and Point2D have been wrapped. * Modified configure.ac, Makefile.am and pyext/Makefile.am to reflect change to Cython. Added cython.m4 from python-efl (Part of the enlightenment project; LGPL) 2011-08-31 Dave Mallows * Fixed python tests by installing python extension to pyext/build 2011-08-23 Andy Buckley * Adding rebinning interface to Histo1D and Profile1D, and adding a test (and a new test feature for output message formatting) * Adding first implementation of 1D bin merging to Axis1D. 2011-08-22 Andy Buckley * Adding copy constructors and assignment operators to Histo1D, Profile1D, and Scatter2D, and their respective bins/points. * Remove use of sign(weight) in filling sum(w2) -- I think this was an historical attempt based on a scaling axiom which turned out to be inappropriate. * Reworking the Bin1D inheritance and composition design so that all bin types store a single distribution object -- a Dbn1D for histo bins and a Dbn2D for profile bins. 2011-08-18 Andy Buckley * Removing the Profile1D -> ProfileBin1D friendship. This is very heartening -- the fewer friend declarations we need, the more indication that the class structure is not pathological! (Or that we've just made everything public... but we haven't) 2011-08-15 Andy Buckley * Inlining all functions in HistoBin1D, ProfileBin1D, and HistoBin2D. * Converting Dbn2D to be composed of two Dbn1Ds and a cross-term. Also tidying the interfaces of the 2D classes and the scaleX/Y methods throughout, and adding Doxygen comments. 2011-08-12 Andy Buckley * Adding proper Doxygen structures and full descriptive comments to Dbn2D. * Adding the persistency state-setting constructors for Profile1D and Dbn2D. * Inlining lots of methods on Dbn1D. Same should be done for Dbn2D, but first it needs to be reimplemented in terms of two Dbn1Ds + the cross-term. 2011-08-11 Andy Buckley * Various typo fixes and comments relating to persistency constructors, Histo2D slicing, etc. * Changing the HistoBin1D state-setting constructors (aargh, these should *not* have already existed) to take Dbn1D as an argument rather than a long list of doubles. 2011-08-01 Andy Buckley * Adding tests to check that implicit construction of Weights objects from literal doubles and ints works. 2011-07-28 Andy Buckley * Bumping version number to 0.4.0beta0 -- there have been substantial changes recently and YODA is now in a state where it should be interesting for outsiders to start playing with it. * Templating the Axis1D on the distribution type to be used for total and under/overflow statistics: Profile1D now has Dbn2D objects handling its total and overflow statistics. 2011-07-26 Andy Buckley * Added a Histo1D::integral(index1, index2) method. Not sure how or if to extend this to Profile1D. * Implementing incomplete Scatter2D operator+ and operator- functions. 2011-07-25 Andy Buckley * Adding a Weights class, designed to seamlessly replace double-type weights and weighted moments with a named and vectorised form. 2011-07-19 Andy Buckley * Add Profile1D and Scatter2D division operators. * Add xMin/xMax synonyms to the Axis1D, cf. the bins. 2011-07-18 Andy Buckley * Add a first stab at a Histo1D/Histo1D division operator. 2011-07-10 Andy Buckley * Add construction of Histo1Ds from Profile1D and Histo1D, and construction of Profile1Ds from those and Scatter2D. 2011-07-07 Andy Buckley * Add construction of a Histo1D from Scatter2D. 2011-06-15 Andy Buckley * Making the AIDA reader work, including reading of annotations and a few tweaks to the simple type persistency system. Test histo 1b updated. 2011-06-12 Andy Buckley * Removing Histo1D::area * Filling and using under/overflow and total db on Histo1D, and adding boolean arg to integral, sumW, etc. * Fixing for C++ change in behaviour of std::make_pair * Adding addAnnotation, and mapping annotations to Python. 2011-02-22 Andy Buckley * Use distutils rather than setuptools for the Python interface build. * Renaming Bin, HistoBin and ProfileBin to be Bin1D, HistoBin1D, ProfileBin1D. Bin is now a top-level abstract class with minimal functionality to be shared between 1D and 2D bins. 2011-01-12 Andy Buckley * Type annotations in mkScatter * Added many vector constructors and addPoint functions to Scatter2D. 2011-01-11 Andy Buckley * Add lexical_cast support to annotation get and set functions. * Write out annotations in AIDA format, and copy annotations in mkScatter -- using a new AnalysisObject::setAnnotations method. * Convert DPS output to use interim Scatter construction * Make (unused) yoda.plot subpackage. * Write out annotations in YODA format. * Make Scatter2D representations of Histo1D and Profile1D. * Write out Scatter2D objects in AIDA and YODA formats. * Make Scatter2D and Point2D work. Add a few extra methods... evolution and tweaking required. 2011-01-10 Andy Buckley * Add Boost checks and header includes. Not used yet. * Hide Utils:: content from Doxygen and nvector -> Utils::nvector. * Removing unused YAML stuff: we aren't going that way for persistency anymore. * Renaming Axis -> Axis1D * Removing dead-end templated Scatter stuff. * Move (generated) config files into the Config subdir. * Move sortedvector and indexedset into the Utils dir. * Move the "utils" directory and namespace to "Utils" * Put the Doxyfile under configure control by moving it to Doxyfile.in and using the @PACKAGE_VERSION@ token. * Make Doxygen find the .icc file and hide functions with name _* and in the YAML namespace. * Removing the Binning argument and enum in favour of explicit bin edge vectors, possibly produced explicitly via the MathUtils linspace and logspace functions, or the new Axis::mkBinEdgesLin/Log alias functions. * Fixed Axis, Histo1D and Profile1D constructors, by adding a path argument, passing the path and title args to the AnalysisObject base constructor properly. * Removed several old and unused files such as Tree.h 2011-01-09 Andy Buckley * Updating copyright comments to be valid into 2011. * Persistency fixes, and changing the interface to use the annotated path. * Using annotations for path and title. * Adding tests of collection and iterator range AO writing. * Adding static write functions on Writer*.h implementations to avoid needing to make an explicit Writer object via the create() functions. * Rename Exception.h -> Exceptions.h * Added AnnotationError. * Re-enable persistency of collections with begin/end iterators. 2011-01-08 Andy Buckley * Enabling quiet compilation. * More annotation functionality. 2011-01-07 Andy Buckley * Sorting out autoheaders to be more useful. * Rewriting AIDA writer to use DPS representation (no reloading) for Histo1D and Profile1D objects. * Adding persistency system hooks, since RTTI just sucks too much. * Renaming test files to have more meaningful names. 2010-12-10 Andy Buckley * Some tweaks to Axis, Bin, etc. to use the sortedvector. Seems to be working! (I must be checking it wrongly...) * Adding another candidate object for the axis bin container: a sorted extension to STL std::vector with an insert method. This will do as a development placeholder: a proper sorted & indexed container may be substituted later. * Fix test code: titles are no longer given as histogram constructor arguments. 2010-11-21 Andy Buckley * Adding indexed set for holding bins on axes. Still not sure it's what we want, as (I just realised) STL sets are iterator-immutable because they are self-keyed and changes to elements would also change their sorting. 2010-09-19 Andy Buckley * Restarting ChangeLog contributions! Many changes in the huge time since last update... activity on YODA has renewed and we have a better picture of the distinctive features we require. New idea: named weight vector filling, allowing "parallel" histograms for various event weight variations. I/O remains an awkward issue, especially since the classes are now much richer than they used to be, and don't know about paths. Output can be easily put on top: not an issue... and we can probably do something with pickling. But reading in from C++? 2008-09-16 Andy Buckley * Moved duplicate Histo1D/Profile1D code on to Axis, making Axis a templated class at the same time. 2008-09-12 Andy Buckley * Started work on a little plotting tool, initially for Herwig++ parton pT cut testing, but incrementally enhancing it to be a command-line quick plotter seems like a good idea. * Added some more test programs... working towards a proper test suite. * Added "no path & title" constructors - you don't always want to write out the histo, since sometimes it's just a good way to gather statistics. * Fixed YODA mapping to allow use of vectors of bins as Python lists. * Added Profile1D functionality. * Fixed Dbn1D to use sign(weight) as part of the "w**2" measure, so that negative weights behave themselves. 2008-05-23 Andy Buckley * Added Dbn1D class to centralise the calculation of statistics from unbounded, unbinned, weighted distributions. 2008-05-15 Andy Buckley * Added Profile1D class. * Fixed NaN errors from zero weights. 2008-04-14 Andy Buckley * Python SWIG interface now compiles and can be used: the subtlety that was breaking it was that SWIG has to be prodded in pretty non-obvious ways to make std::vectors of classes without default (no-arg) constructors. See http://osdir.com/ml/programming.swig/2004-04/msg00011.html for about the only reference to this to be found anywhere! * Basic AIDA writer now available - it doesn't yet output all the necessary information though, especially not for merging parallel runs. diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,30 +1,30 @@ SUBDIRS = tinyxml yamlcpp . lib_LTLIBRARIES = libYODA.la libYODA_la_SOURCES = \ Exceptions.cc \ Reader.cc \ ReaderYODA.cc \ ReaderFLAT.cc \ ReaderAIDA.cc \ Writer.cc \ WriterYODA.cc \ WriterFLAT.cc \ WriterAIDA.cc \ Dbn0D.cc \ Dbn1D.cc \ Counter.cc \ Histo1D.cc \ Histo2D.cc \ Profile1D.cc \ Profile2D.cc \ Scatter1D.cc \ Scatter2D.cc \ Scatter3D.cc libYODA_la_LDFLAGS = -avoid-version libYODA_la_LIBADD = $(builddir)/tinyxml/libyoda-tinyxml.la $(builddir)/yamlcpp/libyoda-yaml-cpp.la -libYODA_la_CPPFLAGS = $(AM_CPPFLAGS) -DTIXML_USE_STL -I$(srcdir)/yamlcpp -I$(srcdir) -DYAMLCPP_API=3 -DYAML_NAMESPACE=YODA_YAML +libYODA_la_CPPFLAGS = $(AM_CPPFLAGS) -DTIXML_USE_STL -I$(srcdir)/yamlcpp -I$(srcdir) -DYAML_NAMESPACE=YODA_YAML EXTRA_DIST = zstr diff --git a/src/ReaderYODA.cc b/src/ReaderYODA.cc --- a/src/ReaderYODA.cc +++ b/src/ReaderYODA.cc @@ -1,557 +1,549 @@ // -*- C++ -*- // // This file is part of YODA -- Yet more Objects for Data Analysis // Copyright (C) 2008-2017 The YODA collaboration (see AUTHORS for details) // #include "YODA/ReaderYODA.h" #include "YODA/Utils/StringUtils.h" #include "YODA/Utils/getline.h" #include "YODA/Exceptions.h" #include "YODA/Config/DummyConfig.h" #include "YODA/Counter.h" #include "YODA/Histo1D.h" #include "YODA/Histo2D.h" #include "YODA/Profile1D.h" #include "YODA/Profile2D.h" #include "YODA/Scatter1D.h" #include "YODA/Scatter2D.h" #include "YODA/Scatter3D.h" #include "yaml-cpp/yaml.h" #ifdef YAML_NAMESPACE #define YAML YAML_NAMESPACE #endif #ifdef HAVE_LIBZ #define _XOPEN_SOURCE 700 #include "zstr/zstr.hpp" #endif #include #include using namespace std; namespace YODA { /// Singleton creation function Reader& ReaderYODA::create() { static ReaderYODA _instance; return _instance; } namespace { /// Fast ASCII tokenizer, extended from FastIStringStream by Gavin Salam. class aistringstream { public: // Constructor from char* aistringstream(const char* line=0) { reset(line); } // Constructor from std::string aistringstream(const string& line) { reset(line); } // Re-init to new line as char* void reset(const char* line=0) { _next = const_cast(line); _new_next = _next; _error = false; } // Re-init to new line as std::string void reset(const string& line) { reset(line.c_str()); } // Tokenizing stream operator (forwards to specialisations) template aistringstream& operator >> (T& value) { _get(value); if (_new_next == _next) _error = true; // handy error condition behaviour! _next = _new_next; return *this; } // Allow use of operator>> in a while loop operator bool() const { return !_error; } private: void _get(double& x) { x = std::strtod(_next, &_new_next); } void _get(float& x) { x = std::strtof(_next, &_new_next); } void _get(int& i) { i = std::strtol(_next, &_new_next, 10); } // force base 10! void _get(long& i) { i = std::strtol(_next, &_new_next, 10); } // force base 10! void _get(unsigned int& i) { i = std::strtoul(_next, &_new_next, 10); } // force base 10! void _get(long unsigned int& i) { i = std::strtoul(_next, &_new_next, 10); } // force base 10! void _get(string& x) { /// @todo If _next and _new_next become null? while (std::isspace(*_next)) _next += 1; _new_next = _next; while (!std::isspace(*_new_next)) _new_next += 1; x = string(_next, _new_next-_next); } char *_next, *_new_next; bool _error; }; } void ReaderYODA::read(istream& stream_, vector& aos) { #ifdef HAVE_LIBZ // NB. zstr auto-detects if file is deflated or plain-text zstr::istream stream(stream_); #else istream& stream = stream_; #endif // Data format parsing states, representing current data type /// @todo Extension to e.g. "bar" or multi-counter or binned-value types, and new formats for extended Scatter types enum Context { NONE, //< outside any data block SCATTER1D, SCATTER2D, SCATTER3D, COUNTER, HISTO1D, HISTO2D, PROFILE1D, PROFILE2D }; /// State of the parser: line number, line, parser context, and pointer(s) to the object currently being assembled unsigned int nline = 0; string s; Context context = NONE; // AnalysisObject* aocurr = NULL; //< Generic current AO pointer vector h1binscurr; //< Current H1 bins container vector h2binscurr; //< Current H2 bins container vector p1binscurr; //< Current P1 bins container vector p2binscurr; //< Current P2 bins container vector pt1scurr; //< Current Point1Ds container vector pt2scurr; //< Current Point2Ds container vector pt3scurr; //< Current Point3Ds container Counter* cncurr = NULL; Histo1D* h1curr = NULL; Histo2D* h2curr = NULL; Profile1D* p1curr = NULL; Profile2D* p2curr = NULL; Scatter1D* s1curr = NULL; Scatter2D* s2curr = NULL; Scatter3D* s3curr = NULL; std::vector variationscurr; string annscurr; // Loop over all lines of the input file aistringstream aiss; bool in_anns = false; string fmt = "1"; //int nfmt = 1; while (Utils::getline(stream, s)) { nline += 1; // CLEAN LINES IF NOT IN ANNOTATION MODE if (!in_anns) { // Trim the line Utils::itrim(s); // Ignore blank lines if (s.empty()) continue; // Ignore comments (whole-line only, without indent, and still allowed for compatibility on BEGIN/END lines) if (s.find("#") == 0 && s.find("BEGIN") == string::npos && s.find("END") == string::npos) continue; } // STARTING A NEW CONTEXT if (context == NONE) { // We require a BEGIN line to start a context if (s.find("BEGIN ") == string::npos) { stringstream ss; ss << "Unexpected line in YODA format parsing when BEGIN expected: '" << s << "' on line " << nline; throw ReadError(ss.str()); } // Remove leading #s from the BEGIN line if necessary while (s.find("#") == 0) s = Utils::trim(s.substr(1)); // Split into parts vector parts; istringstream iss(s); string tmp; while (iss >> tmp) parts.push_back(tmp); // Extract context from BEGIN type if (parts.size() < 2 || parts[0] != "BEGIN") { stringstream ss; ss << "Unexpected BEGIN line structure when BEGIN expected: '" << s << "' on line " << nline; throw ReadError(ss.str()); } // Second part is the context name const string ctxstr = parts[1]; // Get block path if possible const string path = (parts.size() >= 3) ? parts[2] : ""; // Set the new context and create a new AO to populate /// @todo Use the block format version for (occasional, careful) format evolution if (Utils::startswith(ctxstr, "YODA_COUNTER")) { context = COUNTER; cncurr = new Counter(path); aocurr = cncurr; } else if (Utils::startswith(ctxstr, "YODA_SCATTER1D")) { context = SCATTER1D; s1curr = new Scatter1D(path); aocurr = s1curr; } else if (Utils::startswith(ctxstr, "YODA_SCATTER2D")) { context = SCATTER2D; s2curr = new Scatter2D(path); aocurr = s2curr; } else if (Utils::startswith(ctxstr, "YODA_SCATTER3D")) { context = SCATTER3D; s3curr = new Scatter3D(path); aocurr = s3curr; } else if (Utils::startswith(ctxstr, "YODA_HISTO1D")) { context = HISTO1D; h1curr = new Histo1D(path); aocurr = h1curr; } else if (Utils::startswith(ctxstr, "YODA_HISTO2D")) { context = HISTO2D; h2curr = new Histo2D(path); aocurr = h2curr; } else if (Utils::startswith(ctxstr, "YODA_PROFILE1D")) { context = PROFILE1D; p1curr = new Profile1D(path); aocurr = p1curr; } else if (Utils::startswith(ctxstr, "YODA_PROFILE2D")) { context = PROFILE2D; p2curr = new Profile2D(path); aocurr = p2curr; } // cout << aocurr->path() << " " << nline << " " << context << endl; // Get block format version if possible (assume version=1 if none found) const size_t vpos = ctxstr.find_last_of("V"); fmt = vpos != string::npos ? ctxstr.substr(vpos+1) : "1"; // cout << fmt << endl; // From version 2 onwards, use the in_anns state from BEGIN until --- if (fmt != "1") in_anns = true; } else { //< not a BEGIN line // Throw error if a BEGIN line is found if (s.find("BEGIN ") != string::npos) ///< @todo require pos = 0 from fmt=V2 throw ReadError("Unexpected BEGIN line in YODA format parsing before ending current BEGIN..END block"); // FINISHING THE CURRENT CONTEXT // Clear/reset context and register AO /// @todo Throw error if mismatch between BEGIN (context) and END types if (s.find("END ") != string::npos) { ///< @todo require pos = 0 from fmt=V2 switch (context) { case COUNTER: break; case HISTO1D: h1curr->addBins(h1binscurr); h1binscurr.clear(); break; case HISTO2D: h2curr->addBins(h2binscurr); h2binscurr.clear(); break; case PROFILE1D: p1curr->addBins(p1binscurr); p1binscurr.clear(); break; case PROFILE2D: p2curr->addBins(p2binscurr); p2binscurr.clear(); break; case SCATTER1D: s1curr->addPoints(pt1scurr); pt1scurr.clear(); break; case SCATTER2D: s2curr->addPoints(pt2scurr); pt2scurr.clear(); break; case SCATTER3D: s3curr->addPoints(pt3scurr); pt3scurr.clear(); break; case NONE: break; } // Set all annotations try { - // YAML::Node anns = YAML::Load(annscurr); - istringstream iss(annscurr); - YAML::Parser parser(iss); - YAML::Node anns; - parser.GetNextDocument(anns); - for (YAML::Iterator it = anns.begin(); it != anns.end(); ++it) { - string key, val; - it.first() >> key; + YAML::Node anns = YAML::Load(annscurr); + // for (YAML::const_iterator it = anns.begin(); it != anns.end(); ++it) { + for (const auto& it : anns) { + const string key = it.first.as(); + // const string val = it.second.as(); YAML::Emitter em; - em << YAML::Flow ; - em << it.second(); - val = em.c_str(); - // cout << "@@@ '" << key << "', '" << val << "'" << endl; + em << YAML::Flow << it.second; //< use single-line formatting, for lists & maps + const string val = em.c_str(); + // // The Variations annotation is just a placeholder to help collect the right columns // Don't want to be saving it to the actual AO, since the method variations() // provides the info that's needed without needing to keep the annotation up to date if (!(key.find("Variations") != string::npos)) aocurr->setAnnotation(key, val); } } catch (...) { /// @todo Is there a case for just giving up on these annotations, printing the error msg, and keep going? As an option? const string err = "Problem during annotation parsing of YAML block:\n'''\n" + annscurr + "\n'''"; // cerr << err << endl; throw ReadError(err); } annscurr.clear(); variationscurr.clear(); in_anns = false; // Put this AO in the completed stack aos.push_back(aocurr); // Clear all current-object pointers aocurr = nullptr; cncurr = nullptr; h1curr = nullptr; h2curr = nullptr; p1curr = nullptr; p2curr = nullptr; s1curr = nullptr; s2curr = nullptr; s3curr = nullptr; context = NONE; continue; } // ANNOTATIONS PARSING if (fmt == "1") { // First convert to one-key-per-line YAML syntax const size_t ieq = s.find("="); if (ieq != string::npos) s.replace(ieq, 1, ": "); // Special-case treatment for syntax clashes const size_t icost = s.find(": *"); if (icost != string::npos) { s.replace(icost, 1, ": '*"); s += "'"; } // Store reformatted annotation const size_t ico = s.find(":"); if (ico != string::npos) { annscurr += (annscurr.empty() ? "" : "\n") + s; continue; } } else if (in_anns) { if (s == "---") { in_anns = false; } else { annscurr += (annscurr.empty() ? "" : "\n") + s; - // in order to handle multi-error points in scatters, we need to know which variations are stored, if any - // can't wait until we process the annotations at the end, since need to know when filling points - // this is a little inelegant though... + // In order to handle multi-error points in scatters, we need to know which variations are stored, if any + // can't wait until we process the annotations at the end, since need to know when filling points. + // This is a little inelegant though... if (s.find("Variations") != string::npos) { - istringstream iss(s); - YAML::Parser parser(iss); - YAML::Node anns; - parser.GetNextDocument(anns); - for (YAML::Iterator it = anns.begin(); it != anns.end(); ++it) { - for (YAML::Iterator it2 = it.second().begin(); it2 != it.second().end(); ++it2) { - string val; - *it2 >> val; + YAML::Node anns = YAML::Load(s); + for (const auto& it : anns) { + for (const auto& it2 : it.second) { + const string val = it2.second.as(); variationscurr.push_back(val); } } } } continue; } // DATA PARSING aiss.reset(s); // double sumw(0), sumw2(0), sumwx(0), sumwx2(0), sumwy(0), sumwy2(0), sumwz(0), sumwz2(0), sumwxy(0), sumwxz(0), sumwyz(0), n(0); switch (context) { case COUNTER: { double sumw(0), sumw2(0), n(0); aiss >> sumw >> sumw2 >> n; cncurr->setDbn(Dbn0D(n, sumw, sumw2)); } break; case HISTO1D: { string xoflow1, xoflow2; double xmin(0), xmax(0); double sumw(0), sumw2(0), sumwx(0), sumwx2(0), n(0); /// @todo Improve/factor this "bin" string-or-float parsing... esp for mixed case of 2D overflows /// @todo When outflows are treated as "infinity bins" and don't require a distinct type, string replace under/over -> -+inf if (s.find("Total") != string::npos || s.find("Underflow") != string::npos || s.find("Overflow") != string::npos) { aiss >> xoflow1 >> xoflow2; } else { aiss >> xmin >> xmax; } // The rest is the same for overflows and in-range bins aiss >> sumw >> sumw2 >> sumwx >> sumwx2 >> n; const Dbn1D dbn(n, sumw, sumw2, sumwx, sumwx2); if (xoflow1 == "Total") h1curr->setTotalDbn(dbn); else if (xoflow1 == "Underflow") h1curr->setUnderflow(dbn); else if (xoflow1 == "Overflow") h1curr->setOverflow(dbn); // else h1curr->addBin(HistoBin1D(std::make_pair(xmin,xmax), dbn)); else h1binscurr.push_back(HistoBin1D(std::make_pair(xmin,xmax), dbn)); } break; case HISTO2D: { string xoflow1, xoflow2, yoflow1, yoflow2; double xmin(0), xmax(0), ymin(0), ymax(0); double sumw(0), sumw2(0), sumwx(0), sumwx2(0), sumwy(0), sumwy2(0), sumwxy(0), n(0); /// @todo Improve/factor this "bin" string-or-float parsing... esp for mixed case of 2D overflows /// @todo When outflows are treated as "infinity bins" and don't require a distinct type, string replace under/over -> -+inf if (s.find("Total") != string::npos) { aiss >> xoflow1 >> xoflow2; // >> yoflow1 >> yoflow2; } else if (s.find("Underflow") != string::npos || s.find("Overflow") != string::npos) { throw ReadError("2D histogram overflow syntax is not yet defined / handled"); } else { aiss >> xmin >> xmax >> ymin >> ymax; } // The rest is the same for overflows and in-range bins aiss >> sumw >> sumw2 >> sumwx >> sumwx2 >> sumwy >> sumwy2 >> sumwxy >> n; const Dbn2D dbn(n, sumw, sumw2, sumwx, sumwx2, sumwy, sumwy2, sumwxy); if (xoflow1 == "Total") h2curr->setTotalDbn(dbn); // else if (xoflow1 == "Underflow") p1curr->setUnderflow(dbn); // else if (xoflow1 == "Overflow") p1curr->setOverflow(dbn); else { assert(xoflow1.empty()); // h2curr->addBin(HistoBin2D(std::make_pair(xmin,xmax), std::make_pair(ymin,ymax), dbn)); h2binscurr.push_back(HistoBin2D(std::make_pair(xmin,xmax), std::make_pair(ymin,ymax), dbn)); } } break; case PROFILE1D: { string xoflow1, xoflow2; double xmin(0), xmax(0); double sumw(0), sumw2(0), sumwx(0), sumwx2(0), sumwy(0), sumwy2(0), n(0); /// @todo Improve/factor this "bin" string-or-float parsing... esp for mixed case of 2D overflows /// @todo When outflows are treated as "infinity bins" and don't require a distinct type, string replace under/over -> -+inf if (s.find("Total") != string::npos || s.find("Underflow") != string::npos || s.find("Overflow") != string::npos) { aiss >> xoflow1 >> xoflow2; } else { aiss >> xmin >> xmax; } // The rest is the same for overflows and in-range bins aiss >> sumw >> sumw2 >> sumwx >> sumwx2 >> sumwy >> sumwy2 >> n; const double DUMMYWXY = 0; const Dbn2D dbn(n, sumw, sumw2, sumwx, sumwx2, sumwy, sumwy2, DUMMYWXY); if (xoflow1 == "Total") p1curr->setTotalDbn(dbn); else if (xoflow1 == "Underflow") p1curr->setUnderflow(dbn); else if (xoflow1 == "Overflow") p1curr->setOverflow(dbn); // else p1curr->addBin(ProfileBin1D(std::make_pair(xmin,xmax), dbn)); else p1binscurr.push_back(ProfileBin1D(std::make_pair(xmin,xmax), dbn)); } break; case PROFILE2D: { string xoflow1, xoflow2, yoflow1, yoflow2; double xmin(0), xmax(0), ymin(0), ymax(0); double sumw(0), sumw2(0), sumwx(0), sumwx2(0), sumwy(0), sumwy2(0), sumwz(0), sumwz2(0), sumwxy(0), sumwxz(0), sumwyz(0), n(0); /// @todo Improve/factor this "bin" string-or-float parsing... esp for mixed case of 2D overflows /// @todo When outflows are treated as "infinity bins" and don't require a distinct type, string replace under/over -> -+inf if (s.find("Total") != string::npos) { aiss >> xoflow1 >> xoflow2; // >> yoflow1 >> yoflow2; } else if (s.find("Underflow") != string::npos || s.find("Overflow") != string::npos) { throw ReadError("2D profile overflow syntax is not yet defined / handled"); } else { aiss >> xmin >> xmax >> ymin >> ymax; } // The rest is the same for overflows and in-range bins aiss >> sumw >> sumw2 >> sumwx >> sumwx2 >> sumwy >> sumwy2 >> sumwz >> sumwz2 >> sumwxy >> sumwxz >> sumwyz >> n; const Dbn3D dbn(n, sumw, sumw2, sumwx, sumwx2, sumwy, sumwy2, sumwz, sumwz2, sumwxy, sumwxz, sumwyz); if (xoflow1 == "Total") p2curr->setTotalDbn(dbn); // else if (xoflow1 == "Underflow") p2curr->setUnderflow(dbn); // else if (xoflow1 == "Overflow") p2curr->setOverflow(dbn); else { assert(xoflow1.empty()); // p2curr->addBin(ProfileBin2D(std::make_pair(xmin,xmax), std::make_pair(ymin,ymax), dbn)); p2binscurr.push_back(ProfileBin2D(std::make_pair(xmin,xmax), std::make_pair(ymin,ymax), dbn)); } } break; case SCATTER1D: { double x(0), exm(0), exp(0); aiss >> x >> exm >> exp; // set nominal point Point1D thispoint=Point1D(x, exm, exp); // check if we stored variations of this point if (variationscurr.size()>0){ // for each variation, store the alt errors. // start at 1 since we have already filled nominal ! for (unsigned int ivar=1; ivar> exm >> exp; thispoint.setXErrs(exm,exp,thisvariation); } } pt1scurr.push_back(thispoint); } break; case SCATTER2D: { double x(0), y(0), exm(0), exp(0), eym(0), eyp(0); aiss >> x >> exm >> exp >> y >> eym >> eyp; // set nominal point Point2D thispoint=Point2D(x, y, exm, exp, eym, eyp); // check if we stored variations of this point if (variationscurr.size()>0){ // for each variation, store the alt errors. // start at 1 since we have already filled nominal ! for (unsigned int ivar=1; ivar> eym >> eyp; thispoint.setYErrs(eym,eyp,thisvariation); } } pt2scurr.push_back(thispoint); } break; case SCATTER3D: { double x(0), y(0), z(0), exm(0), exp(0), eym(0), eyp(0), ezm(0), ezp(0); aiss >> x >> exm >> exp >> y >> eym >> eyp >> z >> ezm >> ezp; // set nominal point Point3D thispoint=Point3D(x, y, z, exm, exp, eym, eyp, ezm, ezp); // check if we stored variations of this point if (variationscurr.size()>0){ // for each variation, store the alt errors. // start at 1 since we have already filled nominal ! for (unsigned int ivar=1; ivar> ezm >> ezp; thispoint.setZErrs(ezm,ezp,thisvariation); } } pt3scurr.push_back(thispoint); } break; default: throw ReadError("Unknown context in YODA format parsing: how did this happen?"); } // cout << "AO CONTENT " << nline << endl; // cout << " " << xmin << " " << xmax << " " << ymin << " " << ymax << " / '" << xoflow1 << "' '" << xoflow2 << "' '" << yoflow1 << "' '" << yoflow2 << "'" << endl; // cout << " " << sumw << " " << sumw2 << " " << sumwx << " " << sumwx2 << " " << sumwy << " " << sumwy2 << " " << sumwz << " " << sumwz2 << " " << sumwxy << " " << sumwxz << " " << sumwyz << " " << n << endl; // cout << " " << x << " " << y << " " << z << " " << exm << " " << exp << " " << eym << " " << eyp << " " << ezm << " " << ezp << endl; } } } } diff --git a/src/yamlcpp/Makefile.am b/src/yamlcpp/Makefile.am --- a/src/yamlcpp/Makefile.am +++ b/src/yamlcpp/Makefile.am @@ -1,24 +1,20 @@ -## How to make our mangled yaml-cpp from the yaml-cpp-0.3.0.tar.gz tarball -# tar xf yaml-cpp-0.3.0.tar.gz -# for i in `find yaml-cpp -type f -name "*.h" -o -name "*.cpp"`; do \ +## How to make our mangled yaml-cpp from the yaml-cpp-0.6.0.tar.gz tarball +# tar xf yaml-cpp-0.6.0.tar.gz +# for i in `find yaml-cpp-* -type f -name "*.h" -o -name "*.cpp"`; do \ # sed -e 's/namespace YAML/namespace YODA_YAML/' -e 's/YAML::/YODA_YAML::/g' -i; done -# mv yaml-cpp{,.bak} && cp yaml-cpp.bak/include/yaml-cpp . -r && cp yaml-cpp.bak/src/*.{h,cpp} . EXTRA_DIST = yaml-cpp noinst_LTLIBRARIES = libyoda-yaml-cpp.la -libyoda_yaml_cpp_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir) -DYAMLCPP_API=3 -DYAML_NAMESPACE=YODA_YAML +libyoda_yaml_cpp_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir) libyoda_yaml_cpp_la_SOURCES = \ - aliasmanager.cpp emitterutils.cpp ostream.cpp simplekey.cpp \ - binary.cpp exp.cpp parser.cpp singledocparser.cpp \ - conversion.cpp iterator.cpp regex.cpp stream.cpp \ - directives.cpp nodebuilder.cpp scanner.cpp tag.cpp \ - emitfromevents.cpp node.cpp scanscalar.cpp \ - emitter.cpp nodeownership.cpp scantag.cpp \ - emitterstate.cpp null.cpp scantoken.cpp + binary.cpp emitterstate.cpp node.cpp parser.cpp simplekey.cpp \ + convert.cpp emitterutils.cpp node_data.cpp regex_yaml.cpp singledocparser.cpp \ + directives.cpp exceptions.cpp nodeevents.cpp scanner.cpp stream.cpp \ + emit.cpp exp.cpp null.cpp scanscalar.cpp tag.cpp \ + emitfromevents.cpp memory.cpp ostream_wrapper.cpp scantag.cpp \ + emitter.cpp nodebuilder.cpp parse.cpp scantoken.cpp dist_noinst_HEADERS = \ - collectionstack.h iterpriv.h regeximpl.h streamcharsource.h \ - directives.h nodebuilder.h scanner.h stream.h \ - emitterstate.h nodeownership.h scanscalar.h stringsource.h \ - emitterutils.h ptr_stack.h scantag.h tag.h \ - exp.h ptr_vector.h setting.h token.h \ - indentation.h regex.h singledocparser.h + collectionstack.h exp.h ptr_vector.h scanscalar.h streamcharsource.h token.h \ + directives.h indentation.h regeximpl.h scantag.h stream.h \ + emitterstate.h nodebuilder.h regex_yaml.h setting.h stringsource.h \ + emitterutils.h nodeevents.h scanner.h singledocparser.h tag.h diff --git a/src/yamlcpp/aliasmanager.cpp b/src/yamlcpp/aliasmanager.cpp deleted file mode 100644 --- a/src/yamlcpp/aliasmanager.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "yaml-cpp/aliasmanager.h" -#include "yaml-cpp/node.h" -#include -#include - -namespace YODA_YAML -{ - AliasManager::AliasManager(): m_curAnchor(0) - { - } - - void AliasManager::RegisterReference(const Node& node) - { - m_anchorByIdentity.insert(std::make_pair(&node, _CreateNewAnchor())); - } - - anchor_t AliasManager::LookupAnchor(const Node& node) const - { - AnchorByIdentity::const_iterator it = m_anchorByIdentity.find(&node); - if(it == m_anchorByIdentity.end()) - return 0; - return it->second; - } - - anchor_t AliasManager::_CreateNewAnchor() - { - return ++m_curAnchor; - } -} diff --git a/src/yamlcpp/binary.cpp b/src/yamlcpp/binary.cpp --- a/src/yamlcpp/binary.cpp +++ b/src/yamlcpp/binary.cpp @@ -1,102 +1,93 @@ #include "yaml-cpp/binary.h" -#include "yaml-cpp/node.h" -namespace YODA_YAML -{ - static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +namespace YODA_YAML { +static const char encoding[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - std::string EncodeBase64(const unsigned char *data, std::size_t size) - { - const char PAD = '='; - - std::string ret; - ret.resize(4 * size / 3 + 3); - char *out = &ret[0]; - - std::size_t chunks = size / 3; - std::size_t remainder = size % 3; - - for(std::size_t i=0;i> 2]; - *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)]; - *out++ = encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)]; - *out++ = encoding[data[2] & 0x3f]; - } - - switch(remainder) { - case 0: - break; - case 1: - *out++ = encoding[data[0] >> 2]; - *out++ = encoding[((data[0] & 0x3) << 4)]; - *out++ = PAD; - *out++ = PAD; - break; - case 2: - *out++ = encoding[data[0] >> 2]; - *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)]; - *out++ = encoding[((data[1] & 0xf) << 2)]; - *out++ = PAD; - break; - } - - ret.resize(out - &ret[0]); - return ret; +std::string EncodeBase64(const unsigned char *data, std::size_t size) { + const char PAD = '='; + + std::string ret; + ret.resize(4 * size / 3 + 3); + char *out = &ret[0]; + + std::size_t chunks = size / 3; + std::size_t remainder = size % 3; + + for (std::size_t i = 0; i < chunks; i++, data += 3) { + *out++ = encoding[data[0] >> 2]; + *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)]; + *out++ = encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)]; + *out++ = encoding[data[2] & 0x3f]; + } + + switch (remainder) { + case 0: + break; + case 1: + *out++ = encoding[data[0] >> 2]; + *out++ = encoding[((data[0] & 0x3) << 4)]; + *out++ = PAD; + *out++ = PAD; + break; + case 2: + *out++ = encoding[data[0] >> 2]; + *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)]; + *out++ = encoding[((data[1] & 0xf) << 2)]; + *out++ = PAD; + break; + } + + ret.resize(out - &ret[0]); + return ret; +} + +static const unsigned char decoding[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, + 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, + 255, 0, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, + 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, +}; + +std::vector DecodeBase64(const std::string &input) { + typedef std::vector ret_type; + if (input.empty()) + return ret_type(); + + ret_type ret(3 * input.size() / 4 + 1); + unsigned char *out = &ret[0]; + + unsigned value = 0; + for (std::size_t i = 0; i < input.size(); i++) { + unsigned char d = decoding[static_cast(input[i])]; + if (d == 255) + return ret_type(); + + value = (value << 6) | d; + if (i % 4 == 3) { + *out++ = value >> 16; + if (i > 0 && input[i - 1] != '=') + *out++ = value >> 8; + if (input[i] != '=') + *out++ = value; } - - static const unsigned char decoding[] = { - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, - 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, - 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, - }; + } - std::vector DecodeBase64(const std::string& input) - { - typedef std::vector ret_type; - if(input.empty()) - return ret_type(); - - ret_type ret(3 * input.size() / 4 + 1); - unsigned char *out = &ret[0]; - - unsigned value = 0; - for(std::size_t i=0;i(input[i])]; - if(d == 255) - return ret_type(); - - value = (value << 6) | d; - if(i % 4 == 3) { - *out++ = value >> 16; - if(i > 0 && input[i - 1] != '=') - *out++ = value >> 8; - if(input[i] != '=') - *out++ = value; - } - } - - ret.resize(out - &ret[0]); - return ret; - } - - void operator >> (const Node& node, Binary& binary) - { - std::string scalar; - node.GetScalar(scalar); - std::vector data = DecodeBase64(scalar); - binary.swap(data); - } + ret.resize(out - &ret[0]); + return ret; } +} diff --git a/src/yamlcpp/collectionstack.h b/src/yamlcpp/collectionstack.h --- a/src/yamlcpp/collectionstack.h +++ b/src/yamlcpp/collectionstack.h @@ -1,35 +1,39 @@ #ifndef COLLECTIONSTACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define COLLECTIONSTACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include #include -namespace YODA_YAML -{ - struct CollectionType { - enum value { None, BlockMap, BlockSeq, FlowMap, FlowSeq, CompactMap }; - }; +namespace YODA_YAML { +struct CollectionType { + enum value { NoCollection, BlockMap, BlockSeq, FlowMap, FlowSeq, CompactMap }; +}; - class CollectionStack - { - public: - CollectionType::value GetCurCollectionType() const { - if(collectionStack.empty()) - return CollectionType::None; - return collectionStack.top(); - } - - void PushCollectionType(CollectionType::value type) { collectionStack.push(type); } - void PopCollectionType(CollectionType::value type) { assert(type == GetCurCollectionType()); collectionStack.pop(); } - - private: - std::stack collectionStack; - }; +class CollectionStack { + public: + CollectionType::value GetCurCollectionType() const { + if (collectionStack.empty()) + return CollectionType::NoCollection; + return collectionStack.top(); + } + + void PushCollectionType(CollectionType::value type) { + collectionStack.push(type); + } + void PopCollectionType(CollectionType::value type) { + assert(type == GetCurCollectionType()); + collectionStack.pop(); + } + + private: + std::stack collectionStack; +}; } -#endif // COLLECTIONSTACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // COLLECTIONSTACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/conversion.cpp b/src/yamlcpp/conversion.cpp deleted file mode 100644 --- a/src/yamlcpp/conversion.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "yaml-cpp/conversion.h" -#include - -//////////////////////////////////////////////////////////////// -// Specializations for converting a string to specific types - -namespace -{ - // we're not gonna mess with the mess that is all the isupper/etc. functions - bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; } - bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; } - char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; } - - std::string tolower(const std::string& str) - { - std::string s(str); - std::transform(s.begin(), s.end(), s.begin(), ToLower); - return s; - } - - template - bool IsEntirely(const std::string& str, T func) - { - for(std::size_t i=0;i + +#include "yaml-cpp/node/convert.h" + +namespace { +// we're not gonna mess with the mess that is all the isupper/etc. functions +bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; } +bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; } +char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; } + +std::string tolower(const std::string& str) { + std::string s(str); + std::transform(s.begin(), s.end(), s.begin(), ToLower); + return s; +} + +template +bool IsEntirely(const std::string& str, T func) { + for (std::size_t i = 0; i < str.size(); i++) + if (!func(str[i])) + return false; + + return true; +} + +// IsFlexibleCase +// . Returns true if 'str' is: +// . UPPERCASE +// . lowercase +// . Capitalized +bool IsFlexibleCase(const std::string& str) { + if (str.empty()) + return true; + + if (IsEntirely(str, IsLower)) + return true; + + bool firstcaps = IsUpper(str[0]); + std::string rest = str.substr(1); + return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper)); +} +} + +namespace YODA_YAML { +bool convert::decode(const Node& node, bool& rhs) { + if (!node.IsScalar()) + return false; + + // we can't use iostream bool extraction operators as they don't + // recognize all possible values in the table below (taken from + // http://yaml.org/type/bool.html) + static const struct { + std::string truename, falsename; + } names[] = { + {"y", "n"}, {"yes", "no"}, {"true", "false"}, {"on", "off"}, + }; + + if (!IsFlexibleCase(node.Scalar())) + return false; + + for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); i++) { + if (names[i].truename == tolower(node.Scalar())) { + rhs = true; + return true; + } + + if (names[i].falsename == tolower(node.Scalar())) { + rhs = false; + return true; + } + } + + return false; +} +} diff --git a/src/yamlcpp/directives.cpp b/src/yamlcpp/directives.cpp --- a/src/yamlcpp/directives.cpp +++ b/src/yamlcpp/directives.cpp @@ -1,24 +1,22 @@ #include "directives.h" -namespace YODA_YAML -{ - Directives::Directives() - { - // version - version.isDefault = true; - version.major = 1; - version.minor = 2; - } - - const std::string Directives::TranslateTagHandle(const std::string& handle) const - { - std::map ::const_iterator it = tags.find(handle); - if(it == tags.end()) { - if(handle == "!!") - return "tag:yaml.org,2002:"; - return handle; - } - - return it->second; - } +namespace YODA_YAML { +Directives::Directives() { + // version + version.isDefault = true; + version.major = 1; + version.minor = 2; } + +const std::string Directives::TranslateTagHandle( + const std::string& handle) const { + std::map::const_iterator it = tags.find(handle); + if (it == tags.end()) { + if (handle == "!!") + return "tag:yaml.org,2002:"; + return handle; + } + + return it->second; +} +} diff --git a/src/yamlcpp/directives.h b/src/yamlcpp/directives.h --- a/src/yamlcpp/directives.h +++ b/src/yamlcpp/directives.h @@ -1,29 +1,29 @@ #ifndef DIRECTIVES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define DIRECTIVES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include #include -namespace YODA_YAML -{ - struct Version { - bool isDefault; - int major, minor; - }; - - struct Directives { - Directives(); - - const std::string TranslateTagHandle(const std::string& handle) const; +namespace YODA_YAML { +struct Version { + bool isDefault; + int major, minor; +}; - Version version; - std::map tags; - }; +struct Directives { + Directives(); + + const std::string TranslateTagHandle(const std::string& handle) const; + + Version version; + std::map tags; +}; } -#endif // DIRECTIVES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // DIRECTIVES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/emit.cpp b/src/yamlcpp/emit.cpp new file mode 100644 --- /dev/null +++ b/src/yamlcpp/emit.cpp @@ -0,0 +1,25 @@ +#include "yaml-cpp/node/emit.h" +#include "yaml-cpp/emitfromevents.h" +#include "yaml-cpp/emitter.h" +#include "nodeevents.h" + +namespace YODA_YAML { +Emitter& operator<<(Emitter& out, const Node& node) { + EmitFromEvents emitFromEvents(out); + NodeEvents events(node); + events.Emit(emitFromEvents); + return out; +} + +std::ostream& operator<<(std::ostream& out, const Node& node) { + Emitter emitter(out); + emitter << node; + return out; +} + +std::string Dump(const Node& node) { + Emitter emitter; + emitter << node; + return emitter.c_str(); +} +} // namespace YODA_YAML diff --git a/src/yamlcpp/emitfromevents.cpp b/src/yamlcpp/emitfromevents.cpp --- a/src/yamlcpp/emitfromevents.cpp +++ b/src/yamlcpp/emitfromevents.cpp @@ -1,105 +1,119 @@ -#include "yaml-cpp/emitfromevents.h" -#include "yaml-cpp/emitter.h" -#include "yaml-cpp/null.h" #include #include +#include "yaml-cpp/emitfromevents.h" +#include "yaml-cpp/emitter.h" +#include "yaml-cpp/emittermanip.h" +#include "yaml-cpp/null.h" + +namespace YODA_YAML { +struct Mark; +} // namespace YODA_YAML + namespace { - std::string ToString(YODA_YAML::anchor_t anchor) { - std::stringstream stream; - stream << anchor; - return stream.str(); - } +std::string ToString(YODA_YAML::anchor_t anchor) { + std::stringstream stream; + stream << anchor; + return stream.str(); +} } -namespace YODA_YAML -{ - EmitFromEvents::EmitFromEvents(Emitter& emitter): m_emitter(emitter) - { - } - - void EmitFromEvents::OnDocumentStart(const Mark&) - { - } - - void EmitFromEvents::OnDocumentEnd() - { - } - - void EmitFromEvents::OnNull(const Mark&, anchor_t anchor) - { - BeginNode(); - EmitProps("", anchor); - m_emitter << Null; - } - - void EmitFromEvents::OnAlias(const Mark&, anchor_t anchor) - { - BeginNode(); - m_emitter << Alias(ToString(anchor)); - } - - void EmitFromEvents::OnScalar(const Mark&, const std::string& tag, anchor_t anchor, const std::string& value) - { - BeginNode(); - EmitProps(tag, anchor); - m_emitter << value; - } - - void EmitFromEvents::OnSequenceStart(const Mark&, const std::string& tag, anchor_t anchor) - { - BeginNode(); - EmitProps(tag, anchor); - m_emitter << BeginSeq; - m_stateStack.push(State::WaitingForSequenceEntry); - } - - void EmitFromEvents::OnSequenceEnd() - { - m_emitter << EndSeq; - assert(m_stateStack.top() == State::WaitingForSequenceEntry); - m_stateStack.pop(); - } - - void EmitFromEvents::OnMapStart(const Mark&, const std::string& tag, anchor_t anchor) - { - BeginNode(); - EmitProps(tag, anchor); - m_emitter << BeginMap; - m_stateStack.push(State::WaitingForKey); - } +namespace YODA_YAML { +EmitFromEvents::EmitFromEvents(Emitter& emitter) : m_emitter(emitter) {} - void EmitFromEvents::OnMapEnd() - { - m_emitter << EndMap; - assert(m_stateStack.top() == State::WaitingForKey); - m_stateStack.pop(); - } +void EmitFromEvents::OnDocumentStart(const Mark&) {} - void EmitFromEvents::BeginNode() - { - if(m_stateStack.empty()) - return; - - switch(m_stateStack.top()) { - case State::WaitingForKey: - m_emitter << Key; - m_stateStack.top() = State::WaitingForValue; - break; - case State::WaitingForValue: - m_emitter << Value; - m_stateStack.top() = State::WaitingForKey; - break; - default: - break; - } - } - - void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor) - { - if(!tag.empty() && tag != "?") - m_emitter << VerbatimTag(tag); - if(anchor) - m_emitter << Anchor(ToString(anchor)); - } +void EmitFromEvents::OnDocumentEnd() {} + +void EmitFromEvents::OnNull(const Mark&, anchor_t anchor) { + BeginNode(); + EmitProps("", anchor); + m_emitter << Null; } + +void EmitFromEvents::OnAlias(const Mark&, anchor_t anchor) { + BeginNode(); + m_emitter << Alias(ToString(anchor)); +} + +void EmitFromEvents::OnScalar(const Mark&, const std::string& tag, + anchor_t anchor, const std::string& value) { + BeginNode(); + EmitProps(tag, anchor); + m_emitter << value; +} + +void EmitFromEvents::OnSequenceStart(const Mark&, const std::string& tag, + anchor_t anchor, + EmitterStyle::value style) { + BeginNode(); + EmitProps(tag, anchor); + switch (style) { + case EmitterStyle::Block: + m_emitter << Block; + break; + case EmitterStyle::Flow: + m_emitter << Flow; + break; + default: + break; + } + m_emitter << BeginSeq; + m_stateStack.push(State::WaitingForSequenceEntry); +} + +void EmitFromEvents::OnSequenceEnd() { + m_emitter << EndSeq; + assert(m_stateStack.top() == State::WaitingForSequenceEntry); + m_stateStack.pop(); +} + +void EmitFromEvents::OnMapStart(const Mark&, const std::string& tag, + anchor_t anchor, EmitterStyle::value style) { + BeginNode(); + EmitProps(tag, anchor); + switch (style) { + case EmitterStyle::Block: + m_emitter << Block; + break; + case EmitterStyle::Flow: + m_emitter << Flow; + break; + default: + break; + } + m_emitter << BeginMap; + m_stateStack.push(State::WaitingForKey); +} + +void EmitFromEvents::OnMapEnd() { + m_emitter << EndMap; + assert(m_stateStack.top() == State::WaitingForKey); + m_stateStack.pop(); +} + +void EmitFromEvents::BeginNode() { + if (m_stateStack.empty()) + return; + + switch (m_stateStack.top()) { + case State::WaitingForKey: + m_emitter << Key; + m_stateStack.top() = State::WaitingForValue; + break; + case State::WaitingForValue: + m_emitter << Value; + m_stateStack.top() = State::WaitingForKey; + break; + default: + break; + } +} + +void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor) { + if (!tag.empty() && tag != "?" && tag != "!") + m_emitter << VerbatimTag(tag); + if (anchor) + m_emitter << Anchor(ToString(anchor)); +} +} diff --git a/src/yamlcpp/emitter.cpp b/src/yamlcpp/emitter.cpp --- a/src/yamlcpp/emitter.cpp +++ b/src/yamlcpp/emitter.cpp @@ -1,882 +1,911 @@ -#include "yaml-cpp/emitter.h" -#include "emitterstate.h" -#include "emitterutils.h" -#include "indentation.h" -#include "yaml-cpp/exceptions.h" #include -namespace YODA_YAML -{ - Emitter::Emitter(): m_pState(new EmitterState) - { - } - - Emitter::~Emitter() - { - } - - const char *Emitter::c_str() const - { - return m_stream.str(); - } - - unsigned Emitter::size() const - { - return m_stream.pos(); - } - - // state checking - bool Emitter::good() const - { - return m_pState->good(); - } - - const std::string Emitter::GetLastError() const - { - return m_pState->GetLastError(); - } +#include "emitterutils.h" +#include "indentation.h" // IWYU pragma: keep +#include "yaml-cpp/emitter.h" +#include "yaml-cpp/emitterdef.h" +#include "yaml-cpp/emittermanip.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep - // global setters - bool Emitter::SetOutputCharset(EMITTER_MANIP value) - { - return m_pState->SetOutputCharset(value, GLOBAL); - } +namespace YODA_YAML { +class Binary; +struct _Null; - bool Emitter::SetStringFormat(EMITTER_MANIP value) - { - return m_pState->SetStringFormat(value, GLOBAL); - } - - bool Emitter::SetBoolFormat(EMITTER_MANIP value) - { - bool ok = false; - if(m_pState->SetBoolFormat(value, GLOBAL)) - ok = true; - if(m_pState->SetBoolCaseFormat(value, GLOBAL)) - ok = true; - if(m_pState->SetBoolLengthFormat(value, GLOBAL)) - ok = true; - return ok; - } - - bool Emitter::SetIntBase(EMITTER_MANIP value) - { - return m_pState->SetIntFormat(value, GLOBAL); - } - - bool Emitter::SetSeqFormat(EMITTER_MANIP value) - { - return m_pState->SetFlowType(GT_SEQ, value, GLOBAL); - } - - bool Emitter::SetMapFormat(EMITTER_MANIP value) - { - bool ok = false; - if(m_pState->SetFlowType(GT_MAP, value, GLOBAL)) - ok = true; - if(m_pState->SetMapKeyFormat(value, GLOBAL)) - ok = true; - return ok; - } - - bool Emitter::SetIndent(unsigned n) - { - return m_pState->SetIndent(n, GLOBAL); - } - - bool Emitter::SetPreCommentIndent(unsigned n) - { - return m_pState->SetPreCommentIndent(n, GLOBAL); - } - - bool Emitter::SetPostCommentIndent(unsigned n) - { - return m_pState->SetPostCommentIndent(n, GLOBAL); - } - - bool Emitter::SetFloatPrecision(unsigned n) - { - return m_pState->SetFloatPrecision(n, GLOBAL); - } +Emitter::Emitter() : m_pState(new EmitterState) {} - bool Emitter::SetDoublePrecision(unsigned n) - { - return m_pState->SetDoublePrecision(n, GLOBAL); - } +Emitter::Emitter(std::ostream& stream) + : m_pState(new EmitterState), m_stream(stream) {} - // SetLocalValue - // . Either start/end a group, or set a modifier locally - Emitter& Emitter::SetLocalValue(EMITTER_MANIP value) - { - if(!good()) - return *this; - - switch(value) { - case BeginDoc: - EmitBeginDoc(); - break; - case EndDoc: - EmitEndDoc(); - break; - case BeginSeq: - EmitBeginSeq(); - break; - case EndSeq: - EmitEndSeq(); - break; - case BeginMap: - EmitBeginMap(); - break; - case EndMap: - EmitEndMap(); - break; - case Key: - EmitKey(); - break; - case Value: - EmitValue(); - break; - case TagByKind: - EmitKindTag(); - break; - case Newline: - EmitNewline(); - break; - default: - m_pState->SetLocalValue(value); - break; - } - return *this; - } - - Emitter& Emitter::SetLocalIndent(const _Indent& indent) - { - m_pState->SetIndent(indent.value, LOCAL); - return *this; - } +Emitter::~Emitter() {} - Emitter& Emitter::SetLocalPrecision(const _Precision& precision) - { - if(precision.floatPrecision >= 0) - m_pState->SetFloatPrecision(precision.floatPrecision, LOCAL); - if(precision.doublePrecision >= 0) - m_pState->SetDoublePrecision(precision.doublePrecision, LOCAL); - return *this; - } +const char* Emitter::c_str() const { return m_stream.str(); } - // GotoNextPreAtomicState - // . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom) - bool Emitter::GotoNextPreAtomicState() - { - if(!good()) - return true; - - unsigned curIndent = m_pState->GetCurIndent(); - - EMITTER_STATE curState = m_pState->GetCurState(); - switch(curState) { - // document-level - case ES_WAITING_FOR_DOC: - m_pState->SwitchState(ES_WRITING_DOC); - return true; - case ES_WRITING_DOC: - return true; - case ES_DONE_WITH_DOC: - EmitBeginDoc(); - return false; - - // block sequence - case ES_WAITING_FOR_BLOCK_SEQ_ENTRY: - m_stream << IndentTo(curIndent) << "-"; - m_pState->RequireSoftSeparation(); - m_pState->SwitchState(ES_WRITING_BLOCK_SEQ_ENTRY); - return true; - case ES_WRITING_BLOCK_SEQ_ENTRY: - return true; - case ES_DONE_WITH_BLOCK_SEQ_ENTRY: - m_stream << '\n'; - m_pState->SwitchState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY); - return false; - - // flow sequence - case ES_WAITING_FOR_FLOW_SEQ_ENTRY: - m_pState->SwitchState(ES_WRITING_FLOW_SEQ_ENTRY); - return true; - case ES_WRITING_FLOW_SEQ_ENTRY: - return true; - case ES_DONE_WITH_FLOW_SEQ_ENTRY: - EmitSeparationIfNecessary(); - m_stream << ','; - m_pState->RequireSoftSeparation(); - m_pState->SwitchState(ES_WAITING_FOR_FLOW_SEQ_ENTRY); - return false; - - // block map - case ES_WAITING_FOR_BLOCK_MAP_ENTRY: - m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN); - return true; - case ES_WAITING_FOR_BLOCK_MAP_KEY: - if(m_pState->CurrentlyInLongKey()) { - m_stream << IndentTo(curIndent) << '?'; - m_pState->RequireSoftSeparation(); - } - m_pState->SwitchState(ES_WRITING_BLOCK_MAP_KEY); - return true; - case ES_WRITING_BLOCK_MAP_KEY: - return true; - case ES_DONE_WITH_BLOCK_MAP_KEY: - m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN); - return true; - case ES_WAITING_FOR_BLOCK_MAP_VALUE: - m_pState->SwitchState(ES_WRITING_BLOCK_MAP_VALUE); - return true; - case ES_WRITING_BLOCK_MAP_VALUE: - return true; - case ES_DONE_WITH_BLOCK_MAP_VALUE: - m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN); - return true; - - // flow map - case ES_WAITING_FOR_FLOW_MAP_ENTRY: - m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN); - return true; - case ES_WAITING_FOR_FLOW_MAP_KEY: - EmitSeparationIfNecessary(); - m_pState->SwitchState(ES_WRITING_FLOW_MAP_KEY); - if(m_pState->CurrentlyInLongKey()) { - m_stream << '?'; - m_pState->RequireSoftSeparation(); - } - return true; - case ES_WRITING_FLOW_MAP_KEY: - return true; - case ES_DONE_WITH_FLOW_MAP_KEY: - m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN); - return true; - case ES_WAITING_FOR_FLOW_MAP_VALUE: - EmitSeparationIfNecessary(); - m_stream << ':'; - m_pState->RequireSoftSeparation(); - m_pState->SwitchState(ES_WRITING_FLOW_MAP_VALUE); - return true; - case ES_WRITING_FLOW_MAP_VALUE: - return true; - case ES_DONE_WITH_FLOW_MAP_VALUE: - m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN); - return true; - default: - assert(false); - } +std::size_t Emitter::size() const { return m_stream.pos(); } - assert(false); - return true; - } - - // PreAtomicWrite - // . Depending on the emitter state, write to the stream to get it - // in position to do an atomic write (e.g., scalar, sequence, or map) - void Emitter::PreAtomicWrite() - { - if(!good()) - return; - - while(!GotoNextPreAtomicState()) - ; - } - - // PostAtomicWrite - // . Clean up - void Emitter::PostAtomicWrite() - { - if(!good()) - return; - - EMITTER_STATE curState = m_pState->GetCurState(); - switch(curState) { - // document-level - case ES_WRITING_DOC: - m_pState->SwitchState(ES_DONE_WITH_DOC); - break; +// state checking +bool Emitter::good() const { return m_pState->good(); } - // block seq - case ES_WRITING_BLOCK_SEQ_ENTRY: - m_pState->SwitchState(ES_DONE_WITH_BLOCK_SEQ_ENTRY); - break; - - // flow seq - case ES_WRITING_FLOW_SEQ_ENTRY: - m_pState->SwitchState(ES_DONE_WITH_FLOW_SEQ_ENTRY); - break; - - // block map - case ES_WRITING_BLOCK_MAP_KEY: - if(!m_pState->CurrentlyInLongKey()) { - m_stream << ':'; - m_pState->RequireSoftSeparation(); - } - m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_KEY); - break; - case ES_WRITING_BLOCK_MAP_VALUE: - m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_VALUE); - break; - - // flow map - case ES_WRITING_FLOW_MAP_KEY: - m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_KEY); - break; - case ES_WRITING_FLOW_MAP_VALUE: - m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_VALUE); - break; - default: - assert(false); - }; - - m_pState->ClearModifiedSettings(); - } - - // EmitSeparationIfNecessary - void Emitter::EmitSeparationIfNecessary() - { - if(!good()) - return; - - if(m_pState->RequiresSoftSeparation()) - m_stream << ' '; - else if(m_pState->RequiresHardSeparation()) - m_stream << '\n'; - m_pState->UnsetSeparation(); - } - - // EmitBeginDoc - void Emitter::EmitBeginDoc() - { - if(!good()) - return; - - EMITTER_STATE curState = m_pState->GetCurState(); - if(curState != ES_WAITING_FOR_DOC && curState != ES_WRITING_DOC && curState != ES_DONE_WITH_DOC) { - m_pState->SetError("Unexpected begin document"); - return; - } - - if(curState == ES_WRITING_DOC || curState == ES_DONE_WITH_DOC) - m_stream << '\n'; - m_stream << "---\n"; - - m_pState->UnsetSeparation(); - m_pState->SwitchState(ES_WAITING_FOR_DOC); - } - - // EmitEndDoc - void Emitter::EmitEndDoc() - { - if(!good()) - return; - - - EMITTER_STATE curState = m_pState->GetCurState(); - if(curState != ES_WAITING_FOR_DOC && curState != ES_WRITING_DOC && curState != ES_DONE_WITH_DOC) { - m_pState->SetError("Unexpected end document"); - return; - } - - if(curState == ES_WRITING_DOC || curState == ES_DONE_WITH_DOC) - m_stream << '\n'; - m_stream << "...\n"; - - m_pState->UnsetSeparation(); - m_pState->SwitchState(ES_WAITING_FOR_DOC); - } - - // EmitBeginSeq - void Emitter::EmitBeginSeq() - { - if(!good()) - return; - - // must have a long key if we're emitting a sequence - m_pState->StartLongKey(); - - PreAtomicWrite(); - - EMITTER_STATE curState = m_pState->GetCurState(); - EMITTER_MANIP flowType = m_pState->GetFlowType(GT_SEQ); - if(flowType == Block) { - if(curState == ES_WRITING_BLOCK_SEQ_ENTRY || - curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE || - curState == ES_WRITING_DOC - ) { - if(m_pState->RequiresHardSeparation() || curState != ES_WRITING_DOC) { - m_stream << "\n"; - m_pState->UnsetSeparation(); - } - } - m_pState->PushState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY); - } else if(flowType == Flow) { - EmitSeparationIfNecessary(); - m_stream << "["; - m_pState->PushState(ES_WAITING_FOR_FLOW_SEQ_ENTRY); - } else - assert(false); - - m_pState->BeginGroup(GT_SEQ); - } - - // EmitEndSeq - void Emitter::EmitEndSeq() - { - if(!good()) - return; - - if(m_pState->GetCurGroupType() != GT_SEQ) - return m_pState->SetError(ErrorMsg::UNEXPECTED_END_SEQ); - - EMITTER_STATE curState = m_pState->GetCurState(); - FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); - if(flowType == FT_BLOCK) { - // Note: block sequences are *not* allowed to be empty, but we convert it - // to a flow sequence if it is - assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY || curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY); - if(curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY) { - // Note: only one of these will actually output anything for a given situation - EmitSeparationIfNecessary(); - unsigned curIndent = m_pState->GetCurIndent(); - m_stream << IndentTo(curIndent); - - m_stream << "[]"; - } - } else if(flowType == FT_FLOW) { - // Note: flow sequences are allowed to be empty - assert(curState == ES_DONE_WITH_FLOW_SEQ_ENTRY || curState == ES_WAITING_FOR_FLOW_SEQ_ENTRY); - m_stream << "]"; - } else - assert(false); - - m_pState->PopState(); - m_pState->EndGroup(GT_SEQ); - - PostAtomicWrite(); - } - - // EmitBeginMap - void Emitter::EmitBeginMap() - { - if(!good()) - return; - - // must have a long key if we're emitting a map - m_pState->StartLongKey(); - - PreAtomicWrite(); - - EMITTER_STATE curState = m_pState->GetCurState(); - EMITTER_MANIP flowType = m_pState->GetFlowType(GT_MAP); - if(flowType == Block) { - if(curState == ES_WRITING_BLOCK_SEQ_ENTRY || - curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE || - curState == ES_WRITING_DOC - ) { - if(m_pState->RequiresHardSeparation() || (curState != ES_WRITING_DOC && curState != ES_WRITING_BLOCK_SEQ_ENTRY)) { - m_stream << "\n"; - m_pState->UnsetSeparation(); - } - } - m_pState->PushState(ES_WAITING_FOR_BLOCK_MAP_ENTRY); - } else if(flowType == Flow) { - EmitSeparationIfNecessary(); - m_stream << "{"; - m_pState->PushState(ES_WAITING_FOR_FLOW_MAP_ENTRY); - } else - assert(false); - - m_pState->BeginGroup(GT_MAP); - } - - // EmitEndMap - void Emitter::EmitEndMap() - { - if(!good()) - return; - - if(m_pState->GetCurGroupType() != GT_MAP) - return m_pState->SetError(ErrorMsg::UNEXPECTED_END_MAP); - - EMITTER_STATE curState = m_pState->GetCurState(); - FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); - if(flowType == FT_BLOCK) { - // Note: block sequences are *not* allowed to be empty, but we convert it - // to a flow sequence if it is - assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE || curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY); - if(curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY) { - // Note: only one of these will actually output anything for a given situation - EmitSeparationIfNecessary(); - unsigned curIndent = m_pState->GetCurIndent(); - m_stream << IndentTo(curIndent); - m_stream << "{}"; - } - } else if(flowType == FT_FLOW) { - // Note: flow maps are allowed to be empty - assert(curState == ES_DONE_WITH_FLOW_MAP_VALUE || curState == ES_WAITING_FOR_FLOW_MAP_ENTRY); - EmitSeparationIfNecessary(); - m_stream << "}"; - } else - assert(false); - - m_pState->PopState(); - m_pState->EndGroup(GT_MAP); - - PostAtomicWrite(); - } - - // EmitKey - void Emitter::EmitKey() - { - if(!good()) - return; - - EMITTER_STATE curState = m_pState->GetCurState(); - FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); - if(curState != ES_WAITING_FOR_BLOCK_MAP_ENTRY && curState != ES_DONE_WITH_BLOCK_MAP_VALUE - && curState != ES_WAITING_FOR_FLOW_MAP_ENTRY && curState != ES_DONE_WITH_FLOW_MAP_VALUE) - return m_pState->SetError(ErrorMsg::UNEXPECTED_KEY_TOKEN); - - if(flowType == FT_BLOCK) { - if(curState == ES_DONE_WITH_BLOCK_MAP_VALUE) - m_stream << '\n'; - unsigned curIndent = m_pState->GetCurIndent(); - m_stream << IndentTo(curIndent); - m_pState->UnsetSeparation(); - m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_KEY); - } else if(flowType == FT_FLOW) { - EmitSeparationIfNecessary(); - if(curState == ES_DONE_WITH_FLOW_MAP_VALUE) { - m_stream << ','; - m_pState->RequireSoftSeparation(); - } - m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_KEY); - } else - assert(false); - - if(m_pState->GetMapKeyFormat() == LongKey) - m_pState->StartLongKey(); - else if(m_pState->GetMapKeyFormat() == Auto) - m_pState->StartSimpleKey(); - else - assert(false); - } - - // EmitValue - void Emitter::EmitValue() - { - if(!good()) - return; - - EMITTER_STATE curState = m_pState->GetCurState(); - FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); - if(curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_DONE_WITH_FLOW_MAP_KEY) - return m_pState->SetError(ErrorMsg::UNEXPECTED_VALUE_TOKEN); - - if(flowType == FT_BLOCK) { - if(m_pState->CurrentlyInLongKey()) { - m_stream << '\n'; - m_stream << IndentTo(m_pState->GetCurIndent()); - m_stream << ':'; - m_pState->RequireSoftSeparation(); - } - m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_VALUE); - } else if(flowType == FT_FLOW) { - m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_VALUE); - } else - assert(false); - } - - // EmitNewline - void Emitter::EmitNewline() - { - if(!good()) - return; - - if(CanEmitNewline()) { - m_stream << '\n'; - m_pState->UnsetSeparation(); - } - } - - bool Emitter::CanEmitNewline() const - { - FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); - if(flowType == FT_BLOCK && m_pState->CurrentlyInLongKey()) - return true; - - EMITTER_STATE curState = m_pState->GetCurState(); - return curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_WAITING_FOR_BLOCK_MAP_VALUE && curState != ES_WRITING_BLOCK_MAP_VALUE; - } - - // ******************************************************************************************* - // overloads of Write - - Emitter& Emitter::Write(const std::string& str) - { - if(!good()) - return *this; - - // literal scalars must use long keys - if(m_pState->GetStringFormat() == Literal && m_pState->GetCurGroupFlowType() != FT_FLOW) - m_pState->StartLongKey(); - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - - bool escapeNonAscii = m_pState->GetOutputCharset() == EscapeNonAscii; - EMITTER_MANIP strFmt = m_pState->GetStringFormat(); - FLOW_TYPE flowType = m_pState->GetCurGroupFlowType(); - unsigned curIndent = m_pState->GetCurIndent(); - - switch(strFmt) { - case Auto: - Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii); - break; - case SingleQuoted: - if(!Utils::WriteSingleQuotedString(m_stream, str)) { - m_pState->SetError(ErrorMsg::SINGLE_QUOTED_CHAR); - return *this; - } - break; - case DoubleQuoted: - Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii); - break; - case Literal: - if(flowType == FT_FLOW) - Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii); - else - Utils::WriteLiteralString(m_stream, str, curIndent + m_pState->GetIndent()); - break; - default: - assert(false); - } - - PostAtomicWrite(); - return *this; - } - - void Emitter::PreWriteIntegralType(std::stringstream& str) - { - PreAtomicWrite(); - EmitSeparationIfNecessary(); - - EMITTER_MANIP intFmt = m_pState->GetIntFormat(); - switch(intFmt) { - case Dec: - str << std::dec; - break; - case Hex: - str << "0x"; - str << std::hex; - break; - case Oct: - str << "0"; - str << std::oct; - break; - default: - assert(false); - } - } - - void Emitter::PreWriteStreamable(std::stringstream&) - { - PreAtomicWrite(); - EmitSeparationIfNecessary(); - } - - unsigned Emitter::GetFloatPrecision() const - { - return m_pState->GetFloatPrecision(); - } - - unsigned Emitter::GetDoublePrecision() const - { - return m_pState->GetDoublePrecision(); - } - - void Emitter::PostWriteIntegralType(const std::stringstream& str) - { - m_stream << str.str(); - PostAtomicWrite(); - } - - void Emitter::PostWriteStreamable(const std::stringstream& str) - { - m_stream << str.str(); - PostAtomicWrite(); - } - - const char *Emitter::ComputeFullBoolName(bool b) const - { - const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool ? YesNoBool : m_pState->GetBoolFormat()); - const EMITTER_MANIP caseFmt = m_pState->GetBoolCaseFormat(); - switch(mainFmt) { - case YesNoBool: - switch(caseFmt) { - case UpperCase: return b ? "YES" : "NO"; - case CamelCase: return b ? "Yes" : "No"; - case LowerCase: return b ? "yes" : "no"; - default: break; - } - break; - case OnOffBool: - switch(caseFmt) { - case UpperCase: return b ? "ON" : "OFF"; - case CamelCase: return b ? "On" : "Off"; - case LowerCase: return b ? "on" : "off"; - default: break; - } - break; - case TrueFalseBool: - switch(caseFmt) { - case UpperCase: return b ? "TRUE" : "FALSE"; - case CamelCase: return b ? "True" : "False"; - case LowerCase: return b ? "true" : "false"; - default: break; - } - break; - default: - break; - } - return b ? "y" : "n"; // should never get here, but it can't hurt to give these answers - } - - Emitter& Emitter::Write(bool b) - { - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - - const char *name = ComputeFullBoolName(b); - if(m_pState->GetBoolLengthFormat() == ShortBool) - m_stream << name[0]; - else - m_stream << name; - - PostAtomicWrite(); - return *this; - } - - Emitter& Emitter::Write(char ch) - { - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - - Utils::WriteChar(m_stream, ch); - - PostAtomicWrite(); - return *this; - } - - Emitter& Emitter::Write(const _Alias& alias) - { - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - if(!Utils::WriteAlias(m_stream, alias.content)) { - m_pState->SetError(ErrorMsg::INVALID_ALIAS); - return *this; - } - PostAtomicWrite(); - return *this; - } - - Emitter& Emitter::Write(const _Anchor& anchor) - { - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - if(!Utils::WriteAnchor(m_stream, anchor.content)) { - m_pState->SetError(ErrorMsg::INVALID_ANCHOR); - return *this; - } - m_pState->RequireHardSeparation(); - // Note: no PostAtomicWrite() because we need another value for this node - return *this; - } - - Emitter& Emitter::Write(const _Tag& tag) - { - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - - bool success = false; - if(tag.type == _Tag::Type::Verbatim) - success = Utils::WriteTag(m_stream, tag.content, true); - else if(tag.type == _Tag::Type::PrimaryHandle) - success = Utils::WriteTag(m_stream, tag.content, false); - else - success = Utils::WriteTagWithPrefix(m_stream, tag.prefix, tag.content); - - if(!success) { - m_pState->SetError(ErrorMsg::INVALID_TAG); - return *this; - } - - m_pState->RequireHardSeparation(); - // Note: no PostAtomicWrite() because we need another value for this node - return *this; - } - - void Emitter::EmitKindTag() - { - Write(LocalTag("")); - } - - Emitter& Emitter::Write(const _Comment& comment) - { - if(!good()) - return *this; - - if(m_stream.col() > 0) - m_stream << Indentation(m_pState->GetPreCommentIndent()); - Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent()); - m_pState->RequireHardSeparation(); - m_pState->ForceHardSeparation(); - - return *this; - } - - Emitter& Emitter::Write(const _Null& /*null*/) - { - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - m_stream << "~"; - PostAtomicWrite(); - return *this; - } - - Emitter& Emitter::Write(const Binary& binary) - { - Write(SecondaryTag("binary")); - - if(!good()) - return *this; - - PreAtomicWrite(); - EmitSeparationIfNecessary(); - Utils::WriteBinary(m_stream, binary); - PostAtomicWrite(); - return *this; - } +const std::string Emitter::GetLastError() const { + return m_pState->GetLastError(); } +// global setters +bool Emitter::SetOutputCharset(EMITTER_MANIP value) { + return m_pState->SetOutputCharset(value, FmtScope::Global); +} + +bool Emitter::SetStringFormat(EMITTER_MANIP value) { + return m_pState->SetStringFormat(value, FmtScope::Global); +} + +bool Emitter::SetBoolFormat(EMITTER_MANIP value) { + bool ok = false; + if (m_pState->SetBoolFormat(value, FmtScope::Global)) + ok = true; + if (m_pState->SetBoolCaseFormat(value, FmtScope::Global)) + ok = true; + if (m_pState->SetBoolLengthFormat(value, FmtScope::Global)) + ok = true; + return ok; +} + +bool Emitter::SetIntBase(EMITTER_MANIP value) { + return m_pState->SetIntFormat(value, FmtScope::Global); +} + +bool Emitter::SetSeqFormat(EMITTER_MANIP value) { + return m_pState->SetFlowType(GroupType::Seq, value, FmtScope::Global); +} + +bool Emitter::SetMapFormat(EMITTER_MANIP value) { + bool ok = false; + if (m_pState->SetFlowType(GroupType::Map, value, FmtScope::Global)) + ok = true; + if (m_pState->SetMapKeyFormat(value, FmtScope::Global)) + ok = true; + return ok; +} + +bool Emitter::SetIndent(std::size_t n) { + return m_pState->SetIndent(n, FmtScope::Global); +} + +bool Emitter::SetPreCommentIndent(std::size_t n) { + return m_pState->SetPreCommentIndent(n, FmtScope::Global); +} + +bool Emitter::SetPostCommentIndent(std::size_t n) { + return m_pState->SetPostCommentIndent(n, FmtScope::Global); +} + +bool Emitter::SetFloatPrecision(std::size_t n) { + return m_pState->SetFloatPrecision(n, FmtScope::Global); +} + +bool Emitter::SetDoublePrecision(std::size_t n) { + return m_pState->SetDoublePrecision(n, FmtScope::Global); +} + +// SetLocalValue +// . Either start/end a group, or set a modifier locally +Emitter& Emitter::SetLocalValue(EMITTER_MANIP value) { + if (!good()) + return *this; + + switch (value) { + case BeginDoc: + EmitBeginDoc(); + break; + case EndDoc: + EmitEndDoc(); + break; + case BeginSeq: + EmitBeginSeq(); + break; + case EndSeq: + EmitEndSeq(); + break; + case BeginMap: + EmitBeginMap(); + break; + case EndMap: + EmitEndMap(); + break; + case Key: + case Value: + // deprecated (these can be deduced by the parity of nodes in a map) + break; + case TagByKind: + EmitKindTag(); + break; + case Newline: + EmitNewline(); + break; + default: + m_pState->SetLocalValue(value); + break; + } + return *this; +} + +Emitter& Emitter::SetLocalIndent(const _Indent& indent) { + m_pState->SetIndent(indent.value, FmtScope::Local); + return *this; +} + +Emitter& Emitter::SetLocalPrecision(const _Precision& precision) { + if (precision.floatPrecision >= 0) + m_pState->SetFloatPrecision(precision.floatPrecision, FmtScope::Local); + if (precision.doublePrecision >= 0) + m_pState->SetDoublePrecision(precision.doublePrecision, FmtScope::Local); + return *this; +} + +// EmitBeginDoc +void Emitter::EmitBeginDoc() { + if (!good()) + return; + + if (m_pState->CurGroupType() != GroupType::NoType) { + m_pState->SetError("Unexpected begin document"); + return; + } + + if (m_pState->HasAnchor() || m_pState->HasTag()) { + m_pState->SetError("Unexpected begin document"); + return; + } + + if (m_stream.col() > 0) + m_stream << "\n"; + m_stream << "---\n"; + + m_pState->StartedDoc(); +} + +// EmitEndDoc +void Emitter::EmitEndDoc() { + if (!good()) + return; + + if (m_pState->CurGroupType() != GroupType::NoType) { + m_pState->SetError("Unexpected begin document"); + return; + } + + if (m_pState->HasAnchor() || m_pState->HasTag()) { + m_pState->SetError("Unexpected begin document"); + return; + } + + if (m_stream.col() > 0) + m_stream << "\n"; + m_stream << "...\n"; +} + +// EmitBeginSeq +void Emitter::EmitBeginSeq() { + if (!good()) + return; + + PrepareNode(m_pState->NextGroupType(GroupType::Seq)); + + m_pState->StartedGroup(GroupType::Seq); +} + +// EmitEndSeq +void Emitter::EmitEndSeq() { + if (!good()) + return; + + if (m_pState->CurGroupChildCount() == 0) + m_pState->ForceFlow(); + + if (m_pState->CurGroupFlowType() == FlowType::Flow) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(m_pState->CurIndent()); + if (m_pState->CurGroupChildCount() == 0) + m_stream << "["; + m_stream << "]"; + } + + m_pState->EndedGroup(GroupType::Seq); +} + +// EmitBeginMap +void Emitter::EmitBeginMap() { + if (!good()) + return; + + PrepareNode(m_pState->NextGroupType(GroupType::Map)); + + m_pState->StartedGroup(GroupType::Map); +} + +// EmitEndMap +void Emitter::EmitEndMap() { + if (!good()) + return; + + if (m_pState->CurGroupChildCount() == 0) + m_pState->ForceFlow(); + + if (m_pState->CurGroupFlowType() == FlowType::Flow) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(m_pState->CurIndent()); + if (m_pState->CurGroupChildCount() == 0) + m_stream << "{"; + m_stream << "}"; + } + + m_pState->EndedGroup(GroupType::Map); +} + +// EmitNewline +void Emitter::EmitNewline() { + if (!good()) + return; + + PrepareNode(EmitterNodeType::NoType); + m_stream << "\n"; + m_pState->SetNonContent(); +} + +bool Emitter::CanEmitNewline() const { return true; } + +// Put the stream in a state so we can simply write the next node +// E.g., if we're in a sequence, write the "- " +void Emitter::PrepareNode(EmitterNodeType::value child) { + switch (m_pState->CurGroupNodeType()) { + case EmitterNodeType::NoType: + PrepareTopNode(child); + break; + case EmitterNodeType::FlowSeq: + FlowSeqPrepareNode(child); + break; + case EmitterNodeType::BlockSeq: + BlockSeqPrepareNode(child); + break; + case EmitterNodeType::FlowMap: + FlowMapPrepareNode(child); + break; + case EmitterNodeType::BlockMap: + BlockMapPrepareNode(child); + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + assert(false); + break; + } +} + +void Emitter::PrepareTopNode(EmitterNodeType::value child) { + if (child == EmitterNodeType::NoType) + return; + + if (m_pState->CurGroupChildCount() > 0 && m_stream.col() > 0) { + if (child != EmitterNodeType::NoType) + EmitBeginDoc(); + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + // TODO: if we were writing null, and + // we wanted it blank, we wouldn't want a space + SpaceOrIndentTo(m_pState->HasBegunContent(), 0); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + if (m_pState->HasBegunNode()) + m_stream << "\n"; + break; + } +} + +void Emitter::FlowSeqPrepareNode(EmitterNodeType::value child) { + const std::size_t lastIndent = m_pState->LastIndent(); + + if (!m_pState->HasBegunNode()) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(lastIndent); + if (m_pState->CurGroupChildCount() == 0) + m_stream << "["; + else + m_stream << ","; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo( + m_pState->HasBegunContent() || m_pState->CurGroupChildCount() > 0, + lastIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + assert(false); + break; + } +} + +void Emitter::BlockSeqPrepareNode(EmitterNodeType::value child) { + const std::size_t curIndent = m_pState->CurIndent(); + const std::size_t nextIndent = curIndent + m_pState->CurGroupIndent(); + + if (child == EmitterNodeType::NoType) + return; + + if (!m_pState->HasBegunContent()) { + if (m_pState->CurGroupChildCount() > 0 || m_stream.comment()) { + m_stream << "\n"; + } + m_stream << IndentTo(curIndent); + m_stream << "-"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo(m_pState->HasBegunContent(), nextIndent); + break; + case EmitterNodeType::BlockSeq: + m_stream << "\n"; + break; + case EmitterNodeType::BlockMap: + if (m_pState->HasBegunContent() || m_stream.comment()) + m_stream << "\n"; + break; + } +} + +void Emitter::FlowMapPrepareNode(EmitterNodeType::value child) { + if (m_pState->CurGroupChildCount() % 2 == 0) { + if (m_pState->GetMapKeyFormat() == LongKey) + m_pState->SetLongKey(); + + if (m_pState->CurGroupLongKey()) + FlowMapPrepareLongKey(child); + else + FlowMapPrepareSimpleKey(child); + } else { + if (m_pState->CurGroupLongKey()) + FlowMapPrepareLongKeyValue(child); + else + FlowMapPrepareSimpleKeyValue(child); + } +} + +void Emitter::FlowMapPrepareLongKey(EmitterNodeType::value child) { + const std::size_t lastIndent = m_pState->LastIndent(); + + if (!m_pState->HasBegunNode()) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(lastIndent); + if (m_pState->CurGroupChildCount() == 0) + m_stream << "{ ?"; + else + m_stream << ", ?"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo( + m_pState->HasBegunContent() || m_pState->CurGroupChildCount() > 0, + lastIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + assert(false); + break; + } +} + +void Emitter::FlowMapPrepareLongKeyValue(EmitterNodeType::value child) { + const std::size_t lastIndent = m_pState->LastIndent(); + + if (!m_pState->HasBegunNode()) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(lastIndent); + m_stream << ":"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo( + m_pState->HasBegunContent() || m_pState->CurGroupChildCount() > 0, + lastIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + assert(false); + break; + } +} + +void Emitter::FlowMapPrepareSimpleKey(EmitterNodeType::value child) { + const std::size_t lastIndent = m_pState->LastIndent(); + + if (!m_pState->HasBegunNode()) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(lastIndent); + if (m_pState->CurGroupChildCount() == 0) + m_stream << "{"; + else + m_stream << ","; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo( + m_pState->HasBegunContent() || m_pState->CurGroupChildCount() > 0, + lastIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + assert(false); + break; + } +} + +void Emitter::FlowMapPrepareSimpleKeyValue(EmitterNodeType::value child) { + const std::size_t lastIndent = m_pState->LastIndent(); + + if (!m_pState->HasBegunNode()) { + if (m_stream.comment()) + m_stream << "\n"; + m_stream << IndentTo(lastIndent); + m_stream << ":"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo( + m_pState->HasBegunContent() || m_pState->CurGroupChildCount() > 0, + lastIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + assert(false); + break; + } +} + +void Emitter::BlockMapPrepareNode(EmitterNodeType::value child) { + if (m_pState->CurGroupChildCount() % 2 == 0) { + if (m_pState->GetMapKeyFormat() == LongKey) + m_pState->SetLongKey(); + if (child == EmitterNodeType::BlockSeq || + child == EmitterNodeType::BlockMap) + m_pState->SetLongKey(); + + if (m_pState->CurGroupLongKey()) + BlockMapPrepareLongKey(child); + else + BlockMapPrepareSimpleKey(child); + } else { + if (m_pState->CurGroupLongKey()) + BlockMapPrepareLongKeyValue(child); + else + BlockMapPrepareSimpleKeyValue(child); + } +} + +void Emitter::BlockMapPrepareLongKey(EmitterNodeType::value child) { + const std::size_t curIndent = m_pState->CurIndent(); + const std::size_t childCount = m_pState->CurGroupChildCount(); + + if (child == EmitterNodeType::NoType) + return; + + if (!m_pState->HasBegunContent()) { + if (childCount > 0) { + m_stream << "\n"; + } + if (m_stream.comment()) { + m_stream << "\n"; + } + m_stream << IndentTo(curIndent); + m_stream << "?"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo(true, curIndent + 1); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + break; + } +} + +void Emitter::BlockMapPrepareLongKeyValue(EmitterNodeType::value child) { + const std::size_t curIndent = m_pState->CurIndent(); + + if (child == EmitterNodeType::NoType) + return; + + if (!m_pState->HasBegunContent()) { + m_stream << "\n"; + m_stream << IndentTo(curIndent); + m_stream << ":"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + SpaceOrIndentTo(true, curIndent + 1); + break; + } +} + +void Emitter::BlockMapPrepareSimpleKey(EmitterNodeType::value child) { + const std::size_t curIndent = m_pState->CurIndent(); + const std::size_t childCount = m_pState->CurGroupChildCount(); + + if (child == EmitterNodeType::NoType) + return; + + if (!m_pState->HasBegunNode()) { + if (childCount > 0) { + m_stream << "\n"; + } + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo(m_pState->HasBegunContent(), curIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + break; + } +} + +void Emitter::BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child) { + const std::size_t curIndent = m_pState->CurIndent(); + const std::size_t nextIndent = curIndent + m_pState->CurGroupIndent(); + + if (!m_pState->HasBegunNode()) { + m_stream << ":"; + } + + switch (child) { + case EmitterNodeType::NoType: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo(true, nextIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + m_stream << "\n"; + break; + } +} + +// SpaceOrIndentTo +// . Prepares for some more content by proper spacing +void Emitter::SpaceOrIndentTo(bool requireSpace, std::size_t indent) { + if (m_stream.comment()) + m_stream << "\n"; + if (m_stream.col() > 0 && requireSpace) + m_stream << " "; + m_stream << IndentTo(indent); +} + +void Emitter::PrepareIntegralStream(std::stringstream& stream) const { + + switch (m_pState->GetIntFormat()) { + case Dec: + stream << std::dec; + break; + case Hex: + stream << "0x"; + stream << std::hex; + break; + case Oct: + stream << "0"; + stream << std::oct; + break; + default: + assert(false); + } +} + +void Emitter::StartedScalar() { m_pState->StartedScalar(); } + +// ******************************************************************************************* +// overloads of Write + +Emitter& Emitter::Write(const std::string& str) { + if (!good()) + return *this; + + const bool escapeNonAscii = m_pState->GetOutputCharset() == EscapeNonAscii; + const StringFormat::value strFormat = + Utils::ComputeStringFormat(str, m_pState->GetStringFormat(), + m_pState->CurGroupFlowType(), escapeNonAscii); + + if (strFormat == StringFormat::Literal) + m_pState->SetMapKeyFormat(YODA_YAML::LongKey, FmtScope::Local); + + PrepareNode(EmitterNodeType::Scalar); + + switch (strFormat) { + case StringFormat::Plain: + m_stream << str; + break; + case StringFormat::SingleQuoted: + Utils::WriteSingleQuotedString(m_stream, str); + break; + case StringFormat::DoubleQuoted: + Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii); + break; + case StringFormat::Literal: + Utils::WriteLiteralString(m_stream, str, + m_pState->CurIndent() + m_pState->GetIndent()); + break; + } + + StartedScalar(); + + return *this; +} + +std::size_t Emitter::GetFloatPrecision() const { + return m_pState->GetFloatPrecision(); +} + +std::size_t Emitter::GetDoublePrecision() const { + return m_pState->GetDoublePrecision(); +} + +const char* Emitter::ComputeFullBoolName(bool b) const { + const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool + ? YesNoBool + : m_pState->GetBoolFormat()); + const EMITTER_MANIP caseFmt = m_pState->GetBoolCaseFormat(); + switch (mainFmt) { + case YesNoBool: + switch (caseFmt) { + case UpperCase: + return b ? "YES" : "NO"; + case CamelCase: + return b ? "Yes" : "No"; + case LowerCase: + return b ? "yes" : "no"; + default: + break; + } + break; + case OnOffBool: + switch (caseFmt) { + case UpperCase: + return b ? "ON" : "OFF"; + case CamelCase: + return b ? "On" : "Off"; + case LowerCase: + return b ? "on" : "off"; + default: + break; + } + break; + case TrueFalseBool: + switch (caseFmt) { + case UpperCase: + return b ? "TRUE" : "FALSE"; + case CamelCase: + return b ? "True" : "False"; + case LowerCase: + return b ? "true" : "false"; + default: + break; + } + break; + default: + break; + } + return b ? "y" : "n"; // should never get here, but it can't hurt to give + // these answers +} + +Emitter& Emitter::Write(bool b) { + if (!good()) + return *this; + + PrepareNode(EmitterNodeType::Scalar); + + const char* name = ComputeFullBoolName(b); + if (m_pState->GetBoolLengthFormat() == ShortBool) + m_stream << name[0]; + else + m_stream << name; + + StartedScalar(); + + return *this; +} + +Emitter& Emitter::Write(char ch) { + if (!good()) + return *this; + + PrepareNode(EmitterNodeType::Scalar); + Utils::WriteChar(m_stream, ch); + StartedScalar(); + + return *this; +} + +Emitter& Emitter::Write(const _Alias& alias) { + if (!good()) + return *this; + + if (m_pState->HasAnchor() || m_pState->HasTag()) { + m_pState->SetError(ErrorMsg::INVALID_ALIAS); + return *this; + } + + PrepareNode(EmitterNodeType::Scalar); + + if (!Utils::WriteAlias(m_stream, alias.content)) { + m_pState->SetError(ErrorMsg::INVALID_ALIAS); + return *this; + } + + StartedScalar(); + + return *this; +} + +Emitter& Emitter::Write(const _Anchor& anchor) { + if (!good()) + return *this; + + if (m_pState->HasAnchor()) { + m_pState->SetError(ErrorMsg::INVALID_ANCHOR); + return *this; + } + + PrepareNode(EmitterNodeType::Property); + + if (!Utils::WriteAnchor(m_stream, anchor.content)) { + m_pState->SetError(ErrorMsg::INVALID_ANCHOR); + return *this; + } + + m_pState->SetAnchor(); + + return *this; +} + +Emitter& Emitter::Write(const _Tag& tag) { + if (!good()) + return *this; + + if (m_pState->HasTag()) { + m_pState->SetError(ErrorMsg::INVALID_TAG); + return *this; + } + + PrepareNode(EmitterNodeType::Property); + + bool success = false; + if (tag.type == _Tag::Type::Verbatim) + success = Utils::WriteTag(m_stream, tag.content, true); + else if (tag.type == _Tag::Type::PrimaryHandle) + success = Utils::WriteTag(m_stream, tag.content, false); + else + success = Utils::WriteTagWithPrefix(m_stream, tag.prefix, tag.content); + + if (!success) { + m_pState->SetError(ErrorMsg::INVALID_TAG); + return *this; + } + + m_pState->SetTag(); + + return *this; +} + +void Emitter::EmitKindTag() { Write(LocalTag("")); } + +Emitter& Emitter::Write(const _Comment& comment) { + if (!good()) + return *this; + + PrepareNode(EmitterNodeType::NoType); + + if (m_stream.col() > 0) + m_stream << Indentation(m_pState->GetPreCommentIndent()); + Utils::WriteComment(m_stream, comment.content, + m_pState->GetPostCommentIndent()); + + m_pState->SetNonContent(); + + return *this; +} + +Emitter& Emitter::Write(const _Null& /*null*/) { + if (!good()) + return *this; + + PrepareNode(EmitterNodeType::Scalar); + + m_stream << "~"; + + StartedScalar(); + + return *this; +} + +Emitter& Emitter::Write(const Binary& binary) { + Write(SecondaryTag("binary")); + + if (!good()) + return *this; + + PrepareNode(EmitterNodeType::Scalar); + Utils::WriteBinary(m_stream, binary); + StartedScalar(); + + return *this; +} +} diff --git a/src/yamlcpp/emitterstate.cpp b/src/yamlcpp/emitterstate.cpp --- a/src/yamlcpp/emitterstate.cpp +++ b/src/yamlcpp/emitterstate.cpp @@ -1,283 +1,365 @@ -#include "emitterstate.h" -#include "yaml-cpp/exceptions.h" #include -namespace YODA_YAML -{ - EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_requiresSoftSeparation(false), m_requiresHardSeparation(false) - { - // start up - m_stateStack.push(ES_WAITING_FOR_DOC); +#include "emitterstate.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep - // set default global manipulators - m_charset.set(EmitNonAscii); - m_strFmt.set(Auto); - m_boolFmt.set(TrueFalseBool); - m_boolLengthFmt.set(LongBool); - m_boolCaseFmt.set(LowerCase); - m_intFmt.set(Dec); - m_indent.set(2); - m_preCommentIndent.set(2); - m_postCommentIndent.set(1); - m_seqFmt.set(Block); - m_mapFmt.set(Block); - m_mapKeyFmt.set(Auto); - m_floatPrecision.set(6); - m_doublePrecision.set(15); - } +namespace YODA_YAML { +EmitterState::EmitterState() + : m_isGood(true), + m_curIndent(0), + m_hasAnchor(false), + m_hasTag(false), + m_hasNonContent(false), + m_docCount(0) { + // set default global manipulators + m_charset.set(EmitNonAscii); + m_strFmt.set(Auto); + m_boolFmt.set(TrueFalseBool); + m_boolLengthFmt.set(LongBool); + m_boolCaseFmt.set(LowerCase); + m_intFmt.set(Dec); + m_indent.set(2); + m_preCommentIndent.set(2); + m_postCommentIndent.set(1); + m_seqFmt.set(Block); + m_mapFmt.set(Block); + m_mapKeyFmt.set(Auto); + m_floatPrecision.set(std::numeric_limits::digits10 + 1); + m_doublePrecision.set(std::numeric_limits::digits10 + 1); +} - EmitterState::~EmitterState() - { - } +EmitterState::~EmitterState() {} - // SetLocalValue - // . We blindly tries to set all possible formatters to this value - // . Only the ones that make sense will be accepted - void EmitterState::SetLocalValue(EMITTER_MANIP value) - { - SetOutputCharset(value, LOCAL); - SetStringFormat(value, LOCAL); - SetBoolFormat(value, LOCAL); - SetBoolCaseFormat(value, LOCAL); - SetBoolLengthFormat(value, LOCAL); - SetIntFormat(value, LOCAL); - SetFlowType(GT_SEQ, value, LOCAL); - SetFlowType(GT_MAP, value, LOCAL); - SetMapKeyFormat(value, LOCAL); - } +// SetLocalValue +// . We blindly tries to set all possible formatters to this value +// . Only the ones that make sense will be accepted +void EmitterState::SetLocalValue(EMITTER_MANIP value) { + SetOutputCharset(value, FmtScope::Local); + SetStringFormat(value, FmtScope::Local); + SetBoolFormat(value, FmtScope::Local); + SetBoolCaseFormat(value, FmtScope::Local); + SetBoolLengthFormat(value, FmtScope::Local); + SetIntFormat(value, FmtScope::Local); + SetFlowType(GroupType::Seq, value, FmtScope::Local); + SetFlowType(GroupType::Map, value, FmtScope::Local); + SetMapKeyFormat(value, FmtScope::Local); +} - void EmitterState::BeginGroup(GROUP_TYPE type) - { - unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent); - m_curIndent += lastIndent; +void EmitterState::SetAnchor() { m_hasAnchor = true; } - std::unique_ptr pGroup(new Group(type)); +void EmitterState::SetTag() { m_hasTag = true; } - // transfer settings (which last until this group is done) - pGroup->modifiedSettings = m_modifiedSettings; +void EmitterState::SetNonContent() { m_hasNonContent = true; } - // set up group - pGroup->flow = GetFlowType(type); - pGroup->indent = GetIndent(); - pGroup->usingLongKey = (GetMapKeyFormat() == LongKey ? true : false); +void EmitterState::SetLongKey() { + assert(!m_groups.empty()); + if (m_groups.empty()) { + return; + } - m_groups.push(std::move(pGroup)); - } + assert(m_groups.back()->type == GroupType::Map); + m_groups.back()->longKey = true; +} - void EmitterState::EndGroup(GROUP_TYPE type) - { - if(m_groups.empty()) - return SetError(ErrorMsg::UNMATCHED_GROUP_TAG); +void EmitterState::ForceFlow() { + assert(!m_groups.empty()); + if (m_groups.empty()) { + return; + } - // get rid of the current group - { - std::unique_ptr pFinishedGroup = m_groups.pop(); - if(pFinishedGroup->type != type) - return SetError(ErrorMsg::UNMATCHED_GROUP_TAG); - } + m_groups.back()->flowType = FlowType::Flow; +} - // reset old settings - unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent); - assert(m_curIndent >= lastIndent); - m_curIndent -= lastIndent; +void EmitterState::StartedNode() { + if (m_groups.empty()) { + m_docCount++; + } else { + m_groups.back()->childCount++; + if (m_groups.back()->childCount % 2 == 0) { + m_groups.back()->longKey = false; + } + } - // some global settings that we changed may have been overridden - // by a local setting we just popped, so we need to restore them - m_globalModifiedSettings.restore(); - } + m_hasAnchor = false; + m_hasTag = false; + m_hasNonContent = false; +} - GROUP_TYPE EmitterState::GetCurGroupType() const - { - if(m_groups.empty()) - return GT_NONE; +EmitterNodeType::value EmitterState::NextGroupType( + GroupType::value type) const { + if (type == GroupType::Seq) { + if (GetFlowType(type) == Block) + return EmitterNodeType::BlockSeq; + else + return EmitterNodeType::FlowSeq; + } else { + if (GetFlowType(type) == Block) + return EmitterNodeType::BlockMap; + else + return EmitterNodeType::FlowMap; + } - return m_groups.top().type; - } + // can't happen + assert(false); + return EmitterNodeType::NoType; +} - FLOW_TYPE EmitterState::GetCurGroupFlowType() const - { - if(m_groups.empty()) - return FT_NONE; +void EmitterState::StartedDoc() { + m_hasAnchor = false; + m_hasTag = false; + m_hasNonContent = false; +} - return (m_groups.top().flow == Flow ? FT_FLOW : FT_BLOCK); - } +void EmitterState::EndedDoc() { + m_hasAnchor = false; + m_hasTag = false; + m_hasNonContent = false; +} - bool EmitterState::CurrentlyInLongKey() - { - if(m_groups.empty()) - return false; - return m_groups.top().usingLongKey; - } +void EmitterState::StartedScalar() { + StartedNode(); + ClearModifiedSettings(); +} - void EmitterState::StartLongKey() - { - if(!m_groups.empty()) - m_groups.top().usingLongKey = true; - } +void EmitterState::StartedGroup(GroupType::value type) { + StartedNode(); - void EmitterState::StartSimpleKey() - { - if(!m_groups.empty()) - m_groups.top().usingLongKey = false; - } + const std::size_t lastGroupIndent = + (m_groups.empty() ? 0 : m_groups.back()->indent); + m_curIndent += lastGroupIndent; - void EmitterState::ClearModifiedSettings() - { - m_modifiedSettings.clear(); - } + // TODO: Create move constructors for settings types to simplify transfer + std::unique_ptr pGroup(new Group(type)); - bool EmitterState::SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case EmitNonAscii: - case EscapeNonAscii: - _Set(m_charset, value, scope); - return true; - default: - return false; - } - } + // transfer settings (which last until this group is done) + // + // NB: if pGroup->modifiedSettings == m_modifiedSettings, + // m_modifiedSettings is not changed! + pGroup->modifiedSettings = std::move(m_modifiedSettings); - bool EmitterState::SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case Auto: - case SingleQuoted: - case DoubleQuoted: - case Literal: - _Set(m_strFmt, value, scope); - return true; - default: - return false; - } - } + // set up group + if (GetFlowType(type) == Block) { + pGroup->flowType = FlowType::Block; + } else { + pGroup->flowType = FlowType::Flow; + } + pGroup->indent = GetIndent(); - bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case OnOffBool: - case TrueFalseBool: - case YesNoBool: - _Set(m_boolFmt, value, scope); - return true; - default: - return false; - } - } + m_groups.push_back(std::move(pGroup)); +} - bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case LongBool: - case ShortBool: - _Set(m_boolLengthFmt, value, scope); - return true; - default: - return false; - } - } +void EmitterState::EndedGroup(GroupType::value type) { + if (m_groups.empty()) { + if (type == GroupType::Seq) { + return SetError(ErrorMsg::UNEXPECTED_END_SEQ); + } else { + return SetError(ErrorMsg::UNEXPECTED_END_MAP); + } + } - bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case UpperCase: - case LowerCase: - case CamelCase: - _Set(m_boolCaseFmt, value, scope); - return true; - default: - return false; - } - } + // get rid of the current group + { + std::unique_ptr pFinishedGroup = std::move(m_groups.back()); + m_groups.pop_back(); + if (pFinishedGroup->type != type) { + return SetError(ErrorMsg::UNMATCHED_GROUP_TAG); + } + } - bool EmitterState::SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case Dec: - case Hex: - case Oct: - _Set(m_intFmt, value, scope); - return true; - default: - return false; - } - } + // reset old settings + std::size_t lastIndent = (m_groups.empty() ? 0 : m_groups.back()->indent); + assert(m_curIndent >= lastIndent); + m_curIndent -= lastIndent; - bool EmitterState::SetIndent(unsigned value, FMT_SCOPE scope) - { - if(value == 0) - return false; + // some global settings that we changed may have been overridden + // by a local setting we just popped, so we need to restore them + m_globalModifiedSettings.restore(); - _Set(m_indent, value, scope); - return true; - } + ClearModifiedSettings(); +} - bool EmitterState::SetPreCommentIndent(unsigned value, FMT_SCOPE scope) - { - if(value == 0) - return false; +EmitterNodeType::value EmitterState::CurGroupNodeType() const { + if (m_groups.empty()) { + return EmitterNodeType::NoType; + } - _Set(m_preCommentIndent, value, scope); - return true; - } + return m_groups.back()->NodeType(); +} - bool EmitterState::SetPostCommentIndent(unsigned value, FMT_SCOPE scope) - { - if(value == 0) - return false; +GroupType::value EmitterState::CurGroupType() const { + return m_groups.empty() ? GroupType::NoType : m_groups.back()->type; +} - _Set(m_postCommentIndent, value, scope); - return true; - } +FlowType::value EmitterState::CurGroupFlowType() const { + return m_groups.empty() ? FlowType::NoType : m_groups.back()->flowType; +} - bool EmitterState::SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case Block: - case Flow: - _Set(groupType == GT_SEQ ? m_seqFmt : m_mapFmt, value, scope); - return true; - default: - return false; - } - } +std::size_t EmitterState::CurGroupIndent() const { + return m_groups.empty() ? 0 : m_groups.back()->indent; +} - EMITTER_MANIP EmitterState::GetFlowType(GROUP_TYPE groupType) const - { - // force flow style if we're currently in a flow - FLOW_TYPE flowType = GetCurGroupFlowType(); - if(flowType == FT_FLOW) - return Flow; +std::size_t EmitterState::CurGroupChildCount() const { + return m_groups.empty() ? m_docCount : m_groups.back()->childCount; +} - // otherwise, go with what's asked of use - return (groupType == GT_SEQ ? m_seqFmt.get() : m_mapFmt.get()); - } +bool EmitterState::CurGroupLongKey() const { + return m_groups.empty() ? false : m_groups.back()->longKey; +} - bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope) - { - switch(value) { - case Auto: - case LongKey: - _Set(m_mapKeyFmt, value, scope); - return true; - default: - return false; - } - } +std::size_t EmitterState::LastIndent() const { + if (m_groups.size() <= 1) { + return 0; + } - bool EmitterState::SetFloatPrecision(int value, FMT_SCOPE scope) - { - if(value < 0 || value > std::numeric_limits::digits10) - return false; - _Set(m_floatPrecision, value, scope); - return true; - } + return m_curIndent - m_groups[m_groups.size() - 2]->indent; +} - bool EmitterState::SetDoublePrecision(int value, FMT_SCOPE scope) - { - if(value < 0 || value > std::numeric_limits::digits10) - return false; - _Set(m_doublePrecision, value, scope); - return true; - } +void EmitterState::ClearModifiedSettings() { m_modifiedSettings.clear(); } + +bool EmitterState::SetOutputCharset(EMITTER_MANIP value, + FmtScope::value scope) { + switch (value) { + case EmitNonAscii: + case EscapeNonAscii: + _Set(m_charset, value, scope); + return true; + default: + return false; + } } + +bool EmitterState::SetStringFormat(EMITTER_MANIP value, FmtScope::value scope) { + switch (value) { + case Auto: + case SingleQuoted: + case DoubleQuoted: + case Literal: + _Set(m_strFmt, value, scope); + return true; + default: + return false; + } +} + +bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope) { + switch (value) { + case OnOffBool: + case TrueFalseBool: + case YesNoBool: + _Set(m_boolFmt, value, scope); + return true; + default: + return false; + } +} + +bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, + FmtScope::value scope) { + switch (value) { + case LongBool: + case ShortBool: + _Set(m_boolLengthFmt, value, scope); + return true; + default: + return false; + } +} + +bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, + FmtScope::value scope) { + switch (value) { + case UpperCase: + case LowerCase: + case CamelCase: + _Set(m_boolCaseFmt, value, scope); + return true; + default: + return false; + } +} + +bool EmitterState::SetIntFormat(EMITTER_MANIP value, FmtScope::value scope) { + switch (value) { + case Dec: + case Hex: + case Oct: + _Set(m_intFmt, value, scope); + return true; + default: + return false; + } +} + +bool EmitterState::SetIndent(std::size_t value, FmtScope::value scope) { + if (value <= 1) + return false; + + _Set(m_indent, value, scope); + return true; +} + +bool EmitterState::SetPreCommentIndent(std::size_t value, + FmtScope::value scope) { + if (value == 0) + return false; + + _Set(m_preCommentIndent, value, scope); + return true; +} + +bool EmitterState::SetPostCommentIndent(std::size_t value, + FmtScope::value scope) { + if (value == 0) + return false; + + _Set(m_postCommentIndent, value, scope); + return true; +} + +bool EmitterState::SetFlowType(GroupType::value groupType, EMITTER_MANIP value, + FmtScope::value scope) { + switch (value) { + case Block: + case Flow: + _Set(groupType == GroupType::Seq ? m_seqFmt : m_mapFmt, value, scope); + return true; + default: + return false; + } +} + +EMITTER_MANIP EmitterState::GetFlowType(GroupType::value groupType) const { + // force flow style if we're currently in a flow + if (CurGroupFlowType() == FlowType::Flow) + return Flow; + + // otherwise, go with what's asked of us + return (groupType == GroupType::Seq ? m_seqFmt.get() : m_mapFmt.get()); +} + +bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope) { + switch (value) { + case Auto: + case LongKey: + _Set(m_mapKeyFmt, value, scope); + return true; + default: + return false; + } +} + +bool EmitterState::SetFloatPrecision(std::size_t value, FmtScope::value scope) { + if (value > std::numeric_limits::digits10 + 1) + return false; + _Set(m_floatPrecision, value, scope); + return true; +} + +bool EmitterState::SetDoublePrecision(std::size_t value, + FmtScope::value scope) { + if (value > std::numeric_limits::digits10 + 1) + return false; + _Set(m_doublePrecision, value, scope); + return true; +} +} diff --git a/src/yamlcpp/emitterstate.h b/src/yamlcpp/emitterstate.h --- a/src/yamlcpp/emitterstate.h +++ b/src/yamlcpp/emitterstate.h @@ -1,217 +1,203 @@ #ifndef EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include "setting.h" +#include "yaml-cpp/emitterdef.h" +#include "yaml-cpp/emittermanip.h" -#include "ptr_stack.h" -#include "setting.h" -#include "yaml-cpp/emittermanip.h" #include +#include +#include +#include #include -#include -#include -namespace YODA_YAML -{ - enum FMT_SCOPE { - LOCAL, - GLOBAL - }; - - enum GROUP_TYPE { - GT_NONE, - GT_SEQ, - GT_MAP - }; - - enum FLOW_TYPE { - FT_NONE, - FT_FLOW, - FT_BLOCK - }; - - enum NODE_STATE { - NS_START, - NS_READY_FOR_ATOM, - NS_END - }; - - enum EMITTER_STATE { - ES_WAITING_FOR_DOC, - ES_WRITING_DOC, - ES_DONE_WITH_DOC, - - // block seq - ES_WAITING_FOR_BLOCK_SEQ_ENTRY, - ES_WRITING_BLOCK_SEQ_ENTRY, - ES_DONE_WITH_BLOCK_SEQ_ENTRY, - - // flow seq - ES_WAITING_FOR_FLOW_SEQ_ENTRY, - ES_WRITING_FLOW_SEQ_ENTRY, - ES_DONE_WITH_FLOW_SEQ_ENTRY, - - // block map - ES_WAITING_FOR_BLOCK_MAP_ENTRY, - ES_WAITING_FOR_BLOCK_MAP_KEY, - ES_WRITING_BLOCK_MAP_KEY, - ES_DONE_WITH_BLOCK_MAP_KEY, - ES_WAITING_FOR_BLOCK_MAP_VALUE, - ES_WRITING_BLOCK_MAP_VALUE, - ES_DONE_WITH_BLOCK_MAP_VALUE, - - // flow map - ES_WAITING_FOR_FLOW_MAP_ENTRY, - ES_WAITING_FOR_FLOW_MAP_KEY, - ES_WRITING_FLOW_MAP_KEY, - ES_DONE_WITH_FLOW_MAP_KEY, - ES_WAITING_FOR_FLOW_MAP_VALUE, - ES_WRITING_FLOW_MAP_VALUE, - ES_DONE_WITH_FLOW_MAP_VALUE - }; - - class EmitterState - { - public: - EmitterState(); - ~EmitterState(); - - // basic state checking - bool good() const { return m_isGood; } - const std::string GetLastError() const { return m_lastError; } - void SetError(const std::string& error) { m_isGood = false; m_lastError = error; } - - // main state of the machine - EMITTER_STATE GetCurState() const { return m_stateStack.top(); } - void SwitchState(EMITTER_STATE state) { PopState(); PushState(state); } - void PushState(EMITTER_STATE state) { m_stateStack.push(state); } - void PopState() { m_stateStack.pop(); } - - void SetLocalValue(EMITTER_MANIP value); - - // group handling - void BeginGroup(GROUP_TYPE type); - void EndGroup(GROUP_TYPE type); - - GROUP_TYPE GetCurGroupType() const; - FLOW_TYPE GetCurGroupFlowType() const; - int GetCurIndent() const { return m_curIndent; } - - bool CurrentlyInLongKey(); - void StartLongKey(); - void StartSimpleKey(); +namespace YODA_YAML { +struct FmtScope { + enum value { Local, Global }; +}; +struct GroupType { + enum value { NoType, Seq, Map }; +}; +struct FlowType { + enum value { NoType, Flow, Block }; +}; - bool RequiresSoftSeparation() const { return m_requiresSoftSeparation; } - bool RequiresHardSeparation() const { return m_requiresHardSeparation; } - void RequireSoftSeparation() { m_requiresSoftSeparation = true; } - void RequireHardSeparation() { m_requiresSoftSeparation = true; m_requiresHardSeparation = true; } - void ForceHardSeparation() { m_requiresSoftSeparation = false; } - void UnsetSeparation() { m_requiresSoftSeparation = false; m_requiresHardSeparation = false; } +class EmitterState { + public: + EmitterState(); + ~EmitterState(); - void ClearModifiedSettings(); + // basic state checking + bool good() const { return m_isGood; } + const std::string GetLastError() const { return m_lastError; } + void SetError(const std::string& error) { + m_isGood = false; + m_lastError = error; + } - // formatters - bool SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); } + // node handling + void SetAnchor(); + void SetTag(); + void SetNonContent(); + void SetLongKey(); + void ForceFlow(); + void StartedDoc(); + void EndedDoc(); + void StartedScalar(); + void StartedGroup(GroupType::value type); + void EndedGroup(GroupType::value type); - bool SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); } - - bool SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); } + EmitterNodeType::value NextGroupType(GroupType::value type) const; + EmitterNodeType::value CurGroupNodeType() const; - bool SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); } + GroupType::value CurGroupType() const; + FlowType::value CurGroupFlowType() const; + std::size_t CurGroupIndent() const; + std::size_t CurGroupChildCount() const; + bool CurGroupLongKey() const; - bool SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); } + std::size_t LastIndent() const; + std::size_t CurIndent() const { return m_curIndent; } + bool HasAnchor() const { return m_hasAnchor; } + bool HasTag() const { return m_hasTag; } + bool HasBegunNode() const { + return m_hasAnchor || m_hasTag || m_hasNonContent; + } + bool HasBegunContent() const { return m_hasAnchor || m_hasTag; } - bool SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); } + void ClearModifiedSettings(); - bool SetIndent(unsigned value, FMT_SCOPE scope); - int GetIndent() const { return m_indent.get(); } - - bool SetPreCommentIndent(unsigned value, FMT_SCOPE scope); - int GetPreCommentIndent() const { return m_preCommentIndent.get(); } - bool SetPostCommentIndent(unsigned value, FMT_SCOPE scope); - int GetPostCommentIndent() const { return m_postCommentIndent.get(); } - - bool SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetFlowType(GROUP_TYPE groupType) const; - - bool SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope); - EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); } - - bool SetFloatPrecision(int value, FMT_SCOPE scope); - unsigned GetFloatPrecision() const { return m_floatPrecision.get(); } - bool SetDoublePrecision(int value, FMT_SCOPE scope); - unsigned GetDoublePrecision() const { return m_doublePrecision.get(); } - - private: - template - void _Set(Setting& fmt, T value, FMT_SCOPE scope); - - private: - // basic state ok? - bool m_isGood; - std::string m_lastError; - - // other state - std::stack m_stateStack; - - Setting m_charset; - Setting m_strFmt; - Setting m_boolFmt; - Setting m_boolLengthFmt; - Setting m_boolCaseFmt; - Setting m_intFmt; - Setting m_indent; - Setting m_preCommentIndent, m_postCommentIndent; - Setting m_seqFmt; - Setting m_mapFmt; - Setting m_mapKeyFmt; - Setting m_floatPrecision; - Setting m_doublePrecision; - - SettingChanges m_modifiedSettings; - SettingChanges m_globalModifiedSettings; - - struct Group { - Group(GROUP_TYPE type_): type(type_), usingLongKey(false), indent(0) {} - - GROUP_TYPE type; - EMITTER_MANIP flow; - bool usingLongKey; - int indent; - - SettingChanges modifiedSettings; - }; + // formatters + void SetLocalValue(EMITTER_MANIP value); - ptr_stack m_groups; - unsigned m_curIndent; - bool m_requiresSoftSeparation; - bool m_requiresHardSeparation; - }; + bool SetOutputCharset(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); } - template - void EmitterState::_Set(Setting& fmt, T value, FMT_SCOPE scope) { - switch(scope) { - case LOCAL: - m_modifiedSettings.push(fmt.set(value)); - break; - case GLOBAL: - fmt.set(value); - m_globalModifiedSettings.push(fmt.set(value)); // this pushes an identity set, so when we restore, - // it restores to the value here, and not the previous one - break; - default: - assert(false); - } - } + bool SetStringFormat(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); } + + bool SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); } + + bool SetBoolLengthFormat(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); } + + bool SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); } + + bool SetIntFormat(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); } + + bool SetIndent(std::size_t value, FmtScope::value scope); + std::size_t GetIndent() const { return m_indent.get(); } + + bool SetPreCommentIndent(std::size_t value, FmtScope::value scope); + std::size_t GetPreCommentIndent() const { return m_preCommentIndent.get(); } + bool SetPostCommentIndent(std::size_t value, FmtScope::value scope); + std::size_t GetPostCommentIndent() const { return m_postCommentIndent.get(); } + + bool SetFlowType(GroupType::value groupType, EMITTER_MANIP value, + FmtScope::value scope); + EMITTER_MANIP GetFlowType(GroupType::value groupType) const; + + bool SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope); + EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); } + + bool SetFloatPrecision(std::size_t value, FmtScope::value scope); + std::size_t GetFloatPrecision() const { return m_floatPrecision.get(); } + bool SetDoublePrecision(std::size_t value, FmtScope::value scope); + std::size_t GetDoublePrecision() const { return m_doublePrecision.get(); } + + private: + template + void _Set(Setting& fmt, T value, FmtScope::value scope); + + void StartedNode(); + + private: + // basic state ok? + bool m_isGood; + std::string m_lastError; + + // other state + Setting m_charset; + Setting m_strFmt; + Setting m_boolFmt; + Setting m_boolLengthFmt; + Setting m_boolCaseFmt; + Setting m_intFmt; + Setting m_indent; + Setting m_preCommentIndent, m_postCommentIndent; + Setting m_seqFmt; + Setting m_mapFmt; + Setting m_mapKeyFmt; + Setting m_floatPrecision; + Setting m_doublePrecision; + + SettingChanges m_modifiedSettings; + SettingChanges m_globalModifiedSettings; + + struct Group { + explicit Group(GroupType::value type_) + : type(type_), indent(0), childCount(0), longKey(false) {} + + GroupType::value type; + FlowType::value flowType; + std::size_t indent; + std::size_t childCount; + bool longKey; + + SettingChanges modifiedSettings; + + EmitterNodeType::value NodeType() const { + if (type == GroupType::Seq) { + if (flowType == FlowType::Flow) + return EmitterNodeType::FlowSeq; + else + return EmitterNodeType::BlockSeq; + } else { + if (flowType == FlowType::Flow) + return EmitterNodeType::FlowMap; + else + return EmitterNodeType::BlockMap; + } + + // can't get here + assert(false); + return EmitterNodeType::NoType; + } + }; + + std::vector> m_groups; + std::size_t m_curIndent; + bool m_hasAnchor; + bool m_hasTag; + bool m_hasNonContent; + std::size_t m_docCount; +}; + +template +void EmitterState::_Set(Setting& fmt, T value, FmtScope::value scope) { + switch (scope) { + case FmtScope::Local: + m_modifiedSettings.push(fmt.set(value)); + break; + case FmtScope::Global: + fmt.set(value); + m_globalModifiedSettings.push( + fmt.set(value)); // this pushes an identity set, so when we restore, + // it restores to the value here, and not the previous one + break; + default: + assert(false); + } +} } -#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/emitterutils.cpp b/src/yamlcpp/emitterutils.cpp --- a/src/yamlcpp/emitterutils.cpp +++ b/src/yamlcpp/emitterutils.cpp @@ -1,378 +1,483 @@ +#include +#include + #include "emitterutils.h" #include "exp.h" #include "indentation.h" -#include "yaml-cpp/binary.h" -#include "yaml-cpp/exceptions.h" +#include "regex_yaml.h" +#include "regeximpl.h" #include "stringsource.h" -#include -#include +#include "yaml-cpp/binary.h" // IWYU pragma: keep +#include "yaml-cpp/ostream_wrapper.h" +#include "yaml-cpp/null.h" -namespace YODA_YAML -{ - namespace Utils - { - namespace { - enum {REPLACEMENT_CHARACTER = 0xFFFD}; +namespace YODA_YAML { +namespace Utils { +namespace { +enum { REPLACEMENT_CHARACTER = 0xFFFD }; - bool IsAnchorChar(int ch) { // test for ns-anchor-char - switch (ch) { - case ',': case '[': case ']': case '{': case '}': // c-flow-indicator - case ' ': case '\t': // s-white - case 0xFEFF: // c-byte-order-mark - case 0xA: case 0xD: // b-char - return false; - case 0x85: - return true; - } +bool IsAnchorChar(int ch) { // test for ns-anchor-char + switch (ch) { + case ',': + case '[': + case ']': + case '{': + case '}': // c-flow-indicator + case ' ': + case '\t': // s-white + case 0xFEFF: // c-byte-order-mark + case 0xA: + case 0xD: // b-char + return false; + case 0x85: + return true; + } - if (ch < 0x20) - return false; + if (ch < 0x20) { + return false; + } - if (ch < 0x7E) - return true; + if (ch < 0x7E) { + return true; + } - if (ch < 0xA0) - return false; - if (ch >= 0xD800 && ch <= 0xDFFF) - return false; - if ((ch & 0xFFFE) == 0xFFFE) - return false; - if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) - return false; - if (ch > 0x10FFFF) - return false; + if (ch < 0xA0) { + return false; + } + if (ch >= 0xD800 && ch <= 0xDFFF) { + return false; + } + if ((ch & 0xFFFE) == 0xFFFE) { + return false; + } + if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) { + return false; + } + if (ch > 0x10FFFF) { + return false; + } - return true; - } - - int Utf8BytesIndicated(char ch) { - int byteVal = static_cast(ch); - switch (byteVal >> 4) { - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: - return 1; - case 12: case 13: - return 2; - case 14: - return 3; - case 15: - return 4; - default: - return -1; - } - } - - bool IsTrailingByte(char ch) { - return (ch & 0xC0) == 0x80; - } - - bool GetNextCodePointAndAdvance(int& codePoint, std::string::const_iterator& first, std::string::const_iterator last) { - if (first == last) - return false; - - int nBytes = Utf8BytesIndicated(*first); - if (nBytes < 1) { - // Bad lead byte - ++first; - codePoint = REPLACEMENT_CHARACTER; - return true; - } - - if (nBytes == 1) { - codePoint = *first++; - return true; - } - - // Gather bits from trailing bytes - codePoint = static_cast(*first) & ~(0xFF << (7 - nBytes)); - ++first; - --nBytes; - for (; nBytes > 0; ++first, --nBytes) { - if ((first == last) || !IsTrailingByte(*first)) { - codePoint = REPLACEMENT_CHARACTER; - break; - } - codePoint <<= 6; - codePoint |= *first & 0x3F; - } - - // Check for illegal code points - if (codePoint > 0x10FFFF) - codePoint = REPLACEMENT_CHARACTER; - else if (codePoint >= 0xD800 && codePoint <= 0xDFFF) - codePoint = REPLACEMENT_CHARACTER; - else if ((codePoint & 0xFFFE) == 0xFFFE) - codePoint = REPLACEMENT_CHARACTER; - else if (codePoint >= 0xFDD0 && codePoint <= 0xFDEF) - codePoint = REPLACEMENT_CHARACTER; - return true; - } - - void WriteCodePoint(ostream& out, int codePoint) { - if (codePoint < 0 || codePoint > 0x10FFFF) { - codePoint = REPLACEMENT_CHARACTER; - } - if (codePoint < 0x7F) { - out << static_cast(codePoint); - } else if (codePoint < 0x7FF) { - out << static_cast(0xC0 | (codePoint >> 6)) - << static_cast(0x80 | (codePoint & 0x3F)); - } else if (codePoint < 0xFFFF) { - out << static_cast(0xE0 | (codePoint >> 12)) - << static_cast(0x80 | ((codePoint >> 6) & 0x3F)) - << static_cast(0x80 | (codePoint & 0x3F)); - } else { - out << static_cast(0xF0 | (codePoint >> 18)) - << static_cast(0x80 | ((codePoint >> 12) & 0x3F)) - << static_cast(0x80 | ((codePoint >> 6) & 0x3F)) - << static_cast(0x80 | (codePoint & 0x3F)); - } - } - - bool IsValidPlainScalar(const std::string& str, bool inFlow, bool allowOnlyAscii) { - if(str.empty()) - return false; - - // first check the start - const RegEx& start = (inFlow ? Exp::PlainScalarInFlow() : Exp::PlainScalar()); - if(!start.Matches(str)) - return false; - - // and check the end for plain whitespace (which can't be faithfully kept in a plain scalar) - if(!str.empty() && *str.rbegin() == ' ') - return false; - - // then check until something is disallowed - const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow() : Exp::EndScalar()) - || (Exp::BlankOrBreak() + Exp::Comment()) - || Exp::NotPrintable() - || Exp::Utf8_ByteOrderMark() - || Exp::Break() - || Exp::Tab(); - StringCharSource buffer(str.c_str(), str.size()); - while(buffer) { - if(disallowed.Matches(buffer)) - return false; - if(allowOnlyAscii && (0x7F < static_cast(buffer[0]))) - return false; - ++buffer; - } - - return true; - } - - void WriteDoubleQuoteEscapeSequence(ostream& out, int codePoint) { - static const char hexDigits[] = "0123456789abcdef"; - - char escSeq[] = "\\U00000000"; - int digits = 8; - if (codePoint < 0xFF) { - escSeq[1] = 'x'; - digits = 2; - } else if (codePoint < 0xFFFF) { - escSeq[1] = 'u'; - digits = 4; - } - - // Write digits into the escape sequence - int i = 2; - for (; digits > 0; --digits, ++i) { - escSeq[i] = hexDigits[(codePoint >> (4 * (digits - 1))) & 0xF]; - } - - escSeq[i] = 0; // terminate with NUL character - out << escSeq; - } - - bool WriteAliasName(ostream& out, const std::string& str) { - int codePoint; - for(std::string::const_iterator i = str.begin(); - GetNextCodePointAndAdvance(codePoint, i, str.end()); - ) - { - if (!IsAnchorChar(codePoint)) - return false; - - WriteCodePoint(out, codePoint); - } - return true; - } - } - - bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii) - { - if(IsValidPlainScalar(str, inFlow, escapeNonAscii)) { - out << str; - return true; - } else - return WriteDoubleQuotedString(out, str, escapeNonAscii); - } - - bool WriteSingleQuotedString(ostream& out, const std::string& str) - { - out << "'"; - int codePoint; - for(std::string::const_iterator i = str.begin(); - GetNextCodePointAndAdvance(codePoint, i, str.end()); - ) - { - if (codePoint == '\n') - return false; // We can't handle a new line and the attendant indentation yet - - if (codePoint == '\'') - out << "''"; - else - WriteCodePoint(out, codePoint); - } - out << "'"; - return true; - } - - bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii) - { - out << "\""; - int codePoint; - for(std::string::const_iterator i = str.begin(); - GetNextCodePointAndAdvance(codePoint, i, str.end()); - ) - { - if (codePoint == '\"') - out << "\\\""; - else if (codePoint == '\\') - out << "\\\\"; - else if (codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space - WriteDoubleQuoteEscapeSequence(out, codePoint); - else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2) - WriteDoubleQuoteEscapeSequence(out, codePoint); - else if (escapeNonAscii && codePoint > 0x7E) - WriteDoubleQuoteEscapeSequence(out, codePoint); - else - WriteCodePoint(out, codePoint); - } - out << "\""; - return true; - } - - bool WriteLiteralString(ostream& out, const std::string& str, int indent) - { - out << "|\n"; - out << IndentTo(indent); - int codePoint; - for(std::string::const_iterator i = str.begin(); - GetNextCodePointAndAdvance(codePoint, i, str.end()); - ) - { - if (codePoint == '\n') - out << "\n" << IndentTo(indent); - else - WriteCodePoint(out, codePoint); - } - return true; - } - - bool WriteChar(ostream& out, char ch) - { - if(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')) - out << ch; - else if((0x20 <= ch && ch <= 0x7e) || ch == ' ') - out << "\"" << ch << "\""; - else if(ch == '\t') - out << "\"\\t\""; - else if(ch == '\n') - out << "\"\\n\""; - else if(ch == '\b') - out << "\"\\b\""; - else { - out << "\""; - WriteDoubleQuoteEscapeSequence(out, ch); - out << "\""; - } - return true; - } - - bool WriteComment(ostream& out, const std::string& str, int postCommentIndent) - { - const unsigned curIndent = out.col(); - out << "#" << Indentation(postCommentIndent); - int codePoint; - for(std::string::const_iterator i = str.begin(); - GetNextCodePointAndAdvance(codePoint, i, str.end()); - ) - { - if(codePoint == '\n') - out << "\n" << IndentTo(curIndent) << "#" << Indentation(postCommentIndent); - else - WriteCodePoint(out, codePoint); - } - return true; - } - - bool WriteAlias(ostream& out, const std::string& str) - { - out << "*"; - return WriteAliasName(out, str); - } - - bool WriteAnchor(ostream& out, const std::string& str) - { - out << "&"; - return WriteAliasName(out, str); - } - - bool WriteTag(ostream& out, const std::string& str, bool verbatim) - { - out << (verbatim ? "!<" : "!"); - StringCharSource buffer(str.c_str(), str.size()); - const RegEx& reValid = verbatim ? Exp::URI() : Exp::Tag(); - while(buffer) { - int n = reValid.Match(buffer); - if(n <= 0) - return false; - - while(--n >= 0) { - out << buffer[0]; - ++buffer; - } - } - if (verbatim) - out << ">"; - return true; - } - - bool WriteTagWithPrefix(ostream& out, const std::string& prefix, const std::string& tag) - { - out << "!"; - StringCharSource prefixBuffer(prefix.c_str(), prefix.size()); - while(prefixBuffer) { - int n = Exp::URI().Match(prefixBuffer); - if(n <= 0) - return false; - - while(--n >= 0) { - out << prefixBuffer[0]; - ++prefixBuffer; - } - } - - out << "!"; - StringCharSource tagBuffer(tag.c_str(), tag.size()); - while(tagBuffer) { - int n = Exp::Tag().Match(tagBuffer); - if(n <= 0) - return false; - - while(--n >= 0) { - out << tagBuffer[0]; - ++tagBuffer; - } - } - return true; - } - - bool WriteBinary(ostream& out, const Binary& binary) - { - WriteDoubleQuotedString(out, EncodeBase64(binary.data(), binary.size()), false); - return true; - } - } + return true; } +int Utf8BytesIndicated(char ch) { + int byteVal = static_cast(ch); + switch (byteVal >> 4) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + return 1; + case 12: + case 13: + return 2; + case 14: + return 3; + case 15: + return 4; + default: + return -1; + } +} + +bool IsTrailingByte(char ch) { return (ch & 0xC0) == 0x80; } + +bool GetNextCodePointAndAdvance(int& codePoint, + std::string::const_iterator& first, + std::string::const_iterator last) { + if (first == last) + return false; + + int nBytes = Utf8BytesIndicated(*first); + if (nBytes < 1) { + // Bad lead byte + ++first; + codePoint = REPLACEMENT_CHARACTER; + return true; + } + + if (nBytes == 1) { + codePoint = *first++; + return true; + } + + // Gather bits from trailing bytes + codePoint = static_cast(*first) & ~(0xFF << (7 - nBytes)); + ++first; + --nBytes; + for (; nBytes > 0; ++first, --nBytes) { + if ((first == last) || !IsTrailingByte(*first)) { + codePoint = REPLACEMENT_CHARACTER; + break; + } + codePoint <<= 6; + codePoint |= *first & 0x3F; + } + + // Check for illegal code points + if (codePoint > 0x10FFFF) + codePoint = REPLACEMENT_CHARACTER; + else if (codePoint >= 0xD800 && codePoint <= 0xDFFF) + codePoint = REPLACEMENT_CHARACTER; + else if ((codePoint & 0xFFFE) == 0xFFFE) + codePoint = REPLACEMENT_CHARACTER; + else if (codePoint >= 0xFDD0 && codePoint <= 0xFDEF) + codePoint = REPLACEMENT_CHARACTER; + return true; +} + +void WriteCodePoint(ostream_wrapper& out, int codePoint) { + if (codePoint < 0 || codePoint > 0x10FFFF) { + codePoint = REPLACEMENT_CHARACTER; + } + if (codePoint < 0x7F) { + out << static_cast(codePoint); + } else if (codePoint < 0x7FF) { + out << static_cast(0xC0 | (codePoint >> 6)) + << static_cast(0x80 | (codePoint & 0x3F)); + } else if (codePoint < 0xFFFF) { + out << static_cast(0xE0 | (codePoint >> 12)) + << static_cast(0x80 | ((codePoint >> 6) & 0x3F)) + << static_cast(0x80 | (codePoint & 0x3F)); + } else { + out << static_cast(0xF0 | (codePoint >> 18)) + << static_cast(0x80 | ((codePoint >> 12) & 0x3F)) + << static_cast(0x80 | ((codePoint >> 6) & 0x3F)) + << static_cast(0x80 | (codePoint & 0x3F)); + } +} + +bool IsValidPlainScalar(const std::string& str, FlowType::value flowType, + bool allowOnlyAscii) { + // check against null + if (IsNullString(str)) { + return false; + } + + // check the start + const RegEx& start = (flowType == FlowType::Flow ? Exp::PlainScalarInFlow() + : Exp::PlainScalar()); + if (!start.Matches(str)) { + return false; + } + + // and check the end for plain whitespace (which can't be faithfully kept in a + // plain scalar) + if (!str.empty() && *str.rbegin() == ' ') { + return false; + } + + // then check until something is disallowed + static const RegEx& disallowed_flow = + Exp::EndScalarInFlow() || (Exp::BlankOrBreak() + Exp::Comment()) || + Exp::NotPrintable() || Exp::Utf8_ByteOrderMark() || Exp::Break() || + Exp::Tab(); + static const RegEx& disallowed_block = + Exp::EndScalar() || (Exp::BlankOrBreak() + Exp::Comment()) || + Exp::NotPrintable() || Exp::Utf8_ByteOrderMark() || Exp::Break() || + Exp::Tab(); + const RegEx& disallowed = + flowType == FlowType::Flow ? disallowed_flow : disallowed_block; + + StringCharSource buffer(str.c_str(), str.size()); + while (buffer) { + if (disallowed.Matches(buffer)) { + return false; + } + if (allowOnlyAscii && (0x80 <= static_cast(buffer[0]))) { + return false; + } + ++buffer; + } + + return true; +} + +bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) { + // TODO: check for non-printable characters? + for (std::size_t i = 0; i < str.size(); i++) { + if (escapeNonAscii && (0x80 <= static_cast(str[i]))) { + return false; + } + if (str[i] == '\n') { + return false; + } + } + return true; +} + +bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType, + bool escapeNonAscii) { + if (flowType == FlowType::Flow) { + return false; + } + + // TODO: check for non-printable characters? + for (std::size_t i = 0; i < str.size(); i++) { + if (escapeNonAscii && (0x80 <= static_cast(str[i]))) { + return false; + } + } + return true; +} + +void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint) { + static const char hexDigits[] = "0123456789abcdef"; + + out << "\\"; + int digits = 8; + if (codePoint < 0xFF) { + out << "x"; + digits = 2; + } else if (codePoint < 0xFFFF) { + out << "u"; + digits = 4; + } else { + out << "U"; + digits = 8; + } + + // Write digits into the escape sequence + for (; digits > 0; --digits) + out << hexDigits[(codePoint >> (4 * (digits - 1))) & 0xF]; +} + +bool WriteAliasName(ostream_wrapper& out, const std::string& str) { + int codePoint; + for (std::string::const_iterator i = str.begin(); + GetNextCodePointAndAdvance(codePoint, i, str.end());) { + if (!IsAnchorChar(codePoint)) { + return false; + } + + WriteCodePoint(out, codePoint); + } + return true; +} +} + +StringFormat::value ComputeStringFormat(const std::string& str, + EMITTER_MANIP strFormat, + FlowType::value flowType, + bool escapeNonAscii) { + switch (strFormat) { + case Auto: + if (IsValidPlainScalar(str, flowType, escapeNonAscii)) { + return StringFormat::Plain; + } + return StringFormat::DoubleQuoted; + case SingleQuoted: + if (IsValidSingleQuotedScalar(str, escapeNonAscii)) { + return StringFormat::SingleQuoted; + } + return StringFormat::DoubleQuoted; + case DoubleQuoted: + return StringFormat::DoubleQuoted; + case Literal: + if (IsValidLiteralScalar(str, flowType, escapeNonAscii)) { + return StringFormat::Literal; + } + return StringFormat::DoubleQuoted; + default: + break; + } + + return StringFormat::DoubleQuoted; +} + +bool WriteSingleQuotedString(ostream_wrapper& out, const std::string& str) { + out << "'"; + int codePoint; + for (std::string::const_iterator i = str.begin(); + GetNextCodePointAndAdvance(codePoint, i, str.end());) { + if (codePoint == '\n') { + return false; // We can't handle a new line and the attendant indentation + // yet + } + + if (codePoint == '\'') { + out << "''"; + } else { + WriteCodePoint(out, codePoint); + } + } + out << "'"; + return true; +} + +bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, + bool escapeNonAscii) { + out << "\""; + int codePoint; + for (std::string::const_iterator i = str.begin(); + GetNextCodePointAndAdvance(codePoint, i, str.end());) { + switch (codePoint) { + case '\"': + out << "\\\""; + break; + case '\\': + out << "\\\\"; + break; + case '\n': + out << "\\n"; + break; + case '\t': + out << "\\t"; + break; + case '\r': + out << "\\r"; + break; + case '\b': + out << "\\b"; + break; + default: + if (codePoint < 0x20 || + (codePoint >= 0x80 && + codePoint <= 0xA0)) { // Control characters and non-breaking space + WriteDoubleQuoteEscapeSequence(out, codePoint); + } else if (codePoint == 0xFEFF) { // Byte order marks (ZWNS) should be + // escaped (YAML 1.2, sec. 5.2) + WriteDoubleQuoteEscapeSequence(out, codePoint); + } else if (escapeNonAscii && codePoint > 0x7E) { + WriteDoubleQuoteEscapeSequence(out, codePoint); + } else { + WriteCodePoint(out, codePoint); + } + } + } + out << "\""; + return true; +} + +bool WriteLiteralString(ostream_wrapper& out, const std::string& str, + std::size_t indent) { + out << "|\n"; + out << IndentTo(indent); + int codePoint; + for (std::string::const_iterator i = str.begin(); + GetNextCodePointAndAdvance(codePoint, i, str.end());) { + if (codePoint == '\n') { + out << "\n" << IndentTo(indent); + } else { + WriteCodePoint(out, codePoint); + } + } + return true; +} + +bool WriteChar(ostream_wrapper& out, char ch) { + if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')) { + out << ch; + } else if (ch == '\"') { + out << "\"\\\"\""; + } else if (ch == '\t') { + out << "\"\\t\""; + } else if (ch == '\n') { + out << "\"\\n\""; + } else if (ch == '\b') { + out << "\"\\b\""; + } else if (ch == '\\') { + out << "\"\\\\\""; + } else if ((0x20 <= ch && ch <= 0x7e) || ch == ' ') { + out << "\"" << ch << "\""; + } else { + out << "\""; + WriteDoubleQuoteEscapeSequence(out, ch); + out << "\""; + } + return true; +} + +bool WriteComment(ostream_wrapper& out, const std::string& str, + std::size_t postCommentIndent) { + const std::size_t curIndent = out.col(); + out << "#" << Indentation(postCommentIndent); + out.set_comment(); + int codePoint; + for (std::string::const_iterator i = str.begin(); + GetNextCodePointAndAdvance(codePoint, i, str.end());) { + if (codePoint == '\n') { + out << "\n" << IndentTo(curIndent) << "#" + << Indentation(postCommentIndent); + out.set_comment(); + } else { + WriteCodePoint(out, codePoint); + } + } + return true; +} + +bool WriteAlias(ostream_wrapper& out, const std::string& str) { + out << "*"; + return WriteAliasName(out, str); +} + +bool WriteAnchor(ostream_wrapper& out, const std::string& str) { + out << "&"; + return WriteAliasName(out, str); +} + +bool WriteTag(ostream_wrapper& out, const std::string& str, bool verbatim) { + out << (verbatim ? "!<" : "!"); + StringCharSource buffer(str.c_str(), str.size()); + const RegEx& reValid = verbatim ? Exp::URI() : Exp::Tag(); + while (buffer) { + int n = reValid.Match(buffer); + if (n <= 0) { + return false; + } + + while (--n >= 0) { + out << buffer[0]; + ++buffer; + } + } + if (verbatim) { + out << ">"; + } + return true; +} + +bool WriteTagWithPrefix(ostream_wrapper& out, const std::string& prefix, + const std::string& tag) { + out << "!"; + StringCharSource prefixBuffer(prefix.c_str(), prefix.size()); + while (prefixBuffer) { + int n = Exp::URI().Match(prefixBuffer); + if (n <= 0) { + return false; + } + + while (--n >= 0) { + out << prefixBuffer[0]; + ++prefixBuffer; + } + } + + out << "!"; + StringCharSource tagBuffer(tag.c_str(), tag.size()); + while (tagBuffer) { + int n = Exp::Tag().Match(tagBuffer); + if (n <= 0) { + return false; + } + + while (--n >= 0) { + out << tagBuffer[0]; + ++tagBuffer; + } + } + return true; +} + +bool WriteBinary(ostream_wrapper& out, const Binary& binary) { + WriteDoubleQuotedString(out, EncodeBase64(binary.data(), binary.size()), + false); + return true; +} +} +} diff --git a/src/yamlcpp/emitterutils.h b/src/yamlcpp/emitterutils.h --- a/src/yamlcpp/emitterutils.h +++ b/src/yamlcpp/emitterutils.h @@ -1,32 +1,50 @@ #ifndef EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - -#include "yaml-cpp/ostream.h" #include -namespace YODA_YAML -{ - class Binary; - - namespace Utils - { - bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii); - bool WriteSingleQuotedString(ostream& out, const std::string& str); - bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii); - bool WriteLiteralString(ostream& out, const std::string& str, int indent); - bool WriteChar(ostream& out, char ch); - bool WriteComment(ostream& out, const std::string& str, int postCommentIndent); - bool WriteAlias(ostream& out, const std::string& str); - bool WriteAnchor(ostream& out, const std::string& str); - bool WriteTag(ostream& out, const std::string& str, bool verbatim); - bool WriteTagWithPrefix(ostream& out, const std::string& prefix, const std::string& tag); - bool WriteBinary(ostream& out, const Binary& binary); - } +#include "emitterstate.h" +#include "yaml-cpp/emittermanip.h" +#include "yaml-cpp/ostream_wrapper.h" + +namespace YODA_YAML { +class ostream_wrapper; +} // namespace YODA_YAML + +namespace YODA_YAML { +class Binary; + +struct StringFormat { + enum value { Plain, SingleQuoted, DoubleQuoted, Literal }; +}; + +namespace Utils { +StringFormat::value ComputeStringFormat(const std::string& str, + EMITTER_MANIP strFormat, + FlowType::value flowType, + bool escapeNonAscii); + +bool WriteSingleQuotedString(ostream_wrapper& out, const std::string& str); +bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, + bool escapeNonAscii); +bool WriteLiteralString(ostream_wrapper& out, const std::string& str, + std::size_t indent); +bool WriteChar(ostream_wrapper& out, char ch); +bool WriteComment(ostream_wrapper& out, const std::string& str, + std::size_t postCommentIndent); +bool WriteAlias(ostream_wrapper& out, const std::string& str); +bool WriteAnchor(ostream_wrapper& out, const std::string& str); +bool WriteTag(ostream_wrapper& out, const std::string& str, bool verbatim); +bool WriteTagWithPrefix(ostream_wrapper& out, const std::string& prefix, + const std::string& tag); +bool WriteBinary(ostream_wrapper& out, const Binary& binary); +} } -#endif // EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/exceptions.cpp b/src/yamlcpp/exceptions.cpp new file mode 100644 --- /dev/null +++ b/src/yamlcpp/exceptions.cpp @@ -0,0 +1,31 @@ +#include "yaml-cpp/exceptions.h" + +// This is here for compatibility with older versions of Visual Studio +// which don't support noexcept +#ifdef _MSC_VER + #define YAML_CPP_NOEXCEPT _NOEXCEPT +#else + #define YAML_CPP_NOEXCEPT noexcept +#endif + +namespace YODA_YAML { + +// These destructors are defined out-of-line so the vtable is only emitted once. +Exception::~Exception() YAML_CPP_NOEXCEPT {} +ParserException::~ParserException() YAML_CPP_NOEXCEPT {} +RepresentationException::~RepresentationException() YAML_CPP_NOEXCEPT {} +InvalidScalar::~InvalidScalar() YAML_CPP_NOEXCEPT {} +KeyNotFound::~KeyNotFound() YAML_CPP_NOEXCEPT {} +InvalidNode::~InvalidNode() YAML_CPP_NOEXCEPT {} +BadConversion::~BadConversion() YAML_CPP_NOEXCEPT {} +BadDereference::~BadDereference() YAML_CPP_NOEXCEPT {} +BadSubscript::~BadSubscript() YAML_CPP_NOEXCEPT {} +BadPushback::~BadPushback() YAML_CPP_NOEXCEPT {} +BadInsert::~BadInsert() YAML_CPP_NOEXCEPT {} +EmitterException::~EmitterException() YAML_CPP_NOEXCEPT {} +BadFile::~BadFile() YAML_CPP_NOEXCEPT {} +} + +#undef YAML_CPP_NOEXCEPT + + diff --git a/src/yamlcpp/exp.cpp b/src/yamlcpp/exp.cpp --- a/src/yamlcpp/exp.cpp +++ b/src/yamlcpp/exp.cpp @@ -1,113 +1,136 @@ -#include "exp.h" -#include "yaml-cpp/exceptions.h" #include -namespace YODA_YAML -{ - namespace Exp - { - unsigned ParseHex(const std::string& str, const Mark& mark) - { - unsigned value = 0; - for(std::size_t i=0;i(ch)); - } + value = (value << 4) + digit; + } - // Escape - // . Translates the next 'codeLength' characters into a hex number and returns the result. - // . Throws if it's not actually hex. - std::string Escape(Stream& in, int codeLength) - { - // grab string - std::string str; - for(int i=0;i(ch)); } - // legal unicode? - if((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) { - std::stringstream msg; - msg << ErrorMsg::INVALID_UNICODE << value; - throw ParserException(in.mark(), msg.str()); - } +// Escape +// . Translates the next 'codeLength' characters into a hex number and returns +// the result. +// . Throws if it's not actually hex. +std::string Escape(Stream& in, int codeLength) { + // grab string + std::string str; + for (int i = 0; i < codeLength; i++) + str += in.get(); - // now break it up into chars - if(value <= 0x7F) - return Str(value); - else if(value <= 0x7FF) - return Str(0xC0 + (value >> 6)) + Str(0x80 + (value & 0x3F)); - else if(value <= 0xFFFF) - return Str(0xE0 + (value >> 12)) + Str(0x80 + ((value >> 6) & 0x3F)) + Str(0x80 + (value & 0x3F)); - else - return Str(0xF0 + (value >> 18)) + Str(0x80 + ((value >> 12) & 0x3F)) + - Str(0x80 + ((value >> 6) & 0x3F)) + Str(0x80 + (value & 0x3F)); - } + // get the value + unsigned value = ParseHex(str, in.mark()); - // Escape - // . Escapes the sequence starting 'in' (it must begin with a '\' or single quote) - // and returns the result. - // . Throws if it's an unknown escape character. - std::string Escape(Stream& in) - { - // eat slash - char escape = in.get(); + // legal unicode? + if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) { + std::stringstream msg; + msg << ErrorMsg::INVALID_UNICODE << value; + throw ParserException(in.mark(), msg.str()); + } - // switch on escape character - char ch = in.get(); + // now break it up into chars + if (value <= 0x7F) + return Str(value); + else if (value <= 0x7FF) + return Str(0xC0 + (value >> 6)) + Str(0x80 + (value & 0x3F)); + else if (value <= 0xFFFF) + return Str(0xE0 + (value >> 12)) + Str(0x80 + ((value >> 6) & 0x3F)) + + Str(0x80 + (value & 0x3F)); + else + return Str(0xF0 + (value >> 18)) + Str(0x80 + ((value >> 12) & 0x3F)) + + Str(0x80 + ((value >> 6) & 0x3F)) + Str(0x80 + (value & 0x3F)); +} - // first do single quote, since it's easier - if(escape == '\'' && ch == '\'') - return "\'"; +// Escape +// . Escapes the sequence starting 'in' (it must begin with a '\' or single +// quote) +// and returns the result. +// . Throws if it's an unknown escape character. +std::string Escape(Stream& in) { + // eat slash + char escape = in.get(); - // now do the slash (we're not gonna check if it's a slash - you better pass one!) - switch(ch) { - case '0': return std::string(1, '\x00'); - case 'a': return "\x07"; - case 'b': return "\x08"; - case 't': - case '\t': return "\x09"; - case 'n': return "\x0A"; - case 'v': return "\x0B"; - case 'f': return "\x0C"; - case 'r': return "\x0D"; - case 'e': return "\x1B"; - case ' ': return "\x20"; - case '\"': return "\""; - case '\'': return "\'"; - case '\\': return "\\"; - case '/': return "/"; - case 'N': return "\x85"; - case '_': return "\xA0"; - case 'L': return "\xE2\x80\xA8"; // LS (#x2028) - case 'P': return "\xE2\x80\xA9"; // PS (#x2029) - case 'x': return Escape(in, 2); - case 'u': return Escape(in, 4); - case 'U': return Escape(in, 8); - } + // switch on escape character + char ch = in.get(); - std::stringstream msg; - throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch); - } - } + // first do single quote, since it's easier + if (escape == '\'' && ch == '\'') + return "\'"; + + // now do the slash (we're not gonna check if it's a slash - you better pass + // one!) + switch (ch) { + case '0': + return std::string(1, '\x00'); + case 'a': + return "\x07"; + case 'b': + return "\x08"; + case 't': + case '\t': + return "\x09"; + case 'n': + return "\x0A"; + case 'v': + return "\x0B"; + case 'f': + return "\x0C"; + case 'r': + return "\x0D"; + case 'e': + return "\x1B"; + case ' ': + return "\x20"; + case '\"': + return "\""; + case '\'': + return "\'"; + case '\\': + return "\\"; + case '/': + return "/"; + case 'N': + return "\x85"; + case '_': + return "\xA0"; + case 'L': + return "\xE2\x80\xA8"; // LS (#x2028) + case 'P': + return "\xE2\x80\xA9"; // PS (#x2029) + case 'x': + return Escape(in, 2); + case 'u': + return Escape(in, 4); + case 'U': + return Escape(in, 8); + } + + std::stringstream msg; + throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch); } +} +} diff --git a/src/yamlcpp/exp.h b/src/yamlcpp/exp.h --- a/src/yamlcpp/exp.h +++ b/src/yamlcpp/exp.h @@ -1,196 +1,222 @@ #ifndef EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include +#include -#include "regex.h" -#include -#include +#include "regex_yaml.h" #include "stream.h" -namespace YODA_YAML -{ - //////////////////////////////////////////////////////////////////////////////// - // Here we store a bunch of expressions for matching different parts of the file. +namespace YODA_YAML { +//////////////////////////////////////////////////////////////////////////////// +// Here we store a bunch of expressions for matching different parts of the +// file. - namespace Exp - { - // misc - inline const RegEx& Space() { - static const RegEx e = RegEx(' '); - return e; - } - inline const RegEx& Tab() { - static const RegEx e = RegEx('\t'); - return e; - } - inline const RegEx& Blank() { - static const RegEx e = Space() || Tab(); - return e; - } - inline const RegEx& Break() { - static const RegEx e = RegEx('\n') || RegEx("\r\n"); - return e; - } - inline const RegEx& BlankOrBreak() { - static const RegEx e = Blank() || Break(); - return e; - } - inline const RegEx& Digit() { - static const RegEx e = RegEx('0', '9'); - return e; - } - inline const RegEx& Alpha() { - static const RegEx e = RegEx('a', 'z') || RegEx('A', 'Z'); - return e; - } - inline const RegEx& AlphaNumeric() { - static const RegEx e = Alpha() || Digit(); - return e; - } - inline const RegEx& Word() { - static const RegEx e = AlphaNumeric() || RegEx('-'); - return e; - } - inline const RegEx& Hex() { - static const RegEx e = Digit() || RegEx('A', 'F') || RegEx('a', 'f'); - return e; - } - // Valid Unicode code points that are not part of c-printable (YAML 1.2, sec. 5.1) - inline const RegEx& NotPrintable() { - static const RegEx e = RegEx(0) || - RegEx("\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x7F", REGEX_OR) || - RegEx(0x0E, 0x1F) || - (RegEx('\xC2') + (RegEx('\x80', '\x84') || RegEx('\x86', '\x9F'))); - return e; - } - inline const RegEx& Utf8_ByteOrderMark() { - static const RegEx e = RegEx("\xEF\xBB\xBF"); - return e; - } - - // actual tags - - inline const RegEx& DocStart() { - static const RegEx e = RegEx("---") + (BlankOrBreak() || RegEx()); - return e; - } - inline const RegEx& DocEnd() { - static const RegEx e = RegEx("...") + (BlankOrBreak() || RegEx()); - return e; - } - inline const RegEx& DocIndicator() { - static const RegEx e = DocStart() || DocEnd(); - return e; - } - inline const RegEx& BlockEntry() { - static const RegEx e = RegEx('-') + (BlankOrBreak() || RegEx()); - return e; - } - inline const RegEx& Key() { - static const RegEx e = RegEx('?'); - return e; - } - inline const RegEx& KeyInFlow() { - static const RegEx e = RegEx('?') + BlankOrBreak(); - return e; - } - inline const RegEx& Value() { - static const RegEx e = RegEx(':') + (BlankOrBreak() || RegEx()); - return e; - } - inline const RegEx& ValueInFlow() { - static const RegEx e = RegEx(':') + (BlankOrBreak() || RegEx(",}", REGEX_OR)); - return e; - } - inline const RegEx& ValueInJSONFlow() { - static const RegEx e = RegEx(':'); - return e; - } - inline const RegEx Comment() { - static const RegEx e = RegEx('#'); - return e; - } - inline const RegEx& Anchor() { - static const RegEx e = !(RegEx("[]{},", REGEX_OR) || BlankOrBreak()); - return e; - } - inline const RegEx& AnchorEnd() { - static const RegEx e = RegEx("?:,]}%@`", REGEX_OR) || BlankOrBreak(); - return e; - } - inline const RegEx& URI() { - static const RegEx e = Word() || RegEx("#;/?:@&=+$,_.!~*'()[]", REGEX_OR) || (RegEx('%') + Hex() + Hex()); - return e; - } - inline const RegEx& Tag() { - static const RegEx e = Word() || RegEx("#;/?:@&=+$_.~*'", REGEX_OR) || (RegEx('%') + Hex() + Hex()); - return e; - } - - // Plain scalar rules: - // . Cannot start with a blank. - // . Can never start with any of , [ ] { } # & * ! | > \' \" % @ ` - // . In the block context - ? : must be not be followed with a space. - // . In the flow context ? is illegal and : and - must not be followed with a space. - inline const RegEx& PlainScalar() { - static const RegEx e = !(BlankOrBreak() || RegEx(",[]{}#&*!|>\'\"%@`", REGEX_OR) || (RegEx("-?:", REGEX_OR) + (BlankOrBreak() || RegEx()))); - return e; - } - inline const RegEx& PlainScalarInFlow() { - static const RegEx e = !(BlankOrBreak() || RegEx("?,[]{}#&*!|>\'\"%@`", REGEX_OR) || (RegEx("-:", REGEX_OR) + Blank())); - return e; - } - inline const RegEx& EndScalar() { - static const RegEx e = RegEx(':') + (BlankOrBreak() || RegEx()); - return e; - } - inline const RegEx& EndScalarInFlow() { - static const RegEx e = (RegEx(':') + (BlankOrBreak() || RegEx() || RegEx(",]}", REGEX_OR))) || RegEx(",?[]{}", REGEX_OR); - return e; - } - - inline const RegEx& EscSingleQuote() { - static const RegEx e = RegEx("\'\'"); - return e; - } - inline const RegEx& EscBreak() { - static const RegEx e = RegEx('\\') + Break(); - return e; - } - - inline const RegEx& ChompIndicator() { - static const RegEx e = RegEx("+-", REGEX_OR); - return e; - } - inline const RegEx& Chomp() { - static const RegEx e = (ChompIndicator() + Digit()) || (Digit() + ChompIndicator()) || ChompIndicator() || Digit(); - return e; - } - - // and some functions - std::string Escape(Stream& in); - } - - namespace Keys - { - const char Directive = '%'; - const char FlowSeqStart = '['; - const char FlowSeqEnd = ']'; - const char FlowMapStart = '{'; - const char FlowMapEnd = '}'; - const char FlowEntry = ','; - const char Alias = '*'; - const char Anchor = '&'; - const char Tag = '!'; - const char LiteralScalar = '|'; - const char FoldedScalar = '>'; - const char VerbatimTagStart = '<'; - const char VerbatimTagEnd = '>'; - } +namespace Exp { +// misc +inline const RegEx& Empty() { + static const RegEx e; + return e; +} +inline const RegEx& Space() { + static const RegEx e = RegEx(' '); + return e; +} +inline const RegEx& Tab() { + static const RegEx e = RegEx('\t'); + return e; +} +inline const RegEx& Blank() { + static const RegEx e = Space() || Tab(); + return e; +} +inline const RegEx& Break() { + static const RegEx e = RegEx('\n') || RegEx("\r\n"); + return e; +} +inline const RegEx& BlankOrBreak() { + static const RegEx e = Blank() || Break(); + return e; +} +inline const RegEx& Digit() { + static const RegEx e = RegEx('0', '9'); + return e; +} +inline const RegEx& Alpha() { + static const RegEx e = RegEx('a', 'z') || RegEx('A', 'Z'); + return e; +} +inline const RegEx& AlphaNumeric() { + static const RegEx e = Alpha() || Digit(); + return e; +} +inline const RegEx& Word() { + static const RegEx e = AlphaNumeric() || RegEx('-'); + return e; +} +inline const RegEx& Hex() { + static const RegEx e = Digit() || RegEx('A', 'F') || RegEx('a', 'f'); + return e; +} +// Valid Unicode code points that are not part of c-printable (YAML 1.2, sec. +// 5.1) +inline const RegEx& NotPrintable() { + static const RegEx e = + RegEx(0) || + RegEx("\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x7F", REGEX_OR) || + RegEx(0x0E, 0x1F) || + (RegEx('\xC2') + (RegEx('\x80', '\x84') || RegEx('\x86', '\x9F'))); + return e; +} +inline const RegEx& Utf8_ByteOrderMark() { + static const RegEx e = RegEx("\xEF\xBB\xBF"); + return e; } -#endif // EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +// actual tags + +inline const RegEx& DocStart() { + static const RegEx e = RegEx("---") + (BlankOrBreak() || RegEx()); + return e; +} +inline const RegEx& DocEnd() { + static const RegEx e = RegEx("...") + (BlankOrBreak() || RegEx()); + return e; +} +inline const RegEx& DocIndicator() { + static const RegEx e = DocStart() || DocEnd(); + return e; +} +inline const RegEx& BlockEntry() { + static const RegEx e = RegEx('-') + (BlankOrBreak() || RegEx()); + return e; +} +inline const RegEx& Key() { + static const RegEx e = RegEx('?') + BlankOrBreak(); + return e; +} +inline const RegEx& KeyInFlow() { + static const RegEx e = RegEx('?') + BlankOrBreak(); + return e; +} +inline const RegEx& Value() { + static const RegEx e = RegEx(':') + (BlankOrBreak() || RegEx()); + return e; +} +inline const RegEx& ValueInFlow() { + static const RegEx e = RegEx(':') + (BlankOrBreak() || RegEx(",}", REGEX_OR)); + return e; +} +inline const RegEx& ValueInJSONFlow() { + static const RegEx e = RegEx(':'); + return e; +} +inline const RegEx Comment() { + static const RegEx e = RegEx('#'); + return e; +} +inline const RegEx& Anchor() { + static const RegEx e = !(RegEx("[]{},", REGEX_OR) || BlankOrBreak()); + return e; +} +inline const RegEx& AnchorEnd() { + static const RegEx e = RegEx("?:,]}%@`", REGEX_OR) || BlankOrBreak(); + return e; +} +inline const RegEx& URI() { + static const RegEx e = Word() || RegEx("#;/?:@&=+$,_.!~*'()[]", REGEX_OR) || + (RegEx('%') + Hex() + Hex()); + return e; +} +inline const RegEx& Tag() { + static const RegEx e = Word() || RegEx("#;/?:@&=+$_.~*'()", REGEX_OR) || + (RegEx('%') + Hex() + Hex()); + return e; +} + +// Plain scalar rules: +// . Cannot start with a blank. +// . Can never start with any of , [ ] { } # & * ! | > \' \" % @ ` +// . In the block context - ? : must be not be followed with a space. +// . In the flow context ? is illegal and : and - must not be followed with a +// space. +inline const RegEx& PlainScalar() { + static const RegEx e = + !(BlankOrBreak() || RegEx(",[]{}#&*!|>\'\"%@`", REGEX_OR) || + (RegEx("-?:", REGEX_OR) + (BlankOrBreak() || RegEx()))); + return e; +} +inline const RegEx& PlainScalarInFlow() { + static const RegEx e = + !(BlankOrBreak() || RegEx("?,[]{}#&*!|>\'\"%@`", REGEX_OR) || + (RegEx("-:", REGEX_OR) + Blank())); + return e; +} +inline const RegEx& EndScalar() { + static const RegEx e = RegEx(':') + (BlankOrBreak() || RegEx()); + return e; +} +inline const RegEx& EndScalarInFlow() { + static const RegEx e = + (RegEx(':') + (BlankOrBreak() || RegEx() || RegEx(",]}", REGEX_OR))) || + RegEx(",?[]{}", REGEX_OR); + return e; +} + +inline const RegEx& ScanScalarEndInFlow() { + static const RegEx e = (EndScalarInFlow() || (BlankOrBreak() + Comment())); + return e; +} + +inline const RegEx& ScanScalarEnd() { + static const RegEx e = EndScalar() || (BlankOrBreak() + Comment()); + return e; +} +inline const RegEx& EscSingleQuote() { + static const RegEx e = RegEx("\'\'"); + return e; +} +inline const RegEx& EscBreak() { + static const RegEx e = RegEx('\\') + Break(); + return e; +} + +inline const RegEx& ChompIndicator() { + static const RegEx e = RegEx("+-", REGEX_OR); + return e; +} +inline const RegEx& Chomp() { + static const RegEx e = (ChompIndicator() + Digit()) || + (Digit() + ChompIndicator()) || ChompIndicator() || + Digit(); + return e; +} + +// and some functions +std::string Escape(Stream& in); +} // namespace Exp + +namespace Keys { +const char Directive = '%'; +const char FlowSeqStart = '['; +const char FlowSeqEnd = ']'; +const char FlowMapStart = '{'; +const char FlowMapEnd = '}'; +const char FlowEntry = ','; +const char Alias = '*'; +const char Anchor = '&'; +const char Tag = '!'; +const char LiteralScalar = '|'; +const char FoldedScalar = '>'; +const char VerbatimTagStart = '<'; +const char VerbatimTagEnd = '>'; +} // namespace Keys +} // namespace YODA_YAML + +#endif // EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/indentation.h b/src/yamlcpp/indentation.h --- a/src/yamlcpp/indentation.h +++ b/src/yamlcpp/indentation.h @@ -1,38 +1,41 @@ #ifndef INDENTATION_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define INDENTATION_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include +#include -#include "yaml-cpp/ostream.h" -#include +#include "yaml-cpp/ostream_wrapper.h" -namespace YODA_YAML -{ - struct Indentation { - Indentation(unsigned n_): n(n_) {} - unsigned n; - }; - - inline ostream& operator << (ostream& out, const Indentation& indent) { - for(unsigned i=0;i pData): m_pData(std::move(pData)) - { - } - - Iterator::Iterator(const Iterator& rhs): m_pData(new IterPriv(*rhs.m_pData)) - { - } - - Iterator& Iterator::operator = (const Iterator& rhs) - { - if(this == &rhs) - return *this; - - m_pData.reset(new IterPriv(*rhs.m_pData)); - return *this; - } - - Iterator::~Iterator() - { - } - - Iterator& Iterator::operator ++ () - { - if(m_pData->type == IterPriv::IT_SEQ) - ++m_pData->seqIter; - else if(m_pData->type == IterPriv::IT_MAP) - ++m_pData->mapIter; - - return *this; - } - - Iterator Iterator::operator ++ (int) - { - Iterator temp = *this; - - if(m_pData->type == IterPriv::IT_SEQ) - ++m_pData->seqIter; - else if(m_pData->type == IterPriv::IT_MAP) - ++m_pData->mapIter; - - return temp; - } - - const Node& Iterator::operator * () const - { - if(m_pData->type == IterPriv::IT_SEQ) - return **m_pData->seqIter; - - throw BadDereference(); - } - - const Node *Iterator::operator -> () const - { - if(m_pData->type == IterPriv::IT_SEQ) - return *m_pData->seqIter; - - throw BadDereference(); - } - - const Node& Iterator::first() const - { - if(m_pData->type == IterPriv::IT_MAP) - return *m_pData->mapIter->first; - - throw BadDereference(); - } - - const Node& Iterator::second() const - { - if(m_pData->type == IterPriv::IT_MAP) - return *m_pData->mapIter->second; - - throw BadDereference(); - } - - bool operator == (const Iterator& it, const Iterator& jt) - { - if(it.m_pData->type != jt.m_pData->type) - return false; - - if(it.m_pData->type == IterPriv::IT_SEQ) - return it.m_pData->seqIter == jt.m_pData->seqIter; - else if(it.m_pData->type == IterPriv::IT_MAP) - return it.m_pData->mapIter == jt.m_pData->mapIter; - - return true; - } - - bool operator != (const Iterator& it, const Iterator& jt) - { - return !(it == jt); - } -} diff --git a/src/yamlcpp/memory.cpp b/src/yamlcpp/memory.cpp new file mode 100644 --- /dev/null +++ b/src/yamlcpp/memory.cpp @@ -0,0 +1,26 @@ +#include "yaml-cpp/node/detail/memory.h" +#include "yaml-cpp/node/detail/node.h" // IWYU pragma: keep +#include "yaml-cpp/node/ptr.h" + +namespace YODA_YAML { +namespace detail { + +void memory_holder::merge(memory_holder& rhs) { + if (m_pMemory == rhs.m_pMemory) + return; + + m_pMemory->merge(*rhs.m_pMemory); + rhs.m_pMemory = m_pMemory; +} + +node& memory::create_node() { + shared_node pNode(new node); + m_nodes.insert(pNode); + return *pNode; +} + +void memory::merge(const memory& rhs) { + m_nodes.insert(rhs.m_nodes.begin(), rhs.m_nodes.end()); +} +} +} diff --git a/src/yamlcpp/node.cpp b/src/yamlcpp/node.cpp --- a/src/yamlcpp/node.cpp +++ b/src/yamlcpp/node.cpp @@ -1,269 +1,12 @@ -#include "yaml-cpp/node.h" -#include "yaml-cpp/aliasmanager.h" -#include "yaml-cpp/emitfromevents.h" -#include "yaml-cpp/emitter.h" -#include "yaml-cpp/eventhandler.h" -#include "iterpriv.h" +#include "yaml-cpp/node/node.h" #include "nodebuilder.h" -#include "nodeownership.h" -#include "scanner.h" -#include "tag.h" -#include "token.h" -#include -#include +#include "nodeevents.h" -namespace YODA_YAML -{ - bool ltnode::operator()(const Node *pNode1, const Node *pNode2) const { - return *pNode1 < *pNode2; - } - - Node::Node(): m_pOwnership(new NodeOwnership), m_type(NodeType::Null) - { - } - - Node::Node(NodeOwnership& owner): m_pOwnership(new NodeOwnership(&owner)), m_type(NodeType::Null) - { - } - - Node::~Node() - { - Clear(); - } - - void Node::Clear() - { - m_pOwnership.reset(new NodeOwnership); - m_type = NodeType::Null; - m_tag.clear(); - m_scalarData.clear(); - m_seqData.clear(); - m_mapData.clear(); - } - - bool Node::IsAliased() const - { - return m_pOwnership->IsAliased(*this); - } - - Node& Node::CreateNode() - { - return m_pOwnership->Create(); - } - - std::unique_ptr Node::Clone() const - { - std::unique_ptr pNode(new Node); - NodeBuilder nodeBuilder(*pNode); - EmitEvents(nodeBuilder); - return pNode; - } - - void Node::EmitEvents(EventHandler& eventHandler) const - { - eventHandler.OnDocumentStart(m_mark); - AliasManager am; - EmitEvents(am, eventHandler); - eventHandler.OnDocumentEnd(); - } - - void Node::EmitEvents(AliasManager& am, EventHandler& eventHandler) const - { - anchor_t anchor = NullAnchor; - if(IsAliased()) { - anchor = am.LookupAnchor(*this); - if(anchor) { - eventHandler.OnAlias(m_mark, anchor); - return; - } - - am.RegisterReference(*this); - anchor = am.LookupAnchor(*this); - } - - switch(m_type) { - case NodeType::Null: - eventHandler.OnNull(m_mark, anchor); - break; - case NodeType::Scalar: - eventHandler.OnScalar(m_mark, m_tag, anchor, m_scalarData); - break; - case NodeType::Sequence: - eventHandler.OnSequenceStart(m_mark, m_tag, anchor); - for(std::size_t i=0;iEmitEvents(am, eventHandler); - eventHandler.OnSequenceEnd(); - break; - case NodeType::Map: - eventHandler.OnMapStart(m_mark, m_tag, anchor); - for(node_map::const_iterator it=m_mapData.begin();it!=m_mapData.end();++it) { - it->first->EmitEvents(am, eventHandler); - it->second->EmitEvents(am, eventHandler); - } - eventHandler.OnMapEnd(); - break; - } - } - - void Node::Init(NodeType::value type, const Mark& mark, const std::string& tag) - { - Clear(); - m_mark = mark; - m_type = type; - m_tag = tag; - } - - void Node::MarkAsAliased() - { - m_pOwnership->MarkAsAliased(*this); - } - - void Node::SetScalarData(const std::string& data) - { - assert(m_type == NodeType::Scalar); // TODO: throw? - m_scalarData = data; - } - - void Node::Append(Node& node) - { - assert(m_type == NodeType::Sequence); // TODO: throw? - m_seqData.push_back(&node); - } - - void Node::Insert(Node& key, Node& value) - { - assert(m_type == NodeType::Map); // TODO: throw? - m_mapData[&key] = &value; - } - - // begin - // Returns an iterator to the beginning of this (sequence or map). - Iterator Node::begin() const - { - switch(m_type) { - case NodeType::Null: - case NodeType::Scalar: - return Iterator(); - case NodeType::Sequence: - return Iterator(std::unique_ptr(new IterPriv(m_seqData.begin()))); - case NodeType::Map: - return Iterator(std::unique_ptr(new IterPriv(m_mapData.begin()))); - } - - assert(false); - return Iterator(); - } - - // end - // . Returns an iterator to the end of this (sequence or map). - Iterator Node::end() const - { - switch(m_type) { - case NodeType::Null: - case NodeType::Scalar: - return Iterator(); - case NodeType::Sequence: - return Iterator(std::unique_ptr(new IterPriv(m_seqData.end()))); - case NodeType::Map: - return Iterator(std::unique_ptr(new IterPriv(m_mapData.end()))); - } - - assert(false); - return Iterator(); - } - - // size - // . Returns the size of a sequence or map node - // . Otherwise, returns zero. - std::size_t Node::size() const - { - switch(m_type) { - case NodeType::Null: - case NodeType::Scalar: - return 0; - case NodeType::Sequence: - return m_seqData.size(); - case NodeType::Map: - return m_mapData.size(); - } - - assert(false); - return 0; - } - - const Node *Node::FindAtIndex(std::size_t i) const - { - if(m_type == NodeType::Sequence) - return m_seqData[i]; - return 0; - } - - bool Node::GetScalar(std::string& s) const - { - switch(m_type) { - case NodeType::Null: - s = "~"; - return true; - case NodeType::Scalar: - s = m_scalarData; - return true; - case NodeType::Sequence: - case NodeType::Map: - return false; - } - - assert(false); - return false; - } - - Emitter& operator << (Emitter& out, const Node& node) - { - EmitFromEvents emitFromEvents(out); - node.EmitEvents(emitFromEvents); - return out; - } - - int Node::Compare(const Node& rhs) const - { - if(m_type != rhs.m_type) - return rhs.m_type - m_type; - - switch(m_type) { - case NodeType::Null: - return 0; - case NodeType::Scalar: - return m_scalarData.compare(rhs.m_scalarData); - case NodeType::Sequence: - if(m_seqData.size() < rhs.m_seqData.size()) - return 1; - else if(m_seqData.size() > rhs.m_seqData.size()) - return -1; - for(std::size_t i=0;iCompare(*rhs.m_seqData[i])) - return cmp; - return 0; - case NodeType::Map: - if(m_mapData.size() < rhs.m_mapData.size()) - return 1; - else if(m_mapData.size() > rhs.m_mapData.size()) - return -1; - node_map::const_iterator it = m_mapData.begin(); - node_map::const_iterator jt = rhs.m_mapData.begin(); - for(;it!=m_mapData.end() && jt!=rhs.m_mapData.end();it++, jt++) { - if(int cmp = it->first->Compare(*jt->first)) - return cmp; - if(int cmp = it->second->Compare(*jt->second)) - return cmp; - } - return 0; - } - - assert(false); - return 0; - } - - bool operator < (const Node& n1, const Node& n2) - { - return n1.Compare(n2) < 0; - } +namespace YODA_YAML { +Node Clone(const Node& node) { + NodeEvents events(node); + NodeBuilder builder; + events.Emit(builder); + return builder.Root(); } +} diff --git a/src/yamlcpp/node_data.cpp b/src/yamlcpp/node_data.cpp new file mode 100644 --- /dev/null +++ b/src/yamlcpp/node_data.cpp @@ -0,0 +1,300 @@ +#include +#include +#include + +#include "yaml-cpp/exceptions.h" +#include "yaml-cpp/node/detail/memory.h" +#include "yaml-cpp/node/detail/node.h" // IWYU pragma: keep +#include "yaml-cpp/node/detail/node_data.h" +#include "yaml-cpp/node/detail/node_iterator.h" +#include "yaml-cpp/node/ptr.h" +#include "yaml-cpp/node/type.h" + +namespace YODA_YAML { +namespace detail { + +std::string node_data::empty_scalar; + +node_data::node_data() + : m_isDefined(false), + m_mark(Mark::null_mark()), + m_type(NodeType::Null), + m_style(EmitterStyle::Default), + m_seqSize(0) {} + +void node_data::mark_defined() { + if (m_type == NodeType::Undefined) + m_type = NodeType::Null; + m_isDefined = true; +} + +void node_data::set_mark(const Mark& mark) { m_mark = mark; } + +void node_data::set_type(NodeType::value type) { + if (type == NodeType::Undefined) { + m_type = type; + m_isDefined = false; + return; + } + + m_isDefined = true; + if (type == m_type) + return; + + m_type = type; + + switch (m_type) { + case NodeType::Null: + break; + case NodeType::Scalar: + m_scalar.clear(); + break; + case NodeType::Sequence: + reset_sequence(); + break; + case NodeType::Map: + reset_map(); + break; + case NodeType::Undefined: + assert(false); + break; + } +} + +void node_data::set_tag(const std::string& tag) { m_tag = tag; } + +void node_data::set_style(EmitterStyle::value style) { m_style = style; } + +void node_data::set_null() { + m_isDefined = true; + m_type = NodeType::Null; +} + +void node_data::set_scalar(const std::string& scalar) { + m_isDefined = true; + m_type = NodeType::Scalar; + m_scalar = scalar; +} + +// size/iterator +std::size_t node_data::size() const { + if (!m_isDefined) + return 0; + + switch (m_type) { + case NodeType::Sequence: + compute_seq_size(); + return m_seqSize; + case NodeType::Map: + compute_map_size(); + return m_map.size() - m_undefinedPairs.size(); + default: + return 0; + } + return 0; +} + +void node_data::compute_seq_size() const { + while (m_seqSize < m_sequence.size() && m_sequence[m_seqSize]->is_defined()) + m_seqSize++; +} + +void node_data::compute_map_size() const { + kv_pairs::iterator it = m_undefinedPairs.begin(); + while (it != m_undefinedPairs.end()) { + kv_pairs::iterator jt = std::next(it); + if (it->first->is_defined() && it->second->is_defined()) + m_undefinedPairs.erase(it); + it = jt; + } +} + +const_node_iterator node_data::begin() const { + if (!m_isDefined) + return const_node_iterator(); + + switch (m_type) { + case NodeType::Sequence: + return const_node_iterator(m_sequence.begin()); + case NodeType::Map: + return const_node_iterator(m_map.begin(), m_map.end()); + default: + return const_node_iterator(); + } +} + +node_iterator node_data::begin() { + if (!m_isDefined) + return node_iterator(); + + switch (m_type) { + case NodeType::Sequence: + return node_iterator(m_sequence.begin()); + case NodeType::Map: + return node_iterator(m_map.begin(), m_map.end()); + default: + return node_iterator(); + } +} + +const_node_iterator node_data::end() const { + if (!m_isDefined) + return const_node_iterator(); + + switch (m_type) { + case NodeType::Sequence: + return const_node_iterator(m_sequence.end()); + case NodeType::Map: + return const_node_iterator(m_map.end(), m_map.end()); + default: + return const_node_iterator(); + } +} + +node_iterator node_data::end() { + if (!m_isDefined) + return node_iterator(); + + switch (m_type) { + case NodeType::Sequence: + return node_iterator(m_sequence.end()); + case NodeType::Map: + return node_iterator(m_map.end(), m_map.end()); + default: + return node_iterator(); + } +} + +// sequence +void node_data::push_back(node& node, shared_memory_holder /* pMemory */) { + if (m_type == NodeType::Undefined || m_type == NodeType::Null) { + m_type = NodeType::Sequence; + reset_sequence(); + } + + if (m_type != NodeType::Sequence) + throw BadPushback(); + + m_sequence.push_back(&node); +} + +void node_data::insert(node& key, node& value, shared_memory_holder pMemory) { + switch (m_type) { + case NodeType::Map: + break; + case NodeType::Undefined: + case NodeType::Null: + case NodeType::Sequence: + convert_to_map(pMemory); + break; + case NodeType::Scalar: + throw BadSubscript(); + } + + insert_map_pair(key, value); +} + +// indexing +node* node_data::get(node& key, shared_memory_holder /* pMemory */) const { + if (m_type != NodeType::Map) { + return NULL; + } + + for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->is(key)) + return it->second; + } + + return NULL; +} + +node& node_data::get(node& key, shared_memory_holder pMemory) { + switch (m_type) { + case NodeType::Map: + break; + case NodeType::Undefined: + case NodeType::Null: + case NodeType::Sequence: + convert_to_map(pMemory); + break; + case NodeType::Scalar: + throw BadSubscript(); + } + + for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->is(key)) + return *it->second; + } + + node& value = pMemory->create_node(); + insert_map_pair(key, value); + return value; +} + +bool node_data::remove(node& key, shared_memory_holder /* pMemory */) { + if (m_type != NodeType::Map) + return false; + + for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->is(key)) { + m_map.erase(it); + return true; + } + } + + return false; +} + +void node_data::reset_sequence() { + m_sequence.clear(); + m_seqSize = 0; +} + +void node_data::reset_map() { + m_map.clear(); + m_undefinedPairs.clear(); +} + +void node_data::insert_map_pair(node& key, node& value) { + m_map.emplace_back(&key, &value); + + if (!key.is_defined() || !value.is_defined()) + m_undefinedPairs.emplace_back(&key, &value); +} + +void node_data::convert_to_map(shared_memory_holder pMemory) { + switch (m_type) { + case NodeType::Undefined: + case NodeType::Null: + reset_map(); + m_type = NodeType::Map; + break; + case NodeType::Sequence: + convert_sequence_to_map(pMemory); + break; + case NodeType::Map: + break; + case NodeType::Scalar: + assert(false); + break; + } +} + +void node_data::convert_sequence_to_map(shared_memory_holder pMemory) { + assert(m_type == NodeType::Sequence); + + reset_map(); + for (std::size_t i = 0; i < m_sequence.size(); i++) { + std::stringstream stream; + stream << i; + + node& key = pMemory->create_node(); + key.set_scalar(stream.str()); + insert_map_pair(key, *m_sequence[i]); + } + + reset_sequence(); + m_type = NodeType::Map; +} +} +} diff --git a/src/yamlcpp/nodebuilder.cpp b/src/yamlcpp/nodebuilder.cpp --- a/src/yamlcpp/nodebuilder.cpp +++ b/src/yamlcpp/nodebuilder.cpp @@ -1,145 +1,130 @@ -#include "nodebuilder.h" -#include "yaml-cpp/mark.h" -#include "yaml-cpp/node.h" +#include #include -namespace YODA_YAML -{ - NodeBuilder::NodeBuilder(Node& root): m_root(root), m_initializedRoot(false), m_finished(false) - { - m_root.Clear(); - m_anchors.push_back(0); // since the anchors start at 1 - } - - NodeBuilder::~NodeBuilder() - { - } +#include "nodebuilder.h" +#include "yaml-cpp/node/detail/node.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/type.h" - void NodeBuilder::OnDocumentStart(const Mark&) - { - } +namespace YODA_YAML { +struct Mark; - void NodeBuilder::OnDocumentEnd() - { - assert(m_finished); - } +NodeBuilder::NodeBuilder() + : m_pMemory(new detail::memory_holder), m_pRoot(0), m_mapDepth(0) { + m_anchors.push_back(0); // since the anchors start at 1 +} - void NodeBuilder::OnNull(const Mark& mark, anchor_t anchor) - { - Node& node = Push(anchor); - node.Init(NodeType::Null, mark, ""); - Pop(); - } +NodeBuilder::~NodeBuilder() {} - void NodeBuilder::OnAlias(const Mark& /*mark*/, anchor_t anchor) - { - Node& node = *m_anchors[anchor]; - Insert(node); - node.MarkAsAliased(); - } +Node NodeBuilder::Root() { + if (!m_pRoot) + return Node(); - void NodeBuilder::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value) - { - Node& node = Push(anchor); - node.Init(NodeType::Scalar, mark, tag); - node.SetScalarData(value); - Pop(); - } + return Node(*m_pRoot, m_pMemory); +} - void NodeBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor) - { - Node& node = Push(anchor); - node.Init(NodeType::Sequence, mark, tag); - } +void NodeBuilder::OnDocumentStart(const Mark&) {} - void NodeBuilder::OnSequenceEnd() - { - Pop(); - } +void NodeBuilder::OnDocumentEnd() {} - void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor) - { - Node& node = Push(anchor); - node.Init(NodeType::Map, mark, tag); - m_didPushKey.push(false); - } +void NodeBuilder::OnNull(const Mark& mark, anchor_t anchor) { + detail::node& node = Push(mark, anchor); + node.set_null(); + Pop(); +} - void NodeBuilder::OnMapEnd() - { - m_didPushKey.pop(); - Pop(); - } - - Node& NodeBuilder::Push(anchor_t anchor) - { - Node& node = Push(); - RegisterAnchor(anchor, node); - return node; - } - - Node& NodeBuilder::Push() - { - if(!m_initializedRoot) { - m_initializedRoot = true; - return m_root; - } - - Node& node = m_root.CreateNode(); - m_stack.push(&node); - return node; - } - - Node& NodeBuilder::Top() - { - return m_stack.empty() ? m_root : *m_stack.top(); - } - - void NodeBuilder::Pop() - { - assert(!m_finished); - if(m_stack.empty()) { - m_finished = true; - return; - } - - Node& node = *m_stack.top(); - m_stack.pop(); - Insert(node); - } - - void NodeBuilder::Insert(Node& node) - { - Node& curTop = Top(); - switch(curTop.Type()) { - case NodeType::Null: - case NodeType::Scalar: - assert(false); - break; - case NodeType::Sequence: - curTop.Append(node); - break; - case NodeType::Map: - assert(!m_didPushKey.empty()); - if(m_didPushKey.top()) { - assert(!m_pendingKeys.empty()); +void NodeBuilder::OnAlias(const Mark& /* mark */, anchor_t anchor) { + detail::node& node = *m_anchors[anchor]; + Push(node); + Pop(); +} - Node& key = *m_pendingKeys.top(); - m_pendingKeys.pop(); - curTop.Insert(key, node); - m_didPushKey.top() = false; - } else { - m_pendingKeys.push(&node); - m_didPushKey.top() = true; - } - break; - } - } +void NodeBuilder::OnScalar(const Mark& mark, const std::string& tag, + anchor_t anchor, const std::string& value) { + detail::node& node = Push(mark, anchor); + node.set_scalar(value); + node.set_tag(tag); + Pop(); +} - void NodeBuilder::RegisterAnchor(anchor_t anchor, Node& node) - { - if(anchor) { - assert(anchor == m_anchors.size()); - m_anchors.push_back(&node); - } - } +void NodeBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style) { + detail::node& node = Push(mark, anchor); + node.set_tag(tag); + node.set_type(NodeType::Sequence); + node.set_style(style); } + +void NodeBuilder::OnSequenceEnd() { Pop(); } + +void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style) { + detail::node& node = Push(mark, anchor); + node.set_type(NodeType::Map); + node.set_tag(tag); + node.set_style(style); + m_mapDepth++; +} + +void NodeBuilder::OnMapEnd() { + assert(m_mapDepth > 0); + m_mapDepth--; + Pop(); +} + +detail::node& NodeBuilder::Push(const Mark& mark, anchor_t anchor) { + detail::node& node = m_pMemory->create_node(); + node.set_mark(mark); + RegisterAnchor(anchor, node); + Push(node); + return node; +} + +void NodeBuilder::Push(detail::node& node) { + const bool needsKey = + (!m_stack.empty() && m_stack.back()->type() == NodeType::Map && + m_keys.size() < m_mapDepth); + + m_stack.push_back(&node); + if (needsKey) + m_keys.push_back(PushedKey(&node, false)); +} + +void NodeBuilder::Pop() { + assert(!m_stack.empty()); + if (m_stack.size() == 1) { + m_pRoot = m_stack[0]; + m_stack.pop_back(); + return; + } + + detail::node& node = *m_stack.back(); + m_stack.pop_back(); + + detail::node& collection = *m_stack.back(); + + if (collection.type() == NodeType::Sequence) { + collection.push_back(node, m_pMemory); + } else if (collection.type() == NodeType::Map) { + assert(!m_keys.empty()); + PushedKey& key = m_keys.back(); + if (key.second) { + collection.insert(*key.first, node, m_pMemory); + m_keys.pop_back(); + } else { + key.second = true; + } + } else { + assert(false); + m_stack.clear(); + } +} + +void NodeBuilder::RegisterAnchor(anchor_t anchor, detail::node& node) { + if (anchor) { + assert(anchor == m_anchors.size()); + m_anchors.push_back(&node); + } +} +} diff --git a/src/yamlcpp/nodebuilder.h b/src/yamlcpp/nodebuilder.h --- a/src/yamlcpp/nodebuilder.h +++ b/src/yamlcpp/nodebuilder.h @@ -1,61 +1,70 @@ -#ifndef NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#ifndef NODE_NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif -#include "yaml-cpp/eventhandler.h" -#include -#include -#include #include -namespace YODA_YAML -{ - class Node; - - class NodeBuilder: public EventHandler - { - public: - explicit NodeBuilder(Node& root); - virtual ~NodeBuilder(); +#include "yaml-cpp/anchor.h" +#include "yaml-cpp/emitterstyle.h" +#include "yaml-cpp/eventhandler.h" +#include "yaml-cpp/node/ptr.h" - virtual void OnDocumentStart(const Mark& mark); - virtual void OnDocumentEnd(); - - virtual void OnNull(const Mark& mark, anchor_t anchor); - virtual void OnAlias(const Mark& mark, anchor_t anchor); - virtual void OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value); - - virtual void OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor); - virtual void OnSequenceEnd(); - - virtual void OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor); - virtual void OnMapEnd(); - - private: - Node& Push(anchor_t anchor); - Node& Push(); - Node& Top(); - void Pop(); +namespace YODA_YAML { +namespace detail { +class node; +} // namespace detail +struct Mark; +} // namespace YODA_YAML - void Insert(Node& node); - void RegisterAnchor(anchor_t anchor, Node& node); - - private: - Node& m_root; - bool m_initializedRoot; - bool m_finished; - - std::stack m_stack; - std::stack m_pendingKeys; - std::stack m_didPushKey; +namespace YODA_YAML { +class Node; - typedef std::vector Anchors; - Anchors m_anchors; - }; +class NodeBuilder : public EventHandler { + public: + NodeBuilder(); + virtual ~NodeBuilder(); + + Node Root(); + + virtual void OnDocumentStart(const Mark& mark); + virtual void OnDocumentEnd(); + + virtual void OnNull(const Mark& mark, anchor_t anchor); + virtual void OnAlias(const Mark& mark, anchor_t anchor); + virtual void OnScalar(const Mark& mark, const std::string& tag, + anchor_t anchor, const std::string& value); + + virtual void OnSequenceStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style); + virtual void OnSequenceEnd(); + + virtual void OnMapStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style); + virtual void OnMapEnd(); + + private: + detail::node& Push(const Mark& mark, anchor_t anchor); + void Push(detail::node& node); + void Pop(); + void RegisterAnchor(anchor_t anchor, detail::node& node); + + private: + detail::shared_memory_holder m_pMemory; + detail::node* m_pRoot; + + typedef std::vector Nodes; + Nodes m_stack; + Nodes m_anchors; + + typedef std::pair PushedKey; + std::vector m_keys; + std::size_t m_mapDepth; +}; } -#endif // NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // NODE_NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/nodeevents.cpp b/src/yamlcpp/nodeevents.cpp new file mode 100644 --- /dev/null +++ b/src/yamlcpp/nodeevents.cpp @@ -0,0 +1,101 @@ +#include "nodeevents.h" +#include "yaml-cpp/eventhandler.h" +#include "yaml-cpp/mark.h" +#include "yaml-cpp/node/detail/node.h" +#include "yaml-cpp/node/detail/node_iterator.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/type.h" + +namespace YODA_YAML { +void NodeEvents::AliasManager::RegisterReference(const detail::node& node) { + m_anchorByIdentity.insert(std::make_pair(node.ref(), _CreateNewAnchor())); +} + +anchor_t NodeEvents::AliasManager::LookupAnchor( + const detail::node& node) const { + AnchorByIdentity::const_iterator it = m_anchorByIdentity.find(node.ref()); + if (it == m_anchorByIdentity.end()) + return 0; + return it->second; +} + +NodeEvents::NodeEvents(const Node& node) + : m_pMemory(node.m_pMemory), m_root(node.m_pNode) { + if (m_root) + Setup(*m_root); +} + +void NodeEvents::Setup(const detail::node& node) { + int& refCount = m_refCount[node.ref()]; + refCount++; + if (refCount > 1) + return; + + if (node.type() == NodeType::Sequence) { + for (detail::const_node_iterator it = node.begin(); it != node.end(); ++it) + Setup(**it); + } else if (node.type() == NodeType::Map) { + for (detail::const_node_iterator it = node.begin(); it != node.end(); + ++it) { + Setup(*it->first); + Setup(*it->second); + } + } +} + +void NodeEvents::Emit(EventHandler& handler) { + AliasManager am; + + handler.OnDocumentStart(Mark()); + if (m_root) + Emit(*m_root, handler, am); + handler.OnDocumentEnd(); +} + +void NodeEvents::Emit(const detail::node& node, EventHandler& handler, + AliasManager& am) const { + anchor_t anchor = NullAnchor; + if (IsAliased(node)) { + anchor = am.LookupAnchor(node); + if (anchor) { + handler.OnAlias(Mark(), anchor); + return; + } + + am.RegisterReference(node); + anchor = am.LookupAnchor(node); + } + + switch (node.type()) { + case NodeType::Undefined: + break; + case NodeType::Null: + handler.OnNull(Mark(), anchor); + break; + case NodeType::Scalar: + handler.OnScalar(Mark(), node.tag(), anchor, node.scalar()); + break; + case NodeType::Sequence: + handler.OnSequenceStart(Mark(), node.tag(), anchor, node.style()); + for (detail::const_node_iterator it = node.begin(); it != node.end(); + ++it) + Emit(**it, handler, am); + handler.OnSequenceEnd(); + break; + case NodeType::Map: + handler.OnMapStart(Mark(), node.tag(), anchor, node.style()); + for (detail::const_node_iterator it = node.begin(); it != node.end(); + ++it) { + Emit(*it->first, handler, am); + Emit(*it->second, handler, am); + } + handler.OnMapEnd(); + break; + } +} + +bool NodeEvents::IsAliased(const detail::node& node) const { + RefCount::const_iterator it = m_refCount.find(node.ref()); + return it != m_refCount.end() && it->second > 1; +} +} diff --git a/src/yamlcpp/nodeevents.h b/src/yamlcpp/nodeevents.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/nodeevents.h @@ -0,0 +1,64 @@ +#ifndef NODE_NODEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_NODEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include + +#include "yaml-cpp/anchor.h" +#include "yaml-cpp/node/ptr.h" + +namespace YODA_YAML { +namespace detail { +class node; +} // namespace detail +} // namespace YODA_YAML + +namespace YODA_YAML { +class EventHandler; +class Node; + +class NodeEvents { + public: + explicit NodeEvents(const Node& node); + + void Emit(EventHandler& handler); + + private: + class AliasManager { + public: + AliasManager() : m_curAnchor(0) {} + + void RegisterReference(const detail::node& node); + anchor_t LookupAnchor(const detail::node& node) const; + + private: + anchor_t _CreateNewAnchor() { return ++m_curAnchor; } + + private: + typedef std::map AnchorByIdentity; + AnchorByIdentity m_anchorByIdentity; + + anchor_t m_curAnchor; + }; + + void Setup(const detail::node& node); + void Emit(const detail::node& node, EventHandler& handler, + AliasManager& am) const; + bool IsAliased(const detail::node& node) const; + + private: + detail::shared_memory_holder m_pMemory; + detail::node* m_root; + + typedef std::map RefCount; + RefCount m_refCount; +}; +} + +#endif // NODE_NODEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/nodeownership.cpp b/src/yamlcpp/nodeownership.cpp deleted file mode 100644 --- a/src/yamlcpp/nodeownership.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "nodeownership.h" -#include "yaml-cpp/node.h" - -namespace YODA_YAML -{ - NodeOwnership::NodeOwnership(NodeOwnership *pOwner): m_pOwner(pOwner) - { - if(!m_pOwner) - m_pOwner = this; - } - - NodeOwnership::~NodeOwnership() - { - } - - Node& NodeOwnership::_Create() - { - m_nodes.push_back(std::unique_ptr(new Node)); - return m_nodes.back(); - } - - void NodeOwnership::_MarkAsAliased(const Node& node) - { - m_aliasedNodes.insert(&node); - } - - bool NodeOwnership::_IsAliased(const Node& node) const - { - return m_aliasedNodes.count(&node) > 0; - } -} diff --git a/src/yamlcpp/null.cpp b/src/yamlcpp/null.cpp --- a/src/yamlcpp/null.cpp +++ b/src/yamlcpp/null.cpp @@ -1,12 +1,10 @@ #include "yaml-cpp/null.h" -#include "yaml-cpp/node.h" -namespace YODA_YAML -{ - _Null Null; +namespace YODA_YAML { +_Null Null; - bool IsNull(const Node& node) - { - return node.Read(Null); - } +bool IsNullString(const std::string& str) { + return str.empty() || str == "~" || str == "null" || str == "Null" || + str == "NULL"; } +} diff --git a/src/yamlcpp/ostream.cpp b/src/yamlcpp/ostream.cpp deleted file mode 100644 --- a/src/yamlcpp/ostream.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "yaml-cpp/ostream.h" -#include - -namespace YODA_YAML -{ - ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0) - { - reserve(1024); - } - - ostream::~ostream() - { - delete [] m_buffer; - } - - void ostream::reserve(unsigned size) - { - if(size <= m_size) - return; - - char *newBuffer = new char[size]; - std::memset(newBuffer, 0, size * sizeof(char)); - std::memcpy(newBuffer, m_buffer, m_size * sizeof(char)); - delete [] m_buffer; - m_buffer = newBuffer; - m_size = size; - } - - void ostream::put(char ch) - { - if(m_pos >= m_size - 1) // an extra space for the NULL terminator - reserve(m_size * 2); - - m_buffer[m_pos] = ch; - m_pos++; - - if(ch == '\n') { - m_row++; - m_col = 0; - } else - m_col++; - } - - ostream& operator << (ostream& out, const char *str) - { - std::size_t length = std::strlen(str); - for(std::size_t i=0;i +#include +#include + +namespace YODA_YAML { +ostream_wrapper::ostream_wrapper() + : m_buffer(1, '\0'), + m_pStream(0), + m_pos(0), + m_row(0), + m_col(0), + m_comment(false) {} + +ostream_wrapper::ostream_wrapper(std::ostream& stream) + : m_pStream(&stream), m_pos(0), m_row(0), m_col(0), m_comment(false) {} + +ostream_wrapper::~ostream_wrapper() {} + +void ostream_wrapper::write(const std::string& str) { + if (m_pStream) { + m_pStream->write(str.c_str(), str.size()); + } else { + m_buffer.resize(std::max(m_buffer.size(), m_pos + str.size() + 1)); + std::copy(str.begin(), str.end(), m_buffer.begin() + m_pos); + } + + for (std::size_t i = 0; i < str.size(); i++) { + update_pos(str[i]); + } +} + +void ostream_wrapper::write(const char* str, std::size_t size) { + if (m_pStream) { + m_pStream->write(str, size); + } else { + m_buffer.resize(std::max(m_buffer.size(), m_pos + size + 1)); + std::copy(str, str + size, m_buffer.begin() + m_pos); + } + + for (std::size_t i = 0; i < size; i++) { + update_pos(str[i]); + } +} + +void ostream_wrapper::update_pos(char ch) { + m_pos++; + m_col++; + + if (ch == '\n') { + m_row++; + m_col = 0; + m_comment = false; + } +} +} diff --git a/src/yamlcpp/parse.cpp b/src/yamlcpp/parse.cpp new file mode 100644 --- /dev/null +++ b/src/yamlcpp/parse.cpp @@ -0,0 +1,72 @@ +#include "yaml-cpp/node/parse.h" + +#include +#include + +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/parser.h" +#include "nodebuilder.h" + +namespace YODA_YAML { +Node Load(const std::string& input) { + std::stringstream stream(input); + return Load(stream); +} + +Node Load(const char* input) { + std::stringstream stream(input); + return Load(stream); +} + +Node Load(std::istream& input) { + Parser parser(input); + NodeBuilder builder; + if (!parser.HandleNextDocument(builder)) { + return Node(); + } + + return builder.Root(); +} + +Node LoadFile(const std::string& filename) { + std::ifstream fin(filename.c_str()); + if (!fin) { + throw BadFile(); + } + return Load(fin); +} + +std::vector LoadAll(const std::string& input) { + std::stringstream stream(input); + return LoadAll(stream); +} + +std::vector LoadAll(const char* input) { + std::stringstream stream(input); + return LoadAll(stream); +} + +std::vector LoadAll(std::istream& input) { + std::vector docs; + + Parser parser(input); + while (1) { + NodeBuilder builder; + if (!parser.HandleNextDocument(builder)) { + break; + } + docs.push_back(builder.Root()); + } + + return docs; +} + +std::vector LoadAllFromFile(const std::string& filename) { + std::ifstream fin(filename.c_str()); + if (!fin) { + throw BadFile(); + } + return LoadAll(fin); +} +} // namespace YODA_YAML diff --git a/src/yamlcpp/parser.cpp b/src/yamlcpp/parser.cpp --- a/src/yamlcpp/parser.cpp +++ b/src/yamlcpp/parser.cpp @@ -1,152 +1,129 @@ +#include +#include + +#include "directives.h" // IWYU pragma: keep +#include "scanner.h" // IWYU pragma: keep +#include "singledocparser.h" +#include "token.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep #include "yaml-cpp/parser.h" -#include "yaml-cpp/eventhandler.h" -#include "yaml-cpp/exceptions.h" -#include "yaml-cpp/node.h" -#include "directives.h" -#include "nodebuilder.h" -#include "scanner.h" -#include "singledocparser.h" -#include "tag.h" -#include "token.h" -#include -#include -namespace YODA_YAML -{ - Parser::Parser() - { - } - - Parser::Parser(std::istream& in) - { - Load(in); - } +namespace YODA_YAML { +class EventHandler; - Parser::~Parser() - { - } +Parser::Parser() {} - Parser::operator bool() const - { - return m_pScanner.get() && !m_pScanner->empty(); - } +Parser::Parser(std::istream& in) { Load(in); } - void Parser::Load(std::istream& in) - { - m_pScanner.reset(new Scanner(in)); - m_pDirectives.reset(new Directives); - } +Parser::~Parser() {} - // HandleNextDocument - // . Handles the next document - // . Throws a ParserException on error. - // . Returns false if there are no more documents - bool Parser::HandleNextDocument(EventHandler& eventHandler) - { - if(!m_pScanner.get()) - return false; +Parser::operator bool() const { + return m_pScanner.get() && !m_pScanner->empty(); +} - ParseDirectives(); - if(m_pScanner->empty()) - return false; - - SingleDocParser sdp(*m_pScanner, *m_pDirectives); - sdp.HandleDocument(eventHandler); - return true; - } +void Parser::Load(std::istream& in) { + m_pScanner.reset(new Scanner(in)); + m_pDirectives.reset(new Directives); +} - // GetNextDocument - // . Reads the next document in the queue (of tokens). - // . Throws a ParserException on error. - bool Parser::GetNextDocument(Node& document) - { - NodeBuilder builder(document); - return HandleNextDocument(builder); - } +bool Parser::HandleNextDocument(EventHandler& eventHandler) { + if (!m_pScanner.get()) + return false; - // ParseDirectives - // . Reads any directives that are next in the queue. - void Parser::ParseDirectives() - { - bool readDirective = false; + ParseDirectives(); + if (m_pScanner->empty()) { + return false; + } - while(1) { - if(m_pScanner->empty()) - break; + SingleDocParser sdp(*m_pScanner, *m_pDirectives); + sdp.HandleDocument(eventHandler); + return true; +} - Token& token = m_pScanner->peek(); - if(token.type != Token::DIRECTIVE) - break; +void Parser::ParseDirectives() { + bool readDirective = false; - // we keep the directives from the last document if none are specified; - // but if any directives are specific, then we reset them - if(!readDirective) - m_pDirectives.reset(new Directives); + while (1) { + if (m_pScanner->empty()) { + break; + } - readDirective = true; - HandleDirective(token); - m_pScanner->pop(); - } - } + Token& token = m_pScanner->peek(); + if (token.type != Token::DIRECTIVE) { + break; + } - void Parser::HandleDirective(const Token& token) - { - if(token.value == "YAML") - HandleYamlDirective(token); - else if(token.value == "TAG") - HandleTagDirective(token); - } + // we keep the directives from the last document if none are specified; + // but if any directives are specific, then we reset them + if (!readDirective) { + m_pDirectives.reset(new Directives); + } - // HandleYamlDirective - // . Should be of the form 'major.minor' (like a version number) - void Parser::HandleYamlDirective(const Token& token) - { - if(token.params.size() != 1) - throw ParserException(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS); - - if(!m_pDirectives->version.isDefault) - throw ParserException(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE); + readDirective = true; + HandleDirective(token); + m_pScanner->pop(); + } +} - std::stringstream str(token.params[0]); - str >> m_pDirectives->version.major; - str.get(); - str >> m_pDirectives->version.minor; - if(!str || str.peek() != EOF) - throw ParserException(token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]); +void Parser::HandleDirective(const Token& token) { + if (token.value == "YAML") { + HandleYamlDirective(token); + } else if (token.value == "TAG") { + HandleTagDirective(token); + } +} - if(m_pDirectives->version.major > 1) - throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION); +void Parser::HandleYamlDirective(const Token& token) { + if (token.params.size() != 1) { + throw ParserException(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS); + } - m_pDirectives->version.isDefault = false; - // TODO: warning on major == 1, minor > 2? - } + if (!m_pDirectives->version.isDefault) { + throw ParserException(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE); + } - // HandleTagDirective - // . Should be of the form 'handle prefix', where 'handle' is converted to 'prefix' in the file. - void Parser::HandleTagDirective(const Token& token) - { - if(token.params.size() != 2) - throw ParserException(token.mark, ErrorMsg::TAG_DIRECTIVE_ARGS); + std::stringstream str(token.params[0]); + str >> m_pDirectives->version.major; + str.get(); + str >> m_pDirectives->version.minor; + if (!str || str.peek() != EOF) { + throw ParserException( + token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]); + } - const std::string& handle = token.params[0]; - const std::string& prefix = token.params[1]; - if(m_pDirectives->tags.find(handle) != m_pDirectives->tags.end()) - throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE); - - m_pDirectives->tags[handle] = prefix; - } + if (m_pDirectives->version.major > 1) { + throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION); + } - void Parser::PrintTokens(std::ostream& out) - { - if(!m_pScanner.get()) - return; - - while(1) { - if(m_pScanner->empty()) - break; + m_pDirectives->version.isDefault = false; + // TODO: warning on major == 1, minor > 2? +} - out << m_pScanner->peek() << "\n"; - m_pScanner->pop(); - } - } +void Parser::HandleTagDirective(const Token& token) { + if (token.params.size() != 2) + throw ParserException(token.mark, ErrorMsg::TAG_DIRECTIVE_ARGS); + + const std::string& handle = token.params[0]; + const std::string& prefix = token.params[1]; + if (m_pDirectives->tags.find(handle) != m_pDirectives->tags.end()) { + throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE); + } + + m_pDirectives->tags[handle] = prefix; } + +void Parser::PrintTokens(std::ostream& out) { + if (!m_pScanner.get()) { + return; + } + + while (1) { + if (m_pScanner->empty()) { + break; + } + + out << m_pScanner->peek() << "\n"; + m_pScanner->pop(); + } +} +} diff --git a/src/yamlcpp/ptr_vector.h b/src/yamlcpp/ptr_vector.h --- a/src/yamlcpp/ptr_vector.h +++ b/src/yamlcpp/ptr_vector.h @@ -1,47 +1,43 @@ #ifndef PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif -#include "yaml-cpp/noncopyable.h" #include #include #include #include +#include "yaml-cpp/noncopyable.h" + namespace YODA_YAML { - template - class ptr_vector: private YODA_YAML::noncopyable - { - public: - ptr_vector() {} - ~ptr_vector() { clear(); } +// TODO: This class is no longer needed +template +class ptr_vector : private YODA_YAML::noncopyable { + public: + ptr_vector() {} - void clear() { - for(unsigned i=0;i t) { - m_data.push_back(NULL); - m_data.back() = t.release(); - } - T& operator[](std::size_t i) { return *m_data[i]; } - const T& operator[](std::size_t i) const { return *m_data[i]; } + void push_back(std::unique_ptr&& t) { m_data.push_back(std::move(t)); } + T& operator[](std::size_t i) { return *m_data[i]; } + const T& operator[](std::size_t i) const { return *m_data[i]; } - T& back() { return *m_data.back(); } - const T& back() const { return *m_data.back(); } + T& back() { return *(m_data.back().get()); } - private: - std::vector m_data; - }; + const T& back() const { return *(m_data.back().get()); } + + private: + std::vector> m_data; +}; } -#endif // PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/regex.cpp b/src/yamlcpp/regex.cpp deleted file mode 100644 --- a/src/yamlcpp/regex.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "regex.h" - -namespace YODA_YAML -{ - // constructors - RegEx::RegEx(): m_op(REGEX_EMPTY) - { - } - - RegEx::RegEx(REGEX_OP op): m_op(op) - { - } - - RegEx::RegEx(char ch): m_op(REGEX_MATCH), m_a(ch) - { - } - - RegEx::RegEx(char a, char z): m_op(REGEX_RANGE), m_a(a), m_z(z) - { - } - - RegEx::RegEx(const std::string& str, REGEX_OP op): m_op(op) - { - for(std::size_t i=0;i= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include + +#include "yaml-cpp/dll.h" + +namespace YODA_YAML { +class Stream; + +enum REGEX_OP { + REGEX_EMPTY, + REGEX_MATCH, + REGEX_RANGE, + REGEX_OR, + REGEX_AND, + REGEX_NOT, + REGEX_SEQ +}; + +// simplified regular expressions +// . Only straightforward matches (no repeated characters) +// . Only matches from start of string +class YAML_CPP_API RegEx { + public: + RegEx(); + RegEx(char ch); + RegEx(char a, char z); + RegEx(const std::string& str, REGEX_OP op = REGEX_SEQ); + ~RegEx() {} + + friend YAML_CPP_API RegEx operator!(const RegEx& ex); + friend YAML_CPP_API RegEx operator||(const RegEx& ex1, const RegEx& ex2); + friend YAML_CPP_API RegEx operator&&(const RegEx& ex1, const RegEx& ex2); + friend YAML_CPP_API RegEx operator+(const RegEx& ex1, const RegEx& ex2); + + bool Matches(char ch) const; + bool Matches(const std::string& str) const; + bool Matches(const Stream& in) const; + template + bool Matches(const Source& source) const; + + int Match(const std::string& str) const; + int Match(const Stream& in) const; + template + int Match(const Source& source) const; + + private: + RegEx(REGEX_OP op); + + template + bool IsValidSource(const Source& source) const; + template + int MatchUnchecked(const Source& source) const; + + template + int MatchOpEmpty(const Source& source) const; + template + int MatchOpMatch(const Source& source) const; + template + int MatchOpRange(const Source& source) const; + template + int MatchOpOr(const Source& source) const; + template + int MatchOpAnd(const Source& source) const; + template + int MatchOpNot(const Source& source) const; + template + int MatchOpSeq(const Source& source) const; + + private: + REGEX_OP m_op; + char m_a, m_z; + std::vector m_params; +}; +} + +#include "regeximpl.h" + +#endif // REGEX_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/regeximpl.h b/src/yamlcpp/regeximpl.h --- a/src/yamlcpp/regeximpl.h +++ b/src/yamlcpp/regeximpl.h @@ -1,186 +1,186 @@ #ifndef REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include "stream.h" #include "stringsource.h" #include "streamcharsource.h" -namespace YODA_YAML -{ - // query matches - inline bool RegEx::Matches(char ch) const { - std::string str; - str += ch; - return Matches(str); - } - - inline bool RegEx::Matches(const std::string& str) const { - return Match(str) >= 0; - } - - inline bool RegEx::Matches(const Stream& in) const { - return Match(in) >= 0; - } - - template - inline bool RegEx::Matches(const Source& source) const { - return Match(source) >= 0; - } - - // Match - // . Matches the given string against this regular expression. - // . Returns the number of characters matched. - // . Returns -1 if no characters were matched (the reason for - // not returning zero is that we may have an empty regex - // which is ALWAYS successful at matching zero characters). - // . REMEMBER that we only match from the start of the buffer! - inline int RegEx::Match(const std::string& str) const - { - StringCharSource source(str.c_str(), str.size()); - return Match(source); - } - - inline int RegEx::Match(const Stream& in) const - { - StreamCharSource source(in); - return Match(source); - } - - template - inline bool RegEx::IsValidSource(const Source& source) const - { - return source; - } - - template<> - inline bool RegEx::IsValidSource(const StringCharSource&source) const - { - switch(m_op) { - case REGEX_MATCH: - case REGEX_RANGE: - return source; - default: - return true; - } - } - - template - inline int RegEx::Match(const Source& source) const - { - return IsValidSource(source) ? MatchUnchecked(source) : -1; - } - - template - inline int RegEx::MatchUnchecked(const Source& source) const - { - switch(m_op) { - case REGEX_EMPTY: - return MatchOpEmpty(source); - case REGEX_MATCH: - return MatchOpMatch(source); - case REGEX_RANGE: - return MatchOpRange(source); - case REGEX_OR: - return MatchOpOr(source); - case REGEX_AND: - return MatchOpAnd(source); - case REGEX_NOT: - return MatchOpNot(source); - case REGEX_SEQ: - return MatchOpSeq(source); - } - - return -1; - } - - ////////////////////////////////////////////////////////////////////////////// - // Operators - // Note: the convention MatchOp* is that we can assume IsSourceValid(source). - // So we do all our checks *before* we call these functions - - // EmptyOperator - template - inline int RegEx::MatchOpEmpty(const Source& source) const { - return source[0] == Stream::eof() ? 0 : -1; - } - - template <> - inline int RegEx::MatchOpEmpty(const StringCharSource& source) const { - return !source ? 0 : -1; // the empty regex only is successful on the empty string - } - - // MatchOperator - template - inline int RegEx::MatchOpMatch(const Source& source) const { - if(source[0] != m_a) - return -1; - return 1; - } - - // RangeOperator - template - inline int RegEx::MatchOpRange(const Source& source) const { - if(m_a > source[0] || m_z < source[0]) - return -1; - return 1; - } - - // OrOperator - template - inline int RegEx::MatchOpOr(const Source& source) const { - for(std::size_t i=0;i= 0) - return n; - } - return -1; - } - - // AndOperator - // Note: 'AND' is a little funny, since we may be required to match things - // of different lengths. If we find a match, we return the length of - // the FIRST entry on the list. - template - inline int RegEx::MatchOpAnd(const Source& source) const { - int first = -1; - for(std::size_t i=0;i - inline int RegEx::MatchOpNot(const Source& source) const { - if(m_params.empty()) - return -1; - if(m_params[0].MatchUnchecked(source) >= 0) - return -1; - return 1; - } - - // SeqOperator - template - inline int RegEx::MatchOpSeq(const Source& source) const { - int offset = 0; - for(std::size_t i=0;i= 0; +} + +inline bool RegEx::Matches(const Stream& in) const { return Match(in) >= 0; } + +template +inline bool RegEx::Matches(const Source& source) const { + return Match(source) >= 0; +} + +// Match +// . Matches the given string against this regular expression. +// . Returns the number of characters matched. +// . Returns -1 if no characters were matched (the reason for +// not returning zero is that we may have an empty regex +// which is ALWAYS successful at matching zero characters). +// . REMEMBER that we only match from the start of the buffer! +inline int RegEx::Match(const std::string& str) const { + StringCharSource source(str.c_str(), str.size()); + return Match(source); +} + +inline int RegEx::Match(const Stream& in) const { + StreamCharSource source(in); + return Match(source); +} + +template +inline bool RegEx::IsValidSource(const Source& source) const { + return source; +} + +template <> +inline bool RegEx::IsValidSource( + const StringCharSource& source) const { + switch (m_op) { + case REGEX_MATCH: + case REGEX_RANGE: + return source; + default: + return true; + } +} + +template +inline int RegEx::Match(const Source& source) const { + return IsValidSource(source) ? MatchUnchecked(source) : -1; +} + +template +inline int RegEx::MatchUnchecked(const Source& source) const { + switch (m_op) { + case REGEX_EMPTY: + return MatchOpEmpty(source); + case REGEX_MATCH: + return MatchOpMatch(source); + case REGEX_RANGE: + return MatchOpRange(source); + case REGEX_OR: + return MatchOpOr(source); + case REGEX_AND: + return MatchOpAnd(source); + case REGEX_NOT: + return MatchOpNot(source); + case REGEX_SEQ: + return MatchOpSeq(source); + } + + return -1; +} + +////////////////////////////////////////////////////////////////////////////// +// Operators +// Note: the convention MatchOp* is that we can assume +// IsSourceValid(source). +// So we do all our checks *before* we call these functions + +// EmptyOperator +template +inline int RegEx::MatchOpEmpty(const Source& source) const { + return source[0] == Stream::eof() ? 0 : -1; +} + +template <> +inline int RegEx::MatchOpEmpty( + const StringCharSource& source) const { + return !source + ? 0 + : -1; // the empty regex only is successful on the empty string +} + +// MatchOperator +template +inline int RegEx::MatchOpMatch(const Source& source) const { + if (source[0] != m_a) + return -1; + return 1; +} + +// RangeOperator +template +inline int RegEx::MatchOpRange(const Source& source) const { + if (m_a > source[0] || m_z < source[0]) + return -1; + return 1; +} + +// OrOperator +template +inline int RegEx::MatchOpOr(const Source& source) const { + for (std::size_t i = 0; i < m_params.size(); i++) { + int n = m_params[i].MatchUnchecked(source); + if (n >= 0) + return n; + } + return -1; +} + +// AndOperator +// Note: 'AND' is a little funny, since we may be required to match things +// of different lengths. If we find a match, we return the length of +// the FIRST entry on the list. +template +inline int RegEx::MatchOpAnd(const Source& source) const { + int first = -1; + for (std::size_t i = 0; i < m_params.size(); i++) { + int n = m_params[i].MatchUnchecked(source); + if (n == -1) + return -1; + if (i == 0) + first = n; + } + return first; +} + +// NotOperator +template +inline int RegEx::MatchOpNot(const Source& source) const { + if (m_params.empty()) + return -1; + if (m_params[0].MatchUnchecked(source) >= 0) + return -1; + return 1; +} + +// SeqOperator +template +inline int RegEx::MatchOpSeq(const Source& source) const { + int offset = 0; + for (std::size_t i = 0; i < m_params.size(); i++) { + int n = m_params[i].Match(source + offset); // note Match, not + // MatchUnchecked because we + // need to check validity after + // the offset + if (n == -1) + return -1; + offset += n; + } + + return offset; +} +} + +#endif // REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/scanner.cpp b/src/yamlcpp/scanner.cpp --- a/src/yamlcpp/scanner.cpp +++ b/src/yamlcpp/scanner.cpp @@ -1,386 +1,386 @@ -#include "scanner.h" -#include "token.h" -#include "yaml-cpp/exceptions.h" -#include "exp.h" #include #include -namespace YODA_YAML -{ - Scanner::Scanner(std::istream& in) - : INPUT(in), m_startedStream(false), m_endedStream(false), m_simpleKeyAllowed(false), m_canBeJSONFlow(false) - { - } +#include "exp.h" +#include "scanner.h" +#include "token.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep - Scanner::~Scanner() - { - } +namespace YODA_YAML { +Scanner::Scanner(std::istream& in) + : INPUT(in), + m_startedStream(false), + m_endedStream(false), + m_simpleKeyAllowed(false), + m_canBeJSONFlow(false) {} - // empty - // . Returns true if there are no more tokens to be read - bool Scanner::empty() - { - EnsureTokensInQueue(); - return m_tokens.empty(); - } +Scanner::~Scanner() {} - // pop - // . Simply removes the next token on the queue. - void Scanner::pop() - { - EnsureTokensInQueue(); - if(!m_tokens.empty()) - m_tokens.pop(); - } +bool Scanner::empty() { + EnsureTokensInQueue(); + return m_tokens.empty(); +} - // peek - // . Returns (but does not remove) the next token on the queue. - Token& Scanner::peek() - { - EnsureTokensInQueue(); - assert(!m_tokens.empty()); // should we be asserting here? I mean, we really just be checking - // if it's empty before peeking. +void Scanner::pop() { + EnsureTokensInQueue(); + if (!m_tokens.empty()) + m_tokens.pop(); +} + +Token& Scanner::peek() { + EnsureTokensInQueue(); + assert(!m_tokens.empty()); // should we be asserting here? I mean, we really + // just be checking + // if it's empty before peeking. #if 0 static Token *pLast = 0; if(pLast != &m_tokens.front()) std::cerr << "peek: " << m_tokens.front() << "\n"; pLast = &m_tokens.front(); #endif - return m_tokens.front(); - } + return m_tokens.front(); +} - // EnsureTokensInQueue - // . Scan until there's a valid token at the front of the queue, - // or we're sure the queue is empty. - void Scanner::EnsureTokensInQueue() - { - while(1) { - if(!m_tokens.empty()) { - Token& token = m_tokens.front(); +Mark Scanner::mark() const { return INPUT.mark(); } - // if this guy's valid, then we're done - if(token.status == Token::VALID) - return; +void Scanner::EnsureTokensInQueue() { + while (1) { + if (!m_tokens.empty()) { + Token& token = m_tokens.front(); - // here's where we clean up the impossible tokens - if(token.status == Token::INVALID) { - m_tokens.pop(); - continue; - } + // if this guy's valid, then we're done + if (token.status == Token::VALID) { + return; + } - // note: what's left are the unverified tokens - } + // here's where we clean up the impossible tokens + if (token.status == Token::INVALID) { + m_tokens.pop(); + continue; + } - // no token? maybe we've actually finished - if(m_endedStream) - return; + // note: what's left are the unverified tokens + } - // no? then scan... - ScanNextToken(); - } - } + // no token? maybe we've actually finished + if (m_endedStream) { + return; + } - // ScanNextToken - // . The main scanning function; here we branch out and - // scan whatever the next token should be. - void Scanner::ScanNextToken() - { - if(m_endedStream) - return; + // no? then scan... + ScanNextToken(); + } +} - if(!m_startedStream) - return StartStream(); +void Scanner::ScanNextToken() { + if (m_endedStream) { + return; + } - // get rid of whitespace, etc. (in between tokens it should be irrelevent) - ScanToNextToken(); + if (!m_startedStream) { + return StartStream(); + } - // maybe need to end some blocks - PopIndentToHere(); + // get rid of whitespace, etc. (in between tokens it should be irrelevent) + ScanToNextToken(); - // ***** - // And now branch based on the next few characters! - // ***** + // maybe need to end some blocks + PopIndentToHere(); - // end of stream - if(!INPUT) - return EndStream(); + // ***** + // And now branch based on the next few characters! + // ***** - if(INPUT.column() == 0 && INPUT.peek() == Keys::Directive) - return ScanDirective(); + // end of stream + if (!INPUT) { + return EndStream(); + } - // document token - if(INPUT.column() == 0 && Exp::DocStart().Matches(INPUT)) - return ScanDocStart(); + if (INPUT.column() == 0 && INPUT.peek() == Keys::Directive) { + return ScanDirective(); + } - if(INPUT.column() == 0 && Exp::DocEnd().Matches(INPUT)) - return ScanDocEnd(); + // document token + if (INPUT.column() == 0 && Exp::DocStart().Matches(INPUT)) { + return ScanDocStart(); + } - // flow start/end/entry - if(INPUT.peek() == Keys::FlowSeqStart || INPUT.peek() == Keys::FlowMapStart) - return ScanFlowStart(); + if (INPUT.column() == 0 && Exp::DocEnd().Matches(INPUT)) { + return ScanDocEnd(); + } - if(INPUT.peek() == Keys::FlowSeqEnd || INPUT.peek() == Keys::FlowMapEnd) - return ScanFlowEnd(); + // flow start/end/entry + if (INPUT.peek() == Keys::FlowSeqStart || + INPUT.peek() == Keys::FlowMapStart) { + return ScanFlowStart(); + } - if(INPUT.peek() == Keys::FlowEntry) - return ScanFlowEntry(); + if (INPUT.peek() == Keys::FlowSeqEnd || INPUT.peek() == Keys::FlowMapEnd) { + return ScanFlowEnd(); + } - // block/map stuff - if(Exp::BlockEntry().Matches(INPUT)) - return ScanBlockEntry(); + if (INPUT.peek() == Keys::FlowEntry) { + return ScanFlowEntry(); + } - if((InBlockContext() ? Exp::Key() : Exp::KeyInFlow()).Matches(INPUT)) - return ScanKey(); + // block/map stuff + if (Exp::BlockEntry().Matches(INPUT)) { + return ScanBlockEntry(); + } - if(GetValueRegex().Matches(INPUT)) - return ScanValue(); + if ((InBlockContext() ? Exp::Key() : Exp::KeyInFlow()).Matches(INPUT)) { + return ScanKey(); + } - // alias/anchor - if(INPUT.peek() == Keys::Alias || INPUT.peek() == Keys::Anchor) - return ScanAnchorOrAlias(); + if (GetValueRegex().Matches(INPUT)) { + return ScanValue(); + } - // tag - if(INPUT.peek() == Keys::Tag) - return ScanTag(); + // alias/anchor + if (INPUT.peek() == Keys::Alias || INPUT.peek() == Keys::Anchor) { + return ScanAnchorOrAlias(); + } - // special scalars - if(InBlockContext() && (INPUT.peek() == Keys::LiteralScalar || INPUT.peek() == Keys::FoldedScalar)) - return ScanBlockScalar(); + // tag + if (INPUT.peek() == Keys::Tag) { + return ScanTag(); + } - if(INPUT.peek() == '\'' || INPUT.peek() == '\"') - return ScanQuotedScalar(); + // special scalars + if (InBlockContext() && (INPUT.peek() == Keys::LiteralScalar || + INPUT.peek() == Keys::FoldedScalar)) { + return ScanBlockScalar(); + } - // plain scalars - if((InBlockContext() ? Exp::PlainScalar() : Exp::PlainScalarInFlow()).Matches(INPUT)) - return ScanPlainScalar(); + if (INPUT.peek() == '\'' || INPUT.peek() == '\"') { + return ScanQuotedScalar(); + } - // don't know what it is! - throw ParserException(INPUT.mark(), ErrorMsg::UNKNOWN_TOKEN); - } + // plain scalars + if ((InBlockContext() ? Exp::PlainScalar() : Exp::PlainScalarInFlow()) + .Matches(INPUT)) { + return ScanPlainScalar(); + } - // ScanToNextToken - // . Eats input until we reach the next token-like thing. - void Scanner::ScanToNextToken() - { - while(1) { - // first eat whitespace - while(INPUT && IsWhitespaceToBeEaten(INPUT.peek())) { - if(InBlockContext() && Exp::Tab().Matches(INPUT)) - m_simpleKeyAllowed = false; - INPUT.eat(1); - } + // don't know what it is! + throw ParserException(INPUT.mark(), ErrorMsg::UNKNOWN_TOKEN); +} - // then eat a comment - if(Exp::Comment().Matches(INPUT)) { - // eat until line break - while(INPUT && !Exp::Break().Matches(INPUT)) - INPUT.eat(1); - } +void Scanner::ScanToNextToken() { + while (1) { + // first eat whitespace + while (INPUT && IsWhitespaceToBeEaten(INPUT.peek())) { + if (InBlockContext() && Exp::Tab().Matches(INPUT)) { + m_simpleKeyAllowed = false; + } + INPUT.eat(1); + } - // if it's NOT a line break, then we're done! - if(!Exp::Break().Matches(INPUT)) - break; + // then eat a comment + if (Exp::Comment().Matches(INPUT)) { + // eat until line break + while (INPUT && !Exp::Break().Matches(INPUT)) { + INPUT.eat(1); + } + } - // otherwise, let's eat the line break and keep going - int n = Exp::Break().Match(INPUT); - INPUT.eat(n); + // if it's NOT a line break, then we're done! + if (!Exp::Break().Matches(INPUT)) { + break; + } - // oh yeah, and let's get rid of that simple key - InvalidateSimpleKey(); + // otherwise, let's eat the line break and keep going + int n = Exp::Break().Match(INPUT); + INPUT.eat(n); - // new line - we may be able to accept a simple key now - if(InBlockContext()) - m_simpleKeyAllowed = true; - } - } + // oh yeah, and let's get rid of that simple key + InvalidateSimpleKey(); - /////////////////////////////////////////////////////////////////////// - // Misc. helpers + // new line - we may be able to accept a simple key now + if (InBlockContext()) { + m_simpleKeyAllowed = true; + } + } +} - // IsWhitespaceToBeEaten - // . We can eat whitespace if it's a space or tab - // . Note: originally tabs in block context couldn't be eaten - // "where a simple key could be allowed - // (i.e., not at the beginning of a line, or following '-', '?', or ':')" - // I think this is wrong, since tabs can be non-content whitespace; it's just - // that they can't contribute to indentation, so once you've seen a tab in a - // line, you can't start a simple key - bool Scanner::IsWhitespaceToBeEaten(char ch) - { - if(ch == ' ') - return true; +/////////////////////////////////////////////////////////////////////// +// Misc. helpers - if(ch == '\t') - return true; +// IsWhitespaceToBeEaten +// . We can eat whitespace if it's a space or tab +// . Note: originally tabs in block context couldn't be eaten +// "where a simple key could be allowed +// (i.e., not at the beginning of a line, or following '-', '?', or +// ':')" +// I think this is wrong, since tabs can be non-content whitespace; it's just +// that they can't contribute to indentation, so once you've seen a tab in a +// line, you can't start a simple key +bool Scanner::IsWhitespaceToBeEaten(char ch) { + if (ch == ' ') { + return true; + } - return false; - } + if (ch == '\t') { + return true; + } - // GetValueRegex - // . Get the appropriate regex to check if it's a value token - const RegEx& Scanner::GetValueRegex() const - { - if(InBlockContext()) - return Exp::Value(); + return false; +} - return m_canBeJSONFlow ? Exp::ValueInJSONFlow() : Exp::ValueInFlow(); - } +const RegEx& Scanner::GetValueRegex() const { + if (InBlockContext()) { + return Exp::Value(); + } - // StartStream - // . Set the initial conditions for starting a stream. - void Scanner::StartStream() - { - m_startedStream = true; - m_simpleKeyAllowed = true; - std::unique_ptr pIndent(new IndentMarker(-1, IndentMarker::NONE)); - m_indentRefs.push_back(std::move(pIndent)); - m_indents.push(&m_indentRefs.back()); - } + return m_canBeJSONFlow ? Exp::ValueInJSONFlow() : Exp::ValueInFlow(); +} - // EndStream - // . Close out the stream, finish up, etc. - void Scanner::EndStream() - { - // force newline - if(INPUT.column() > 0) - INPUT.ResetColumn(); +void Scanner::StartStream() { + m_startedStream = true; + m_simpleKeyAllowed = true; + std::unique_ptr pIndent( + new IndentMarker(-1, IndentMarker::NONE)); + m_indentRefs.push_back(std::move(pIndent)); + m_indents.push(&m_indentRefs.back()); +} - PopAllIndents(); - PopAllSimpleKeys(); +void Scanner::EndStream() { + // force newline + if (INPUT.column() > 0) { + INPUT.ResetColumn(); + } - m_simpleKeyAllowed = false; - m_endedStream = true; - } + PopAllIndents(); + PopAllSimpleKeys(); - Token *Scanner::PushToken(Token::TYPE type) - { - m_tokens.push(Token(type, INPUT.mark())); - return &m_tokens.back(); - } + m_simpleKeyAllowed = false; + m_endedStream = true; +} - Token::TYPE Scanner::GetStartTokenFor(IndentMarker::INDENT_TYPE type) const - { - switch(type) { - case IndentMarker::SEQ: return Token::BLOCK_SEQ_START; - case IndentMarker::MAP: return Token::BLOCK_MAP_START; - case IndentMarker::NONE: assert(false); break; - } - assert(false); - throw std::runtime_error("yaml-cpp: internal error, invalid indent type"); - } +Token* Scanner::PushToken(Token::TYPE type) { + m_tokens.push(Token(type, INPUT.mark())); + return &m_tokens.back(); +} - // PushIndentTo - // . Pushes an indentation onto the stack, and enqueues the - // proper token (sequence start or mapping start). - // . Returns the indent marker it generates (if any). - Scanner::IndentMarker *Scanner::PushIndentTo(int column, IndentMarker::INDENT_TYPE type) - { - // are we in flow? - if(InFlowContext()) - return 0; +Token::TYPE Scanner::GetStartTokenFor(IndentMarker::INDENT_TYPE type) const { + switch (type) { + case IndentMarker::SEQ: + return Token::BLOCK_SEQ_START; + case IndentMarker::MAP: + return Token::BLOCK_MAP_START; + case IndentMarker::NONE: + assert(false); + break; + } + assert(false); + throw std::runtime_error("yaml-cpp: internal error, invalid indent type"); +} - std::unique_ptr pIndent(new IndentMarker(column, type)); - IndentMarker& indent = *pIndent; - const IndentMarker& lastIndent = *m_indents.top(); +Scanner::IndentMarker* Scanner::PushIndentTo(int column, + IndentMarker::INDENT_TYPE type) { + // are we in flow? + if (InFlowContext()) { + return 0; + } - // is this actually an indentation? - if(indent.column < lastIndent.column) - return 0; - if(indent.column == lastIndent.column && !(indent.type == IndentMarker::SEQ && lastIndent.type == IndentMarker::MAP)) - return 0; + std::unique_ptr pIndent(new IndentMarker(column, type)); + IndentMarker& indent = *pIndent; + const IndentMarker& lastIndent = *m_indents.top(); - // push a start token - indent.pStartToken = PushToken(GetStartTokenFor(type)); + // is this actually an indentation? + if (indent.column < lastIndent.column) { + return 0; + } + if (indent.column == lastIndent.column && + !(indent.type == IndentMarker::SEQ && + lastIndent.type == IndentMarker::MAP)) { + return 0; + } - // and then the indent - m_indents.push(&indent); - m_indentRefs.push_back(std::move(pIndent)); - return &m_indentRefs.back(); - } + // push a start token + indent.pStartToken = PushToken(GetStartTokenFor(type)); - // PopIndentToHere - // . Pops indentations off the stack until we reach the current indentation level, - // and enqueues the proper token each time. - // . Then pops all invalid indentations off. - void Scanner::PopIndentToHere() - { - // are we in flow? - if(InFlowContext()) - return; + // and then the indent + m_indents.push(&indent); + m_indentRefs.push_back(std::move(pIndent)); + return &m_indentRefs.back(); +} - // now pop away - while(!m_indents.empty()) { - const IndentMarker& indent = *m_indents.top(); - if(indent.column < INPUT.column()) - break; - if(indent.column == INPUT.column() && !(indent.type == IndentMarker::SEQ && !Exp::BlockEntry().Matches(INPUT))) - break; +void Scanner::PopIndentToHere() { + // are we in flow? + if (InFlowContext()) { + return; + } - PopIndent(); - } + // now pop away + while (!m_indents.empty()) { + const IndentMarker& indent = *m_indents.top(); + if (indent.column < INPUT.column()) { + break; + } + if (indent.column == INPUT.column() && + !(indent.type == IndentMarker::SEQ && + !Exp::BlockEntry().Matches(INPUT))) { + break; + } - while(!m_indents.empty() && m_indents.top()->status == IndentMarker::INVALID) - PopIndent(); - } + PopIndent(); + } - // PopAllIndents - // . Pops all indentations (except for the base empty one) off the stack, - // and enqueues the proper token each time. - void Scanner::PopAllIndents() - { - // are we in flow? - if(InFlowContext()) - return; + while (!m_indents.empty() && + m_indents.top()->status == IndentMarker::INVALID) { + PopIndent(); + } +} - // now pop away - while(!m_indents.empty()) { - const IndentMarker& indent = *m_indents.top(); - if(indent.type == IndentMarker::NONE) - break; +void Scanner::PopAllIndents() { + // are we in flow? + if (InFlowContext()) { + return; + } - PopIndent(); - } - } + // now pop away + while (!m_indents.empty()) { + const IndentMarker& indent = *m_indents.top(); + if (indent.type == IndentMarker::NONE) { + break; + } - // PopIndent - // . Pops a single indent, pushing the proper token - void Scanner::PopIndent() - { - const IndentMarker& indent = *m_indents.top(); - m_indents.pop(); + PopIndent(); + } +} - if(indent.status != IndentMarker::VALID) { - InvalidateSimpleKey(); - return; - } +void Scanner::PopIndent() { + const IndentMarker& indent = *m_indents.top(); + m_indents.pop(); - if(indent.type == IndentMarker::SEQ) - m_tokens.push(Token(Token::BLOCK_SEQ_END, INPUT.mark())); - else if(indent.type == IndentMarker::MAP) - m_tokens.push(Token(Token::BLOCK_MAP_END, INPUT.mark())); - } + if (indent.status != IndentMarker::VALID) { + InvalidateSimpleKey(); + return; + } - // GetTopIndent - int Scanner::GetTopIndent() const - { - if(m_indents.empty()) - return 0; - return m_indents.top()->column; - } + if (indent.type == IndentMarker::SEQ) { + m_tokens.push(Token(Token::BLOCK_SEQ_END, INPUT.mark())); + } else if (indent.type == IndentMarker::MAP) { + m_tokens.push(Token(Token::BLOCK_MAP_END, INPUT.mark())); + } +} - // ThrowParserException - // . Throws a ParserException with the current token location - // (if available). - // . Does not parse any more tokens. - void Scanner::ThrowParserException(const std::string& msg) const - { - Mark mark = Mark::null(); - if(!m_tokens.empty()) { - const Token& token = m_tokens.front(); - mark = token.mark; - } - throw ParserException(mark, msg); - } +int Scanner::GetTopIndent() const { + if (m_indents.empty()) { + return 0; + } + return m_indents.top()->column; } + +void Scanner::ThrowParserException(const std::string& msg) const { + Mark mark = Mark::null_mark(); + if (!m_tokens.empty()) { + const Token& token = m_tokens.front(); + mark = token.mark; + } + throw ParserException(mark, msg); +} +} // namespace YODA_YAML diff --git a/src/yamlcpp/scanner.h b/src/yamlcpp/scanner.h --- a/src/yamlcpp/scanner.h +++ b/src/yamlcpp/scanner.h @@ -1,132 +1,190 @@ #ifndef SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include -#include -#include #include "ptr_vector.h" #include "stream.h" #include "token.h" +#include "yaml-cpp/mark.h" -namespace YODA_YAML -{ - class Node; - class RegEx; +namespace YODA_YAML { +class Node; +class RegEx; - class Scanner - { - public: - Scanner(std::istream& in); - ~Scanner(); +/** + * A scanner transforms a stream of characters into a stream of tokens. + */ +class Scanner { + public: + explicit Scanner(std::istream &in); + ~Scanner(); - // token queue management (hopefully this looks kinda stl-ish) - bool empty(); - void pop(); - Token& peek(); + /** Returns true if there are no more tokens to be read. */ + bool empty(); - private: - struct IndentMarker { - enum INDENT_TYPE { MAP, SEQ, NONE }; - enum STATUS { VALID, INVALID, UNKNOWN }; - IndentMarker(int column_, INDENT_TYPE type_): column(column_), type(type_), status(VALID), pStartToken(0) {} - - int column; - INDENT_TYPE type; - STATUS status; - Token *pStartToken; - }; - - enum FLOW_MARKER { FLOW_MAP, FLOW_SEQ }; - - private: - // scanning - void EnsureTokensInQueue(); - void ScanNextToken(); - void ScanToNextToken(); - void StartStream(); - void EndStream(); - Token *PushToken(Token::TYPE type); - - bool InFlowContext() const { return !m_flows.empty(); } - bool InBlockContext() const { return m_flows.empty(); } - int GetFlowLevel() const { return m_flows.size(); } - - Token::TYPE GetStartTokenFor(IndentMarker::INDENT_TYPE type) const; - IndentMarker *PushIndentTo(int column, IndentMarker::INDENT_TYPE type); - void PopIndentToHere(); - void PopAllIndents(); - void PopIndent(); - int GetTopIndent() const; + /** Removes the next token in the queue. */ + void pop(); - // checking input - bool CanInsertPotentialSimpleKey() const; - bool ExistsActiveSimpleKey() const; - void InsertPotentialSimpleKey(); - void InvalidateSimpleKey(); - bool VerifySimpleKey(); - void PopAllSimpleKeys(); - - void ThrowParserException(const std::string& msg) const; + /** Returns, but does not remove, the next token in the queue. */ + Token &peek(); - bool IsWhitespaceToBeEaten(char ch); - const RegEx& GetValueRegex() const; + /** Returns the current mark in the input stream. */ + Mark mark() const; - struct SimpleKey { - SimpleKey(const Mark& mark_, int flowLevel_); + private: + struct IndentMarker { + enum INDENT_TYPE { MAP, SEQ, NONE }; + enum STATUS { VALID, INVALID, UNKNOWN }; + IndentMarker(int column_, INDENT_TYPE type_) + : column(column_), type(type_), status(VALID), pStartToken(0) {} - void Validate(); - void Invalidate(); - - Mark mark; - int flowLevel; - IndentMarker *pIndent; - Token *pMapStart, *pKey; - }; + int column; + INDENT_TYPE type; + STATUS status; + Token *pStartToken; + }; - // and the tokens - void ScanDirective(); - void ScanDocStart(); - void ScanDocEnd(); - void ScanBlockSeqStart(); - void ScanBlockMapSTart(); - void ScanBlockEnd(); - void ScanBlockEntry(); - void ScanFlowStart(); - void ScanFlowEnd(); - void ScanFlowEntry(); - void ScanKey(); - void ScanValue(); - void ScanAnchorOrAlias(); - void ScanTag(); - void ScanPlainScalar(); - void ScanQuotedScalar(); - void ScanBlockScalar(); + enum FLOW_MARKER { FLOW_MAP, FLOW_SEQ }; - private: - // the stream - Stream INPUT; + private: + // scanning - // the output (tokens) - std::queue m_tokens; + /** + * Scans until there's a valid token at the front of the queue, or the queue + * is empty. The state can be checked by {@link #empty}, and the next token + * retrieved by {@link #peek}. + */ + void EnsureTokensInQueue(); - // state info - bool m_startedStream, m_endedStream; - bool m_simpleKeyAllowed; - bool m_canBeJSONFlow; - std::stack m_simpleKeys; - std::stack m_indents; - ptr_vector m_indentRefs; // for "garbage collection" - std::stack m_flows; - }; + /** + * The main scanning function; this method branches out to scan whatever the + * next token should be. + */ + void ScanNextToken(); + + /** Eats the input stream until it reaches the next token-like thing. */ + void ScanToNextToken(); + + /** Sets the initial conditions for starting a stream. */ + void StartStream(); + + /** Closes out the stream, finish up, etc. */ + void EndStream(); + + Token *PushToken(Token::TYPE type); + + bool InFlowContext() const { return !m_flows.empty(); } + bool InBlockContext() const { return m_flows.empty(); } + std::size_t GetFlowLevel() const { return m_flows.size(); } + + Token::TYPE GetStartTokenFor(IndentMarker::INDENT_TYPE type) const; + + /** + * Pushes an indentation onto the stack, and enqueues the proper token + * (sequence start or mapping start). + * + * @return the indent marker it generates (if any). + */ + IndentMarker *PushIndentTo(int column, IndentMarker::INDENT_TYPE type); + + /** + * Pops indentations off the stack until it reaches the current indentation + * level, and enqueues the proper token each time. Then pops all invalid + * indentations off. + */ + void PopIndentToHere(); + + /** + * Pops all indentations (except for the base empty one) off the stack, and + * enqueues the proper token each time. + */ + void PopAllIndents(); + + /** Pops a single indent, pushing the proper token. */ + void PopIndent(); + int GetTopIndent() const; + + // checking input + bool CanInsertPotentialSimpleKey() const; + bool ExistsActiveSimpleKey() const; + void InsertPotentialSimpleKey(); + void InvalidateSimpleKey(); + bool VerifySimpleKey(); + void PopAllSimpleKeys(); + + /** + * Throws a ParserException with the current token location (if available), + * and does not parse any more tokens. + */ + void ThrowParserException(const std::string &msg) const; + + bool IsWhitespaceToBeEaten(char ch); + + /** + * Returns the appropriate regex to check if the next token is a value token. + */ + const RegEx &GetValueRegex() const; + + struct SimpleKey { + SimpleKey(const Mark &mark_, std::size_t flowLevel_); + + void Validate(); + void Invalidate(); + + Mark mark; + std::size_t flowLevel; + IndentMarker *pIndent; + Token *pMapStart, *pKey; + }; + + // and the tokens + void ScanDirective(); + void ScanDocStart(); + void ScanDocEnd(); + void ScanBlockSeqStart(); + void ScanBlockMapSTart(); + void ScanBlockEnd(); + void ScanBlockEntry(); + void ScanFlowStart(); + void ScanFlowEnd(); + void ScanFlowEntry(); + void ScanKey(); + void ScanValue(); + void ScanAnchorOrAlias(); + void ScanTag(); + void ScanPlainScalar(); + void ScanQuotedScalar(); + void ScanBlockScalar(); + + private: + // the stream + Stream INPUT; + + // the output (tokens) + std::queue m_tokens; + + // state info + bool m_startedStream, m_endedStream; + bool m_simpleKeyAllowed; + bool m_canBeJSONFlow; + std::stack m_simpleKeys; + std::stack m_indents; + ptr_vector m_indentRefs; // for "garbage collection" + std::stack m_flows; +}; } -#endif // SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/scanscalar.cpp b/src/yamlcpp/scanscalar.cpp --- a/src/yamlcpp/scanscalar.cpp +++ b/src/yamlcpp/scanscalar.cpp @@ -1,214 +1,250 @@ #include "scanscalar.h" -#include "scanner.h" + +#include + #include "exp.h" -#include "yaml-cpp/exceptions.h" -#include "token.h" +#include "regeximpl.h" +#include "stream.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep -namespace YODA_YAML -{ - // ScanScalar - // . This is where the scalar magic happens. - // - // . We do the scanning in three phases: - // 1. Scan until newline - // 2. Eat newline - // 3. Scan leading blanks. - // - // . Depending on the parameters given, we store or stop - // and different places in the above flow. - std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) - { - bool foundNonEmptyLine = false; - bool pastOpeningBreak = (params.fold == FOLD_FLOW); - bool emptyLine = false, moreIndented = false; - int foldedNewlineCount = 0; - bool foldedNewlineStartedMoreIndented = false; - std::size_t lastEscapedChar = std::string::npos; - std::string scalar; - params.leadingSpaces = false; +namespace YODA_YAML { +// ScanScalar +// . This is where the scalar magic happens. +// +// . We do the scanning in three phases: +// 1. Scan until newline +// 2. Eat newline +// 3. Scan leading blanks. +// +// . Depending on the parameters given, we store or stop +// and different places in the above flow. +std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) { + bool foundNonEmptyLine = false; + bool pastOpeningBreak = (params.fold == FOLD_FLOW); + bool emptyLine = false, moreIndented = false; + int foldedNewlineCount = 0; + bool foldedNewlineStartedMoreIndented = false; + std::size_t lastEscapedChar = std::string::npos; + std::string scalar; + params.leadingSpaces = false; - while(INPUT) { - // ******************************** - // Phase #1: scan until line ending - - std::size_t lastNonWhitespaceChar = scalar.size(); - bool escapedNewline = false; - while(!params.end.Matches(INPUT) && !Exp::Break().Matches(INPUT)) { - if(!INPUT) - break; + if (!params.end) { + params.end = &Exp::Empty(); + } - // document indicator? - if(INPUT.column() == 0 && Exp::DocIndicator().Matches(INPUT)) { - if(params.onDocIndicator == BREAK) - break; - else if(params.onDocIndicator == THROW) - throw ParserException(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR); - } + while (INPUT) { + // ******************************** + // Phase #1: scan until line ending - foundNonEmptyLine = true; - pastOpeningBreak = true; + std::size_t lastNonWhitespaceChar = scalar.size(); + bool escapedNewline = false; + while (!params.end->Matches(INPUT) && !Exp::Break().Matches(INPUT)) { + if (!INPUT) { + break; + } - // escaped newline? (only if we're escaping on slash) - if(params.escape == '\\' && Exp::EscBreak().Matches(INPUT)) { - // eat escape character and get out (but preserve trailing whitespace!) - INPUT.get(); - lastNonWhitespaceChar = scalar.size(); - lastEscapedChar = scalar.size(); - escapedNewline = true; - break; - } + // document indicator? + if (INPUT.column() == 0 && Exp::DocIndicator().Matches(INPUT)) { + if (params.onDocIndicator == BREAK) { + break; + } else if (params.onDocIndicator == THROW) { + throw ParserException(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR); + } + } - // escape this? - if(INPUT.peek() == params.escape) { - scalar += Exp::Escape(INPUT); - lastNonWhitespaceChar = scalar.size(); - lastEscapedChar = scalar.size(); - continue; - } + foundNonEmptyLine = true; + pastOpeningBreak = true; - // otherwise, just add the damn character - char ch = INPUT.get(); - scalar += ch; - if(ch != ' ' && ch != '\t') - lastNonWhitespaceChar = scalar.size(); - } + // escaped newline? (only if we're escaping on slash) + if (params.escape == '\\' && Exp::EscBreak().Matches(INPUT)) { + // eat escape character and get out (but preserve trailing whitespace!) + INPUT.get(); + lastNonWhitespaceChar = scalar.size(); + lastEscapedChar = scalar.size(); + escapedNewline = true; + break; + } - // eof? if we're looking to eat something, then we throw - if(!INPUT) { - if(params.eatEnd) - throw ParserException(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR); - break; - } + // escape this? + if (INPUT.peek() == params.escape) { + scalar += Exp::Escape(INPUT); + lastNonWhitespaceChar = scalar.size(); + lastEscapedChar = scalar.size(); + continue; + } - // doc indicator? - if(params.onDocIndicator == BREAK && INPUT.column() == 0 && Exp::DocIndicator().Matches(INPUT)) - break; + // otherwise, just add the damn character + char ch = INPUT.get(); + scalar += ch; + if (ch != ' ' && ch != '\t') { + lastNonWhitespaceChar = scalar.size(); + } + } - // are we done via character match? - int n = params.end.Match(INPUT); - if(n >= 0) { - if(params.eatEnd) - INPUT.eat(n); - break; - } - - // do we remove trailing whitespace? - if(params.fold == FOLD_FLOW) - scalar.erase(lastNonWhitespaceChar); - - // ******************************** - // Phase #2: eat line ending - n = Exp::Break().Match(INPUT); - INPUT.eat(n); + // eof? if we're looking to eat something, then we throw + if (!INPUT) { + if (params.eatEnd) { + throw ParserException(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR); + } + break; + } - // ******************************** - // Phase #3: scan initial spaces + // doc indicator? + if (params.onDocIndicator == BREAK && INPUT.column() == 0 && + Exp::DocIndicator().Matches(INPUT)) { + break; + } - // first the required indentation - while(INPUT.peek() == ' ' && (INPUT.column() < params.indent || (params.detectIndent && !foundNonEmptyLine))) - INPUT.eat(1); + // are we done via character match? + int n = params.end->Match(INPUT); + if (n >= 0) { + if (params.eatEnd) { + INPUT.eat(n); + } + break; + } - // update indent if we're auto-detecting - if(params.detectIndent && !foundNonEmptyLine) - params.indent = std::max(params.indent, INPUT.column()); + // do we remove trailing whitespace? + if (params.fold == FOLD_FLOW) + scalar.erase(lastNonWhitespaceChar); - // and then the rest of the whitespace - while(Exp::Blank().Matches(INPUT)) { - // we check for tabs that masquerade as indentation - if(INPUT.peek() == '\t'&& INPUT.column() < params.indent && params.onTabInIndentation == THROW) - throw ParserException(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION); + // ******************************** + // Phase #2: eat line ending + n = Exp::Break().Match(INPUT); + INPUT.eat(n); - if(!params.eatLeadingWhitespace) - break; + // ******************************** + // Phase #3: scan initial spaces - INPUT.eat(1); - } + // first the required indentation + while (INPUT.peek() == ' ' && + (INPUT.column() < params.indent || + (params.detectIndent && !foundNonEmptyLine)) && + !params.end->Matches(INPUT)) { + INPUT.eat(1); + } - // was this an empty line? - bool nextEmptyLine = Exp::Break().Matches(INPUT); - bool nextMoreIndented = Exp::Blank().Matches(INPUT); - if(params.fold == FOLD_BLOCK && foldedNewlineCount == 0 && nextEmptyLine) - foldedNewlineStartedMoreIndented = moreIndented; + // update indent if we're auto-detecting + if (params.detectIndent && !foundNonEmptyLine) { + params.indent = std::max(params.indent, INPUT.column()); + } - // for block scalars, we always start with a newline, so we should ignore it (not fold or keep) - if(pastOpeningBreak) { - switch(params.fold) { - case DONT_FOLD: - scalar += "\n"; - break; - case FOLD_BLOCK: - if(!emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented && INPUT.column() >= params.indent) - scalar += " "; - else if(nextEmptyLine) - foldedNewlineCount++; - else - scalar += "\n"; - - if(!nextEmptyLine && foldedNewlineCount > 0) { - scalar += std::string(foldedNewlineCount - 1, '\n'); - if(foldedNewlineStartedMoreIndented || nextMoreIndented | !foundNonEmptyLine) - scalar += "\n"; - foldedNewlineCount = 0; - } - break; - case FOLD_FLOW: - if(nextEmptyLine) - scalar += "\n"; - else if(!emptyLine && !nextEmptyLine && !escapedNewline) - scalar += " "; - break; - } - } + // and then the rest of the whitespace + while (Exp::Blank().Matches(INPUT)) { + // we check for tabs that masquerade as indentation + if (INPUT.peek() == '\t' && INPUT.column() < params.indent && + params.onTabInIndentation == THROW) { + throw ParserException(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION); + } - emptyLine = nextEmptyLine; - moreIndented = nextMoreIndented; - pastOpeningBreak = true; + if (!params.eatLeadingWhitespace) { + break; + } - // are we done via indentation? - if(!emptyLine && INPUT.column() < params.indent) { - params.leadingSpaces = true; - break; - } - } + if (params.end->Matches(INPUT)) { + break; + } - // post-processing - if(params.trimTrailingSpaces) { - std::size_t pos = scalar.find_last_not_of(' '); - if(lastEscapedChar != std::string::npos) { - if(pos < lastEscapedChar || pos == std::string::npos) - pos = lastEscapedChar; + INPUT.eat(1); + } + + // was this an empty line? + bool nextEmptyLine = Exp::Break().Matches(INPUT); + bool nextMoreIndented = Exp::Blank().Matches(INPUT); + if (params.fold == FOLD_BLOCK && foldedNewlineCount == 0 && nextEmptyLine) + foldedNewlineStartedMoreIndented = moreIndented; + + // for block scalars, we always start with a newline, so we should ignore it + // (not fold or keep) + if (pastOpeningBreak) { + switch (params.fold) { + case DONT_FOLD: + scalar += "\n"; + break; + case FOLD_BLOCK: + if (!emptyLine && !nextEmptyLine && !moreIndented && + !nextMoreIndented && INPUT.column() >= params.indent) { + scalar += " "; + } else if (nextEmptyLine) { + foldedNewlineCount++; + } else { + scalar += "\n"; + } + + if (!nextEmptyLine && foldedNewlineCount > 0) { + scalar += std::string(foldedNewlineCount - 1, '\n'); + if (foldedNewlineStartedMoreIndented || + nextMoreIndented | !foundNonEmptyLine) { + scalar += "\n"; } - if(pos < scalar.size()) - scalar.erase(pos + 1); - } + foldedNewlineCount = 0; + } + break; + case FOLD_FLOW: + if (nextEmptyLine) { + scalar += "\n"; + } else if (!emptyLine && !nextEmptyLine && !escapedNewline) { + scalar += " "; + } + break; + } + } - switch(params.chomp) { - case CLIP: { - std::size_t pos = scalar.find_last_not_of('\n'); - if(lastEscapedChar != std::string::npos) { - if(pos < lastEscapedChar || pos == std::string::npos) - pos = lastEscapedChar; - } - if(pos == std::string::npos) - scalar.erase(); - else if(pos + 1 < scalar.size()) - scalar.erase(pos + 2); - } break; - case STRIP: { - std::size_t pos = scalar.find_last_not_of('\n'); - if(lastEscapedChar != std::string::npos) { - if(pos < lastEscapedChar || pos == std::string::npos) - pos = lastEscapedChar; - } - if(pos == std::string::npos) - scalar.erase(); - else if(pos < scalar.size()) - scalar.erase(pos + 1); - } break; - default: - break; - } + emptyLine = nextEmptyLine; + moreIndented = nextMoreIndented; + pastOpeningBreak = true; - return scalar; - } + // are we done via indentation? + if (!emptyLine && INPUT.column() < params.indent) { + params.leadingSpaces = true; + break; + } + } + + // post-processing + if (params.trimTrailingSpaces) { + std::size_t pos = scalar.find_last_not_of(' '); + if (lastEscapedChar != std::string::npos) { + if (pos < lastEscapedChar || pos == std::string::npos) { + pos = lastEscapedChar; + } + } + if (pos < scalar.size()) { + scalar.erase(pos + 1); + } + } + + switch (params.chomp) { + case CLIP: { + std::size_t pos = scalar.find_last_not_of('\n'); + if (lastEscapedChar != std::string::npos) { + if (pos < lastEscapedChar || pos == std::string::npos) { + pos = lastEscapedChar; + } + } + if (pos == std::string::npos) { + scalar.erase(); + } else if (pos + 1 < scalar.size()) { + scalar.erase(pos + 2); + } + } break; + case STRIP: { + std::size_t pos = scalar.find_last_not_of('\n'); + if (lastEscapedChar != std::string::npos) { + if (pos < lastEscapedChar || pos == std::string::npos) { + pos = lastEscapedChar; + } + } + if (pos == std::string::npos) { + scalar.erase(); + } else if (pos < scalar.size()) { + scalar.erase(pos + 1); + } + } break; + default: + break; + } + + return scalar; } +} diff --git a/src/yamlcpp/scanscalar.h b/src/yamlcpp/scanscalar.h --- a/src/yamlcpp/scanscalar.h +++ b/src/yamlcpp/scanscalar.h @@ -1,45 +1,63 @@ #ifndef SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include -#include -#include "regex.h" +#include "regex_yaml.h" #include "stream.h" -namespace YODA_YAML -{ - enum CHOMP { STRIP = -1, CLIP, KEEP }; - enum ACTION { NONE, BREAK, THROW }; - enum FOLD { DONT_FOLD, FOLD_BLOCK, FOLD_FLOW }; +namespace YODA_YAML { +enum CHOMP { STRIP = -1, CLIP, KEEP }; +enum ACTION { NONE, BREAK, THROW }; +enum FOLD { DONT_FOLD, FOLD_BLOCK, FOLD_FLOW }; - struct ScanScalarParams { - ScanScalarParams(): eatEnd(false), indent(0), detectIndent(false), eatLeadingWhitespace(0), escape(0), fold(DONT_FOLD), - trimTrailingSpaces(0), chomp(CLIP), onDocIndicator(NONE), onTabInIndentation(NONE), leadingSpaces(false) {} +struct ScanScalarParams { + ScanScalarParams() + : end(nullptr), + eatEnd(false), + indent(0), + detectIndent(false), + eatLeadingWhitespace(0), + escape(0), + fold(DONT_FOLD), + trimTrailingSpaces(0), + chomp(CLIP), + onDocIndicator(NONE), + onTabInIndentation(NONE), + leadingSpaces(false) {} - // input: - RegEx end; // what condition ends this scalar? - bool eatEnd; // should we eat that condition when we see it? - int indent; // what level of indentation should be eaten and ignored? - bool detectIndent; // should we try to autodetect the indent? - bool eatLeadingWhitespace; // should we continue eating this delicious indentation after 'indent' spaces? - char escape; // what character do we escape on (i.e., slash or single quote) (0 for none) - FOLD fold; // how do we fold line ends? - bool trimTrailingSpaces; // do we remove all trailing spaces (at the very end) - CHOMP chomp; // do we strip, clip, or keep trailing newlines (at the very end) - // Note: strip means kill all, clip means keep at most one, keep means keep all - ACTION onDocIndicator; // what do we do if we see a document indicator? - ACTION onTabInIndentation; // what do we do if we see a tab where we should be seeing indentation spaces + // input: + const RegEx* end; // what condition ends this scalar? + // unowned. + bool eatEnd; // should we eat that condition when we see it? + int indent; // what level of indentation should be eaten and ignored? + bool detectIndent; // should we try to autodetect the indent? + bool eatLeadingWhitespace; // should we continue eating this delicious + // indentation after 'indent' spaces? + char escape; // what character do we escape on (i.e., slash or single quote) + // (0 for none) + FOLD fold; // how do we fold line ends? + bool trimTrailingSpaces; // do we remove all trailing spaces (at the very + // end) + CHOMP chomp; // do we strip, clip, or keep trailing newlines (at the very + // end) + // Note: strip means kill all, clip means keep at most one, keep means keep + // all + ACTION onDocIndicator; // what do we do if we see a document indicator? + ACTION onTabInIndentation; // what do we do if we see a tab where we should + // be seeing indentation spaces - // output: - bool leadingSpaces; - }; + // output: + bool leadingSpaces; +}; - std::string ScanScalar(Stream& INPUT, ScanScalarParams& info); +std::string ScanScalar(Stream& INPUT, ScanScalarParams& info); } -#endif // SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/scantag.cpp b/src/yamlcpp/scantag.cpp --- a/src/yamlcpp/scantag.cpp +++ b/src/yamlcpp/scantag.cpp @@ -1,84 +1,81 @@ -#include "scanner.h" -#include "regex.h" #include "exp.h" -#include "yaml-cpp/exceptions.h" +#include "regex_yaml.h" +#include "regeximpl.h" +#include "stream.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep +#include "yaml-cpp/mark.h" -namespace YODA_YAML -{ - const std::string ScanVerbatimTag(Stream& INPUT) - { - std::string tag; - - // eat the start character - INPUT.get(); - - while(INPUT) { - if(INPUT.peek() == Keys::VerbatimTagEnd) { - // eat the end character - INPUT.get(); - return tag; - } - - int n = Exp::URI().Match(INPUT); - if(n <= 0) - break; - - tag += INPUT.get(n); - } +namespace YODA_YAML { +const std::string ScanVerbatimTag(Stream& INPUT) { + std::string tag; - throw ParserException(INPUT.mark(), ErrorMsg::END_OF_VERBATIM_TAG); - } - - const std::string ScanTagHandle(Stream& INPUT, bool& canBeHandle) - { - std::string tag; - canBeHandle = true; - Mark firstNonWordChar; - - while(INPUT) { - if(INPUT.peek() == Keys::Tag) { - if(!canBeHandle) - throw ParserException(firstNonWordChar, ErrorMsg::CHAR_IN_TAG_HANDLE); - break; - } + // eat the start character + INPUT.get(); - int n = 0; - if(canBeHandle) { - n = Exp::Word().Match(INPUT); - if(n <= 0) { - canBeHandle = false; - firstNonWordChar = INPUT.mark(); - } - } - - if(!canBeHandle) - n = Exp::Tag().Match(INPUT); + while (INPUT) { + if (INPUT.peek() == Keys::VerbatimTagEnd) { + // eat the end character + INPUT.get(); + return tag; + } - if(n <= 0) - break; - - tag += INPUT.get(n); - } + int n = Exp::URI().Match(INPUT); + if (n <= 0) + break; - return tag; - } - - const std::string ScanTagSuffix(Stream& INPUT) - { - std::string tag; - - while(INPUT) { - int n = Exp::Tag().Match(INPUT); - if(n <= 0) - break; - - tag += INPUT.get(n); - } - - if(tag.empty()) - throw ParserException(INPUT.mark(), ErrorMsg::TAG_WITH_NO_SUFFIX); - - return tag; - } + tag += INPUT.get(n); + } + + throw ParserException(INPUT.mark(), ErrorMsg::END_OF_VERBATIM_TAG); } +const std::string ScanTagHandle(Stream& INPUT, bool& canBeHandle) { + std::string tag; + canBeHandle = true; + Mark firstNonWordChar; + + while (INPUT) { + if (INPUT.peek() == Keys::Tag) { + if (!canBeHandle) + throw ParserException(firstNonWordChar, ErrorMsg::CHAR_IN_TAG_HANDLE); + break; + } + + int n = 0; + if (canBeHandle) { + n = Exp::Word().Match(INPUT); + if (n <= 0) { + canBeHandle = false; + firstNonWordChar = INPUT.mark(); + } + } + + if (!canBeHandle) + n = Exp::Tag().Match(INPUT); + + if (n <= 0) + break; + + tag += INPUT.get(n); + } + + return tag; +} + +const std::string ScanTagSuffix(Stream& INPUT) { + std::string tag; + + while (INPUT) { + int n = Exp::Tag().Match(INPUT); + if (n <= 0) + break; + + tag += INPUT.get(n); + } + + if (tag.empty()) + throw ParserException(INPUT.mark(), ErrorMsg::TAG_WITH_NO_SUFFIX); + + return tag; +} +} diff --git a/src/yamlcpp/scantag.h b/src/yamlcpp/scantag.h --- a/src/yamlcpp/scantag.h +++ b/src/yamlcpp/scantag.h @@ -1,20 +1,19 @@ #ifndef SCANTAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define SCANTAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include #include "stream.h" -namespace YODA_YAML -{ - const std::string ScanVerbatimTag(Stream& INPUT); - const std::string ScanTagHandle(Stream& INPUT, bool& canBeHandle); - const std::string ScanTagSuffix(Stream& INPUT); +namespace YODA_YAML { +const std::string ScanVerbatimTag(Stream& INPUT); +const std::string ScanTagHandle(Stream& INPUT, bool& canBeHandle); +const std::string ScanTagSuffix(Stream& INPUT); } -#endif // SCANTAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // SCANTAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/scantoken.cpp b/src/yamlcpp/scantoken.cpp --- a/src/yamlcpp/scantoken.cpp +++ b/src/yamlcpp/scantoken.cpp @@ -1,439 +1,437 @@ -#include "scanner.h" -#include "token.h" -#include "yaml-cpp/exceptions.h" -#include "exp.h" -#include "scanscalar.h" -#include "scantag.h" -#include "tag.h" #include -namespace YODA_YAML -{ - /////////////////////////////////////////////////////////////////////// - // Specialization for scanning specific tokens +#include "exp.h" +#include "regex_yaml.h" +#include "regeximpl.h" +#include "scanner.h" +#include "scanscalar.h" +#include "scantag.h" // IWYU pragma: keep +#include "tag.h" // IWYU pragma: keep +#include "token.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep +#include "yaml-cpp/mark.h" - // Directive - // . Note: no semantic checking is done here (that's for the parser to do) - void Scanner::ScanDirective() - { - std::string name; - std::vector params; +namespace YODA_YAML { +/////////////////////////////////////////////////////////////////////// +// Specialization for scanning specific tokens - // pop indents and simple keys - PopAllIndents(); - PopAllSimpleKeys(); +// Directive +// . Note: no semantic checking is done here (that's for the parser to do) +void Scanner::ScanDirective() { + std::string name; + std::vector params; - m_simpleKeyAllowed = false; - m_canBeJSONFlow = false; + // pop indents and simple keys + PopAllIndents(); + PopAllSimpleKeys(); - // store pos and eat indicator - Token token(Token::DIRECTIVE, INPUT.mark()); - INPUT.eat(1); + m_simpleKeyAllowed = false; + m_canBeJSONFlow = false; - // read name - while(INPUT && !Exp::BlankOrBreak().Matches(INPUT)) - token.value += INPUT.get(); + // store pos and eat indicator + Token token(Token::DIRECTIVE, INPUT.mark()); + INPUT.eat(1); - // read parameters - while(1) { - // first get rid of whitespace - while(Exp::Blank().Matches(INPUT)) - INPUT.eat(1); + // read name + while (INPUT && !Exp::BlankOrBreak().Matches(INPUT)) + token.value += INPUT.get(); - // break on newline or comment - if(!INPUT || Exp::Break().Matches(INPUT) || Exp::Comment().Matches(INPUT)) - break; + // read parameters + while (1) { + // first get rid of whitespace + while (Exp::Blank().Matches(INPUT)) + INPUT.eat(1); - // now read parameter - std::string param; - while(INPUT && !Exp::BlankOrBreak().Matches(INPUT)) - param += INPUT.get(); + // break on newline or comment + if (!INPUT || Exp::Break().Matches(INPUT) || Exp::Comment().Matches(INPUT)) + break; - token.params.push_back(param); - } - - m_tokens.push(token); - } + // now read parameter + std::string param; + while (INPUT && !Exp::BlankOrBreak().Matches(INPUT)) + param += INPUT.get(); - // DocStart - void Scanner::ScanDocStart() - { - PopAllIndents(); - PopAllSimpleKeys(); - m_simpleKeyAllowed = false; - m_canBeJSONFlow = false; + token.params.push_back(param); + } - // eat - Mark mark = INPUT.mark(); - INPUT.eat(3); - m_tokens.push(Token(Token::DOC_START, mark)); - } + m_tokens.push(token); +} - // DocEnd - void Scanner::ScanDocEnd() - { - PopAllIndents(); - PopAllSimpleKeys(); - m_simpleKeyAllowed = false; - m_canBeJSONFlow = false; +// DocStart +void Scanner::ScanDocStart() { + PopAllIndents(); + PopAllSimpleKeys(); + m_simpleKeyAllowed = false; + m_canBeJSONFlow = false; - // eat - Mark mark = INPUT.mark(); - INPUT.eat(3); - m_tokens.push(Token(Token::DOC_END, mark)); - } + // eat + Mark mark = INPUT.mark(); + INPUT.eat(3); + m_tokens.push(Token(Token::DOC_START, mark)); +} - // FlowStart - void Scanner::ScanFlowStart() - { - // flows can be simple keys - InsertPotentialSimpleKey(); - m_simpleKeyAllowed = true; - m_canBeJSONFlow = false; +// DocEnd +void Scanner::ScanDocEnd() { + PopAllIndents(); + PopAllSimpleKeys(); + m_simpleKeyAllowed = false; + m_canBeJSONFlow = false; - // eat - Mark mark = INPUT.mark(); - char ch = INPUT.get(); - FLOW_MARKER flowType = (ch == Keys::FlowSeqStart ? FLOW_SEQ : FLOW_MAP); - m_flows.push(flowType); - Token::TYPE type = (flowType == FLOW_SEQ ? Token::FLOW_SEQ_START : Token::FLOW_MAP_START); - m_tokens.push(Token(type, mark)); - } + // eat + Mark mark = INPUT.mark(); + INPUT.eat(3); + m_tokens.push(Token(Token::DOC_END, mark)); +} - // FlowEnd - void Scanner::ScanFlowEnd() - { - if(InBlockContext()) - throw ParserException(INPUT.mark(), ErrorMsg::FLOW_END); +// FlowStart +void Scanner::ScanFlowStart() { + // flows can be simple keys + InsertPotentialSimpleKey(); + m_simpleKeyAllowed = true; + m_canBeJSONFlow = false; - // we might have a solo entry in the flow context - if(InFlowContext()) { - if(m_flows.top() == FLOW_MAP && VerifySimpleKey()) - m_tokens.push(Token(Token::VALUE, INPUT.mark())); - else if(m_flows.top() == FLOW_SEQ) - InvalidateSimpleKey(); - } + // eat + Mark mark = INPUT.mark(); + char ch = INPUT.get(); + FLOW_MARKER flowType = (ch == Keys::FlowSeqStart ? FLOW_SEQ : FLOW_MAP); + m_flows.push(flowType); + Token::TYPE type = + (flowType == FLOW_SEQ ? Token::FLOW_SEQ_START : Token::FLOW_MAP_START); + m_tokens.push(Token(type, mark)); +} - m_simpleKeyAllowed = false; - m_canBeJSONFlow = true; +// FlowEnd +void Scanner::ScanFlowEnd() { + if (InBlockContext()) + throw ParserException(INPUT.mark(), ErrorMsg::FLOW_END); - // eat - Mark mark = INPUT.mark(); - char ch = INPUT.get(); + // we might have a solo entry in the flow context + if (InFlowContext()) { + if (m_flows.top() == FLOW_MAP && VerifySimpleKey()) + m_tokens.push(Token(Token::VALUE, INPUT.mark())); + else if (m_flows.top() == FLOW_SEQ) + InvalidateSimpleKey(); + } - // check that it matches the start - FLOW_MARKER flowType = (ch == Keys::FlowSeqEnd ? FLOW_SEQ : FLOW_MAP); - if(m_flows.top() != flowType) - throw ParserException(mark, ErrorMsg::FLOW_END); - m_flows.pop(); - - Token::TYPE type = (flowType ? Token::FLOW_SEQ_END : Token::FLOW_MAP_END); - m_tokens.push(Token(type, mark)); - } + m_simpleKeyAllowed = false; + m_canBeJSONFlow = true; - // FlowEntry - void Scanner::ScanFlowEntry() - { - // we might have a solo entry in the flow context - if(InFlowContext()) { - if(m_flows.top() == FLOW_MAP && VerifySimpleKey()) - m_tokens.push(Token(Token::VALUE, INPUT.mark())); - else if(m_flows.top() == FLOW_SEQ) - InvalidateSimpleKey(); - } - - m_simpleKeyAllowed = true; - m_canBeJSONFlow = false; + // eat + Mark mark = INPUT.mark(); + char ch = INPUT.get(); - // eat - Mark mark = INPUT.mark(); - INPUT.eat(1); - m_tokens.push(Token(Token::FLOW_ENTRY, mark)); - } + // check that it matches the start + FLOW_MARKER flowType = (ch == Keys::FlowSeqEnd ? FLOW_SEQ : FLOW_MAP); + if (m_flows.top() != flowType) + throw ParserException(mark, ErrorMsg::FLOW_END); + m_flows.pop(); - // BlockEntry - void Scanner::ScanBlockEntry() - { - // we better be in the block context! - if(InFlowContext()) - throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY); + Token::TYPE type = (flowType ? Token::FLOW_SEQ_END : Token::FLOW_MAP_END); + m_tokens.push(Token(type, mark)); +} - // can we put it here? - if(!m_simpleKeyAllowed) - throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY); +// FlowEntry +void Scanner::ScanFlowEntry() { + // we might have a solo entry in the flow context + if (InFlowContext()) { + if (m_flows.top() == FLOW_MAP && VerifySimpleKey()) + m_tokens.push(Token(Token::VALUE, INPUT.mark())); + else if (m_flows.top() == FLOW_SEQ) + InvalidateSimpleKey(); + } - PushIndentTo(INPUT.column(), IndentMarker::SEQ); - m_simpleKeyAllowed = true; - m_canBeJSONFlow = false; + m_simpleKeyAllowed = true; + m_canBeJSONFlow = false; - // eat - Mark mark = INPUT.mark(); - INPUT.eat(1); - m_tokens.push(Token(Token::BLOCK_ENTRY, mark)); - } + // eat + Mark mark = INPUT.mark(); + INPUT.eat(1); + m_tokens.push(Token(Token::FLOW_ENTRY, mark)); +} - // Key - void Scanner::ScanKey() - { - // handle keys diffently in the block context (and manage indents) - if(InBlockContext()) { - if(!m_simpleKeyAllowed) - throw ParserException(INPUT.mark(), ErrorMsg::MAP_KEY); +// BlockEntry +void Scanner::ScanBlockEntry() { + // we better be in the block context! + if (InFlowContext()) + throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY); - PushIndentTo(INPUT.column(), IndentMarker::MAP); - } + // can we put it here? + if (!m_simpleKeyAllowed) + throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY); - // can only put a simple key here if we're in block context - m_simpleKeyAllowed = InBlockContext(); + PushIndentTo(INPUT.column(), IndentMarker::SEQ); + m_simpleKeyAllowed = true; + m_canBeJSONFlow = false; - // eat - Mark mark = INPUT.mark(); - INPUT.eat(1); - m_tokens.push(Token(Token::KEY, mark)); - } + // eat + Mark mark = INPUT.mark(); + INPUT.eat(1); + m_tokens.push(Token(Token::BLOCK_ENTRY, mark)); +} - // Value - void Scanner::ScanValue() - { - // and check that simple key - bool isSimpleKey = VerifySimpleKey(); - m_canBeJSONFlow = false; - - if(isSimpleKey) { - // can't follow a simple key with another simple key (dunno why, though - it seems fine) - m_simpleKeyAllowed = false; - } else { - // handle values diffently in the block context (and manage indents) - if(InBlockContext()) { - if(!m_simpleKeyAllowed) - throw ParserException(INPUT.mark(), ErrorMsg::MAP_VALUE); +// Key +void Scanner::ScanKey() { + // handle keys diffently in the block context (and manage indents) + if (InBlockContext()) { + if (!m_simpleKeyAllowed) + throw ParserException(INPUT.mark(), ErrorMsg::MAP_KEY); - PushIndentTo(INPUT.column(), IndentMarker::MAP); - } + PushIndentTo(INPUT.column(), IndentMarker::MAP); + } - // can only put a simple key here if we're in block context - m_simpleKeyAllowed = InBlockContext(); - } + // can only put a simple key here if we're in block context + m_simpleKeyAllowed = InBlockContext(); - // eat - Mark mark = INPUT.mark(); - INPUT.eat(1); - m_tokens.push(Token(Token::VALUE, mark)); - } + // eat + Mark mark = INPUT.mark(); + INPUT.eat(1); + m_tokens.push(Token(Token::KEY, mark)); +} - // AnchorOrAlias - void Scanner::ScanAnchorOrAlias() - { - bool alias; - std::string name; +// Value +void Scanner::ScanValue() { + // and check that simple key + bool isSimpleKey = VerifySimpleKey(); + m_canBeJSONFlow = false; - // insert a potential simple key - InsertPotentialSimpleKey(); - m_simpleKeyAllowed = false; - m_canBeJSONFlow = false; + if (isSimpleKey) { + // can't follow a simple key with another simple key (dunno why, though - it + // seems fine) + m_simpleKeyAllowed = false; + } else { + // handle values diffently in the block context (and manage indents) + if (InBlockContext()) { + if (!m_simpleKeyAllowed) + throw ParserException(INPUT.mark(), ErrorMsg::MAP_VALUE); - // eat the indicator - Mark mark = INPUT.mark(); - char indicator = INPUT.get(); - alias = (indicator == Keys::Alias); + PushIndentTo(INPUT.column(), IndentMarker::MAP); + } - // now eat the content - while(INPUT && Exp::Anchor().Matches(INPUT)) - name += INPUT.get(); + // can only put a simple key here if we're in block context + m_simpleKeyAllowed = InBlockContext(); + } - // we need to have read SOMETHING! - if(name.empty()) - throw ParserException(INPUT.mark(), alias ? ErrorMsg::ALIAS_NOT_FOUND : ErrorMsg::ANCHOR_NOT_FOUND); + // eat + Mark mark = INPUT.mark(); + INPUT.eat(1); + m_tokens.push(Token(Token::VALUE, mark)); +} - // and needs to end correctly - if(INPUT && !Exp::AnchorEnd().Matches(INPUT)) - throw ParserException(INPUT.mark(), alias ? ErrorMsg::CHAR_IN_ALIAS : ErrorMsg::CHAR_IN_ANCHOR); +// AnchorOrAlias +void Scanner::ScanAnchorOrAlias() { + bool alias; + std::string name; - // and we're done - Token token(alias ? Token::ALIAS : Token::ANCHOR, mark); - token.value = name; - m_tokens.push(token); - } + // insert a potential simple key + InsertPotentialSimpleKey(); + m_simpleKeyAllowed = false; + m_canBeJSONFlow = false; - // Tag - void Scanner::ScanTag() - { - // insert a potential simple key - InsertPotentialSimpleKey(); - m_simpleKeyAllowed = false; - m_canBeJSONFlow = false; + // eat the indicator + Mark mark = INPUT.mark(); + char indicator = INPUT.get(); + alias = (indicator == Keys::Alias); - Token token(Token::TAG, INPUT.mark()); + // now eat the content + while (INPUT && Exp::Anchor().Matches(INPUT)) + name += INPUT.get(); - // eat the indicator - INPUT.get(); - - if(INPUT && INPUT.peek() == Keys::VerbatimTagStart){ - std::string tag = ScanVerbatimTag(INPUT); + // we need to have read SOMETHING! + if (name.empty()) + throw ParserException(INPUT.mark(), alias ? ErrorMsg::ALIAS_NOT_FOUND + : ErrorMsg::ANCHOR_NOT_FOUND); - token.value = tag; - token.data = Tag::VERBATIM; - } else { - bool canBeHandle; - token.value = ScanTagHandle(INPUT, canBeHandle); - if(!canBeHandle && token.value.empty()) - token.data = Tag::NON_SPECIFIC; - else if(token.value.empty()) - token.data = Tag::SECONDARY_HANDLE; - else - token.data = Tag::PRIMARY_HANDLE; - - // is there a suffix? - if(canBeHandle && INPUT.peek() == Keys::Tag) { - // eat the indicator - INPUT.get(); - token.params.push_back(ScanTagSuffix(INPUT)); - token.data = Tag::NAMED_HANDLE; - } - } + // and needs to end correctly + if (INPUT && !Exp::AnchorEnd().Matches(INPUT)) + throw ParserException(INPUT.mark(), alias ? ErrorMsg::CHAR_IN_ALIAS + : ErrorMsg::CHAR_IN_ANCHOR); - m_tokens.push(token); - } + // and we're done + Token token(alias ? Token::ALIAS : Token::ANCHOR, mark); + token.value = name; + m_tokens.push(token); +} - // PlainScalar - void Scanner::ScanPlainScalar() - { - std::string scalar; +// Tag +void Scanner::ScanTag() { + // insert a potential simple key + InsertPotentialSimpleKey(); + m_simpleKeyAllowed = false; + m_canBeJSONFlow = false; - // set up the scanning parameters - ScanScalarParams params; - params.end = (InFlowContext() ? Exp::EndScalarInFlow() : Exp::EndScalar()) || (Exp::BlankOrBreak() + Exp::Comment()); - params.eatEnd = false; - params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1); - params.fold = FOLD_FLOW; - params.eatLeadingWhitespace = true; - params.trimTrailingSpaces = true; - params.chomp = STRIP; - params.onDocIndicator = BREAK; - params.onTabInIndentation = THROW; + Token token(Token::TAG, INPUT.mark()); - // insert a potential simple key - InsertPotentialSimpleKey(); + // eat the indicator + INPUT.get(); - Mark mark = INPUT.mark(); - scalar = ScanScalar(INPUT, params); + if (INPUT && INPUT.peek() == Keys::VerbatimTagStart) { + std::string tag = ScanVerbatimTag(INPUT); - // can have a simple key only if we ended the scalar by starting a new line - m_simpleKeyAllowed = params.leadingSpaces; - m_canBeJSONFlow = false; + token.value = tag; + token.data = Tag::VERBATIM; + } else { + bool canBeHandle; + token.value = ScanTagHandle(INPUT, canBeHandle); + if (!canBeHandle && token.value.empty()) + token.data = Tag::NON_SPECIFIC; + else if (token.value.empty()) + token.data = Tag::SECONDARY_HANDLE; + else + token.data = Tag::PRIMARY_HANDLE; - // finally, check and see if we ended on an illegal character - //if(Exp::IllegalCharInScalar.Matches(INPUT)) - // throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_SCALAR); + // is there a suffix? + if (canBeHandle && INPUT.peek() == Keys::Tag) { + // eat the indicator + INPUT.get(); + token.params.push_back(ScanTagSuffix(INPUT)); + token.data = Tag::NAMED_HANDLE; + } + } - Token token(Token::PLAIN_SCALAR, mark); - token.value = scalar; - m_tokens.push(token); - } + m_tokens.push(token); +} - // QuotedScalar - void Scanner::ScanQuotedScalar() - { - std::string scalar; +// PlainScalar +void Scanner::ScanPlainScalar() { + std::string scalar; - // peek at single or double quote (don't eat because we need to preserve (for the time being) the input position) - char quote = INPUT.peek(); - bool single = (quote == '\''); + // set up the scanning parameters + ScanScalarParams params; + params.end = + (InFlowContext() ? &Exp::ScanScalarEndInFlow() : &Exp::ScanScalarEnd()); + params.eatEnd = false; + params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1); + params.fold = FOLD_FLOW; + params.eatLeadingWhitespace = true; + params.trimTrailingSpaces = true; + params.chomp = STRIP; + params.onDocIndicator = BREAK; + params.onTabInIndentation = THROW; - // setup the scanning parameters - ScanScalarParams params; - params.end = (single ? RegEx(quote) && !Exp::EscSingleQuote() : RegEx(quote)); - params.eatEnd = true; - params.escape = (single ? '\'' : '\\'); - params.indent = 0; - params.fold = FOLD_FLOW; - params.eatLeadingWhitespace = true; - params.trimTrailingSpaces = false; - params.chomp = CLIP; - params.onDocIndicator = THROW; + // insert a potential simple key + InsertPotentialSimpleKey(); - // insert a potential simple key - InsertPotentialSimpleKey(); + Mark mark = INPUT.mark(); + scalar = ScanScalar(INPUT, params); - Mark mark = INPUT.mark(); + // can have a simple key only if we ended the scalar by starting a new line + m_simpleKeyAllowed = params.leadingSpaces; + m_canBeJSONFlow = false; - // now eat that opening quote - INPUT.get(); - - // and scan - scalar = ScanScalar(INPUT, params); - m_simpleKeyAllowed = false; - m_canBeJSONFlow = true; + // finally, check and see if we ended on an illegal character + // if(Exp::IllegalCharInScalar.Matches(INPUT)) + // throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_SCALAR); - Token token(Token::NON_PLAIN_SCALAR, mark); - token.value = scalar; - m_tokens.push(token); - } + Token token(Token::PLAIN_SCALAR, mark); + token.value = scalar; + m_tokens.push(token); +} - // BlockScalarToken - // . These need a little extra processing beforehand. - // . We need to scan the line where the indicator is (this doesn't count as part of the scalar), - // and then we need to figure out what level of indentation we'll be using. - void Scanner::ScanBlockScalar() - { - std::string scalar; +// QuotedScalar +void Scanner::ScanQuotedScalar() { + std::string scalar; - ScanScalarParams params; - params.indent = 1; - params.detectIndent = true; + // peek at single or double quote (don't eat because we need to preserve (for + // the time being) the input position) + char quote = INPUT.peek(); + bool single = (quote == '\''); - // eat block indicator ('|' or '>') - Mark mark = INPUT.mark(); - char indicator = INPUT.get(); - params.fold = (indicator == Keys::FoldedScalar ? FOLD_BLOCK : DONT_FOLD); + // setup the scanning parameters + ScanScalarParams params; + RegEx end = (single ? RegEx(quote) && !Exp::EscSingleQuote() : RegEx(quote)); + params.end = &end; + params.eatEnd = true; + params.escape = (single ? '\'' : '\\'); + params.indent = 0; + params.fold = FOLD_FLOW; + params.eatLeadingWhitespace = true; + params.trimTrailingSpaces = false; + params.chomp = CLIP; + params.onDocIndicator = THROW; - // eat chomping/indentation indicators - params.chomp = CLIP; - int n = Exp::Chomp().Match(INPUT); - for(int i=0;i= 0) - params.indent += GetTopIndent(); +// BlockScalarToken +// . These need a little extra processing beforehand. +// . We need to scan the line where the indicator is (this doesn't count as part +// of the scalar), +// and then we need to figure out what level of indentation we'll be using. +void Scanner::ScanBlockScalar() { + std::string scalar; - params.eatLeadingWhitespace = false; - params.trimTrailingSpaces = false; - params.onTabInIndentation = THROW; + ScanScalarParams params; + params.indent = 1; + params.detectIndent = true; - scalar = ScanScalar(INPUT, params); + // eat block indicator ('|' or '>') + Mark mark = INPUT.mark(); + char indicator = INPUT.get(); + params.fold = (indicator == Keys::FoldedScalar ? FOLD_BLOCK : DONT_FOLD); - // simple keys always ok after block scalars (since we're gonna start a new line anyways) - m_simpleKeyAllowed = true; - m_canBeJSONFlow = false; + // eat chomping/indentation indicators + params.chomp = CLIP; + int n = Exp::Chomp().Match(INPUT); + for (int i = 0; i < n; i++) { + char ch = INPUT.get(); + if (ch == '+') + params.chomp = KEEP; + else if (ch == '-') + params.chomp = STRIP; + else if (Exp::Digit().Matches(ch)) { + if (ch == '0') + throw ParserException(INPUT.mark(), ErrorMsg::ZERO_INDENT_IN_BLOCK); - Token token(Token::NON_PLAIN_SCALAR, mark); - token.value = scalar; - m_tokens.push(token); - } + params.indent = ch - '0'; + params.detectIndent = false; + } + } + + // now eat whitespace + while (Exp::Blank().Matches(INPUT)) + INPUT.eat(1); + + // and comments to the end of the line + if (Exp::Comment().Matches(INPUT)) + while (INPUT && !Exp::Break().Matches(INPUT)) + INPUT.eat(1); + + // if it's not a line break, then we ran into a bad character inline + if (INPUT && !Exp::Break().Matches(INPUT)) + throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_BLOCK); + + // set the initial indentation + if (GetTopIndent() >= 0) + params.indent += GetTopIndent(); + + params.eatLeadingWhitespace = false; + params.trimTrailingSpaces = false; + params.onTabInIndentation = THROW; + + scalar = ScanScalar(INPUT, params); + + // simple keys always ok after block scalars (since we're gonna start a new + // line anyways) + m_simpleKeyAllowed = true; + m_canBeJSONFlow = false; + + Token token(Token::NON_PLAIN_SCALAR, mark); + token.value = scalar; + m_tokens.push(token); } +} diff --git a/src/yamlcpp/setting.h b/src/yamlcpp/setting.h --- a/src/yamlcpp/setting.h +++ b/src/yamlcpp/setting.h @@ -1,105 +1,95 @@ #ifndef SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include #include #include "yaml-cpp/noncopyable.h" -namespace YODA_YAML -{ - class SettingChangeBase; +namespace YODA_YAML { +class SettingChangeBase; - template - class Setting - { - public: - Setting(): m_value() {} +template +class Setting { + public: + Setting() : m_value() {} - const T get() const { return m_value; } - std::unique_ptr set(const T& value); - void restore(const Setting& oldSetting) { - m_value = oldSetting.get(); - } + const T get() const { return m_value; } + std::unique_ptr set(const T& value); + void restore(const Setting& oldSetting) { m_value = oldSetting.get(); } - private: - T m_value; - }; + private: + T m_value; +}; - class SettingChangeBase - { - public: - virtual ~SettingChangeBase() {} - virtual void pop() = 0; - }; +class SettingChangeBase { + public: + virtual ~SettingChangeBase() {} + virtual void pop() = 0; +}; - template - class SettingChange: public SettingChangeBase - { - public: - SettingChange(Setting *pSetting): m_pCurSetting(pSetting) { - // copy old setting to save its state - m_oldSetting = *pSetting; - } +template +class SettingChange : public SettingChangeBase { + public: + SettingChange(Setting* pSetting) : m_pCurSetting(pSetting) { + // copy old setting to save its state + m_oldSetting = *pSetting; + } - virtual void pop() { - m_pCurSetting->restore(m_oldSetting); - } + virtual void pop() { m_pCurSetting->restore(m_oldSetting); } - private: - Setting *m_pCurSetting; - Setting m_oldSetting; - }; + private: + Setting* m_pCurSetting; + Setting m_oldSetting; +}; - template - inline std::unique_ptr Setting::set(const T& value) { - std::unique_ptr pChange(new SettingChange (this)); - m_value = value; - return pChange; - } - - class SettingChanges: private noncopyable - { - public: - SettingChanges() {} - ~SettingChanges() { clear(); } - - void clear() { - restore(); - - for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it) - delete *it; - m_settingChanges.clear(); - } - - void restore() { - for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it) - (*it)->pop(); - } - - void push(std::unique_ptr pSettingChange) { - m_settingChanges.push_back(pSettingChange.release()); - } - - // like std::auto_ptr - assignment is transfer of ownership - SettingChanges& operator = (SettingChanges& rhs) { - if(this == &rhs) - return *this; - - clear(); - m_settingChanges = rhs.m_settingChanges; - rhs.m_settingChanges.clear(); - return *this; - } - - private: - typedef std::vector setting_changes; - setting_changes m_settingChanges; - }; +template +inline std::unique_ptr Setting::set(const T& value) { + std::unique_ptr pChange(new SettingChange(this)); + m_value = value; + return pChange; } -#endif // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +class SettingChanges : private noncopyable { + public: + SettingChanges() {} + ~SettingChanges() { clear(); } + + void clear() { + restore(); + m_settingChanges.clear(); + } + + void restore() { + for (setting_changes::const_iterator it = m_settingChanges.begin(); + it != m_settingChanges.end(); ++it) + (*it)->pop(); + } + + void push(std::unique_ptr pSettingChange) { + m_settingChanges.push_back(std::move(pSettingChange)); + } + + // like std::unique_ptr - assignment is transfer of ownership + SettingChanges& operator=(SettingChanges&& rhs) { + if (this == &rhs) + return *this; + + clear(); + std::swap(m_settingChanges, rhs.m_settingChanges); + + return *this; + } + + private: + typedef std::vector> setting_changes; + setting_changes m_settingChanges; +}; +} + +#endif // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/simplekey.cpp b/src/yamlcpp/simplekey.cpp --- a/src/yamlcpp/simplekey.cpp +++ b/src/yamlcpp/simplekey.cpp @@ -1,139 +1,128 @@ #include "scanner.h" #include "token.h" -#include "yaml-cpp/exceptions.h" -#include "exp.h" -namespace YODA_YAML -{ - Scanner::SimpleKey::SimpleKey(const Mark& mark_, int flowLevel_) - : mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0) - { - } +namespace YODA_YAML { +struct Mark; - void Scanner::SimpleKey::Validate() - { - // Note: pIndent will *not* be garbage here; - // we "garbage collect" them so we can - // always refer to them - if(pIndent) - pIndent->status = IndentMarker::VALID; - if(pMapStart) - pMapStart->status = Token::VALID; - if(pKey) - pKey->status = Token::VALID; - } +Scanner::SimpleKey::SimpleKey(const Mark& mark_, std::size_t flowLevel_) + : mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0) {} - void Scanner::SimpleKey::Invalidate() - { - if(pIndent) - pIndent->status = IndentMarker::INVALID; - if(pMapStart) - pMapStart->status = Token::INVALID; - if(pKey) - pKey->status = Token::INVALID; - } - - // CanInsertPotentialSimpleKey - bool Scanner::CanInsertPotentialSimpleKey() const - { - if(!m_simpleKeyAllowed) - return false; - - return !ExistsActiveSimpleKey(); - } - - // ExistsActiveSimpleKey - // . Returns true if there's a potential simple key at our flow level - // (there's allowed at most one per flow level, i.e., at the start of the flow start token) - bool Scanner::ExistsActiveSimpleKey() const - { - if(m_simpleKeys.empty()) - return false; - - const SimpleKey& key = m_simpleKeys.top(); - return key.flowLevel == GetFlowLevel(); - } - - // InsertPotentialSimpleKey - // . If we can, add a potential simple key to the queue, - // and save it on a stack. - void Scanner::InsertPotentialSimpleKey() - { - if(!CanInsertPotentialSimpleKey()) - return; - - SimpleKey key(INPUT.mark(), GetFlowLevel()); - - // first add a map start, if necessary - if(InBlockContext()) { - key.pIndent = PushIndentTo(INPUT.column(), IndentMarker::MAP); - if(key.pIndent) { - key.pIndent->status = IndentMarker::UNKNOWN; - key.pMapStart = key.pIndent->pStartToken; - key.pMapStart->status = Token::UNVERIFIED; - } - } - - // then add the (now unverified) key - m_tokens.push(Token(Token::KEY, INPUT.mark())); - key.pKey = &m_tokens.back(); - key.pKey->status = Token::UNVERIFIED; - - m_simpleKeys.push(key); - } - - // InvalidateSimpleKey - // . Automatically invalidate the simple key in our flow level - void Scanner::InvalidateSimpleKey() - { - if(m_simpleKeys.empty()) - return; - - // grab top key - SimpleKey& key = m_simpleKeys.top(); - if(key.flowLevel != GetFlowLevel()) - return; - - key.Invalidate(); - m_simpleKeys.pop(); - } - - // VerifySimpleKey - // . Determines whether the latest simple key to be added is valid, - // and if so, makes it valid. - bool Scanner::VerifySimpleKey() - { - if(m_simpleKeys.empty()) - return false; - - // grab top key - SimpleKey key = m_simpleKeys.top(); - - // only validate if we're in the correct flow level - if(key.flowLevel != GetFlowLevel()) - return false; - - m_simpleKeys.pop(); - - bool isValid = true; - - // needs to be less than 1024 characters and inline - if(INPUT.line() != key.mark.line || INPUT.pos() - key.mark.pos > 1024) - isValid = false; - - // invalidate key - if(isValid) - key.Validate(); - else - key.Invalidate(); - - return isValid; - } - - void Scanner::PopAllSimpleKeys() - { - while(!m_simpleKeys.empty()) - m_simpleKeys.pop(); - } +void Scanner::SimpleKey::Validate() { + // Note: pIndent will *not* be garbage here; + // we "garbage collect" them so we can + // always refer to them + if (pIndent) + pIndent->status = IndentMarker::VALID; + if (pMapStart) + pMapStart->status = Token::VALID; + if (pKey) + pKey->status = Token::VALID; } +void Scanner::SimpleKey::Invalidate() { + if (pIndent) + pIndent->status = IndentMarker::INVALID; + if (pMapStart) + pMapStart->status = Token::INVALID; + if (pKey) + pKey->status = Token::INVALID; +} + +// CanInsertPotentialSimpleKey +bool Scanner::CanInsertPotentialSimpleKey() const { + if (!m_simpleKeyAllowed) + return false; + + return !ExistsActiveSimpleKey(); +} + +// ExistsActiveSimpleKey +// . Returns true if there's a potential simple key at our flow level +// (there's allowed at most one per flow level, i.e., at the start of the flow +// start token) +bool Scanner::ExistsActiveSimpleKey() const { + if (m_simpleKeys.empty()) + return false; + + const SimpleKey& key = m_simpleKeys.top(); + return key.flowLevel == GetFlowLevel(); +} + +// InsertPotentialSimpleKey +// . If we can, add a potential simple key to the queue, +// and save it on a stack. +void Scanner::InsertPotentialSimpleKey() { + if (!CanInsertPotentialSimpleKey()) + return; + + SimpleKey key(INPUT.mark(), GetFlowLevel()); + + // first add a map start, if necessary + if (InBlockContext()) { + key.pIndent = PushIndentTo(INPUT.column(), IndentMarker::MAP); + if (key.pIndent) { + key.pIndent->status = IndentMarker::UNKNOWN; + key.pMapStart = key.pIndent->pStartToken; + key.pMapStart->status = Token::UNVERIFIED; + } + } + + // then add the (now unverified) key + m_tokens.push(Token(Token::KEY, INPUT.mark())); + key.pKey = &m_tokens.back(); + key.pKey->status = Token::UNVERIFIED; + + m_simpleKeys.push(key); +} + +// InvalidateSimpleKey +// . Automatically invalidate the simple key in our flow level +void Scanner::InvalidateSimpleKey() { + if (m_simpleKeys.empty()) + return; + + // grab top key + SimpleKey& key = m_simpleKeys.top(); + if (key.flowLevel != GetFlowLevel()) + return; + + key.Invalidate(); + m_simpleKeys.pop(); +} + +// VerifySimpleKey +// . Determines whether the latest simple key to be added is valid, +// and if so, makes it valid. +bool Scanner::VerifySimpleKey() { + if (m_simpleKeys.empty()) + return false; + + // grab top key + SimpleKey key = m_simpleKeys.top(); + + // only validate if we're in the correct flow level + if (key.flowLevel != GetFlowLevel()) + return false; + + m_simpleKeys.pop(); + + bool isValid = true; + + // needs to be less than 1024 characters and inline + if (INPUT.line() != key.mark.line || INPUT.pos() - key.mark.pos > 1024) + isValid = false; + + // invalidate key + if (isValid) + key.Validate(); + else + key.Invalidate(); + + return isValid; +} + +void Scanner::PopAllSimpleKeys() { + while (!m_simpleKeys.empty()) + m_simpleKeys.pop(); +} +} diff --git a/src/yamlcpp/singledocparser.cpp b/src/yamlcpp/singledocparser.cpp --- a/src/yamlcpp/singledocparser.cpp +++ b/src/yamlcpp/singledocparser.cpp @@ -1,381 +1,414 @@ +#include +#include +#include + +#include "collectionstack.h" // IWYU pragma: keep +#include "scanner.h" #include "singledocparser.h" -#include "collectionstack.h" -#include "directives.h" -#include "yaml-cpp/eventhandler.h" -#include "yaml-cpp/exceptions.h" -#include "scanner.h" #include "tag.h" #include "token.h" -#include -#include -#include +#include "yaml-cpp/emitterstyle.h" +#include "yaml-cpp/eventhandler.h" +#include "yaml-cpp/exceptions.h" // IWYU pragma: keep +#include "yaml-cpp/mark.h" +#include "yaml-cpp/null.h" -namespace YODA_YAML -{ - SingleDocParser::SingleDocParser(Scanner& scanner, const Directives& directives): m_scanner(scanner), m_directives(directives), m_pCollectionStack(new CollectionStack), m_curAnchor(0) - { - } +namespace YODA_YAML { +SingleDocParser::SingleDocParser(Scanner& scanner, const Directives& directives) + : m_scanner(scanner), + m_directives(directives), + m_pCollectionStack(new CollectionStack), + m_curAnchor(0) {} - SingleDocParser::~SingleDocParser() - { - } +SingleDocParser::~SingleDocParser() {} - // HandleDocument - // . Handles the next document - // . Throws a ParserException on error. - void SingleDocParser::HandleDocument(EventHandler& eventHandler) - { - assert(!m_scanner.empty()); // guaranteed that there are tokens - assert(!m_curAnchor); +// HandleDocument +// . Handles the next document +// . Throws a ParserException on error. +void SingleDocParser::HandleDocument(EventHandler& eventHandler) { + assert(!m_scanner.empty()); // guaranteed that there are tokens + assert(!m_curAnchor); - eventHandler.OnDocumentStart(m_scanner.peek().mark); - - // eat doc start - if(m_scanner.peek().type == Token::DOC_START) - m_scanner.pop(); - - // recurse! - HandleNode(eventHandler); - - eventHandler.OnDocumentEnd(); - - // and finally eat any doc ends we see - while(!m_scanner.empty() && m_scanner.peek().type == Token::DOC_END) - m_scanner.pop(); - } - - void SingleDocParser::HandleNode(EventHandler& eventHandler) - { - // an empty node *is* a possibility - if(m_scanner.empty()) { - eventHandler.OnNull(Mark::null(), NullAnchor); - return; - } - - // save location - Mark mark = m_scanner.peek().mark; - - // special case: a value node by itself must be a map, with no header - if(m_scanner.peek().type == Token::VALUE) { - eventHandler.OnMapStart(mark, "", NullAnchor); - HandleMap(eventHandler); - eventHandler.OnMapEnd(); - return; - } - - // special case: an alias node - if(m_scanner.peek().type == Token::ALIAS) { - eventHandler.OnAlias(mark, LookupAnchor(mark, m_scanner.peek().value)); - m_scanner.pop(); - return; - } - - std::string tag; - anchor_t anchor; - ParseProperties(tag, anchor); - - const Token& token = m_scanner.peek(); - - // add non-specific tags - if(tag.empty()) - tag = (token.type == Token::NON_PLAIN_SCALAR ? "!" : "?"); - - // now split based on what kind of node we should be - switch(token.type) { - case Token::PLAIN_SCALAR: - case Token::NON_PLAIN_SCALAR: - eventHandler.OnScalar(mark, tag, anchor, token.value); - m_scanner.pop(); - return; - case Token::FLOW_SEQ_START: - case Token::BLOCK_SEQ_START: - eventHandler.OnSequenceStart(mark, tag, anchor); - HandleSequence(eventHandler); - eventHandler.OnSequenceEnd(); - return; - case Token::FLOW_MAP_START: - case Token::BLOCK_MAP_START: - eventHandler.OnMapStart(mark, tag, anchor); - HandleMap(eventHandler); - eventHandler.OnMapEnd(); - return; - case Token::KEY: - // compact maps can only go in a flow sequence - if(m_pCollectionStack->GetCurCollectionType() == CollectionType::FlowSeq) { - eventHandler.OnMapStart(mark, tag, anchor); - HandleMap(eventHandler); - eventHandler.OnMapEnd(); - return; - } - break; - default: - break; - } - - if(tag == "?") - eventHandler.OnNull(mark, anchor); - else - eventHandler.OnScalar(mark, tag, anchor, ""); - } - - void SingleDocParser::HandleSequence(EventHandler& eventHandler) - { - // split based on start token - switch(m_scanner.peek().type) { - case Token::BLOCK_SEQ_START: HandleBlockSequence(eventHandler); break; - case Token::FLOW_SEQ_START: HandleFlowSequence(eventHandler); break; - default: break; - } - } - - void SingleDocParser::HandleBlockSequence(EventHandler& eventHandler) - { - // eat start token - m_scanner.pop(); - m_pCollectionStack->PushCollectionType(CollectionType::BlockSeq); - - while(1) { - if(m_scanner.empty()) - throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ); - - Token token = m_scanner.peek(); - if(token.type != Token::BLOCK_ENTRY && token.type != Token::BLOCK_SEQ_END) - throw ParserException(token.mark, ErrorMsg::END_OF_SEQ); - - m_scanner.pop(); - if(token.type == Token::BLOCK_SEQ_END) - break; - - // check for null - if(!m_scanner.empty()) { - const Token& token = m_scanner.peek(); - if(token.type == Token::BLOCK_ENTRY || token.type == Token::BLOCK_SEQ_END) { - eventHandler.OnNull(token.mark, NullAnchor); - continue; - } - } - - HandleNode(eventHandler); - } - - m_pCollectionStack->PopCollectionType(CollectionType::BlockSeq); - } - - void SingleDocParser::HandleFlowSequence(EventHandler& eventHandler) - { - // eat start token - m_scanner.pop(); - m_pCollectionStack->PushCollectionType(CollectionType::FlowSeq); - - while(1) { - if(m_scanner.empty()) - throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ_FLOW); - - // first check for end - if(m_scanner.peek().type == Token::FLOW_SEQ_END) { - m_scanner.pop(); - break; - } - - // then read the node - HandleNode(eventHandler); - - // now eat the separator (or could be a sequence end, which we ignore - but if it's neither, then it's a bad node) - Token& token = m_scanner.peek(); - if(token.type == Token::FLOW_ENTRY) - m_scanner.pop(); - else if(token.type != Token::FLOW_SEQ_END) - throw ParserException(token.mark, ErrorMsg::END_OF_SEQ_FLOW); - } - - m_pCollectionStack->PopCollectionType(CollectionType::FlowSeq); - } - - void SingleDocParser::HandleMap(EventHandler& eventHandler) - { - // split based on start token - switch(m_scanner.peek().type) { - case Token::BLOCK_MAP_START: HandleBlockMap(eventHandler); break; - case Token::FLOW_MAP_START: HandleFlowMap(eventHandler); break; - case Token::KEY: HandleCompactMap(eventHandler); break; - case Token::VALUE: HandleCompactMapWithNoKey(eventHandler); break; - default: break; - } - } - - void SingleDocParser::HandleBlockMap(EventHandler& eventHandler) - { - // eat start token - m_scanner.pop(); - m_pCollectionStack->PushCollectionType(CollectionType::BlockMap); - - while(1) { - if(m_scanner.empty()) - throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP); - - Token token = m_scanner.peek(); - if(token.type != Token::KEY && token.type != Token::VALUE && token.type != Token::BLOCK_MAP_END) - throw ParserException(token.mark, ErrorMsg::END_OF_MAP); - - if(token.type == Token::BLOCK_MAP_END) { - m_scanner.pop(); - break; - } - - // grab key (if non-null) - if(token.type == Token::KEY) { - m_scanner.pop(); - HandleNode(eventHandler); - } else { - eventHandler.OnNull(token.mark, NullAnchor); - } - - // now grab value (optional) - if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) { - m_scanner.pop(); - HandleNode(eventHandler); - } else { - eventHandler.OnNull(token.mark, NullAnchor); - } - } - - m_pCollectionStack->PopCollectionType(CollectionType::BlockMap); - } - - void SingleDocParser::HandleFlowMap(EventHandler& eventHandler) - { - // eat start token - m_scanner.pop(); - m_pCollectionStack->PushCollectionType(CollectionType::FlowMap); - - while(1) { - if(m_scanner.empty()) - throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP_FLOW); - - Token& token = m_scanner.peek(); - // first check for end - if(token.type == Token::FLOW_MAP_END) { - m_scanner.pop(); - break; - } - - // grab key (if non-null) - if(token.type == Token::KEY) { - m_scanner.pop(); - HandleNode(eventHandler); - } else { - eventHandler.OnNull(token.mark, NullAnchor); - } - - // now grab value (optional) - if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) { - m_scanner.pop(); - HandleNode(eventHandler); - } else { - eventHandler.OnNull(token.mark, NullAnchor); - } - - // now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node) - Token& nextToken = m_scanner.peek(); - if(nextToken.type == Token::FLOW_ENTRY) - m_scanner.pop(); - else if(nextToken.type != Token::FLOW_MAP_END) - throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW); - } - - m_pCollectionStack->PopCollectionType(CollectionType::FlowMap); - } - - // . Single "key: value" pair in a flow sequence - void SingleDocParser::HandleCompactMap(EventHandler& eventHandler) - { - m_pCollectionStack->PushCollectionType(CollectionType::CompactMap); - - // grab key - Mark mark = m_scanner.peek().mark; - m_scanner.pop(); - HandleNode(eventHandler); - - // now grab value (optional) - if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) { - m_scanner.pop(); - HandleNode(eventHandler); - } else { - eventHandler.OnNull(mark, NullAnchor); - } - - m_pCollectionStack->PopCollectionType(CollectionType::CompactMap); - } - - // . Single ": value" pair in a flow sequence - void SingleDocParser::HandleCompactMapWithNoKey(EventHandler& eventHandler) - { - m_pCollectionStack->PushCollectionType(CollectionType::CompactMap); - - // null key - eventHandler.OnNull(m_scanner.peek().mark, NullAnchor); - - // grab value - m_scanner.pop(); - HandleNode(eventHandler); - - m_pCollectionStack->PopCollectionType(CollectionType::CompactMap); - } - - // ParseProperties - // . Grabs any tag or anchor tokens and deals with them. - void SingleDocParser::ParseProperties(std::string& tag, anchor_t& anchor) - { - tag.clear(); - anchor = NullAnchor; - - while(1) { - if(m_scanner.empty()) - return; - - switch(m_scanner.peek().type) { - case Token::TAG: ParseTag(tag); break; - case Token::ANCHOR: ParseAnchor(anchor); break; - default: return; - } - } - } - - void SingleDocParser::ParseTag(std::string& tag) - { - Token& token = m_scanner.peek(); - if(!tag.empty()) - throw ParserException(token.mark, ErrorMsg::MULTIPLE_TAGS); - - Tag tagInfo(token); - tag = tagInfo.Translate(m_directives); - m_scanner.pop(); - } - - void SingleDocParser::ParseAnchor(anchor_t& anchor) - { - Token& token = m_scanner.peek(); - if(anchor) - throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS); - - anchor = RegisterAnchor(token.value); - m_scanner.pop(); - } - - anchor_t SingleDocParser::RegisterAnchor(const std::string& name) - { - if(name.empty()) - return NullAnchor; - - return m_anchors[name] = ++m_curAnchor; - } - - anchor_t SingleDocParser::LookupAnchor(const Mark& mark, const std::string& name) const - { - Anchors::const_iterator it = m_anchors.find(name); - if(it == m_anchors.end()) - throw ParserException(mark, ErrorMsg::UNKNOWN_ANCHOR); - - return it->second; - } + eventHandler.OnDocumentStart(m_scanner.peek().mark); + + // eat doc start + if (m_scanner.peek().type == Token::DOC_START) + m_scanner.pop(); + + // recurse! + HandleNode(eventHandler); + + eventHandler.OnDocumentEnd(); + + // and finally eat any doc ends we see + while (!m_scanner.empty() && m_scanner.peek().type == Token::DOC_END) + m_scanner.pop(); } + +void SingleDocParser::HandleNode(EventHandler& eventHandler) { + // an empty node *is* a possibility + if (m_scanner.empty()) { + eventHandler.OnNull(m_scanner.mark(), NullAnchor); + return; + } + + // save location + Mark mark = m_scanner.peek().mark; + + // special case: a value node by itself must be a map, with no header + if (m_scanner.peek().type == Token::VALUE) { + eventHandler.OnMapStart(mark, "?", NullAnchor, EmitterStyle::Default); + HandleMap(eventHandler); + eventHandler.OnMapEnd(); + return; + } + + // special case: an alias node + if (m_scanner.peek().type == Token::ALIAS) { + eventHandler.OnAlias(mark, LookupAnchor(mark, m_scanner.peek().value)); + m_scanner.pop(); + return; + } + + std::string tag; + anchor_t anchor; + ParseProperties(tag, anchor); + + const Token& token = m_scanner.peek(); + + if (token.type == Token::PLAIN_SCALAR && IsNullString(token.value)) { + eventHandler.OnNull(mark, anchor); + m_scanner.pop(); + return; + } + + // add non-specific tags + if (tag.empty()) + tag = (token.type == Token::NON_PLAIN_SCALAR ? "!" : "?"); + + // now split based on what kind of node we should be + switch (token.type) { + case Token::PLAIN_SCALAR: + case Token::NON_PLAIN_SCALAR: + eventHandler.OnScalar(mark, tag, anchor, token.value); + m_scanner.pop(); + return; + case Token::FLOW_SEQ_START: + eventHandler.OnSequenceStart(mark, tag, anchor, EmitterStyle::Flow); + HandleSequence(eventHandler); + eventHandler.OnSequenceEnd(); + return; + case Token::BLOCK_SEQ_START: + eventHandler.OnSequenceStart(mark, tag, anchor, EmitterStyle::Block); + HandleSequence(eventHandler); + eventHandler.OnSequenceEnd(); + return; + case Token::FLOW_MAP_START: + eventHandler.OnMapStart(mark, tag, anchor, EmitterStyle::Flow); + HandleMap(eventHandler); + eventHandler.OnMapEnd(); + return; + case Token::BLOCK_MAP_START: + eventHandler.OnMapStart(mark, tag, anchor, EmitterStyle::Block); + HandleMap(eventHandler); + eventHandler.OnMapEnd(); + return; + case Token::KEY: + // compact maps can only go in a flow sequence + if (m_pCollectionStack->GetCurCollectionType() == + CollectionType::FlowSeq) { + eventHandler.OnMapStart(mark, tag, anchor, EmitterStyle::Flow); + HandleMap(eventHandler); + eventHandler.OnMapEnd(); + return; + } + break; + default: + break; + } + + if (tag == "?") + eventHandler.OnNull(mark, anchor); + else + eventHandler.OnScalar(mark, tag, anchor, ""); +} + +void SingleDocParser::HandleSequence(EventHandler& eventHandler) { + // split based on start token + switch (m_scanner.peek().type) { + case Token::BLOCK_SEQ_START: + HandleBlockSequence(eventHandler); + break; + case Token::FLOW_SEQ_START: + HandleFlowSequence(eventHandler); + break; + default: + break; + } +} + +void SingleDocParser::HandleBlockSequence(EventHandler& eventHandler) { + // eat start token + m_scanner.pop(); + m_pCollectionStack->PushCollectionType(CollectionType::BlockSeq); + + while (1) { + if (m_scanner.empty()) + throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_SEQ); + + Token token = m_scanner.peek(); + if (token.type != Token::BLOCK_ENTRY && token.type != Token::BLOCK_SEQ_END) + throw ParserException(token.mark, ErrorMsg::END_OF_SEQ); + + m_scanner.pop(); + if (token.type == Token::BLOCK_SEQ_END) + break; + + // check for null + if (!m_scanner.empty()) { + const Token& token = m_scanner.peek(); + if (token.type == Token::BLOCK_ENTRY || + token.type == Token::BLOCK_SEQ_END) { + eventHandler.OnNull(token.mark, NullAnchor); + continue; + } + } + + HandleNode(eventHandler); + } + + m_pCollectionStack->PopCollectionType(CollectionType::BlockSeq); +} + +void SingleDocParser::HandleFlowSequence(EventHandler& eventHandler) { + // eat start token + m_scanner.pop(); + m_pCollectionStack->PushCollectionType(CollectionType::FlowSeq); + + while (1) { + if (m_scanner.empty()) + throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_SEQ_FLOW); + + // first check for end + if (m_scanner.peek().type == Token::FLOW_SEQ_END) { + m_scanner.pop(); + break; + } + + // then read the node + HandleNode(eventHandler); + + if (m_scanner.empty()) + throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_SEQ_FLOW); + + // now eat the separator (or could be a sequence end, which we ignore - but + // if it's neither, then it's a bad node) + Token& token = m_scanner.peek(); + if (token.type == Token::FLOW_ENTRY) + m_scanner.pop(); + else if (token.type != Token::FLOW_SEQ_END) + throw ParserException(token.mark, ErrorMsg::END_OF_SEQ_FLOW); + } + + m_pCollectionStack->PopCollectionType(CollectionType::FlowSeq); +} + +void SingleDocParser::HandleMap(EventHandler& eventHandler) { + // split based on start token + switch (m_scanner.peek().type) { + case Token::BLOCK_MAP_START: + HandleBlockMap(eventHandler); + break; + case Token::FLOW_MAP_START: + HandleFlowMap(eventHandler); + break; + case Token::KEY: + HandleCompactMap(eventHandler); + break; + case Token::VALUE: + HandleCompactMapWithNoKey(eventHandler); + break; + default: + break; + } +} + +void SingleDocParser::HandleBlockMap(EventHandler& eventHandler) { + // eat start token + m_scanner.pop(); + m_pCollectionStack->PushCollectionType(CollectionType::BlockMap); + + while (1) { + if (m_scanner.empty()) + throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_MAP); + + Token token = m_scanner.peek(); + if (token.type != Token::KEY && token.type != Token::VALUE && + token.type != Token::BLOCK_MAP_END) + throw ParserException(token.mark, ErrorMsg::END_OF_MAP); + + if (token.type == Token::BLOCK_MAP_END) { + m_scanner.pop(); + break; + } + + // grab key (if non-null) + if (token.type == Token::KEY) { + m_scanner.pop(); + HandleNode(eventHandler); + } else { + eventHandler.OnNull(token.mark, NullAnchor); + } + + // now grab value (optional) + if (!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) { + m_scanner.pop(); + HandleNode(eventHandler); + } else { + eventHandler.OnNull(token.mark, NullAnchor); + } + } + + m_pCollectionStack->PopCollectionType(CollectionType::BlockMap); +} + +void SingleDocParser::HandleFlowMap(EventHandler& eventHandler) { + // eat start token + m_scanner.pop(); + m_pCollectionStack->PushCollectionType(CollectionType::FlowMap); + + while (1) { + if (m_scanner.empty()) + throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_MAP_FLOW); + + Token& token = m_scanner.peek(); + const Mark mark = token.mark; + // first check for end + if (token.type == Token::FLOW_MAP_END) { + m_scanner.pop(); + break; + } + + // grab key (if non-null) + if (token.type == Token::KEY) { + m_scanner.pop(); + HandleNode(eventHandler); + } else { + eventHandler.OnNull(mark, NullAnchor); + } + + // now grab value (optional) + if (!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) { + m_scanner.pop(); + HandleNode(eventHandler); + } else { + eventHandler.OnNull(mark, NullAnchor); + } + + if (m_scanner.empty()) + throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_MAP_FLOW); + + // now eat the separator (or could be a map end, which we ignore - but if + // it's neither, then it's a bad node) + Token& nextToken = m_scanner.peek(); + if (nextToken.type == Token::FLOW_ENTRY) + m_scanner.pop(); + else if (nextToken.type != Token::FLOW_MAP_END) + throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW); + } + + m_pCollectionStack->PopCollectionType(CollectionType::FlowMap); +} + +// . Single "key: value" pair in a flow sequence +void SingleDocParser::HandleCompactMap(EventHandler& eventHandler) { + m_pCollectionStack->PushCollectionType(CollectionType::CompactMap); + + // grab key + Mark mark = m_scanner.peek().mark; + m_scanner.pop(); + HandleNode(eventHandler); + + // now grab value (optional) + if (!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) { + m_scanner.pop(); + HandleNode(eventHandler); + } else { + eventHandler.OnNull(mark, NullAnchor); + } + + m_pCollectionStack->PopCollectionType(CollectionType::CompactMap); +} + +// . Single ": value" pair in a flow sequence +void SingleDocParser::HandleCompactMapWithNoKey(EventHandler& eventHandler) { + m_pCollectionStack->PushCollectionType(CollectionType::CompactMap); + + // null key + eventHandler.OnNull(m_scanner.peek().mark, NullAnchor); + + // grab value + m_scanner.pop(); + HandleNode(eventHandler); + + m_pCollectionStack->PopCollectionType(CollectionType::CompactMap); +} + +// ParseProperties +// . Grabs any tag or anchor tokens and deals with them. +void SingleDocParser::ParseProperties(std::string& tag, anchor_t& anchor) { + tag.clear(); + anchor = NullAnchor; + + while (1) { + if (m_scanner.empty()) + return; + + switch (m_scanner.peek().type) { + case Token::TAG: + ParseTag(tag); + break; + case Token::ANCHOR: + ParseAnchor(anchor); + break; + default: + return; + } + } +} + +void SingleDocParser::ParseTag(std::string& tag) { + Token& token = m_scanner.peek(); + if (!tag.empty()) + throw ParserException(token.mark, ErrorMsg::MULTIPLE_TAGS); + + Tag tagInfo(token); + tag = tagInfo.Translate(m_directives); + m_scanner.pop(); +} + +void SingleDocParser::ParseAnchor(anchor_t& anchor) { + Token& token = m_scanner.peek(); + if (anchor) + throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS); + + anchor = RegisterAnchor(token.value); + m_scanner.pop(); +} + +anchor_t SingleDocParser::RegisterAnchor(const std::string& name) { + if (name.empty()) + return NullAnchor; + + return m_anchors[name] = ++m_curAnchor; +} + +anchor_t SingleDocParser::LookupAnchor(const Mark& mark, + const std::string& name) const { + Anchors::const_iterator it = m_anchors.find(name); + if (it == m_anchors.end()) + throw ParserException(mark, ErrorMsg::UNKNOWN_ANCHOR); + + return it->second; +} +} diff --git a/src/yamlcpp/singledocparser.h b/src/yamlcpp/singledocparser.h --- a/src/yamlcpp/singledocparser.h +++ b/src/yamlcpp/singledocparser.h @@ -1,65 +1,65 @@ #ifndef SINGLEDOCPARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define SINGLEDOCPARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include +#include +#include #include "yaml-cpp/anchor.h" #include "yaml-cpp/noncopyable.h" -#include -#include -#include -namespace YODA_YAML -{ - struct Directives; - struct Mark; - struct Token; - class CollectionStack; - class EventHandler; - class Node; - class Scanner; +namespace YODA_YAML { +class CollectionStack; +class EventHandler; +class Node; +class Scanner; +struct Directives; +struct Mark; +struct Token; - class SingleDocParser: private noncopyable - { - public: - SingleDocParser(Scanner& scanner, const Directives& directives); - ~SingleDocParser(); +class SingleDocParser : private noncopyable { + public: + SingleDocParser(Scanner& scanner, const Directives& directives); + ~SingleDocParser(); - void HandleDocument(EventHandler& eventHandler); + void HandleDocument(EventHandler& eventHandler); - private: - void HandleNode(EventHandler& eventHandler); + private: + void HandleNode(EventHandler& eventHandler); - void HandleSequence(EventHandler& eventHandler); - void HandleBlockSequence(EventHandler& eventHandler); - void HandleFlowSequence(EventHandler& eventHandler); + void HandleSequence(EventHandler& eventHandler); + void HandleBlockSequence(EventHandler& eventHandler); + void HandleFlowSequence(EventHandler& eventHandler); - void HandleMap(EventHandler& eventHandler); - void HandleBlockMap(EventHandler& eventHandler); - void HandleFlowMap(EventHandler& eventHandler); - void HandleCompactMap(EventHandler& eventHandler); - void HandleCompactMapWithNoKey(EventHandler& eventHandler); + void HandleMap(EventHandler& eventHandler); + void HandleBlockMap(EventHandler& eventHandler); + void HandleFlowMap(EventHandler& eventHandler); + void HandleCompactMap(EventHandler& eventHandler); + void HandleCompactMapWithNoKey(EventHandler& eventHandler); - void ParseProperties(std::string& tag, anchor_t& anchor); - void ParseTag(std::string& tag); - void ParseAnchor(anchor_t& anchor); + void ParseProperties(std::string& tag, anchor_t& anchor); + void ParseTag(std::string& tag); + void ParseAnchor(anchor_t& anchor); - anchor_t RegisterAnchor(const std::string& name); - anchor_t LookupAnchor(const Mark& mark, const std::string& name) const; + anchor_t RegisterAnchor(const std::string& name); + anchor_t LookupAnchor(const Mark& mark, const std::string& name) const; - private: - Scanner& m_scanner; - const Directives& m_directives; - std::unique_ptr m_pCollectionStack; + private: + Scanner& m_scanner; + const Directives& m_directives; + std::unique_ptr m_pCollectionStack; - typedef std::map Anchors; - Anchors m_anchors; + typedef std::map Anchors; + Anchors m_anchors; - anchor_t m_curAnchor; - }; + anchor_t m_curAnchor; +}; } -#endif // SINGLEDOCPARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // SINGLEDOCPARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/stream.cpp b/src/yamlcpp/stream.cpp --- a/src/yamlcpp/stream.cpp +++ b/src/yamlcpp/stream.cpp @@ -1,448 +1,448 @@ +#include + #include "stream.h" -#include -#include "exp.h" #ifndef YAML_PREFETCH_SIZE #define YAML_PREFETCH_SIZE 2048 #endif -#define S_ARRAY_SIZE( A ) (sizeof(A)/sizeof(*(A))) -#define S_ARRAY_END( A ) ((A) + S_ARRAY_SIZE(A)) +#define S_ARRAY_SIZE(A) (sizeof(A) / sizeof(*(A))) +#define S_ARRAY_END(A) ((A) + S_ARRAY_SIZE(A)) #define CP_REPLACEMENT_CHARACTER (0xFFFD) -namespace YODA_YAML -{ - enum UtfIntroState { - uis_start, - uis_utfbe_b1, - uis_utf32be_b2, - uis_utf32be_bom3, - uis_utf32be, - uis_utf16be, - uis_utf16be_bom1, - uis_utfle_bom1, - uis_utf16le_bom2, - uis_utf32le_bom3, - uis_utf16le, - uis_utf32le, - uis_utf8_imp, - uis_utf16le_imp, - uis_utf32le_imp3, - uis_utf8_bom1, - uis_utf8_bom2, - uis_utf8, - uis_error - }; +namespace YODA_YAML { +enum UtfIntroState { + uis_start, + uis_utfbe_b1, + uis_utf32be_b2, + uis_utf32be_bom3, + uis_utf32be, + uis_utf16be, + uis_utf16be_bom1, + uis_utfle_bom1, + uis_utf16le_bom2, + uis_utf32le_bom3, + uis_utf16le, + uis_utf32le, + uis_utf8_imp, + uis_utf16le_imp, + uis_utf32le_imp3, + uis_utf8_bom1, + uis_utf8_bom2, + uis_utf8, + uis_error +}; - enum UtfIntroCharType { - uict00, - uictBB, - uictBF, - uictEF, - uictFE, - uictFF, - uictAscii, - uictOther, - uictMax - }; +enum UtfIntroCharType { + uict00, + uictBB, + uictBF, + uictEF, + uictFE, + uictFF, + uictAscii, + uictOther, + uictMax +}; - static bool s_introFinalState[] = { - false, //uis_start - false, //uis_utfbe_b1 - false, //uis_utf32be_b2 - false, //uis_utf32be_bom3 - true, //uis_utf32be - true, //uis_utf16be - false, //uis_utf16be_bom1 - false, //uis_utfle_bom1 - false, //uis_utf16le_bom2 - false, //uis_utf32le_bom3 - true, //uis_utf16le - true, //uis_utf32le - false, //uis_utf8_imp - false, //uis_utf16le_imp - false, //uis_utf32le_imp3 - false, //uis_utf8_bom1 - false, //uis_utf8_bom2 - true, //uis_utf8 - true, //uis_error - }; +static bool s_introFinalState[] = { + false, // uis_start + false, // uis_utfbe_b1 + false, // uis_utf32be_b2 + false, // uis_utf32be_bom3 + true, // uis_utf32be + true, // uis_utf16be + false, // uis_utf16be_bom1 + false, // uis_utfle_bom1 + false, // uis_utf16le_bom2 + false, // uis_utf32le_bom3 + true, // uis_utf16le + true, // uis_utf32le + false, // uis_utf8_imp + false, // uis_utf16le_imp + false, // uis_utf32le_imp3 + false, // uis_utf8_bom1 + false, // uis_utf8_bom2 + true, // uis_utf8 + true, // uis_error +}; - static UtfIntroState s_introTransitions[][uictMax] = { - // uict00, uictBB, uictBF, uictEF, uictFE, uictFF, uictAscii, uictOther - {uis_utfbe_b1, uis_utf8, uis_utf8, uis_utf8_bom1, uis_utf16be_bom1, uis_utfle_bom1, uis_utf8_imp, uis_utf8}, - {uis_utf32be_b2, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16be, uis_utf8}, - {uis_utf32be, uis_utf8, uis_utf8, uis_utf8, uis_utf32be_bom3, uis_utf8, uis_utf8, uis_utf8}, - {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf32be, uis_utf8, uis_utf8}, - {uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be}, - {uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be}, - {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16be, uis_utf8, uis_utf8}, - {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16le_bom2, uis_utf8, uis_utf8, uis_utf8}, - {uis_utf32le_bom3, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le}, - {uis_utf32le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le}, - {uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le}, - {uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le}, - {uis_utf16le_imp, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8}, - {uis_utf32le_imp3, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le}, - {uis_utf32le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le}, - {uis_utf8, uis_utf8_bom2, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8}, - {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8}, - {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8}, - }; +static UtfIntroState s_introTransitions[][uictMax] = { + // uict00, uictBB, uictBF, uictEF, + // uictFE, uictFF, uictAscii, uictOther + {uis_utfbe_b1, uis_utf8, uis_utf8, uis_utf8_bom1, uis_utf16be_bom1, + uis_utfle_bom1, uis_utf8_imp, uis_utf8}, + {uis_utf32be_b2, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, + uis_utf16be, uis_utf8}, + {uis_utf32be, uis_utf8, uis_utf8, uis_utf8, uis_utf32be_bom3, uis_utf8, + uis_utf8, uis_utf8}, + {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf32be, uis_utf8, + uis_utf8}, + {uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, + uis_utf32be, uis_utf32be, uis_utf32be}, + {uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, + uis_utf16be, uis_utf16be, uis_utf16be}, + {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16be, uis_utf8, + uis_utf8}, + {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16le_bom2, uis_utf8, + uis_utf8, uis_utf8}, + {uis_utf32le_bom3, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, + uis_utf16le, uis_utf16le, uis_utf16le}, + {uis_utf32le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, + uis_utf16le, uis_utf16le, uis_utf16le}, + {uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, + uis_utf16le, uis_utf16le, uis_utf16le}, + {uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, + uis_utf32le, uis_utf32le, uis_utf32le}, + {uis_utf16le_imp, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, + uis_utf8, uis_utf8}, + {uis_utf32le_imp3, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, + uis_utf16le, uis_utf16le, uis_utf16le}, + {uis_utf32le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, + uis_utf16le, uis_utf16le, uis_utf16le}, + {uis_utf8, uis_utf8_bom2, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, + uis_utf8}, + {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, + uis_utf8}, + {uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, + uis_utf8}, +}; - static char s_introUngetCount[][uictMax] = { - // uict00, uictBB, uictBF, uictEF, uictFE, uictFF, uictAscii, uictOther - {0, 1, 1, 0, 0, 0, 0, 1}, - {0, 2, 2, 2, 2, 2, 2, 2}, - {3, 3, 3, 3, 0, 3, 3, 3}, - {4, 4, 4, 4, 4, 0, 4, 4}, - {1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1}, - {2, 2, 2, 2, 2, 0, 2, 2}, - {2, 2, 2, 2, 0, 2, 2, 2}, - {0, 1, 1, 1, 1, 1, 1, 1}, - {0, 2, 2, 2, 2, 2, 2, 2}, - {1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1}, - {0, 2, 2, 2, 2, 2, 2, 2}, - {0, 3, 3, 3, 3, 3, 3, 3}, - {4, 4, 4, 4, 4, 4, 4, 4}, - {2, 0, 2, 2, 2, 2, 2, 2}, - {3, 3, 0, 3, 3, 3, 3, 3}, - {1, 1, 1, 1, 1, 1, 1, 1}, - }; +static char s_introUngetCount[][uictMax] = { + // uict00, uictBB, uictBF, uictEF, uictFE, uictFF, uictAscii, uictOther + {0, 1, 1, 0, 0, 0, 0, 1}, + {0, 2, 2, 2, 2, 2, 2, 2}, + {3, 3, 3, 3, 0, 3, 3, 3}, + {4, 4, 4, 4, 4, 0, 4, 4}, + {1, 1, 1, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, + {2, 2, 2, 2, 2, 0, 2, 2}, + {2, 2, 2, 2, 0, 2, 2, 2}, + {0, 1, 1, 1, 1, 1, 1, 1}, + {0, 2, 2, 2, 2, 2, 2, 2}, + {1, 1, 1, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, + {0, 2, 2, 2, 2, 2, 2, 2}, + {0, 3, 3, 3, 3, 3, 3, 3}, + {4, 4, 4, 4, 4, 4, 4, 4}, + {2, 0, 2, 2, 2, 2, 2, 2}, + {3, 3, 0, 3, 3, 3, 3, 3}, + {1, 1, 1, 1, 1, 1, 1, 1}, +}; - inline UtfIntroCharType IntroCharTypeOf(std::istream::int_type ch) - { - if (std::istream::traits_type::eof() == ch) { - return uictOther; - } +inline UtfIntroCharType IntroCharTypeOf(std::istream::int_type ch) { + if (std::istream::traits_type::eof() == ch) { + return uictOther; + } - switch (ch) { - case 0: return uict00; - case 0xBB: return uictBB; - case 0xBF: return uictBF; - case 0xEF: return uictEF; - case 0xFE: return uictFE; - case 0xFF: return uictFF; - } + switch (ch) { + case 0: + return uict00; + case 0xBB: + return uictBB; + case 0xBF: + return uictBF; + case 0xEF: + return uictEF; + case 0xFE: + return uictFE; + case 0xFF: + return uictFF; + } - if ((ch > 0) && (ch < 0xFF)) { - return uictAscii; - } + if ((ch > 0) && (ch < 0xFF)) { + return uictAscii; + } - return uictOther; - } + return uictOther; +} - inline char Utf8Adjust(unsigned long ch, unsigned char lead_bits, unsigned char rshift) - { - const unsigned char header = ((1 << lead_bits) - 1) << (8 - lead_bits); - const unsigned char mask = (0xFF >> (lead_bits + 1)); - return static_cast(static_cast( - header | ((ch >> rshift) & mask) - )); - } +inline char Utf8Adjust(unsigned long ch, unsigned char lead_bits, + unsigned char rshift) { + const unsigned char header = ((1 << lead_bits) - 1) << (8 - lead_bits); + const unsigned char mask = (0xFF >> (lead_bits + 1)); + return static_cast( + static_cast(header | ((ch >> rshift) & mask))); +} - inline void QueueUnicodeCodepoint(std::deque& q, unsigned long ch) - { - // We are not allowed to queue the Stream::eof() codepoint, so - // replace it with CP_REPLACEMENT_CHARACTER - if (static_cast(Stream::eof()) == ch) - { - ch = CP_REPLACEMENT_CHARACTER; - } +inline void QueueUnicodeCodepoint(std::deque& q, unsigned long ch) { + // We are not allowed to queue the Stream::eof() codepoint, so + // replace it with CP_REPLACEMENT_CHARACTER + if (static_cast(Stream::eof()) == ch) { + ch = CP_REPLACEMENT_CHARACTER; + } - if (ch < 0x80) - { - q.push_back(Utf8Adjust(ch, 0, 0)); - } - else if (ch < 0x800) - { - q.push_back(Utf8Adjust(ch, 2, 6)); - q.push_back(Utf8Adjust(ch, 1, 0)); - } - else if (ch < 0x10000) - { - q.push_back(Utf8Adjust(ch, 3, 12)); - q.push_back(Utf8Adjust(ch, 1, 6)); - q.push_back(Utf8Adjust(ch, 1, 0)); - } - else - { - q.push_back(Utf8Adjust(ch, 4, 18)); - q.push_back(Utf8Adjust(ch, 1, 12)); - q.push_back(Utf8Adjust(ch, 1, 6)); - q.push_back(Utf8Adjust(ch, 1, 0)); - } - } + if (ch < 0x80) { + q.push_back(Utf8Adjust(ch, 0, 0)); + } else if (ch < 0x800) { + q.push_back(Utf8Adjust(ch, 2, 6)); + q.push_back(Utf8Adjust(ch, 1, 0)); + } else if (ch < 0x10000) { + q.push_back(Utf8Adjust(ch, 3, 12)); + q.push_back(Utf8Adjust(ch, 1, 6)); + q.push_back(Utf8Adjust(ch, 1, 0)); + } else { + q.push_back(Utf8Adjust(ch, 4, 18)); + q.push_back(Utf8Adjust(ch, 1, 12)); + q.push_back(Utf8Adjust(ch, 1, 6)); + q.push_back(Utf8Adjust(ch, 1, 0)); + } +} - Stream::Stream(std::istream& input) - : m_input(input), - m_pPrefetched(new unsigned char[YAML_PREFETCH_SIZE]), - m_nPrefetchedAvailable(0), m_nPrefetchedUsed(0) - { - typedef std::istream::traits_type char_traits; +Stream::Stream(std::istream& input) + : m_input(input), + m_pPrefetched(new unsigned char[YAML_PREFETCH_SIZE]), + m_nPrefetchedAvailable(0), + m_nPrefetchedUsed(0) { + typedef std::istream::traits_type char_traits; - if(!input) - return; + if (!input) + return; - // Determine (or guess) the character-set by reading the BOM, if any. See - // the YAML specification for the determination algorithm. - char_traits::int_type intro[4]; - int nIntroUsed = 0; - UtfIntroState state = uis_start; - for(; !s_introFinalState[state]; ) { - std::istream::int_type ch = input.get(); - intro[nIntroUsed++] = ch; - UtfIntroCharType charType = IntroCharTypeOf(ch); - UtfIntroState newState = s_introTransitions[state][charType]; - int nUngets = s_introUngetCount[state][charType]; - if(nUngets > 0) { - input.clear(); - for(; nUngets > 0; --nUngets) { - if(char_traits::eof() != intro[--nIntroUsed]) - input.putback(char_traits::to_char_type(intro[nIntroUsed])); - } - } - state = newState; - } + // Determine (or guess) the character-set by reading the BOM, if any. See + // the YAML specification for the determination algorithm. + char_traits::int_type intro[4]; + int nIntroUsed = 0; + UtfIntroState state = uis_start; + for (; !s_introFinalState[state];) { + std::istream::int_type ch = input.get(); + intro[nIntroUsed++] = ch; + UtfIntroCharType charType = IntroCharTypeOf(ch); + UtfIntroState newState = s_introTransitions[state][charType]; + int nUngets = s_introUngetCount[state][charType]; + if (nUngets > 0) { + input.clear(); + for (; nUngets > 0; --nUngets) { + if (char_traits::eof() != intro[--nIntroUsed]) + input.putback(char_traits::to_char_type(intro[nIntroUsed])); + } + } + state = newState; + } - switch (state) { - case uis_utf8: m_charSet = utf8; break; - case uis_utf16le: m_charSet = utf16le; break; - case uis_utf16be: m_charSet = utf16be; break; - case uis_utf32le: m_charSet = utf32le; break; - case uis_utf32be: m_charSet = utf32be; break; - default: m_charSet = utf8; break; - } + switch (state) { + case uis_utf8: + m_charSet = utf8; + break; + case uis_utf16le: + m_charSet = utf16le; + break; + case uis_utf16be: + m_charSet = utf16be; + break; + case uis_utf32le: + m_charSet = utf32le; + break; + case uis_utf32be: + m_charSet = utf32be; + break; + default: + m_charSet = utf8; + break; + } - ReadAheadTo(0); - } + ReadAheadTo(0); +} - Stream::~Stream() - { - delete[] m_pPrefetched; - } +Stream::~Stream() { delete[] m_pPrefetched; } - char Stream::peek() const - { - if (m_readahead.empty()) - { - return Stream::eof(); - } +char Stream::peek() const { + if (m_readahead.empty()) { + return Stream::eof(); + } - return m_readahead[0]; - } - - Stream::operator bool() const - { - return m_input.good() || (!m_readahead.empty() && m_readahead[0] != Stream::eof()); - } + return m_readahead[0]; +} - // get - // . Extracts a character from the stream and updates our position - char Stream::get() - { - char ch = peek(); - AdvanceCurrent(); - m_mark.column++; - - if(ch == '\n') { - m_mark.column = 0; - m_mark.line++; - } - - return ch; - } +Stream::operator bool() const { + return m_input.good() || + (!m_readahead.empty() && m_readahead[0] != Stream::eof()); +} - // get - // . Extracts 'n' characters from the stream and updates our position - std::string Stream::get(int n) - { - std::string ret; - ret.reserve(n); - for(int i=0;i i; - } +void Stream::AdvanceCurrent() { + if (!m_readahead.empty()) { + m_readahead.pop_front(); + m_mark.pos++; + } - void Stream::StreamInUtf8() const - { - unsigned char b = GetNextByte(); - if (m_input.good()) - { - m_readahead.push_back(b); - } - } + ReadAheadTo(0); +} - void Stream::StreamInUtf16() const - { - unsigned long ch = 0; - unsigned char bytes[2]; - int nBigEnd = (m_charSet == utf16be) ? 0 : 1; +bool Stream::_ReadAheadTo(size_t i) const { + while (m_input.good() && (m_readahead.size() <= i)) { + switch (m_charSet) { + case utf8: + StreamInUtf8(); + break; + case utf16le: + StreamInUtf16(); + break; + case utf16be: + StreamInUtf16(); + break; + case utf32le: + StreamInUtf32(); + break; + case utf32be: + StreamInUtf32(); + break; + } + } - bytes[0] = GetNextByte(); - bytes[1] = GetNextByte(); - if (!m_input.good()) - { - return; - } - ch = (static_cast(bytes[nBigEnd]) << 8) | - static_cast(bytes[1 ^ nBigEnd]); + // signal end of stream + if (!m_input.good()) + m_readahead.push_back(Stream::eof()); - if (ch >= 0xDC00 && ch < 0xE000) - { - // Trailing (low) surrogate...ugh, wrong order - QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); - return; - } - else if (ch >= 0xD800 && ch < 0xDC00) - { - // ch is a leading (high) surrogate + return m_readahead.size() > i; +} - // Four byte UTF-8 code point +void Stream::StreamInUtf8() const { + unsigned char b = GetNextByte(); + if (m_input.good()) { + m_readahead.push_back(b); + } +} - // Read the trailing (low) surrogate - for (;;) - { - bytes[0] = GetNextByte(); - bytes[1] = GetNextByte(); - if (!m_input.good()) - { - QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); - return; - } - unsigned long chLow = (static_cast(bytes[nBigEnd]) << 8) | - static_cast(bytes[1 ^ nBigEnd]); - if (chLow < 0xDC00 || ch >= 0xE000) - { - // Trouble...not a low surrogate. Dump a REPLACEMENT CHARACTER into the stream. - QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); +void Stream::StreamInUtf16() const { + unsigned long ch = 0; + unsigned char bytes[2]; + int nBigEnd = (m_charSet == utf16be) ? 0 : 1; - // Deal with the next UTF-16 unit - if (chLow < 0xD800 || ch >= 0xE000) - { - // Easiest case: queue the codepoint and return - QueueUnicodeCodepoint(m_readahead, ch); - return; - } - else - { - // Start the loop over with the new high surrogate - ch = chLow; - continue; - } - } + bytes[0] = GetNextByte(); + bytes[1] = GetNextByte(); + if (!m_input.good()) { + return; + } + ch = (static_cast(bytes[nBigEnd]) << 8) | + static_cast(bytes[1 ^ nBigEnd]); - // Select the payload bits from the high surrogate - ch &= 0x3FF; - ch <<= 10; + if (ch >= 0xDC00 && ch < 0xE000) { + // Trailing (low) surrogate...ugh, wrong order + QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); + return; + } else if (ch >= 0xD800 && ch < 0xDC00) { + // ch is a leading (high) surrogate - // Include bits from low surrogate - ch |= (chLow & 0x3FF); + // Four byte UTF-8 code point - // Add the surrogacy offset - ch += 0x10000; - } - } + // Read the trailing (low) surrogate + for (;;) { + bytes[0] = GetNextByte(); + bytes[1] = GetNextByte(); + if (!m_input.good()) { + QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); + return; + } + unsigned long chLow = (static_cast(bytes[nBigEnd]) << 8) | + static_cast(bytes[1 ^ nBigEnd]); + if (chLow < 0xDC00 || chLow >= 0xE000) { + // Trouble...not a low surrogate. Dump a REPLACEMENT CHARACTER into the + // stream. + QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER); - QueueUnicodeCodepoint(m_readahead, ch); - } + // Deal with the next UTF-16 unit + if (chLow < 0xD800 || chLow >= 0xE000) { + // Easiest case: queue the codepoint and return + QueueUnicodeCodepoint(m_readahead, ch); + return; + } else { + // Start the loop over with the new high surrogate + ch = chLow; + continue; + } + } - inline char* ReadBuffer(unsigned char* pBuffer) - { - return reinterpret_cast(pBuffer); - } + // Select the payload bits from the high surrogate + ch &= 0x3FF; + ch <<= 10; - unsigned char Stream::GetNextByte() const - { - if (m_nPrefetchedUsed >= m_nPrefetchedAvailable) - { - std::streambuf *pBuf = m_input.rdbuf(); - m_nPrefetchedAvailable = pBuf->sgetn(ReadBuffer(m_pPrefetched), - YAML_PREFETCH_SIZE); - m_nPrefetchedUsed = 0; - if (!m_nPrefetchedAvailable) - { - m_input.setstate(std::ios_base::eofbit); - } + // Include bits from low surrogate + ch |= (chLow & 0x3FF); - if (0 == m_nPrefetchedAvailable) - { - return 0; - } - } + // Add the surrogacy offset + ch += 0x10000; + break; + } + } - return m_pPrefetched[m_nPrefetchedUsed++]; - } + QueueUnicodeCodepoint(m_readahead, ch); +} - void Stream::StreamInUtf32() const - { - static int indexes[2][4] = { - {3, 2, 1, 0}, - {0, 1, 2, 3} - }; +inline char* ReadBuffer(unsigned char* pBuffer) { + return reinterpret_cast(pBuffer); +} - unsigned long ch = 0; - unsigned char bytes[4]; - int* pIndexes = (m_charSet == utf32be) ? indexes[1] : indexes[0]; +unsigned char Stream::GetNextByte() const { + if (m_nPrefetchedUsed >= m_nPrefetchedAvailable) { + std::streambuf* pBuf = m_input.rdbuf(); + m_nPrefetchedAvailable = static_cast( + pBuf->sgetn(ReadBuffer(m_pPrefetched), YAML_PREFETCH_SIZE)); + m_nPrefetchedUsed = 0; + if (!m_nPrefetchedAvailable) { + m_input.setstate(std::ios_base::eofbit); + } - bytes[0] = GetNextByte(); - bytes[1] = GetNextByte(); - bytes[2] = GetNextByte(); - bytes[3] = GetNextByte(); - if (!m_input.good()) - { - return; - } + if (0 == m_nPrefetchedAvailable) { + return 0; + } + } - for (int i = 0; i < 4; ++i) - { - ch <<= 8; - ch |= bytes[pIndexes[i]]; - } + return m_pPrefetched[m_nPrefetchedUsed++]; +} - QueueUnicodeCodepoint(m_readahead, ch); - } +void Stream::StreamInUtf32() const { + static int indexes[2][4] = {{3, 2, 1, 0}, {0, 1, 2, 3}}; + + unsigned long ch = 0; + unsigned char bytes[4]; + int* pIndexes = (m_charSet == utf32be) ? indexes[1] : indexes[0]; + + bytes[0] = GetNextByte(); + bytes[1] = GetNextByte(); + bytes[2] = GetNextByte(); + bytes[3] = GetNextByte(); + if (!m_input.good()) { + return; + } + + for (int i = 0; i < 4; ++i) { + ch <<= 8; + ch |= bytes[pIndexes[i]]; + } + + QueueUnicodeCodepoint(m_readahead, ch); } +} diff --git a/src/yamlcpp/stream.h b/src/yamlcpp/stream.h --- a/src/yamlcpp/stream.h +++ b/src/yamlcpp/stream.h @@ -1,79 +1,76 @@ #ifndef STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include "yaml-cpp/noncopyable.h" #include "yaml-cpp/mark.h" #include #include #include #include #include #include -namespace YODA_YAML -{ - class Stream: private noncopyable - { - public: - friend class StreamCharSource; - - Stream(std::istream& input); - ~Stream(); +namespace YODA_YAML { +class Stream : private noncopyable { + public: + friend class StreamCharSource; - operator bool() const; - bool operator !() const { return !static_cast (*this); } + Stream(std::istream& input); + ~Stream(); - char peek() const; - char get(); - std::string get(int n); - void eat(int n = 1); + operator bool() const; + bool operator!() const { return !static_cast(*this); } - static char eof() { return 0x04; } - - const Mark mark() const { return m_mark; } - int pos() const { return m_mark.pos; } - int line() const { return m_mark.line; } - int column() const { return m_mark.column; } - void ResetColumn() { m_mark.column = 0; } + char peek() const; + char get(); + std::string get(int n); + void eat(int n = 1); - private: - enum CharacterSet {utf8, utf16le, utf16be, utf32le, utf32be}; + static char eof() { return 0x04; } - std::istream& m_input; - Mark m_mark; - - CharacterSet m_charSet; - mutable std::deque m_readahead; - unsigned char* const m_pPrefetched; - mutable size_t m_nPrefetchedAvailable; - mutable size_t m_nPrefetchedUsed; - - void AdvanceCurrent(); - char CharAt(size_t i) const; - bool ReadAheadTo(size_t i) const; - bool _ReadAheadTo(size_t i) const; - void StreamInUtf8() const; - void StreamInUtf16() const; - void StreamInUtf32() const; - unsigned char GetNextByte() const; - }; + const Mark mark() const { return m_mark; } + int pos() const { return m_mark.pos; } + int line() const { return m_mark.line; } + int column() const { return m_mark.column; } + void ResetColumn() { m_mark.column = 0; } - // CharAt - // . Unchecked access - inline char Stream::CharAt(size_t i) const { - return m_readahead[i]; - } - - inline bool Stream::ReadAheadTo(size_t i) const { - if(m_readahead.size() > i) - return true; - return _ReadAheadTo(i); - } + private: + enum CharacterSet { utf8, utf16le, utf16be, utf32le, utf32be }; + + std::istream& m_input; + Mark m_mark; + + CharacterSet m_charSet; + mutable std::deque m_readahead; + unsigned char* const m_pPrefetched; + mutable size_t m_nPrefetchedAvailable; + mutable size_t m_nPrefetchedUsed; + + void AdvanceCurrent(); + char CharAt(size_t i) const; + bool ReadAheadTo(size_t i) const; + bool _ReadAheadTo(size_t i) const; + void StreamInUtf8() const; + void StreamInUtf16() const; + void StreamInUtf32() const; + unsigned char GetNextByte() const; +}; + +// CharAt +// . Unchecked access +inline char Stream::CharAt(size_t i) const { return m_readahead[i]; } + +inline bool Stream::ReadAheadTo(size_t i) const { + if (m_readahead.size() > i) + return true; + return _ReadAheadTo(i); +} } -#endif // STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/streamcharsource.h b/src/yamlcpp/streamcharsource.h --- a/src/yamlcpp/streamcharsource.h +++ b/src/yamlcpp/streamcharsource.h @@ -1,48 +1,48 @@ #ifndef STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include "yaml-cpp/noncopyable.h" #include -namespace YODA_YAML -{ - class StreamCharSource - { - public: - StreamCharSource(const Stream& stream): m_offset(0), m_stream(stream) {} - StreamCharSource(const StreamCharSource& source): m_offset(source.m_offset), m_stream(source.m_stream) {} - ~StreamCharSource() {} +namespace YODA_YAML { +class StreamCharSource { + public: + StreamCharSource(const Stream& stream) : m_offset(0), m_stream(stream) {} + StreamCharSource(const StreamCharSource& source) + : m_offset(source.m_offset), m_stream(source.m_stream) {} + ~StreamCharSource() {} - operator bool() const; - char operator [] (std::size_t i) const { return m_stream.CharAt(m_offset + i); } - bool operator !() const { return !static_cast(*this); } + operator bool() const; + char operator[](std::size_t i) const { return m_stream.CharAt(m_offset + i); } + bool operator!() const { return !static_cast(*this); } - const StreamCharSource operator + (int i) const; - - private: - std::size_t m_offset; - const Stream& m_stream; + const StreamCharSource operator+(int i) const; - StreamCharSource& operator = (const StreamCharSource&); // non-assignable - }; - - inline StreamCharSource::operator bool() const { - return m_stream.ReadAheadTo(m_offset); - } - - inline const StreamCharSource StreamCharSource::operator + (int i) const { - StreamCharSource source(*this); - if(static_cast (source.m_offset) + i >= 0) - source.m_offset += i; - else - source.m_offset = 0; - return source; - } + private: + std::size_t m_offset; + const Stream& m_stream; + + StreamCharSource& operator=(const StreamCharSource&); // non-assignable +}; + +inline StreamCharSource::operator bool() const { + return m_stream.ReadAheadTo(m_offset); } -#endif // STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +inline const StreamCharSource StreamCharSource::operator+(int i) const { + StreamCharSource source(*this); + if (static_cast(source.m_offset) + i >= 0) + source.m_offset += i; + else + source.m_offset = 0; + return source; +} +} + +#endif // STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/stringsource.h b/src/yamlcpp/stringsource.h --- a/src/yamlcpp/stringsource.h +++ b/src/yamlcpp/stringsource.h @@ -1,47 +1,48 @@ #ifndef STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include -namespace YODA_YAML -{ - class StringCharSource - { - public: - StringCharSource(const char *str, std::size_t size): m_str(str), m_size(size), m_offset(0) {} +namespace YODA_YAML { +class StringCharSource { + public: + StringCharSource(const char* str, std::size_t size) + : m_str(str), m_size(size), m_offset(0) {} - operator bool() const { return m_offset < m_size; } - char operator [] (std::size_t i) const { return m_str[m_offset + i]; } - bool operator !() const { return !static_cast(*this); } + operator bool() const { return m_offset < m_size; } + char operator[](std::size_t i) const { return m_str[m_offset + i]; } + bool operator!() const { return !static_cast(*this); } - const StringCharSource operator + (int i) const { - StringCharSource source(*this); - if(static_cast (source.m_offset) + i >= 0) - source.m_offset += i; - else - source.m_offset = 0; - return source; - } - - StringCharSource& operator ++ () { - ++m_offset; - return *this; - } - - StringCharSource& operator += (std::size_t offset) { - m_offset += offset; - return *this; - } - private: - const char *m_str; - std::size_t m_size; - std::size_t m_offset; - }; + const StringCharSource operator+(int i) const { + StringCharSource source(*this); + if (static_cast(source.m_offset) + i >= 0) + source.m_offset += i; + else + source.m_offset = 0; + return source; + } + + StringCharSource& operator++() { + ++m_offset; + return *this; + } + + StringCharSource& operator+=(std::size_t offset) { + m_offset += offset; + return *this; + } + + private: + const char* m_str; + std::size_t m_size; + std::size_t m_offset; +}; } -#endif // STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/tag.cpp b/src/yamlcpp/tag.cpp --- a/src/yamlcpp/tag.cpp +++ b/src/yamlcpp/tag.cpp @@ -1,52 +1,49 @@ -#include "tag.h" -#include "directives.h" -#include "token.h" #include #include -namespace YODA_YAML -{ - Tag::Tag(const Token& token): type(static_cast(token.data)) - { - switch(type) { - case VERBATIM: - value = token.value; - break; - case PRIMARY_HANDLE: - value = token.value; - break; - case SECONDARY_HANDLE: - value = token.value; - break; - case NAMED_HANDLE: - handle = token.value; - value = token.params[0]; - break; - case NON_SPECIFIC: - break; - default: - assert(false); - } - } +#include "directives.h" // IWYU pragma: keep +#include "tag.h" +#include "token.h" - const std::string Tag::Translate(const Directives& directives) - { - switch(type) { - case VERBATIM: - return value; - case PRIMARY_HANDLE: - return directives.TranslateTagHandle("!") + value; - case SECONDARY_HANDLE: - return directives.TranslateTagHandle("!!") + value; - case NAMED_HANDLE: - return directives.TranslateTagHandle("!" + handle + "!") + value; - case NON_SPECIFIC: - // TODO: - return "!"; - default: - assert(false); - } - throw std::runtime_error("yaml-cpp: internal error, bad tag type"); - } +namespace YODA_YAML { +Tag::Tag(const Token& token) : type(static_cast(token.data)) { + switch (type) { + case VERBATIM: + value = token.value; + break; + case PRIMARY_HANDLE: + value = token.value; + break; + case SECONDARY_HANDLE: + value = token.value; + break; + case NAMED_HANDLE: + handle = token.value; + value = token.params[0]; + break; + case NON_SPECIFIC: + break; + default: + assert(false); + } } +const std::string Tag::Translate(const Directives& directives) { + switch (type) { + case VERBATIM: + return value; + case PRIMARY_HANDLE: + return directives.TranslateTagHandle("!") + value; + case SECONDARY_HANDLE: + return directives.TranslateTagHandle("!!") + value; + case NAMED_HANDLE: + return directives.TranslateTagHandle("!" + handle + "!") + value; + case NON_SPECIFIC: + // TODO: + return "!"; + default: + assert(false); + } + throw std::runtime_error("yaml-cpp: internal error, bad tag type"); +} +} diff --git a/src/yamlcpp/tag.h b/src/yamlcpp/tag.h --- a/src/yamlcpp/tag.h +++ b/src/yamlcpp/tag.h @@ -1,28 +1,33 @@ #ifndef TAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define TAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif #include -namespace YODA_YAML -{ - struct Token; - struct Directives; +namespace YODA_YAML { +struct Directives; +struct Token; - struct Tag { - enum TYPE { - VERBATIM, PRIMARY_HANDLE, SECONDARY_HANDLE, NAMED_HANDLE, NON_SPECIFIC - }; - - Tag(const Token& token); - const std::string Translate(const Directives& directives); - - TYPE type; - std::string handle, value; - }; +struct Tag { + enum TYPE { + VERBATIM, + PRIMARY_HANDLE, + SECONDARY_HANDLE, + NAMED_HANDLE, + NON_SPECIFIC + }; + + Tag(const Token& token); + const std::string Translate(const Directives& directives); + + TYPE type; + std::string handle, value; +}; } -#endif // TAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // TAG_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/token.h b/src/yamlcpp/token.h --- a/src/yamlcpp/token.h +++ b/src/yamlcpp/token.h @@ -1,85 +1,69 @@ #ifndef TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include "yaml-cpp/mark.h" #include #include #include -namespace YODA_YAML -{ - const std::string TokenNames[] = { - "DIRECTIVE", - "DOC_START", - "DOC_END", - "BLOCK_SEQ_START", - "BLOCK_MAP_START", - "BLOCK_SEQ_END", - "BLOCK_MAP_END", - "BLOCK_ENTRY", - "FLOW_SEQ_START", - "FLOW_MAP_START", - "FLOW_SEQ_END", - "FLOW_MAP_END", - "FLOW_MAP_COMPACT", - "FLOW_ENTRY", - "KEY", - "VALUE", - "ANCHOR", - "ALIAS", - "TAG", - "SCALAR" - }; +namespace YODA_YAML { +const std::string TokenNames[] = { + "DIRECTIVE", "DOC_START", "DOC_END", "BLOCK_SEQ_START", "BLOCK_MAP_START", + "BLOCK_SEQ_END", "BLOCK_MAP_END", "BLOCK_ENTRY", "FLOW_SEQ_START", + "FLOW_MAP_START", "FLOW_SEQ_END", "FLOW_MAP_END", "FLOW_MAP_COMPACT", + "FLOW_ENTRY", "KEY", "VALUE", "ANCHOR", "ALIAS", "TAG", "SCALAR"}; - struct Token { - // enums - enum STATUS { VALID, INVALID, UNVERIFIED }; - enum TYPE { - DIRECTIVE, - DOC_START, - DOC_END, - BLOCK_SEQ_START, - BLOCK_MAP_START, - BLOCK_SEQ_END, - BLOCK_MAP_END, - BLOCK_ENTRY, - FLOW_SEQ_START, - FLOW_MAP_START, - FLOW_SEQ_END, - FLOW_MAP_END, - FLOW_MAP_COMPACT, - FLOW_ENTRY, - KEY, - VALUE, - ANCHOR, - ALIAS, - TAG, - PLAIN_SCALAR, - NON_PLAIN_SCALAR - }; +struct Token { + // enums + enum STATUS { VALID, INVALID, UNVERIFIED }; + enum TYPE { + DIRECTIVE, + DOC_START, + DOC_END, + BLOCK_SEQ_START, + BLOCK_MAP_START, + BLOCK_SEQ_END, + BLOCK_MAP_END, + BLOCK_ENTRY, + FLOW_SEQ_START, + FLOW_MAP_START, + FLOW_SEQ_END, + FLOW_MAP_END, + FLOW_MAP_COMPACT, + FLOW_ENTRY, + KEY, + VALUE, + ANCHOR, + ALIAS, + TAG, + PLAIN_SCALAR, + NON_PLAIN_SCALAR + }; - // data - Token(TYPE type_, const Mark& mark_): status(VALID), type(type_), mark(mark_), data(0) {} + // data + Token(TYPE type_, const Mark& mark_) + : status(VALID), type(type_), mark(mark_), data(0) {} - friend std::ostream& operator << (std::ostream& out, const Token& token) { - out << TokenNames[token.type] << std::string(": ") << token.value; - for(std::size_t i=0;i params; - int data; - }; + STATUS status; + TYPE type; + Mark mark; + std::string value; + std::vector params; + int data; +}; } -#endif // TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/anchor.h b/src/yamlcpp/yaml-cpp/anchor.h --- a/src/yamlcpp/yaml-cpp/anchor.h +++ b/src/yamlcpp/yaml-cpp/anchor.h @@ -1,16 +1,17 @@ #ifndef ANCHOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define ANCHOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif #include -namespace YODA_YAML -{ - typedef std::size_t anchor_t; - const anchor_t NullAnchor = 0; +namespace YODA_YAML { +typedef std::size_t anchor_t; +const anchor_t NullAnchor = 0; } -#endif // ANCHOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // ANCHOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/binary.h b/src/yamlcpp/yaml-cpp/binary.h --- a/src/yamlcpp/yaml-cpp/binary.h +++ b/src/yamlcpp/yaml-cpp/binary.h @@ -1,66 +1,67 @@ #ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif #include #include -namespace YODA_YAML -{ - class Node; - - std::string EncodeBase64(const unsigned char *data, std::size_t size); - std::vector DecodeBase64(const std::string& input); - - class Binary { - public: - Binary(): m_unownedData(0), m_unownedSize(0) {} - Binary(const unsigned char *data, std::size_t size): m_unownedData(data), m_unownedSize(size) {} - - bool owned() const { return !m_unownedData; } - std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; } - const unsigned char *data() const { return owned() ? &m_data[0] : m_unownedData; } - - void swap(std::vector& rhs) { - if(m_unownedData) { - m_data.swap(rhs); - rhs.clear(); - rhs.resize(m_unownedSize); - std::copy(m_unownedData, m_unownedData + m_unownedSize, &rhs[0]); - m_unownedData = 0; - m_unownedSize = 0; - } else { - m_data.swap(rhs); - } - } - - bool operator == (const Binary& rhs) const { - const std::size_t s = size(); - if(s != rhs.size()) - return false; - const unsigned char *d1 = data(); - const unsigned char *d2 = rhs.data(); - for(std::size_t i=0;i m_data; - const unsigned char *m_unownedData; - std::size_t m_unownedSize; - }; - - void operator >> (const Node& node, Binary& binary); +#include "yaml-cpp/dll.h" + +namespace YODA_YAML { +YAML_CPP_API std::string EncodeBase64(const unsigned char *data, + std::size_t size); +YAML_CPP_API std::vector DecodeBase64(const std::string &input); + +class YAML_CPP_API Binary { + public: + Binary() : m_unownedData(0), m_unownedSize(0) {} + Binary(const unsigned char *data_, std::size_t size_) + : m_unownedData(data_), m_unownedSize(size_) {} + + bool owned() const { return !m_unownedData; } + std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; } + const unsigned char *data() const { + return owned() ? &m_data[0] : m_unownedData; + } + + void swap(std::vector &rhs) { + if (m_unownedData) { + m_data.swap(rhs); + rhs.clear(); + rhs.resize(m_unownedSize); + std::copy(m_unownedData, m_unownedData + m_unownedSize, rhs.begin()); + m_unownedData = 0; + m_unownedSize = 0; + } else { + m_data.swap(rhs); + } + } + + bool operator==(const Binary &rhs) const { + const std::size_t s = size(); + if (s != rhs.size()) + return false; + const unsigned char *d1 = data(); + const unsigned char *d2 = rhs.data(); + for (std::size_t i = 0; i < s; i++) { + if (*d1++ != *d2++) + return false; + } + return true; + } + + bool operator!=(const Binary &rhs) const { return !(*this == rhs); } + + private: + std::vector m_data; + const unsigned char *m_unownedData; + std::size_t m_unownedSize; +}; } -#endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/dll.h b/src/yamlcpp/yaml-cpp/dll.h --- a/src/yamlcpp/yaml-cpp/dll.h +++ b/src/yamlcpp/yaml-cpp/dll.h @@ -1,28 +1,33 @@ #ifndef DLL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define DLL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif -// The following ifdef block is the standard way of creating macros which make exporting -// from a DLL simpler. All files within this DLL are compiled with the yaml_cpp_EXPORTS -// symbol defined on the command line. this symbol should not be defined on any project -// that uses this DLL. This way any other project whose source files include this file see -// YAML_CPP_API functions as being imported from a DLL, whereas this DLL sees symbols -// defined with this macro as being exported. +// The following ifdef block is the standard way of creating macros which make +// exporting from a DLL simpler. All files within this DLL are compiled with the +// yaml_cpp_EXPORTS symbol defined on the command line. This symbol should not +// be defined on any project that uses this DLL. This way any other project +// whose source files include this file see YAML_CPP_API functions as being +// imported from a DLL, whereas this DLL sees symbols defined with this macro as +// being exported. #undef YAML_CPP_API -#ifdef YAML_CPP_DLL // Using or Building YAML-CPP DLL (definition defined manually) - #ifdef yaml_cpp_EXPORTS // Building YAML-CPP DLL (definition created by CMake or defined manually) - // #pragma message( "Defining YAML_CPP_API for DLL export" ) - #define YAML_CPP_API __declspec(dllexport) - #else // yaml_cpp_EXPORTS - // #pragma message( "Defining YAML_CPP_API for DLL import" ) - #define YAML_CPP_API __declspec(dllimport) - #endif // yaml_cpp_EXPORTS -#else //YAML_CPP_DLL +#ifdef YAML_CPP_DLL // Using or Building YAML-CPP DLL (definition defined + // manually) +#ifdef yaml_cpp_EXPORTS // Building YAML-CPP DLL (definition created by CMake + // or defined manually) +// #pragma message( "Defining YAML_CPP_API for DLL export" ) +#define YAML_CPP_API __declspec(dllexport) +#else // yaml_cpp_EXPORTS +// #pragma message( "Defining YAML_CPP_API for DLL import" ) +#define YAML_CPP_API __declspec(dllimport) +#endif // yaml_cpp_EXPORTS +#else // YAML_CPP_DLL #define YAML_CPP_API -#endif // YAML_CPP_DLL +#endif // YAML_CPP_DLL -#endif // DLL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // DLL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/emitfromevents.h b/src/yamlcpp/yaml-cpp/emitfromevents.h --- a/src/yamlcpp/yaml-cpp/emitfromevents.h +++ b/src/yamlcpp/yaml-cpp/emitfromevents.h @@ -1,45 +1,57 @@ #ifndef EMITFROMEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EMITFROMEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif -#include "yaml-cpp/eventhandler.h" #include -namespace YODA_YAML -{ - class Emitter; - - class EmitFromEvents: public EventHandler - { - public: - EmitFromEvents(Emitter& emitter); - - virtual void OnDocumentStart(const Mark& mark); - virtual void OnDocumentEnd(); - - virtual void OnNull(const Mark& mark, anchor_t anchor); - virtual void OnAlias(const Mark& mark, anchor_t anchor); - virtual void OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value); - - virtual void OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor); - virtual void OnSequenceEnd(); - - virtual void OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor); - virtual void OnMapEnd(); - - private: - void BeginNode(); - void EmitProps(const std::string& tag, anchor_t anchor); - - private: - Emitter& m_emitter; - - struct State { enum value { WaitingForSequenceEntry, WaitingForKey, WaitingForValue }; }; - std::stack m_stateStack; - }; +#include "yaml-cpp/anchor.h" +#include "yaml-cpp/emitterstyle.h" +#include "yaml-cpp/eventhandler.h" + +namespace YODA_YAML { +struct Mark; +} // namespace YODA_YAML + +namespace YODA_YAML { +class Emitter; + +class EmitFromEvents : public EventHandler { + public: + EmitFromEvents(Emitter& emitter); + + virtual void OnDocumentStart(const Mark& mark); + virtual void OnDocumentEnd(); + + virtual void OnNull(const Mark& mark, anchor_t anchor); + virtual void OnAlias(const Mark& mark, anchor_t anchor); + virtual void OnScalar(const Mark& mark, const std::string& tag, + anchor_t anchor, const std::string& value); + + virtual void OnSequenceStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style); + virtual void OnSequenceEnd(); + + virtual void OnMapStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style); + virtual void OnMapEnd(); + + private: + void BeginNode(); + void EmitProps(const std::string& tag, anchor_t anchor); + + private: + Emitter& m_emitter; + + struct State { + enum value { WaitingForSequenceEntry, WaitingForKey, WaitingForValue }; + }; + std::stack m_stateStack; +}; } -#endif // EMITFROMEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // EMITFROMEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/emitter.h b/src/yamlcpp/yaml-cpp/emitter.h --- a/src/yamlcpp/yaml-cpp/emitter.h +++ b/src/yamlcpp/yaml-cpp/emitter.h @@ -1,186 +1,254 @@ #ifndef EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include +#include +#include +#include +#include "yaml-cpp/binary.h" #include "yaml-cpp/dll.h" -#include "yaml-cpp/binary.h" +#include "yaml-cpp/emitterdef.h" #include "yaml-cpp/emittermanip.h" -#include "yaml-cpp/ostream.h" #include "yaml-cpp/noncopyable.h" #include "yaml-cpp/null.h" -#include -#include -#include +#include "yaml-cpp/ostream_wrapper.h" -namespace YODA_YAML -{ - class EmitterState; +namespace YODA_YAML { +class Binary; +struct _Null; +} // namespace YODA_YAML - class YAML_CPP_API Emitter: private noncopyable - { - public: - Emitter(); - ~Emitter(); +namespace YODA_YAML { +class EmitterState; - // output - const char *c_str() const; - unsigned size() const; +class YAML_CPP_API Emitter : private noncopyable { + public: + Emitter(); + explicit Emitter(std::ostream& stream); + ~Emitter(); - // state checking - bool good() const; - const std::string GetLastError() const; + // output + const char* c_str() const; + std::size_t size() const; - // global setters - bool SetOutputCharset(EMITTER_MANIP value); - bool SetStringFormat(EMITTER_MANIP value); - bool SetBoolFormat(EMITTER_MANIP value); - bool SetIntBase(EMITTER_MANIP value); - bool SetSeqFormat(EMITTER_MANIP value); - bool SetMapFormat(EMITTER_MANIP value); - bool SetIndent(unsigned n); - bool SetPreCommentIndent(unsigned n); - bool SetPostCommentIndent(unsigned n); - bool SetFloatPrecision(unsigned n); - bool SetDoublePrecision(unsigned n); + // state checking + bool good() const; + const std::string GetLastError() const; - // local setters - Emitter& SetLocalValue(EMITTER_MANIP value); - Emitter& SetLocalIndent(const _Indent& indent); - Emitter& SetLocalPrecision(const _Precision& precision); + // global setters + bool SetOutputCharset(EMITTER_MANIP value); + bool SetStringFormat(EMITTER_MANIP value); + bool SetBoolFormat(EMITTER_MANIP value); + bool SetIntBase(EMITTER_MANIP value); + bool SetSeqFormat(EMITTER_MANIP value); + bool SetMapFormat(EMITTER_MANIP value); + bool SetIndent(std::size_t n); + bool SetPreCommentIndent(std::size_t n); + bool SetPostCommentIndent(std::size_t n); + bool SetFloatPrecision(std::size_t n); + bool SetDoublePrecision(std::size_t n); - // overloads of write - Emitter& Write(const std::string& str); - Emitter& Write(bool b); - Emitter& Write(char ch); - Emitter& Write(const _Alias& alias); - Emitter& Write(const _Anchor& anchor); - Emitter& Write(const _Tag& tag); - Emitter& Write(const _Comment& comment); - Emitter& Write(const _Null& null); - Emitter& Write(const Binary& binary); + // local setters + Emitter& SetLocalValue(EMITTER_MANIP value); + Emitter& SetLocalIndent(const _Indent& indent); + Emitter& SetLocalPrecision(const _Precision& precision); - template - Emitter& WriteIntegralType(T value); + // overloads of write + Emitter& Write(const std::string& str); + Emitter& Write(bool b); + Emitter& Write(char ch); + Emitter& Write(const _Alias& alias); + Emitter& Write(const _Anchor& anchor); + Emitter& Write(const _Tag& tag); + Emitter& Write(const _Comment& comment); + Emitter& Write(const _Null& n); + Emitter& Write(const Binary& binary); - template - Emitter& WriteStreamable(T value); + template + Emitter& WriteIntegralType(T value); - private: - void PreWriteIntegralType(std::stringstream& str); - void PreWriteStreamable(std::stringstream& str); - void PostWriteIntegralType(const std::stringstream& str); - void PostWriteStreamable(const std::stringstream& str); + template + Emitter& WriteStreamable(T value); - template void SetStreamablePrecision(std::stringstream&) {} - unsigned GetFloatPrecision() const; - unsigned GetDoublePrecision() const; + private: + template + void SetStreamablePrecision(std::stringstream&) {} + std::size_t GetFloatPrecision() const; + std::size_t GetDoublePrecision() const; - private: - void PreAtomicWrite(); - bool GotoNextPreAtomicState(); - void PostAtomicWrite(); - void EmitSeparationIfNecessary(); + void PrepareIntegralStream(std::stringstream& stream) const; + void StartedScalar(); - void EmitBeginDoc(); - void EmitEndDoc(); - void EmitBeginSeq(); - void EmitEndSeq(); - void EmitBeginMap(); - void EmitEndMap(); - void EmitKey(); - void EmitValue(); - void EmitNewline(); - void EmitKindTag(); - void EmitTag(bool verbatim, const _Tag& tag); + private: + void EmitBeginDoc(); + void EmitEndDoc(); + void EmitBeginSeq(); + void EmitEndSeq(); + void EmitBeginMap(); + void EmitEndMap(); + void EmitNewline(); + void EmitKindTag(); + void EmitTag(bool verbatim, const _Tag& tag); - const char *ComputeFullBoolName(bool b) const; - bool CanEmitNewline() const; + void PrepareNode(EmitterNodeType::value child); + void PrepareTopNode(EmitterNodeType::value child); + void FlowSeqPrepareNode(EmitterNodeType::value child); + void BlockSeqPrepareNode(EmitterNodeType::value child); - private: - ostream m_stream; - std::unique_ptr m_pState; - }; + void FlowMapPrepareNode(EmitterNodeType::value child); - template - inline Emitter& Emitter::WriteIntegralType(T value) - { - if(!good()) - return *this; + void FlowMapPrepareLongKey(EmitterNodeType::value child); + void FlowMapPrepareLongKeyValue(EmitterNodeType::value child); + void FlowMapPrepareSimpleKey(EmitterNodeType::value child); + void FlowMapPrepareSimpleKeyValue(EmitterNodeType::value child); - std::stringstream str; - PreWriteIntegralType(str); - str << value; - PostWriteIntegralType(str); - return *this; - } + void BlockMapPrepareNode(EmitterNodeType::value child); - template - inline Emitter& Emitter::WriteStreamable(T value) - { - if(!good()) - return *this; + void BlockMapPrepareLongKey(EmitterNodeType::value child); + void BlockMapPrepareLongKeyValue(EmitterNodeType::value child); + void BlockMapPrepareSimpleKey(EmitterNodeType::value child); + void BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child); - std::stringstream str; - PreWriteStreamable(str); - SetStreamablePrecision(str); - str << value; - PostWriteStreamable(str); - return *this; - } + void SpaceOrIndentTo(bool requireSpace, std::size_t indent); - template<> - inline void Emitter::SetStreamablePrecision(std::stringstream& str) - { - str.precision(GetFloatPrecision()); - } + const char* ComputeFullBoolName(bool b) const; + bool CanEmitNewline() const; - template<> - inline void Emitter::SetStreamablePrecision(std::stringstream& str) - { - str.precision(GetDoublePrecision()); - } + private: + std::unique_ptr m_pState; + ostream_wrapper m_stream; +}; - // overloads of insertion - inline Emitter& operator << (Emitter& emitter, const std::string& v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, bool v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, char v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, unsigned char v) { return emitter.Write(static_cast(v)); } - inline Emitter& operator << (Emitter& emitter, const _Alias& v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, const _Anchor& v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, const _Tag& v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, const _Comment& v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, const _Null& v) { return emitter.Write(v); } - inline Emitter& operator << (Emitter& emitter, const Binary& b) { return emitter.Write(b); } +template +inline Emitter& Emitter::WriteIntegralType(T value) { + if (!good()) + return *this; - inline Emitter& operator << (Emitter& emitter, const char *v) { return emitter.Write(std::string(v)); } + PrepareNode(EmitterNodeType::Scalar); - inline Emitter& operator << (Emitter& emitter, int v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, unsigned int v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, short v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, unsigned short v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, long v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, unsigned long v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, long long v) { return emitter.WriteIntegralType(v); } - inline Emitter& operator << (Emitter& emitter, unsigned long long v) { return emitter.WriteIntegralType(v); } + std::stringstream stream; + PrepareIntegralStream(stream); + stream << value; + m_stream << stream.str(); - inline Emitter& operator << (Emitter& emitter, float v) { return emitter.WriteStreamable(v); } - inline Emitter& operator << (Emitter& emitter, double v) { return emitter.WriteStreamable(v); } + StartedScalar(); - inline Emitter& operator << (Emitter& emitter, EMITTER_MANIP value) { - return emitter.SetLocalValue(value); - } - - inline Emitter& operator << (Emitter& emitter, _Indent indent) { - return emitter.SetLocalIndent(indent); - } - - inline Emitter& operator << (Emitter& emitter, _Precision precision) { - return emitter.SetLocalPrecision(precision); - } + return *this; } -#endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +template +inline Emitter& Emitter::WriteStreamable(T value) { + if (!good()) + return *this; + + PrepareNode(EmitterNodeType::Scalar); + + std::stringstream stream; + SetStreamablePrecision(stream); + stream << value; + m_stream << stream.str(); + + StartedScalar(); + + return *this; +} + +template <> +inline void Emitter::SetStreamablePrecision(std::stringstream& stream) { + stream.precision(static_cast(GetFloatPrecision())); +} + +template <> +inline void Emitter::SetStreamablePrecision(std::stringstream& stream) { + stream.precision(static_cast(GetDoublePrecision())); +} + +// overloads of insertion +inline Emitter& operator<<(Emitter& emitter, const std::string& v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, bool v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, char v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, unsigned char v) { + return emitter.Write(static_cast(v)); +} +inline Emitter& operator<<(Emitter& emitter, const _Alias& v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, const _Anchor& v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, const _Tag& v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, const _Comment& v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, const _Null& v) { + return emitter.Write(v); +} +inline Emitter& operator<<(Emitter& emitter, const Binary& b) { + return emitter.Write(b); +} + +inline Emitter& operator<<(Emitter& emitter, const char* v) { + return emitter.Write(std::string(v)); +} + +inline Emitter& operator<<(Emitter& emitter, int v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, unsigned int v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, short v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, unsigned short v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, long v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, unsigned long v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, long long v) { + return emitter.WriteIntegralType(v); +} +inline Emitter& operator<<(Emitter& emitter, unsigned long long v) { + return emitter.WriteIntegralType(v); +} + +inline Emitter& operator<<(Emitter& emitter, float v) { + return emitter.WriteStreamable(v); +} +inline Emitter& operator<<(Emitter& emitter, double v) { + return emitter.WriteStreamable(v); +} + +inline Emitter& operator<<(Emitter& emitter, EMITTER_MANIP value) { + return emitter.SetLocalValue(value); +} + +inline Emitter& operator<<(Emitter& emitter, _Indent indent) { + return emitter.SetLocalIndent(indent); +} + +inline Emitter& operator<<(Emitter& emitter, _Precision precision) { + return emitter.SetLocalPrecision(precision); +} +} + +#endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/emitterdef.h b/src/yamlcpp/yaml-cpp/emitterdef.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/emitterdef.h @@ -0,0 +1,16 @@ +#ifndef EMITTERDEF_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define EMITTERDEF_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +namespace YODA_YAML { +struct EmitterNodeType { + enum value { NoType, Property, Scalar, FlowSeq, BlockSeq, FlowMap, BlockMap }; +}; +} + +#endif // EMITTERDEF_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/emittermanip.h b/src/yamlcpp/yaml-cpp/emittermanip.h --- a/src/yamlcpp/yaml-cpp/emittermanip.h +++ b/src/yamlcpp/yaml-cpp/emittermanip.h @@ -1,149 +1,137 @@ #ifndef EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include -namespace YODA_YAML -{ - enum EMITTER_MANIP { - // general manipulators - Auto, - TagByKind, - Newline, +namespace YODA_YAML { +enum EMITTER_MANIP { + // general manipulators + Auto, + TagByKind, + Newline, - // output character set - EmitNonAscii, - EscapeNonAscii, - - // string manipulators - // Auto, // duplicate - SingleQuoted, - DoubleQuoted, - Literal, - - // bool manipulators - YesNoBool, // yes, no - TrueFalseBool, // true, false - OnOffBool, // on, off - UpperCase, // TRUE, N - LowerCase, // f, yes - CamelCase, // No, Off - LongBool, // yes, On - ShortBool, // y, t - - // int manipulators - Dec, - Hex, - Oct, - - // document manipulators - BeginDoc, - EndDoc, - - // sequence manipulators - BeginSeq, - EndSeq, - Flow, - Block, - - // map manipulators - BeginMap, - EndMap, - Key, - Value, - // Flow, // duplicate - // Block, // duplicate - // Auto, // duplicate - LongKey - }; - - struct _Indent { - _Indent(int value_): value(value_) {} - int value; - }; - - inline _Indent Indent(int value) { - return _Indent(value); - } - - struct _Alias { - _Alias(const std::string& content_): content(content_) {} - std::string content; - }; - - inline _Alias Alias(const std::string content) { - return _Alias(content); - } - - struct _Anchor { - _Anchor(const std::string& content_): content(content_) {} - std::string content; - }; + // output character set + EmitNonAscii, + EscapeNonAscii, - inline _Anchor Anchor(const std::string content) { - return _Anchor(content); - } - - struct _Tag { - struct Type { enum value { Verbatim, PrimaryHandle, NamedHandle }; }; - - explicit _Tag(const std::string& prefix_, const std::string& content_, Type::value type_) - : prefix(prefix_), content(content_), type(type_) - { - } - std::string prefix; - std::string content; - Type::value type; - }; - - inline _Tag VerbatimTag(const std::string content) { - return _Tag("", content, _Tag::Type::Verbatim); - } + // string manipulators + // Auto, // duplicate + SingleQuoted, + DoubleQuoted, + Literal, - inline _Tag LocalTag(const std::string content) { - return _Tag("", content, _Tag::Type::PrimaryHandle); - } + // bool manipulators + YesNoBool, // yes, no + TrueFalseBool, // true, false + OnOffBool, // on, off + UpperCase, // TRUE, N + LowerCase, // f, yes + CamelCase, // No, Off + LongBool, // yes, On + ShortBool, // y, t - inline _Tag LocalTag(const std::string& prefix, const std::string content) { - return _Tag(prefix, content, _Tag::Type::NamedHandle); - } + // int manipulators + Dec, + Hex, + Oct, - inline _Tag SecondaryTag(const std::string content) { - return _Tag("", content, _Tag::Type::NamedHandle); - } + // document manipulators + BeginDoc, + EndDoc, - struct _Comment { - _Comment(const std::string& content_): content(content_) {} - std::string content; - }; - - inline _Comment Comment(const std::string content) { - return _Comment(content); - } - - struct _Precision { - _Precision(int floatPrecision_, int doublePrecision_): floatPrecision(floatPrecision_), doublePrecision(doublePrecision_) {} - - int floatPrecision; - int doublePrecision; - }; - - inline _Precision FloatPrecision(int n) { - return _Precision(n, -1); - } + // sequence manipulators + BeginSeq, + EndSeq, + Flow, + Block, - inline _Precision DoublePrecision(int n) { - return _Precision(-1, n); - } + // map manipulators + BeginMap, + EndMap, + Key, + Value, + // Flow, // duplicate + // Block, // duplicate + // Auto, // duplicate + LongKey +}; - inline _Precision Precision(int n) { - return _Precision(n, n); - } +struct _Indent { + _Indent(int value_) : value(value_) {} + int value; +}; + +inline _Indent Indent(int value) { return _Indent(value); } + +struct _Alias { + _Alias(const std::string& content_) : content(content_) {} + std::string content; +}; + +inline _Alias Alias(const std::string content) { return _Alias(content); } + +struct _Anchor { + _Anchor(const std::string& content_) : content(content_) {} + std::string content; +}; + +inline _Anchor Anchor(const std::string content) { return _Anchor(content); } + +struct _Tag { + struct Type { + enum value { Verbatim, PrimaryHandle, NamedHandle }; + }; + + explicit _Tag(const std::string& prefix_, const std::string& content_, + Type::value type_) + : prefix(prefix_), content(content_), type(type_) {} + std::string prefix; + std::string content; + Type::value type; +}; + +inline _Tag VerbatimTag(const std::string content) { + return _Tag("", content, _Tag::Type::Verbatim); } -#endif // EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +inline _Tag LocalTag(const std::string content) { + return _Tag("", content, _Tag::Type::PrimaryHandle); +} + +inline _Tag LocalTag(const std::string& prefix, const std::string content) { + return _Tag(prefix, content, _Tag::Type::NamedHandle); +} + +inline _Tag SecondaryTag(const std::string content) { + return _Tag("", content, _Tag::Type::NamedHandle); +} + +struct _Comment { + _Comment(const std::string& content_) : content(content_) {} + std::string content; +}; + +inline _Comment Comment(const std::string content) { return _Comment(content); } + +struct _Precision { + _Precision(int floatPrecision_, int doublePrecision_) + : floatPrecision(floatPrecision_), doublePrecision(doublePrecision_) {} + + int floatPrecision; + int doublePrecision; +}; + +inline _Precision FloatPrecision(int n) { return _Precision(n, -1); } + +inline _Precision DoublePrecision(int n) { return _Precision(-1, n); } + +inline _Precision Precision(int n) { return _Precision(n, n); } +} + +#endif // EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/emitterstyle.h b/src/yamlcpp/yaml-cpp/emitterstyle.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/emitterstyle.h @@ -0,0 +1,16 @@ +#ifndef EMITTERSTYLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define EMITTERSTYLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +namespace YODA_YAML { +struct EmitterStyle { + enum value { Default, Block, Flow }; +}; +} + +#endif // EMITTERSTYLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/eventhandler.h b/src/yamlcpp/yaml-cpp/eventhandler.h --- a/src/yamlcpp/yaml-cpp/eventhandler.h +++ b/src/yamlcpp/yaml-cpp/eventhandler.h @@ -1,36 +1,40 @@ #ifndef EVENTHANDLER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EVENTHANDLER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif -#include "yaml-cpp/anchor.h" #include -namespace YODA_YAML -{ - struct Mark; - - class EventHandler - { - public: - virtual ~EventHandler() {} +#include "yaml-cpp/anchor.h" +#include "yaml-cpp/emitterstyle.h" - virtual void OnDocumentStart(const Mark& mark) = 0; - virtual void OnDocumentEnd() = 0; - - virtual void OnNull(const Mark& mark, anchor_t anchor) = 0; - virtual void OnAlias(const Mark& mark, anchor_t anchor) = 0; - virtual void OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value) = 0; +namespace YODA_YAML { +struct Mark; - virtual void OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor) = 0; - virtual void OnSequenceEnd() = 0; +class EventHandler { + public: + virtual ~EventHandler() {} - virtual void OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor) = 0; - virtual void OnMapEnd() = 0; - }; + virtual void OnDocumentStart(const Mark& mark) = 0; + virtual void OnDocumentEnd() = 0; + + virtual void OnNull(const Mark& mark, anchor_t anchor) = 0; + virtual void OnAlias(const Mark& mark, anchor_t anchor) = 0; + virtual void OnScalar(const Mark& mark, const std::string& tag, + anchor_t anchor, const std::string& value) = 0; + + virtual void OnSequenceStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style) = 0; + virtual void OnSequenceEnd() = 0; + + virtual void OnMapStart(const Mark& mark, const std::string& tag, + anchor_t anchor, EmitterStyle::value style) = 0; + virtual void OnMapEnd() = 0; +}; } -#endif // EVENTHANDLER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // EVENTHANDLER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/exceptions.h b/src/yamlcpp/yaml-cpp/exceptions.h --- a/src/yamlcpp/yaml-cpp/exceptions.h +++ b/src/yamlcpp/yaml-cpp/exceptions.h @@ -1,164 +1,267 @@ #ifndef EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include "yaml-cpp/mark.h" #include "yaml-cpp/traits.h" +#include #include #include -#include -namespace YODA_YAML -{ - // error messages - namespace ErrorMsg - { - const char * const YAML_DIRECTIVE_ARGS = "YAML directives must have exactly one argument"; - const char * const YAML_VERSION = "bad YAML version: "; - const char * const YAML_MAJOR_VERSION = "YAML major version too large"; - const char * const REPEATED_YAML_DIRECTIVE= "repeated YAML directive"; - const char * const TAG_DIRECTIVE_ARGS = "TAG directives must have exactly two arguments"; - const char * const REPEATED_TAG_DIRECTIVE = "repeated TAG directive"; - const char * const CHAR_IN_TAG_HANDLE = "illegal character found while scanning tag handle"; - const char * const TAG_WITH_NO_SUFFIX = "tag handle with no suffix"; - const char * const END_OF_VERBATIM_TAG = "end of verbatim tag not found"; - const char * const END_OF_MAP = "end of map not found"; - const char * const END_OF_MAP_FLOW = "end of map flow not found"; - const char * const END_OF_SEQ = "end of sequence not found"; - const char * const END_OF_SEQ_FLOW = "end of sequence flow not found"; - const char * const MULTIPLE_TAGS = "cannot assign multiple tags to the same node"; - const char * const MULTIPLE_ANCHORS = "cannot assign multiple anchors to the same node"; - const char * const MULTIPLE_ALIASES = "cannot assign multiple aliases to the same node"; - const char * const ALIAS_CONTENT = "aliases can't have any content, *including* tags"; - const char * const INVALID_HEX = "bad character found while scanning hex number"; - const char * const INVALID_UNICODE = "invalid unicode: "; - const char * const INVALID_ESCAPE = "unknown escape character: "; - const char * const UNKNOWN_TOKEN = "unknown token"; - const char * const DOC_IN_SCALAR = "illegal document indicator in scalar"; - const char * const EOF_IN_SCALAR = "illegal EOF in scalar"; - const char * const CHAR_IN_SCALAR = "illegal character in scalar"; - const char * const TAB_IN_INDENTATION = "illegal tab when looking for indentation"; - const char * const FLOW_END = "illegal flow end"; - const char * const BLOCK_ENTRY = "illegal block entry"; - const char * const MAP_KEY = "illegal map key"; - const char * const MAP_VALUE = "illegal map value"; - const char * const ALIAS_NOT_FOUND = "alias not found after *"; - const char * const ANCHOR_NOT_FOUND = "anchor not found after &"; - const char * const CHAR_IN_ALIAS = "illegal character found while scanning alias"; - const char * const CHAR_IN_ANCHOR = "illegal character found while scanning anchor"; - const char * const ZERO_INDENT_IN_BLOCK = "cannot set zero indentation for a block scalar"; - const char * const CHAR_IN_BLOCK = "unexpected character in block scalar"; - const char * const AMBIGUOUS_ANCHOR = "cannot assign the same alias to multiple nodes"; - const char * const UNKNOWN_ANCHOR = "the referenced anchor is not defined"; +// This is here for compatibility with older versions of Visual Studio +// which don't support noexcept +#ifdef _MSC_VER + #define YAML_CPP_NOEXCEPT _NOEXCEPT +#else + #define YAML_CPP_NOEXCEPT noexcept +#endif - const char * const INVALID_SCALAR = "invalid scalar"; - const char * const KEY_NOT_FOUND = "key not found"; - const char * const BAD_DEREFERENCE = "bad dereference"; - - const char * const UNMATCHED_GROUP_TAG = "unmatched group tag"; - const char * const UNEXPECTED_END_SEQ = "unexpected end sequence token"; - const char * const UNEXPECTED_END_MAP = "unexpected end map token"; - const char * const SINGLE_QUOTED_CHAR = "invalid character in single-quoted string"; - const char * const INVALID_ANCHOR = "invalid anchor"; - const char * const INVALID_ALIAS = "invalid alias"; - const char * const INVALID_TAG = "invalid tag"; - const char * const EXPECTED_KEY_TOKEN = "expected key token"; - const char * const EXPECTED_VALUE_TOKEN = "expected value token"; - const char * const UNEXPECTED_KEY_TOKEN = "unexpected key token"; - const char * const UNEXPECTED_VALUE_TOKEN = "unexpected value token"; +namespace YODA_YAML { +// error messages +namespace ErrorMsg { +const char* const YAML_DIRECTIVE_ARGS = + "YAML directives must have exactly one argument"; +const char* const YAML_VERSION = "bad YAML version: "; +const char* const YAML_MAJOR_VERSION = "YAML major version too large"; +const char* const REPEATED_YAML_DIRECTIVE = "repeated YAML directive"; +const char* const TAG_DIRECTIVE_ARGS = + "TAG directives must have exactly two arguments"; +const char* const REPEATED_TAG_DIRECTIVE = "repeated TAG directive"; +const char* const CHAR_IN_TAG_HANDLE = + "illegal character found while scanning tag handle"; +const char* const TAG_WITH_NO_SUFFIX = "tag handle with no suffix"; +const char* const END_OF_VERBATIM_TAG = "end of verbatim tag not found"; +const char* const END_OF_MAP = "end of map not found"; +const char* const END_OF_MAP_FLOW = "end of map flow not found"; +const char* const END_OF_SEQ = "end of sequence not found"; +const char* const END_OF_SEQ_FLOW = "end of sequence flow not found"; +const char* const MULTIPLE_TAGS = + "cannot assign multiple tags to the same node"; +const char* const MULTIPLE_ANCHORS = + "cannot assign multiple anchors to the same node"; +const char* const MULTIPLE_ALIASES = + "cannot assign multiple aliases to the same node"; +const char* const ALIAS_CONTENT = + "aliases can't have any content, *including* tags"; +const char* const INVALID_HEX = "bad character found while scanning hex number"; +const char* const INVALID_UNICODE = "invalid unicode: "; +const char* const INVALID_ESCAPE = "unknown escape character: "; +const char* const UNKNOWN_TOKEN = "unknown token"; +const char* const DOC_IN_SCALAR = "illegal document indicator in scalar"; +const char* const EOF_IN_SCALAR = "illegal EOF in scalar"; +const char* const CHAR_IN_SCALAR = "illegal character in scalar"; +const char* const TAB_IN_INDENTATION = + "illegal tab when looking for indentation"; +const char* const FLOW_END = "illegal flow end"; +const char* const BLOCK_ENTRY = "illegal block entry"; +const char* const MAP_KEY = "illegal map key"; +const char* const MAP_VALUE = "illegal map value"; +const char* const ALIAS_NOT_FOUND = "alias not found after *"; +const char* const ANCHOR_NOT_FOUND = "anchor not found after &"; +const char* const CHAR_IN_ALIAS = + "illegal character found while scanning alias"; +const char* const CHAR_IN_ANCHOR = + "illegal character found while scanning anchor"; +const char* const ZERO_INDENT_IN_BLOCK = + "cannot set zero indentation for a block scalar"; +const char* const CHAR_IN_BLOCK = "unexpected character in block scalar"; +const char* const AMBIGUOUS_ANCHOR = + "cannot assign the same alias to multiple nodes"; +const char* const UNKNOWN_ANCHOR = "the referenced anchor is not defined"; - template - inline const std::string KEY_NOT_FOUND_WITH_KEY(const T&, typename disable_if >::type * = 0) { - return KEY_NOT_FOUND; - } +const char* const INVALID_NODE = + "invalid node; this may result from using a map iterator as a sequence " + "iterator, or vice-versa"; +const char* const INVALID_SCALAR = "invalid scalar"; +const char* const KEY_NOT_FOUND = "key not found"; +const char* const BAD_CONVERSION = "bad conversion"; +const char* const BAD_DEREFERENCE = "bad dereference"; +const char* const BAD_SUBSCRIPT = "operator[] call on a scalar"; +const char* const BAD_PUSHBACK = "appending to a non-sequence"; +const char* const BAD_INSERT = "inserting in a non-convertible-to-map"; - inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) { - std::stringstream stream; - stream << KEY_NOT_FOUND << ": " << key; - return stream.str(); - } - - template - inline const std::string KEY_NOT_FOUND_WITH_KEY(const T& key, typename enable_if >::type * = 0) { - std::stringstream stream; - stream << KEY_NOT_FOUND << ": " << key; - return stream.str(); - } - } +const char* const UNMATCHED_GROUP_TAG = "unmatched group tag"; +const char* const UNEXPECTED_END_SEQ = "unexpected end sequence token"; +const char* const UNEXPECTED_END_MAP = "unexpected end map token"; +const char* const SINGLE_QUOTED_CHAR = + "invalid character in single-quoted string"; +const char* const INVALID_ANCHOR = "invalid anchor"; +const char* const INVALID_ALIAS = "invalid alias"; +const char* const INVALID_TAG = "invalid tag"; +const char* const BAD_FILE = "bad file"; - class Exception: public std::runtime_error { - public: - Exception(const Mark& mark_, const std::string& msg_) - : std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {} - virtual ~Exception() throw() {} - - Mark mark; - std::string msg; - - private: - static const std::string build_what(const Mark& mark, const std::string& msg) { - std::stringstream output; - output << "yaml-cpp: error at line " << mark.line+1 << ", column " << mark.column+1 << ": " << msg; - return output.str(); - } - }; - - class ParserException: public Exception { - public: - ParserException(const Mark& mark_, const std::string& msg_) - : Exception(mark_, msg_) {} - }; - - class RepresentationException: public Exception { - public: - RepresentationException(const Mark& mark_, const std::string& msg_) - : Exception(mark_, msg_) {} - }; - - // representation exceptions - class InvalidScalar: public RepresentationException { - public: - InvalidScalar(const Mark& mark_) - : RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {} - }; - - class KeyNotFound: public RepresentationException { - public: - template - KeyNotFound(const Mark& mark_, const T& key_) - : RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) {} - }; - - template - class TypedKeyNotFound: public KeyNotFound { - public: - TypedKeyNotFound(const Mark& mark_, const T& key_) - : KeyNotFound(mark_, key_), key(key_) {} - virtual ~TypedKeyNotFound() throw() {} - - T key; - }; - - template - inline TypedKeyNotFound MakeTypedKeyNotFound(const Mark& mark, const T& key) { - return TypedKeyNotFound (mark, key); - } - - class BadDereference: public RepresentationException { - public: - BadDereference() - : RepresentationException(Mark::null(), ErrorMsg::BAD_DEREFERENCE) {} - }; - - class EmitterException: public Exception { - public: - EmitterException(const std::string& msg_) - : Exception(Mark::null(), msg_) {} - }; +template +inline const std::string KEY_NOT_FOUND_WITH_KEY( + const T&, typename disable_if>::type* = 0) { + return KEY_NOT_FOUND; } -#endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) { + std::stringstream stream; + stream << KEY_NOT_FOUND << ": " << key; + return stream.str(); +} + +template +inline const std::string KEY_NOT_FOUND_WITH_KEY( + const T& key, typename enable_if>::type* = 0) { + std::stringstream stream; + stream << KEY_NOT_FOUND << ": " << key; + return stream.str(); +} +} + +class YAML_CPP_API Exception : public std::runtime_error { + public: + Exception(const Mark& mark_, const std::string& msg_) + : std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {} + virtual ~Exception() YAML_CPP_NOEXCEPT; + + Exception(const Exception&) = default; + + Mark mark; + std::string msg; + + private: + static const std::string build_what(const Mark& mark, + const std::string& msg) { + if (mark.is_null()) { + return msg.c_str(); + } + + std::stringstream output; + output << "yaml-cpp: error at line " << mark.line + 1 << ", column " + << mark.column + 1 << ": " << msg; + return output.str(); + } +}; + +class YAML_CPP_API ParserException : public Exception { + public: + ParserException(const Mark& mark_, const std::string& msg_) + : Exception(mark_, msg_) {} + ParserException(const ParserException&) = default; + virtual ~ParserException() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API RepresentationException : public Exception { + public: + RepresentationException(const Mark& mark_, const std::string& msg_) + : Exception(mark_, msg_) {} + RepresentationException(const RepresentationException&) = default; + virtual ~RepresentationException() YAML_CPP_NOEXCEPT; +}; + +// representation exceptions +class YAML_CPP_API InvalidScalar : public RepresentationException { + public: + InvalidScalar(const Mark& mark_) + : RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {} + InvalidScalar(const InvalidScalar&) = default; + virtual ~InvalidScalar() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API KeyNotFound : public RepresentationException { + public: + template + KeyNotFound(const Mark& mark_, const T& key_) + : RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) { + } + KeyNotFound(const KeyNotFound&) = default; + virtual ~KeyNotFound() YAML_CPP_NOEXCEPT; +}; + +template +class YAML_CPP_API TypedKeyNotFound : public KeyNotFound { + public: + TypedKeyNotFound(const Mark& mark_, const T& key_) + : KeyNotFound(mark_, key_), key(key_) {} + virtual ~TypedKeyNotFound() YAML_CPP_NOEXCEPT {} + + T key; +}; + +template +inline TypedKeyNotFound MakeTypedKeyNotFound(const Mark& mark, + const T& key) { + return TypedKeyNotFound(mark, key); +} + +class YAML_CPP_API InvalidNode : public RepresentationException { + public: + InvalidNode() + : RepresentationException(Mark::null_mark(), ErrorMsg::INVALID_NODE) {} + InvalidNode(const InvalidNode&) = default; + virtual ~InvalidNode() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API BadConversion : public RepresentationException { + public: + explicit BadConversion(const Mark& mark_) + : RepresentationException(mark_, ErrorMsg::BAD_CONVERSION) {} + BadConversion(const BadConversion&) = default; + virtual ~BadConversion() YAML_CPP_NOEXCEPT; +}; + +template +class TypedBadConversion : public BadConversion { + public: + explicit TypedBadConversion(const Mark& mark_) : BadConversion(mark_) {} +}; + +class YAML_CPP_API BadDereference : public RepresentationException { + public: + BadDereference() + : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_DEREFERENCE) {} + BadDereference(const BadDereference&) = default; + virtual ~BadDereference() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API BadSubscript : public RepresentationException { + public: + BadSubscript() + : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_SUBSCRIPT) {} + BadSubscript(const BadSubscript&) = default; + virtual ~BadSubscript() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API BadPushback : public RepresentationException { + public: + BadPushback() + : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_PUSHBACK) {} + BadPushback(const BadPushback&) = default; + virtual ~BadPushback() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API BadInsert : public RepresentationException { + public: + BadInsert() + : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_INSERT) {} + BadInsert(const BadInsert&) = default; + virtual ~BadInsert() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API EmitterException : public Exception { + public: + EmitterException(const std::string& msg_) + : Exception(Mark::null_mark(), msg_) {} + EmitterException(const EmitterException&) = default; + virtual ~EmitterException() YAML_CPP_NOEXCEPT; +}; + +class YAML_CPP_API BadFile : public Exception { + public: + BadFile() : Exception(Mark::null_mark(), ErrorMsg::BAD_FILE) {} + BadFile(const BadFile&) = default; + virtual ~BadFile() YAML_CPP_NOEXCEPT; +}; +} + +#undef YAML_CPP_NOEXCEPT + +#endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/mark.h b/src/yamlcpp/yaml-cpp/mark.h --- a/src/yamlcpp/yaml-cpp/mark.h +++ b/src/yamlcpp/yaml-cpp/mark.h @@ -1,26 +1,29 @@ #ifndef MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include "yaml-cpp/dll.h" -namespace YODA_YAML -{ - struct YAML_CPP_API Mark { - Mark(): pos(0), line(0), column(0) {} - - static const Mark null() { return Mark(-1, -1, -1); } - - int pos; - int line, column; - - private: - Mark(int pos_, int line_, int column_): pos(pos_), line(line_), column(column_) {} - }; +namespace YODA_YAML { +struct YAML_CPP_API Mark { + Mark() : pos(0), line(0), column(0) {} + + static const Mark null_mark() { return Mark(-1, -1, -1); } + + bool is_null() const { return pos == -1 && line == -1 && column == -1; } + + int pos; + int line, column; + + private: + Mark(int pos_, int line_, int column_) + : pos(pos_), line(line_), column(column_) {} +}; } -#endif // MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node.h b/src/yamlcpp/yaml-cpp/node.h --- a/src/yamlcpp/yaml-cpp/node.h +++ b/src/yamlcpp/yaml-cpp/node.h @@ -1,135 +1,135 @@ #ifndef NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif #include "yaml-cpp/dll.h" #include "yaml-cpp/exceptions.h" #include "yaml-cpp/mark.h" #include "yaml-cpp/noncopyable.h" #include "yaml-cpp/conversion.h" #include "yaml-cpp/iterator.h" #include "yaml-cpp/ltnode.h" -#include + #include #include #include #include namespace YODA_YAML { class AliasManager; class Content; class NodeOwnership; class Scanner; class Emitter; class EventHandler; struct NodeType { enum value { Null, Scalar, Sequence, Map }; }; class YAML_CPP_API Node: private noncopyable { public: friend class NodeOwnership; friend class NodeBuilder; Node(); ~Node(); void Clear(); std::unique_ptr Clone() const; void EmitEvents(EventHandler& eventHandler) const; void EmitEvents(AliasManager& am, EventHandler& eventHandler) const; NodeType::value Type() const { return m_type; } bool IsAliased() const; // file location of start of this node const Mark GetMark() const { return m_mark; } // accessors Iterator begin() const; Iterator end() const; std::size_t size() const; // extraction of scalars bool GetScalar(std::string& s) const; // we can specialize this for other values template bool Read(T& value) const; template const T to() const; template friend YAML_CPP_API typename enable_if >::type operator >> (const Node& node, T& value); // retrieval for maps and sequences template const Node *FindValue(const T& key) const; template const Node& operator [] (const T& key) const; // specific to maps const Node *FindValue(const char *key) const; const Node *FindValue(char *key) const; const Node& operator [] (const char *key) const; const Node& operator [] (char *key) const; // for tags const std::string& Tag() const { return m_tag; } // emitting friend YAML_CPP_API Emitter& operator << (Emitter& out, const Node& node); // ordering int Compare(const Node& rhs) const; friend bool operator < (const Node& n1, const Node& n2); private: explicit Node(NodeOwnership& owner); Node& CreateNode(); void Init(NodeType::value type, const Mark& mark, const std::string& tag); void MarkAsAliased(); void SetScalarData(const std::string& data); void Append(Node& node); void Insert(Node& key, Node& value); // helper for sequences template friend struct _FindFromNodeAtIndex; const Node *FindAtIndex(std::size_t i) const; // helper for maps template const Node& GetValue(const T& key) const; template const Node *FindValueForKey(const T& key) const; private: std::unique_ptr m_pOwnership; Mark m_mark; std::string m_tag; typedef std::vector node_seq; typedef std::map node_map; NodeType::value m_type; std::string m_scalarData; node_seq m_seqData; node_map m_mapData; }; } #include "yaml-cpp/nodeimpl.h" #include "yaml-cpp/nodereadimpl.h" #endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/convert.h b/src/yamlcpp/yaml-cpp/node/convert.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/convert.h @@ -0,0 +1,331 @@ +#ifndef NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include +#include +#include +#include +#include + +#include "yaml-cpp/binary.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/node/iterator.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/type.h" +#include "yaml-cpp/null.h" + +namespace YODA_YAML { +class Binary; +struct _Null; +template +struct convert; +} // namespace YODA_YAML + +namespace YODA_YAML { +namespace conversion { +inline bool IsInfinity(const std::string& input) { + return input == ".inf" || input == ".Inf" || input == ".INF" || + input == "+.inf" || input == "+.Inf" || input == "+.INF"; +} + +inline bool IsNegativeInfinity(const std::string& input) { + return input == "-.inf" || input == "-.Inf" || input == "-.INF"; +} + +inline bool IsNaN(const std::string& input) { + return input == ".nan" || input == ".NaN" || input == ".NAN"; +} +} + +// Node +template <> +struct convert { + static Node encode(const Node& rhs) { return rhs; } + + static bool decode(const Node& node, Node& rhs) { + rhs.reset(node); + return true; + } +}; + +// std::string +template <> +struct convert { + static Node encode(const std::string& rhs) { return Node(rhs); } + + static bool decode(const Node& node, std::string& rhs) { + if (!node.IsScalar()) + return false; + rhs = node.Scalar(); + return true; + } +}; + +// C-strings can only be encoded +template <> +struct convert { + static Node encode(const char*& rhs) { return Node(rhs); } +}; + +template +struct convert { + static Node encode(const char(&rhs)[N]) { return Node(rhs); } +}; + +template <> +struct convert<_Null> { + static Node encode(const _Null& /* rhs */) { return Node(); } + + static bool decode(const Node& node, _Null& /* rhs */) { + return node.IsNull(); + } +}; + +#define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \ + template <> \ + struct convert { \ + static Node encode(const type& rhs) { \ + std::stringstream stream; \ + stream.precision(std::numeric_limits::digits10 + 1); \ + stream << rhs; \ + return Node(stream.str()); \ + } \ + \ + static bool decode(const Node& node, type& rhs) { \ + if (node.Type() != NodeType::Scalar) \ + return false; \ + const std::string& input = node.Scalar(); \ + std::stringstream stream(input); \ + stream.unsetf(std::ios::dec); \ + if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) \ + return true; \ + if (std::numeric_limits::has_infinity) { \ + if (conversion::IsInfinity(input)) { \ + rhs = std::numeric_limits::infinity(); \ + return true; \ + } else if (conversion::IsNegativeInfinity(input)) { \ + rhs = negative_op std::numeric_limits::infinity(); \ + return true; \ + } \ + } \ + \ + if (std::numeric_limits::has_quiet_NaN && \ + conversion::IsNaN(input)) { \ + rhs = std::numeric_limits::quiet_NaN(); \ + return true; \ + } \ + \ + return false; \ + } \ + } + +#define YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(type) \ + YAML_DEFINE_CONVERT_STREAMABLE(type, -) + +#define YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(type) \ + YAML_DEFINE_CONVERT_STREAMABLE(type, +) + +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(int); +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(short); +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long); +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long long); +YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned); +YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned short); +YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned long); +YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned long long); + +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(char); +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(signed char); +YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned char); + +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(float); +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(double); +YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long double); + +#undef YAML_DEFINE_CONVERT_STREAMABLE_SIGNED +#undef YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED +#undef YAML_DEFINE_CONVERT_STREAMABLE + +// bool +template <> +struct convert { + static Node encode(bool rhs) { return rhs ? Node("true") : Node("false"); } + + YAML_CPP_API static bool decode(const Node& node, bool& rhs); +}; + +// std::map +template +struct convert> { + static Node encode(const std::map& rhs) { + Node node(NodeType::Map); + for (typename std::map::const_iterator it = rhs.begin(); + it != rhs.end(); ++it) + node.force_insert(it->first, it->second); + return node; + } + + static bool decode(const Node& node, std::map& rhs) { + if (!node.IsMap()) + return false; + + rhs.clear(); + for (const_iterator it = node.begin(); it != node.end(); ++it) +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs[it->first.template as()] = it->second.template as(); +#else + rhs[it->first.as()] = it->second.as(); +#endif + return true; + } +}; + +// std::vector +template +struct convert> { + static Node encode(const std::vector& rhs) { + Node node(NodeType::Sequence); + for (typename std::vector::const_iterator it = rhs.begin(); + it != rhs.end(); ++it) + node.push_back(*it); + return node; + } + + static bool decode(const Node& node, std::vector& rhs) { + if (!node.IsSequence()) + return false; + + rhs.clear(); + for (const_iterator it = node.begin(); it != node.end(); ++it) +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs.push_back(it->template as()); +#else + rhs.push_back(it->as()); +#endif + return true; + } +}; + +// std::list +template +struct convert> { + static Node encode(const std::list& rhs) { + Node node(NodeType::Sequence); + for (typename std::list::const_iterator it = rhs.begin(); + it != rhs.end(); ++it) + node.push_back(*it); + return node; + } + + static bool decode(const Node& node, std::list& rhs) { + if (!node.IsSequence()) + return false; + + rhs.clear(); + for (const_iterator it = node.begin(); it != node.end(); ++it) +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs.push_back(it->template as()); +#else + rhs.push_back(it->as()); +#endif + return true; + } +}; + +// std::array +template +struct convert> { + static Node encode(const std::array& rhs) { + Node node(NodeType::Sequence); + for (const auto& element : rhs) { + node.push_back(element); + } + return node; + } + + static bool decode(const Node& node, std::array& rhs) { + if (!isNodeValid(node)) { + return false; + } + + for (auto i = 0u; i < node.size(); ++i) { +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs[i] = node[i].template as(); +#else + rhs[i] = node[i].as(); +#endif + } + return true; + } + + private: + static bool isNodeValid(const Node& node) { + return node.IsSequence() && node.size() == N; + } +}; + +// std::pair +template +struct convert> { + static Node encode(const std::pair& rhs) { + Node node(NodeType::Sequence); + node.push_back(rhs.first); + node.push_back(rhs.second); + return node; + } + + static bool decode(const Node& node, std::pair& rhs) { + if (!node.IsSequence()) + return false; + if (node.size() != 2) + return false; + +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs.first = node[0].template as(); +#else + rhs.first = node[0].as(); +#endif +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs.second = node[1].template as(); +#else + rhs.second = node[1].as(); +#endif + return true; + } +}; + +// binary +template <> +struct convert { + static Node encode(const Binary& rhs) { + return Node(EncodeBase64(rhs.data(), rhs.size())); + } + + static bool decode(const Node& node, Binary& rhs) { + if (!node.IsScalar()) + return false; + + std::vector data = DecodeBase64(node.Scalar()); + if (data.empty() && !node.Scalar().empty()) + return false; + + rhs.swap(data); + return true; + } +}; +} + +#endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/bool_type.h b/src/yamlcpp/yaml-cpp/node/detail/bool_type.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/bool_type.h @@ -0,0 +1,26 @@ +#ifndef NODE_DETAIL_BOOL_TYPE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_DETAIL_BOOL_TYPE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +namespace YODA_YAML { +namespace detail { +struct unspecified_bool { + struct NOT_ALLOWED; + static void true_value(NOT_ALLOWED*) {} +}; +typedef void (*unspecified_bool_type)(unspecified_bool::NOT_ALLOWED*); +} +} + +#define YAML_CPP_OPERATOR_BOOL() \ + operator YODA_YAML::detail::unspecified_bool_type() const { \ + return this->operator!() ? 0 \ + : &YODA_YAML::detail::unspecified_bool::true_value; \ + } + +#endif // NODE_DETAIL_BOOL_TYPE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/impl.h b/src/yamlcpp/yaml-cpp/node/detail/impl.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/impl.h @@ -0,0 +1,185 @@ +#ifndef NODE_DETAIL_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_DETAIL_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/node/detail/node.h" +#include "yaml-cpp/node/detail/node_data.h" +#include + +namespace YODA_YAML { +namespace detail { +template +struct get_idx { + static node* get(const std::vector& /* sequence */, + const Key& /* key */, shared_memory_holder /* pMemory */) { + return 0; + } +}; + +template +struct get_idx::value && + !std::is_same::value>::type> { + static node* get(const std::vector& sequence, const Key& key, + shared_memory_holder /* pMemory */) { + return key < sequence.size() ? sequence[key] : 0; + } + + static node* get(std::vector& sequence, const Key& key, + shared_memory_holder pMemory) { + if (key > sequence.size() || (key > 0 && !sequence[key-1]->is_defined())) + return 0; + if (key == sequence.size()) + sequence.push_back(&pMemory->create_node()); + return sequence[key]; + } +}; + +template +struct get_idx::value>::type> { + static node* get(const std::vector& sequence, const Key& key, + shared_memory_holder pMemory) { + return key >= 0 ? get_idx::get( + sequence, static_cast(key), pMemory) + : 0; + } + static node* get(std::vector& sequence, const Key& key, + shared_memory_holder pMemory) { + return key >= 0 ? get_idx::get( + sequence, static_cast(key), pMemory) + : 0; + } +}; + +template +inline bool node::equals(const T& rhs, shared_memory_holder pMemory) { + T lhs; + if (convert::decode(Node(*this, pMemory), lhs)) { + return lhs == rhs; + } + return false; +} + +inline bool node::equals(const char* rhs, shared_memory_holder pMemory) { + return equals(rhs, pMemory); +} + +// indexing +template +inline node* node_data::get(const Key& key, + shared_memory_holder pMemory) const { + switch (m_type) { + case NodeType::Map: + break; + case NodeType::Undefined: + case NodeType::Null: + return NULL; + case NodeType::Sequence: + if (node* pNode = get_idx::get(m_sequence, key, pMemory)) + return pNode; + return NULL; + case NodeType::Scalar: + throw BadSubscript(); + } + + for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->equals(key, pMemory)) { + return it->second; + } + } + + return NULL; +} + +template +inline node& node_data::get(const Key& key, shared_memory_holder pMemory) { + switch (m_type) { + case NodeType::Map: + break; + case NodeType::Undefined: + case NodeType::Null: + case NodeType::Sequence: + if (node* pNode = get_idx::get(m_sequence, key, pMemory)) { + m_type = NodeType::Sequence; + return *pNode; + } + + convert_to_map(pMemory); + break; + case NodeType::Scalar: + throw BadSubscript(); + } + + for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->equals(key, pMemory)) { + return *it->second; + } + } + + node& k = convert_to_node(key, pMemory); + node& v = pMemory->create_node(); + insert_map_pair(k, v); + return v; +} + +template +inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) { + if (m_type != NodeType::Map) + return false; + + for (kv_pairs::iterator it = m_undefinedPairs.begin(); + it != m_undefinedPairs.end();) { + kv_pairs::iterator jt = std::next(it); + if (it->first->equals(key, pMemory)) + m_undefinedPairs.erase(it); + it = jt; + } + + for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->equals(key, pMemory)) { + m_map.erase(it); + return true; + } + } + + return false; +} + +// map +template +inline void node_data::force_insert(const Key& key, const Value& value, + shared_memory_holder pMemory) { + switch (m_type) { + case NodeType::Map: + break; + case NodeType::Undefined: + case NodeType::Null: + case NodeType::Sequence: + convert_to_map(pMemory); + break; + case NodeType::Scalar: + throw BadInsert(); + } + + node& k = convert_to_node(key, pMemory); + node& v = convert_to_node(value, pMemory); + insert_map_pair(k, v); +} + +template +inline node& node_data::convert_to_node(const T& rhs, + shared_memory_holder pMemory) { + Node value = convert::encode(rhs); + value.EnsureNodeExists(); + pMemory->merge(*value.m_pMemory); + return *value.m_pNode; +} +} +} + +#endif // NODE_DETAIL_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/iterator.h b/src/yamlcpp/yaml-cpp/node/detail/iterator.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/iterator.h @@ -0,0 +1,92 @@ +#ifndef VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/ptr.h" +#include "yaml-cpp/node/detail/node_iterator.h" +#include +#include + +namespace YODA_YAML { +namespace detail { +struct iterator_value; + +template +class iterator_base : public std::iterator { + + private: + template + friend class iterator_base; + struct enabler {}; + typedef node_iterator base_type; + + struct proxy { + explicit proxy(const V& x) : m_ref(x) {} + V* operator->() { return std::addressof(m_ref); } + operator V*() { return std::addressof(m_ref); } + + V m_ref; + }; + + public: + typedef typename iterator_base::value_type value_type; + + public: + iterator_base() : m_iterator(), m_pMemory() {} + explicit iterator_base(base_type rhs, shared_memory_holder pMemory) + : m_iterator(rhs), m_pMemory(pMemory) {} + + template + iterator_base(const iterator_base& rhs, + typename std::enable_if::value, + enabler>::type = enabler()) + : m_iterator(rhs.m_iterator), m_pMemory(rhs.m_pMemory) {} + + iterator_base& operator++() { + ++m_iterator; + return *this; + } + + iterator_base operator++(int) { + iterator_base iterator_pre(*this); + ++(*this); + return iterator_pre; + } + + template + bool operator==(const iterator_base& rhs) const { + return m_iterator == rhs.m_iterator; + } + + template + bool operator!=(const iterator_base& rhs) const { + return m_iterator != rhs.m_iterator; + } + + value_type operator*() const { + const typename base_type::value_type& v = *m_iterator; + if (v.pNode) + return value_type(Node(*v, m_pMemory)); + if (v.first && v.second) + return value_type(Node(*v.first, m_pMemory), Node(*v.second, m_pMemory)); + return value_type(); + } + + proxy operator->() const { return proxy(**this); } + + private: + base_type m_iterator; + shared_memory_holder m_pMemory; +}; +} +} + +#endif // VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/iterator_fwd.h b/src/yamlcpp/yaml-cpp/node/detail/iterator_fwd.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/iterator_fwd.h @@ -0,0 +1,27 @@ +#ifndef VALUE_DETAIL_ITERATOR_FWD_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_DETAIL_ITERATOR_FWD_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/dll.h" +#include +#include +#include + +namespace YODA_YAML { + +namespace detail { +struct iterator_value; +template +class iterator_base; +} + +typedef detail::iterator_base iterator; +typedef detail::iterator_base const_iterator; +} + +#endif // VALUE_DETAIL_ITERATOR_FWD_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/memory.h b/src/yamlcpp/yaml-cpp/node/detail/memory.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/memory.h @@ -0,0 +1,46 @@ +#ifndef VALUE_DETAIL_MEMORY_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_DETAIL_MEMORY_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/ptr.h" + +namespace YODA_YAML { +namespace detail { +class node; +} // namespace detail +} // namespace YODA_YAML + +namespace YODA_YAML { +namespace detail { +class YAML_CPP_API memory { + public: + node& create_node(); + void merge(const memory& rhs); + + private: + typedef std::set Nodes; + Nodes m_nodes; +}; + +class YAML_CPP_API memory_holder { + public: + memory_holder() : m_pMemory(new memory) {} + + node& create_node() { return m_pMemory->create_node(); } + void merge(memory_holder& rhs); + + private: + shared_memory m_pMemory; +}; +} +} + +#endif // VALUE_DETAIL_MEMORY_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/node.h b/src/yamlcpp/yaml-cpp/node/detail/node.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/node.h @@ -0,0 +1,169 @@ +#ifndef NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/emitterstyle.h" +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/type.h" +#include "yaml-cpp/node/ptr.h" +#include "yaml-cpp/node/detail/node_ref.h" +#include + +namespace YODA_YAML { +namespace detail { +class node { + public: + node() : m_pRef(new node_ref) {} + node(const node&) = delete; + node& operator=(const node&) = delete; + + bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; } + const node_ref* ref() const { return m_pRef.get(); } + + bool is_defined() const { return m_pRef->is_defined(); } + const Mark& mark() const { return m_pRef->mark(); } + NodeType::value type() const { return m_pRef->type(); } + + const std::string& scalar() const { return m_pRef->scalar(); } + const std::string& tag() const { return m_pRef->tag(); } + EmitterStyle::value style() const { return m_pRef->style(); } + + template + bool equals(const T& rhs, shared_memory_holder pMemory); + bool equals(const char* rhs, shared_memory_holder pMemory); + + void mark_defined() { + if (is_defined()) + return; + + m_pRef->mark_defined(); + for (nodes::iterator it = m_dependencies.begin(); + it != m_dependencies.end(); ++it) + (*it)->mark_defined(); + m_dependencies.clear(); + } + + void add_dependency(node& rhs) { + if (is_defined()) + rhs.mark_defined(); + else + m_dependencies.insert(&rhs); + } + + void set_ref(const node& rhs) { + if (rhs.is_defined()) + mark_defined(); + m_pRef = rhs.m_pRef; + } + void set_data(const node& rhs) { + if (rhs.is_defined()) + mark_defined(); + m_pRef->set_data(*rhs.m_pRef); + } + + void set_mark(const Mark& mark) { m_pRef->set_mark(mark); } + + void set_type(NodeType::value type) { + if (type != NodeType::Undefined) + mark_defined(); + m_pRef->set_type(type); + } + void set_null() { + mark_defined(); + m_pRef->set_null(); + } + void set_scalar(const std::string& scalar) { + mark_defined(); + m_pRef->set_scalar(scalar); + } + void set_tag(const std::string& tag) { + mark_defined(); + m_pRef->set_tag(tag); + } + + // style + void set_style(EmitterStyle::value style) { + mark_defined(); + m_pRef->set_style(style); + } + + // size/iterator + std::size_t size() const { return m_pRef->size(); } + + const_node_iterator begin() const { + return static_cast(*m_pRef).begin(); + } + node_iterator begin() { return m_pRef->begin(); } + + const_node_iterator end() const { + return static_cast(*m_pRef).end(); + } + node_iterator end() { return m_pRef->end(); } + + // sequence + void push_back(node& input, shared_memory_holder pMemory) { + m_pRef->push_back(input, pMemory); + input.add_dependency(*this); + } + void insert(node& key, node& value, shared_memory_holder pMemory) { + m_pRef->insert(key, value, pMemory); + key.add_dependency(*this); + value.add_dependency(*this); + } + + // indexing + template + node* get(const Key& key, shared_memory_holder pMemory) const { + // NOTE: this returns a non-const node so that the top-level Node can wrap + // it, and returns a pointer so that it can be NULL (if there is no such + // key). + return static_cast(*m_pRef).get(key, pMemory); + } + template + node& get(const Key& key, shared_memory_holder pMemory) { + node& value = m_pRef->get(key, pMemory); + value.add_dependency(*this); + return value; + } + template + bool remove(const Key& key, shared_memory_holder pMemory) { + return m_pRef->remove(key, pMemory); + } + + node* get(node& key, shared_memory_holder pMemory) const { + // NOTE: this returns a non-const node so that the top-level Node can wrap + // it, and returns a pointer so that it can be NULL (if there is no such + // key). + return static_cast(*m_pRef).get(key, pMemory); + } + node& get(node& key, shared_memory_holder pMemory) { + node& value = m_pRef->get(key, pMemory); + key.add_dependency(*this); + value.add_dependency(*this); + return value; + } + bool remove(node& key, shared_memory_holder pMemory) { + return m_pRef->remove(key, pMemory); + } + + // map + template + void force_insert(const Key& key, const Value& value, + shared_memory_holder pMemory) { + m_pRef->force_insert(key, value, pMemory); + } + + private: + shared_node_ref m_pRef; + typedef std::set nodes; + nodes m_dependencies; +}; +} +} + +#endif // NODE_DETAIL_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/node_data.h b/src/yamlcpp/yaml-cpp/node/detail/node_data.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/node_data.h @@ -0,0 +1,127 @@ +#ifndef VALUE_DETAIL_NODE_DATA_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_DETAIL_NODE_DATA_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include +#include +#include +#include + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/detail/node_iterator.h" +#include "yaml-cpp/node/iterator.h" +#include "yaml-cpp/node/ptr.h" +#include "yaml-cpp/node/type.h" + +namespace YODA_YAML { +namespace detail { +class node; +} // namespace detail +} // namespace YODA_YAML + +namespace YODA_YAML { +namespace detail { +class YAML_CPP_API node_data { + public: + node_data(); + node_data(const node_data&) = delete; + node_data& operator=(const node_data&) = delete; + + void mark_defined(); + void set_mark(const Mark& mark); + void set_type(NodeType::value type); + void set_tag(const std::string& tag); + void set_null(); + void set_scalar(const std::string& scalar); + void set_style(EmitterStyle::value style); + + bool is_defined() const { return m_isDefined; } + const Mark& mark() const { return m_mark; } + NodeType::value type() const { + return m_isDefined ? m_type : NodeType::Undefined; + } + const std::string& scalar() const { return m_scalar; } + const std::string& tag() const { return m_tag; } + EmitterStyle::value style() const { return m_style; } + + // size/iterator + std::size_t size() const; + + const_node_iterator begin() const; + node_iterator begin(); + + const_node_iterator end() const; + node_iterator end(); + + // sequence + void push_back(node& node, shared_memory_holder pMemory); + void insert(node& key, node& value, shared_memory_holder pMemory); + + // indexing + template + node* get(const Key& key, shared_memory_holder pMemory) const; + template + node& get(const Key& key, shared_memory_holder pMemory); + template + bool remove(const Key& key, shared_memory_holder pMemory); + + node* get(node& key, shared_memory_holder pMemory) const; + node& get(node& key, shared_memory_holder pMemory); + bool remove(node& key, shared_memory_holder pMemory); + + // map + template + void force_insert(const Key& key, const Value& value, + shared_memory_holder pMemory); + + public: + static std::string empty_scalar; + + private: + void compute_seq_size() const; + void compute_map_size() const; + + void reset_sequence(); + void reset_map(); + + void insert_map_pair(node& key, node& value); + void convert_to_map(shared_memory_holder pMemory); + void convert_sequence_to_map(shared_memory_holder pMemory); + + template + static node& convert_to_node(const T& rhs, shared_memory_holder pMemory); + + private: + bool m_isDefined; + Mark m_mark; + NodeType::value m_type; + std::string m_tag; + EmitterStyle::value m_style; + + // scalar + std::string m_scalar; + + // sequence + typedef std::vector node_seq; + node_seq m_sequence; + + mutable std::size_t m_seqSize; + + // map + typedef std::vector> node_map; + node_map m_map; + + typedef std::pair kv_pair; + typedef std::list kv_pairs; + mutable kv_pairs m_undefinedPairs; +}; +} +} + +#endif // VALUE_DETAIL_NODE_DATA_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/node_iterator.h b/src/yamlcpp/yaml-cpp/node/detail/node_iterator.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/node_iterator.h @@ -0,0 +1,180 @@ +#ifndef VALUE_DETAIL_NODE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_DETAIL_NODE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/ptr.h" +#include +#include +#include +#include +#include +#include + +namespace YODA_YAML { +namespace detail { +struct iterator_type { + enum value { NoneType, Sequence, Map }; +}; + +template +struct node_iterator_value : public std::pair { + typedef std::pair kv; + + node_iterator_value() : kv(), pNode(0) {} + explicit node_iterator_value(V& rhs) : kv(), pNode(&rhs) {} + explicit node_iterator_value(V& key, V& value) : kv(&key, &value), pNode(0) {} + + V& operator*() const { return *pNode; } + V& operator->() const { return *pNode; } + + V* pNode; +}; + +typedef std::vector node_seq; +typedef std::vector> node_map; + +template +struct node_iterator_type { + typedef node_seq::iterator seq; + typedef node_map::iterator map; +}; + +template +struct node_iterator_type { + typedef node_seq::const_iterator seq; + typedef node_map::const_iterator map; +}; + +template +class node_iterator_base + : public std::iterator, + std::ptrdiff_t, node_iterator_value*, + node_iterator_value> { + private: + struct enabler {}; + + struct proxy { + explicit proxy(const node_iterator_value& x) : m_ref(x) {} + node_iterator_value* operator->() { return std::addressof(m_ref); } + operator node_iterator_value*() { return std::addressof(m_ref); } + + node_iterator_value m_ref; + }; + + public: + typedef typename node_iterator_type::seq SeqIter; + typedef typename node_iterator_type::map MapIter; + typedef node_iterator_value value_type; + + node_iterator_base() + : m_type(iterator_type::NoneType), m_seqIt(), m_mapIt(), m_mapEnd() {} + explicit node_iterator_base(SeqIter seqIt) + : m_type(iterator_type::Sequence), + m_seqIt(seqIt), + m_mapIt(), + m_mapEnd() {} + explicit node_iterator_base(MapIter mapIt, MapIter mapEnd) + : m_type(iterator_type::Map), + m_seqIt(), + m_mapIt(mapIt), + m_mapEnd(mapEnd) { + m_mapIt = increment_until_defined(m_mapIt); + } + + template + node_iterator_base(const node_iterator_base& rhs, + typename std::enable_if::value, + enabler>::type = enabler()) + : m_type(rhs.m_type), + m_seqIt(rhs.m_seqIt), + m_mapIt(rhs.m_mapIt), + m_mapEnd(rhs.m_mapEnd) {} + + template + friend class node_iterator_base; + + template + bool operator==(const node_iterator_base& rhs) const { + if (m_type != rhs.m_type) + return false; + + switch (m_type) { + case iterator_type::NoneType: + return true; + case iterator_type::Sequence: + return m_seqIt == rhs.m_seqIt; + case iterator_type::Map: + return m_mapIt == rhs.m_mapIt; + } + return true; + } + + template + bool operator!=(const node_iterator_base& rhs) const { + return !(*this == rhs); + } + + node_iterator_base& operator++() { + switch (m_type) { + case iterator_type::NoneType: + break; + case iterator_type::Sequence: + ++m_seqIt; + break; + case iterator_type::Map: + ++m_mapIt; + m_mapIt = increment_until_defined(m_mapIt); + break; + } + return *this; + } + + node_iterator_base operator++(int) { + node_iterator_base iterator_pre(*this); + ++(*this); + return iterator_pre; + } + + value_type operator*() const { + switch (m_type) { + case iterator_type::NoneType: + return value_type(); + case iterator_type::Sequence: + return value_type(**m_seqIt); + case iterator_type::Map: + return value_type(*m_mapIt->first, *m_mapIt->second); + } + return value_type(); + } + + proxy operator->() const { return proxy(**this); } + + MapIter increment_until_defined(MapIter it) { + while (it != m_mapEnd && !is_defined(it)) + ++it; + return it; + } + + bool is_defined(MapIter it) const { + return it->first->is_defined() && it->second->is_defined(); + } + + private: + typename iterator_type::value m_type; + + SeqIter m_seqIt; + MapIter m_mapIt, m_mapEnd; +}; + +typedef node_iterator_base node_iterator; +typedef node_iterator_base const_node_iterator; +} +} + +#endif // VALUE_DETAIL_NODE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/detail/node_ref.h b/src/yamlcpp/yaml-cpp/node/detail/node_ref.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/detail/node_ref.h @@ -0,0 +1,98 @@ +#ifndef VALUE_DETAIL_NODE_REF_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_DETAIL_NODE_REF_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/type.h" +#include "yaml-cpp/node/ptr.h" +#include "yaml-cpp/node/detail/node_data.h" + +namespace YODA_YAML { +namespace detail { +class node_ref { + public: + node_ref() : m_pData(new node_data) {} + node_ref(const node_ref&) = delete; + node_ref& operator=(const node_ref&) = delete; + + bool is_defined() const { return m_pData->is_defined(); } + const Mark& mark() const { return m_pData->mark(); } + NodeType::value type() const { return m_pData->type(); } + const std::string& scalar() const { return m_pData->scalar(); } + const std::string& tag() const { return m_pData->tag(); } + EmitterStyle::value style() const { return m_pData->style(); } + + void mark_defined() { m_pData->mark_defined(); } + void set_data(const node_ref& rhs) { m_pData = rhs.m_pData; } + + void set_mark(const Mark& mark) { m_pData->set_mark(mark); } + void set_type(NodeType::value type) { m_pData->set_type(type); } + void set_tag(const std::string& tag) { m_pData->set_tag(tag); } + void set_null() { m_pData->set_null(); } + void set_scalar(const std::string& scalar) { m_pData->set_scalar(scalar); } + void set_style(EmitterStyle::value style) { m_pData->set_style(style); } + + // size/iterator + std::size_t size() const { return m_pData->size(); } + + const_node_iterator begin() const { + return static_cast(*m_pData).begin(); + } + node_iterator begin() { return m_pData->begin(); } + + const_node_iterator end() const { + return static_cast(*m_pData).end(); + } + node_iterator end() { return m_pData->end(); } + + // sequence + void push_back(node& node, shared_memory_holder pMemory) { + m_pData->push_back(node, pMemory); + } + void insert(node& key, node& value, shared_memory_holder pMemory) { + m_pData->insert(key, value, pMemory); + } + + // indexing + template + node* get(const Key& key, shared_memory_holder pMemory) const { + return static_cast(*m_pData).get(key, pMemory); + } + template + node& get(const Key& key, shared_memory_holder pMemory) { + return m_pData->get(key, pMemory); + } + template + bool remove(const Key& key, shared_memory_holder pMemory) { + return m_pData->remove(key, pMemory); + } + + node* get(node& key, shared_memory_holder pMemory) const { + return static_cast(*m_pData).get(key, pMemory); + } + node& get(node& key, shared_memory_holder pMemory) { + return m_pData->get(key, pMemory); + } + bool remove(node& key, shared_memory_holder pMemory) { + return m_pData->remove(key, pMemory); + } + + // map + template + void force_insert(const Key& key, const Value& value, + shared_memory_holder pMemory) { + m_pData->force_insert(key, value, pMemory); + } + + private: + shared_node_data m_pData; +}; +} +} + +#endif // VALUE_DETAIL_NODE_REF_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/emit.h b/src/yamlcpp/yaml-cpp/node/emit.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/emit.h @@ -0,0 +1,32 @@ +#ifndef NODE_EMIT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_EMIT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include + +#include "yaml-cpp/dll.h" + +namespace YODA_YAML { +class Emitter; +class Node; + +/** + * Emits the node to the given {@link Emitter}. If there is an error in writing, + * {@link Emitter#good} will return false. + */ +YAML_CPP_API Emitter& operator<<(Emitter& out, const Node& node); + +/** Emits the node to the given output stream. */ +YAML_CPP_API std::ostream& operator<<(std::ostream& out, const Node& node); + +/** Converts the node to a YAML string. */ +YAML_CPP_API std::string Dump(const Node& node); +} // namespace YODA_YAML + +#endif // NODE_EMIT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/impl.h b/src/yamlcpp/yaml-cpp/node/impl.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/impl.h @@ -0,0 +1,448 @@ +#ifndef NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/iterator.h" +#include "yaml-cpp/node/detail/memory.h" +#include "yaml-cpp/node/detail/node.h" +#include "yaml-cpp/exceptions.h" +#include + +namespace YODA_YAML { +inline Node::Node() : m_isValid(true), m_pNode(NULL) {} + +inline Node::Node(NodeType::value type) + : m_isValid(true), + m_pMemory(new detail::memory_holder), + m_pNode(&m_pMemory->create_node()) { + m_pNode->set_type(type); +} + +template +inline Node::Node(const T& rhs) + : m_isValid(true), + m_pMemory(new detail::memory_holder), + m_pNode(&m_pMemory->create_node()) { + Assign(rhs); +} + +inline Node::Node(const detail::iterator_value& rhs) + : m_isValid(rhs.m_isValid), + m_pMemory(rhs.m_pMemory), + m_pNode(rhs.m_pNode) {} + +inline Node::Node(const Node& rhs) + : m_isValid(rhs.m_isValid), + m_pMemory(rhs.m_pMemory), + m_pNode(rhs.m_pNode) {} + +inline Node::Node(Zombie) : m_isValid(false), m_pNode(NULL) {} + +inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory) + : m_isValid(true), m_pMemory(pMemory), m_pNode(&node) {} + +inline Node::~Node() {} + +inline void Node::EnsureNodeExists() const { + if (!m_isValid) + throw InvalidNode(); + if (!m_pNode) { + m_pMemory.reset(new detail::memory_holder); + m_pNode = &m_pMemory->create_node(); + m_pNode->set_null(); + } +} + +inline bool Node::IsDefined() const { + if (!m_isValid) { + return false; + } + return m_pNode ? m_pNode->is_defined() : true; +} + +inline Mark Node::Mark() const { + if (!m_isValid) { + throw InvalidNode(); + } + return m_pNode ? m_pNode->mark() : Mark::null_mark(); +} + +inline NodeType::value Node::Type() const { + if (!m_isValid) + throw InvalidNode(); + return m_pNode ? m_pNode->type() : NodeType::Null; +} + +// access + +// template helpers +template +struct as_if { + explicit as_if(const Node& node_) : node(node_) {} + const Node& node; + + T operator()(const S& fallback) const { + if (!node.m_pNode) + return fallback; + + T t; + if (convert::decode(node, t)) + return t; + return fallback; + } +}; + +template +struct as_if { + explicit as_if(const Node& node_) : node(node_) {} + const Node& node; + + std::string operator()(const S& fallback) const { + if (node.Type() != NodeType::Scalar) + return fallback; + return node.Scalar(); + } +}; + +template +struct as_if { + explicit as_if(const Node& node_) : node(node_) {} + const Node& node; + + T operator()() const { + if (!node.m_pNode) + throw TypedBadConversion(node.Mark()); + + T t; + if (convert::decode(node, t)) + return t; + throw TypedBadConversion(node.Mark()); + } +}; + +template <> +struct as_if { + explicit as_if(const Node& node_) : node(node_) {} + const Node& node; + + std::string operator()() const { + if (node.Type() != NodeType::Scalar) + throw TypedBadConversion(node.Mark()); + return node.Scalar(); + } +}; + +// access functions +template +inline T Node::as() const { + if (!m_isValid) + throw InvalidNode(); + return as_if(*this)(); +} + +template +inline T Node::as(const S& fallback) const { + if (!m_isValid) + return fallback; + return as_if(*this)(fallback); +} + +inline const std::string& Node::Scalar() const { + if (!m_isValid) + throw InvalidNode(); + return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar; +} + +inline const std::string& Node::Tag() const { + if (!m_isValid) + throw InvalidNode(); + return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar; +} + +inline void Node::SetTag(const std::string& tag) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + m_pNode->set_tag(tag); +} + +inline EmitterStyle::value Node::Style() const { + if (!m_isValid) + throw InvalidNode(); + return m_pNode ? m_pNode->style() : EmitterStyle::Default; +} + +inline void Node::SetStyle(EmitterStyle::value style) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + m_pNode->set_style(style); +} + +// assignment +inline bool Node::is(const Node& rhs) const { + if (!m_isValid || !rhs.m_isValid) + throw InvalidNode(); + if (!m_pNode || !rhs.m_pNode) + return false; + return m_pNode->is(*rhs.m_pNode); +} + +template +inline Node& Node::operator=(const T& rhs) { + if (!m_isValid) + throw InvalidNode(); + Assign(rhs); + return *this; +} + +inline void Node::reset(const YODA_YAML::Node& rhs) { + if (!m_isValid || !rhs.m_isValid) + throw InvalidNode(); + m_pMemory = rhs.m_pMemory; + m_pNode = rhs.m_pNode; +} + +template +inline void Node::Assign(const T& rhs) { + if (!m_isValid) + throw InvalidNode(); + AssignData(convert::encode(rhs)); +} + +template <> +inline void Node::Assign(const std::string& rhs) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + m_pNode->set_scalar(rhs); +} + +inline void Node::Assign(const char* rhs) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + m_pNode->set_scalar(rhs); +} + +inline void Node::Assign(char* rhs) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + m_pNode->set_scalar(rhs); +} + +inline Node& Node::operator=(const Node& rhs) { + if (!m_isValid || !rhs.m_isValid) + throw InvalidNode(); + if (is(rhs)) + return *this; + AssignNode(rhs); + return *this; +} + +inline void Node::AssignData(const Node& rhs) { + if (!m_isValid || !rhs.m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + rhs.EnsureNodeExists(); + + m_pNode->set_data(*rhs.m_pNode); + m_pMemory->merge(*rhs.m_pMemory); +} + +inline void Node::AssignNode(const Node& rhs) { + if (!m_isValid || !rhs.m_isValid) + throw InvalidNode(); + rhs.EnsureNodeExists(); + + if (!m_pNode) { + m_pNode = rhs.m_pNode; + m_pMemory = rhs.m_pMemory; + return; + } + + m_pNode->set_ref(*rhs.m_pNode); + m_pMemory->merge(*rhs.m_pMemory); + m_pNode = rhs.m_pNode; +} + +// size/iterator +inline std::size_t Node::size() const { + if (!m_isValid) + throw InvalidNode(); + return m_pNode ? m_pNode->size() : 0; +} + +inline const_iterator Node::begin() const { + if (!m_isValid) + return const_iterator(); + return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory) + : const_iterator(); +} + +inline iterator Node::begin() { + if (!m_isValid) + return iterator(); + return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator(); +} + +inline const_iterator Node::end() const { + if (!m_isValid) + return const_iterator(); + return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator(); +} + +inline iterator Node::end() { + if (!m_isValid) + return iterator(); + return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator(); +} + +// sequence +template +inline void Node::push_back(const T& rhs) { + if (!m_isValid) + throw InvalidNode(); + push_back(Node(rhs)); +} + +inline void Node::push_back(const Node& rhs) { + if (!m_isValid || !rhs.m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + rhs.EnsureNodeExists(); + + m_pNode->push_back(*rhs.m_pNode, m_pMemory); + m_pMemory->merge(*rhs.m_pMemory); +} + +// helpers for indexing +namespace detail { +template +struct to_value_t { + explicit to_value_t(const T& t_) : t(t_) {} + const T& t; + typedef const T& return_type; + + const T& operator()() const { return t; } +}; + +template <> +struct to_value_t { + explicit to_value_t(const char* t_) : t(t_) {} + const char* t; + typedef std::string return_type; + + const std::string operator()() const { return t; } +}; + +template <> +struct to_value_t { + explicit to_value_t(char* t_) : t(t_) {} + const char* t; + typedef std::string return_type; + + const std::string operator()() const { return t; } +}; + +template +struct to_value_t { + explicit to_value_t(const char* t_) : t(t_) {} + const char* t; + typedef std::string return_type; + + const std::string operator()() const { return t; } +}; + +// converts C-strings to std::strings so they can be copied +template +inline typename to_value_t::return_type to_value(const T& t) { + return to_value_t(t)(); +} +} + +// indexing +template +inline const Node Node::operator[](const Key& key) const { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + detail::node* value = static_cast(*m_pNode) + .get(detail::to_value(key), m_pMemory); + if (!value) { + return Node(ZombieNode); + } + return Node(*value, m_pMemory); +} + +template +inline Node Node::operator[](const Key& key) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory); + return Node(value, m_pMemory); +} + +template +inline bool Node::remove(const Key& key) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + return m_pNode->remove(detail::to_value(key), m_pMemory); +} + +inline const Node Node::operator[](const Node& key) const { + if (!m_isValid || !key.m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + key.EnsureNodeExists(); + m_pMemory->merge(*key.m_pMemory); + detail::node* value = + static_cast(*m_pNode).get(*key.m_pNode, m_pMemory); + if (!value) { + return Node(ZombieNode); + } + return Node(*value, m_pMemory); +} + +inline Node Node::operator[](const Node& key) { + if (!m_isValid || !key.m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + key.EnsureNodeExists(); + m_pMemory->merge(*key.m_pMemory); + detail::node& value = m_pNode->get(*key.m_pNode, m_pMemory); + return Node(value, m_pMemory); +} + +inline bool Node::remove(const Node& key) { + if (!m_isValid || !key.m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + key.EnsureNodeExists(); + return m_pNode->remove(*key.m_pNode, m_pMemory); +} + +// map +template +inline void Node::force_insert(const Key& key, const Value& value) { + if (!m_isValid) + throw InvalidNode(); + EnsureNodeExists(); + m_pNode->force_insert(detail::to_value(key), detail::to_value(value), + m_pMemory); +} + +// free functions +inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); } +} + +#endif // NODE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/iterator.h b/src/yamlcpp/yaml-cpp/node/iterator.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/iterator.h @@ -0,0 +1,31 @@ +#ifndef VALUE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/detail/iterator_fwd.h" +#include "yaml-cpp/node/detail/iterator.h" +#include +#include +#include + +namespace YODA_YAML { +namespace detail { +struct iterator_value : public Node, std::pair { + iterator_value() {} + explicit iterator_value(const Node& rhs) + : Node(rhs), + std::pair(Node(Node::ZombieNode), Node(Node::ZombieNode)) {} + explicit iterator_value(const Node& key, const Node& value) + : Node(Node::ZombieNode), std::pair(key, value) {} +}; +} +} + +#endif // VALUE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/node.h b/src/yamlcpp/yaml-cpp/node/node.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/node.h @@ -0,0 +1,145 @@ +#ifndef NODE_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include + +#include "yaml-cpp/dll.h" +#include "yaml-cpp/emitterstyle.h" +#include "yaml-cpp/mark.h" +#include "yaml-cpp/node/detail/bool_type.h" +#include "yaml-cpp/node/detail/iterator_fwd.h" +#include "yaml-cpp/node/ptr.h" +#include "yaml-cpp/node/type.h" + +namespace YODA_YAML { +namespace detail { +class node; +class node_data; +struct iterator_value; +} // namespace detail +} // namespace YODA_YAML + +namespace YODA_YAML { +class YAML_CPP_API Node { + public: + friend class NodeBuilder; + friend class NodeEvents; + friend struct detail::iterator_value; + friend class detail::node; + friend class detail::node_data; + template + friend class detail::iterator_base; + template + friend struct as_if; + + typedef YODA_YAML::iterator iterator; + typedef YODA_YAML::const_iterator const_iterator; + + Node(); + explicit Node(NodeType::value type); + template + explicit Node(const T& rhs); + explicit Node(const detail::iterator_value& rhs); + Node(const Node& rhs); + ~Node(); + + YODA_YAML::Mark Mark() const; + NodeType::value Type() const; + bool IsDefined() const; + bool IsNull() const { return Type() == NodeType::Null; } + bool IsScalar() const { return Type() == NodeType::Scalar; } + bool IsSequence() const { return Type() == NodeType::Sequence; } + bool IsMap() const { return Type() == NodeType::Map; } + + // bool conversions + YAML_CPP_OPERATOR_BOOL() + bool operator!() const { return !IsDefined(); } + + // access + template + T as() const; + template + T as(const S& fallback) const; + const std::string& Scalar() const; + + const std::string& Tag() const; + void SetTag(const std::string& tag); + + // style + // WARNING: This API might change in future releases. + EmitterStyle::value Style() const; + void SetStyle(EmitterStyle::value style); + + // assignment + bool is(const Node& rhs) const; + template + Node& operator=(const T& rhs); + Node& operator=(const Node& rhs); + void reset(const Node& rhs = Node()); + + // size/iterator + std::size_t size() const; + + const_iterator begin() const; + iterator begin(); + + const_iterator end() const; + iterator end(); + + // sequence + template + void push_back(const T& rhs); + void push_back(const Node& rhs); + + // indexing + template + const Node operator[](const Key& key) const; + template + Node operator[](const Key& key); + template + bool remove(const Key& key); + + const Node operator[](const Node& key) const; + Node operator[](const Node& key); + bool remove(const Node& key); + + // map + template + void force_insert(const Key& key, const Value& value); + + private: + enum Zombie { ZombieNode }; + explicit Node(Zombie); + explicit Node(detail::node& node, detail::shared_memory_holder pMemory); + + void EnsureNodeExists() const; + + template + void Assign(const T& rhs); + void Assign(const char* rhs); + void Assign(char* rhs); + + void AssignData(const Node& rhs); + void AssignNode(const Node& rhs); + + private: + bool m_isValid; + mutable detail::shared_memory_holder m_pMemory; + mutable detail::node* m_pNode; +}; + +YAML_CPP_API bool operator==(const Node& lhs, const Node& rhs); + +YAML_CPP_API Node Clone(const Node& node); + +template +struct convert; +} + +#endif // NODE_NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/parse.h b/src/yamlcpp/yaml-cpp/node/parse.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/parse.h @@ -0,0 +1,78 @@ +#ifndef VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include +#include + +#include "yaml-cpp/dll.h" + +namespace YODA_YAML { +class Node; + +/** + * Loads the input string as a single YAML document. + * + * @throws {@link ParserException} if it is malformed. + */ +YAML_CPP_API Node Load(const std::string& input); + +/** + * Loads the input string as a single YAML document. + * + * @throws {@link ParserException} if it is malformed. + */ +YAML_CPP_API Node Load(const char* input); + +/** + * Loads the input stream as a single YAML document. + * + * @throws {@link ParserException} if it is malformed. + */ +YAML_CPP_API Node Load(std::istream& input); + +/** + * Loads the input file as a single YAML document. + * + * @throws {@link ParserException} if it is malformed. + * @throws {@link BadFile} if the file cannot be loaded. + */ +YAML_CPP_API Node LoadFile(const std::string& filename); + +/** + * Loads the input string as a list of YAML documents. + * + * @throws {@link ParserException} if it is malformed. + */ +YAML_CPP_API std::vector LoadAll(const std::string& input); + +/** + * Loads the input string as a list of YAML documents. + * + * @throws {@link ParserException} if it is malformed. + */ +YAML_CPP_API std::vector LoadAll(const char* input); + +/** + * Loads the input stream as a list of YAML documents. + * + * @throws {@link ParserException} if it is malformed. + */ +YAML_CPP_API std::vector LoadAll(std::istream& input); + +/** + * Loads the input file as a list of YAML documents. + * + * @throws {@link ParserException} if it is malformed. + * @throws {@link BadFile} if the file cannot be loaded. + */ +YAML_CPP_API std::vector LoadAllFromFile(const std::string& filename); +} // namespace YODA_YAML + +#endif // VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/ptr.h b/src/yamlcpp/yaml-cpp/node/ptr.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/ptr.h @@ -0,0 +1,29 @@ +#ifndef VALUE_PTR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_PTR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/dll.h" +#include + +namespace YODA_YAML { +namespace detail { +class node; +class node_ref; +class node_data; +class memory; +class memory_holder; + +typedef std::shared_ptr shared_node; +typedef std::shared_ptr shared_node_ref; +typedef std::shared_ptr shared_node_data; +typedef std::shared_ptr shared_memory_holder; +typedef std::shared_ptr shared_memory; +} +} + +#endif // VALUE_PTR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/node/type.h b/src/yamlcpp/yaml-cpp/node/type.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/node/type.h @@ -0,0 +1,16 @@ +#ifndef VALUE_TYPE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_TYPE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +namespace YODA_YAML { +struct NodeType { + enum value { Undefined, Null, Scalar, Sequence, Map }; +}; +} + +#endif // VALUE_TYPE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/noncopyable.h b/src/yamlcpp/yaml-cpp/noncopyable.h --- a/src/yamlcpp/yaml-cpp/noncopyable.h +++ b/src/yamlcpp/yaml-cpp/noncopyable.h @@ -1,25 +1,25 @@ #ifndef NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif #include "yaml-cpp/dll.h" -namespace YODA_YAML -{ - // this is basically boost::noncopyable - class YAML_CPP_API noncopyable - { - protected: - noncopyable() {} - ~noncopyable() {} - - private: - noncopyable(const noncopyable&); - const noncopyable& operator = (const noncopyable&); - }; +namespace YODA_YAML { +// this is basically boost::noncopyable +class YAML_CPP_API noncopyable { + protected: + noncopyable() {} + ~noncopyable() {} + + private: + noncopyable(const noncopyable&); + const noncopyable& operator=(const noncopyable&); +}; } -#endif // NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/null.h b/src/yamlcpp/yaml-cpp/null.h --- a/src/yamlcpp/yaml-cpp/null.h +++ b/src/yamlcpp/yaml-cpp/null.h @@ -1,25 +1,26 @@ #ifndef NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include "yaml-cpp/dll.h" +#include -#include "yaml-cpp/dll.h" +namespace YODA_YAML { +class Node; -namespace YODA_YAML -{ - class Node; - - struct YAML_CPP_API _Null {}; - inline bool operator == (const _Null&, const _Null&) { return true; } - inline bool operator != (const _Null&, const _Null&) { return false; } - - YAML_CPP_API bool IsNull(const Node& node); // old API only - - extern YAML_CPP_API _Null Null; +struct YAML_CPP_API _Null {}; +inline bool operator==(const _Null&, const _Null&) { return true; } +inline bool operator!=(const _Null&, const _Null&) { return false; } + +YAML_CPP_API bool IsNull(const Node& node); // old API only +YAML_CPP_API bool IsNullString(const std::string& str); + +extern YAML_CPP_API _Null Null; } -#endif // NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/ostream_wrapper.h b/src/yamlcpp/yaml-cpp/ostream_wrapper.h new file mode 100644 --- /dev/null +++ b/src/yamlcpp/yaml-cpp/ostream_wrapper.h @@ -0,0 +1,72 @@ +#ifndef OSTREAM_WRAPPER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define OSTREAM_WRAPPER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include + +#include "yaml-cpp/dll.h" + +namespace YODA_YAML { +class YAML_CPP_API ostream_wrapper { + public: + ostream_wrapper(); + explicit ostream_wrapper(std::ostream& stream); + ~ostream_wrapper(); + + void write(const std::string& str); + void write(const char* str, std::size_t size); + + void set_comment() { m_comment = true; } + + const char* str() const { + if (m_pStream) { + return 0; + } else { + m_buffer[m_pos] = '\0'; + return &m_buffer[0]; + } + } + + std::size_t row() const { return m_row; } + std::size_t col() const { return m_col; } + std::size_t pos() const { return m_pos; } + bool comment() const { return m_comment; } + + private: + void update_pos(char ch); + + private: + mutable std::vector m_buffer; + std::ostream* const m_pStream; + + std::size_t m_pos; + std::size_t m_row, m_col; + bool m_comment; +}; + +template +inline ostream_wrapper& operator<<(ostream_wrapper& stream, + const char(&str)[N]) { + stream.write(str, N - 1); + return stream; +} + +inline ostream_wrapper& operator<<(ostream_wrapper& stream, + const std::string& str) { + stream.write(str); + return stream; +} + +inline ostream_wrapper& operator<<(ostream_wrapper& stream, char ch) { + stream.write(&ch, 1); + return stream; +} +} + +#endif // OSTREAM_WRAPPER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/parser.h b/src/yamlcpp/yaml-cpp/parser.h --- a/src/yamlcpp/yaml-cpp/parser.h +++ b/src/yamlcpp/yaml-cpp/parser.h @@ -1,51 +1,86 @@ #ifndef PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +#include +#include #include "yaml-cpp/dll.h" #include "yaml-cpp/noncopyable.h" -#include -#include -namespace YODA_YAML -{ - struct Directives; - struct Mark; - struct Token; - class EventHandler; - class Node; - class Scanner; +namespace YODA_YAML { +class EventHandler; +class Node; +class Scanner; +struct Directives; +struct Token; - class YAML_CPP_API Parser: private noncopyable - { - public: - Parser(); - Parser(std::istream& in); - ~Parser(); +/** + * A parser turns a stream of bytes into one stream of "events" per YAML + * document in the input stream. + */ +class YAML_CPP_API Parser : private noncopyable { + public: + /** Constructs an empty parser (with no input. */ + Parser(); - operator bool() const; + /** + * Constructs a parser from the given input stream. The input stream must + * live as long as the parser. + */ + explicit Parser(std::istream& in); - void Load(std::istream& in); - bool HandleNextDocument(EventHandler& eventHandler); + ~Parser(); - bool GetNextDocument(Node& document); // old API only + /** Evaluates to true if the parser has some valid input to be read. */ + explicit operator bool() const; - void PrintTokens(std::ostream& out); + /** + * Resets the parser with the given input stream. Any existing state is + * erased. + */ + void Load(std::istream& in); - private: - void ParseDirectives(); - void HandleDirective(const Token& token); - void HandleYamlDirective(const Token& token); - void HandleTagDirective(const Token& token); + /** + * Handles the next document by calling events on the {@code eventHandler}. + * + * @throw a ParserException on error. + * @return false if there are no more documents + */ + bool HandleNextDocument(EventHandler& eventHandler); - private: - std::unique_ptr m_pScanner; - std::unique_ptr m_pDirectives; - }; + void PrintTokens(std::ostream& out); + + private: + /** + * Reads any directives that are next in the queue, setting the internal + * {@code m_pDirectives} state. + */ + void ParseDirectives(); + + void HandleDirective(const Token& token); + + /** + * Handles a "YAML" directive, which should be of the form 'major.minor' (like + * a version number). + */ + void HandleYamlDirective(const Token& token); + + /** + * Handles a "TAG" directive, which should be of the form 'handle prefix', + * where 'handle' is converted to 'prefix' in the file. + */ + void HandleTagDirective(const Token& token); + + private: + std::unique_ptr m_pScanner; + std::unique_ptr m_pDirectives; +}; } -#endif // PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/stlemitter.h b/src/yamlcpp/yaml-cpp/stlemitter.h --- a/src/yamlcpp/yaml-cpp/stlemitter.h +++ b/src/yamlcpp/yaml-cpp/stlemitter.h @@ -1,51 +1,51 @@ #ifndef STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif - #include #include #include #include -namespace YODA_YAML -{ - template - inline Emitter& EmitSeq(Emitter& emitter, const Seq& seq) { - emitter << BeginSeq; - for(typename Seq::const_iterator it=seq.begin();it!=seq.end();++it) - emitter << *it; - emitter << EndSeq; - return emitter; - } - - template - inline Emitter& operator << (Emitter& emitter, const std::vector& v) { - return EmitSeq(emitter, v); - } - - template - inline Emitter& operator << (Emitter& emitter, const std::list& v) { - return EmitSeq(emitter, v); - } - - template - inline Emitter& operator << (Emitter& emitter, const std::set& v) { - return EmitSeq(emitter, v); - } - - template - inline Emitter& operator << (Emitter& emitter, const std::map& m) { - typedef typename std::map map; - emitter << BeginMap; - for(typename map::const_iterator it=m.begin();it!=m.end();++it) - emitter << Key << it->first << Value << it->second; - emitter << EndMap; - return emitter; - } +namespace YODA_YAML { +template +inline Emitter& EmitSeq(Emitter& emitter, const Seq& seq) { + emitter << BeginSeq; + for (typename Seq::const_iterator it = seq.begin(); it != seq.end(); ++it) + emitter << *it; + emitter << EndSeq; + return emitter; } -#endif // STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +template +inline Emitter& operator<<(Emitter& emitter, const std::vector& v) { + return EmitSeq(emitter, v); +} + +template +inline Emitter& operator<<(Emitter& emitter, const std::list& v) { + return EmitSeq(emitter, v); +} + +template +inline Emitter& operator<<(Emitter& emitter, const std::set& v) { + return EmitSeq(emitter, v); +} + +template +inline Emitter& operator<<(Emitter& emitter, const std::map& m) { + typedef typename std::map map; + emitter << BeginMap; + for (typename map::const_iterator it = m.begin(); it != m.end(); ++it) + emitter << Key << it->first << Value << it->second; + emitter << EndMap; + return emitter; +} +} + +#endif // STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/traits.h b/src/yamlcpp/yaml-cpp/traits.h --- a/src/yamlcpp/yaml-cpp/traits.h +++ b/src/yamlcpp/yaml-cpp/traits.h @@ -1,57 +1,103 @@ #ifndef TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif +namespace YODA_YAML { +template +struct is_numeric { + enum { value = false }; +}; -namespace YODA_YAML -{ - template - struct is_numeric { enum { value = false }; }; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +#if defined(_MSC_VER) && (_MSC_VER < 1310) +template <> +struct is_numeric<__int64> { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +#else +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +#endif +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; +template <> +struct is_numeric { + enum { value = true }; +}; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; -#if defined(_MSC_VER) && (_MSC_VER < 1310) - template <> struct is_numeric <__int64> { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; -#else - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; -#endif - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; - template <> struct is_numeric { enum { value = true }; }; +template +struct enable_if_c { + typedef T type; +}; - template - struct enable_if_c { - typedef T type; - }; +template +struct enable_if_c {}; - template - struct enable_if_c {}; +template +struct enable_if : public enable_if_c {}; - template - struct enable_if : public enable_if_c {}; +template +struct disable_if_c { + typedef T type; +}; - template - struct disable_if_c { - typedef T type; - }; +template +struct disable_if_c {}; - template - struct disable_if_c {}; - - template - struct disable_if : public disable_if_c {}; +template +struct disable_if : public disable_if_c {}; } -#endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - +#endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/yamlcpp/yaml-cpp/yaml.h b/src/yamlcpp/yaml-cpp/yaml.h --- a/src/yamlcpp/yaml-cpp/yaml.h +++ b/src/yamlcpp/yaml-cpp/yaml.h @@ -1,17 +1,24 @@ #ifndef YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#if defined(_MSC_VER) || \ + (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ + (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 #pragma once #endif #include "yaml-cpp/parser.h" #include "yaml-cpp/emitter.h" +#include "yaml-cpp/emitterstyle.h" #include "yaml-cpp/stlemitter.h" #include "yaml-cpp/exceptions.h" -#include "yaml-cpp/node.h" -#include "yaml-cpp/stlnode.h" -#include "yaml-cpp/iterator.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/node/convert.h" +#include "yaml-cpp/node/iterator.h" +#include "yaml-cpp/node/detail/impl.h" +#include "yaml-cpp/node/parse.h" +#include "yaml-cpp/node/emit.h" -#endif // YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#endif // YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66