Merge lp:~zorba-coders/zorba/feature-json_parser into lp:zorba

Proposed by Paul J. Lucas
Status: Superseded
Proposed branch: lp:~zorba-coders/zorba/feature-json_parser
Merge into: lp:zorba
Diff against target: 6628 lines (+5438/-72)
157 files modified
ChangeLog (+1/-0)
include/zorba/diagnostic.h (+4/-1)
include/zorba/internal/ztd.h (+2/-1)
include/zorba/pregenerated/diagnostic_list.h (+34/-0)
modules/com/zorba-xquery/www/modules/CMakeLists.txt (+4/-0)
modules/com/zorba-xquery/www/modules/converters/json-options.xsd (+60/-0)
modules/com/zorba-xquery/www/modules/converters/json.xq (+217/-0)
modules/com/zorba-xquery/www/modules/pregenerated/errors.xq (+69/-1)
src/context/static_context.cpp (+7/-0)
src/context/static_context.h (+1/-0)
src/diagnostics/diagnostic.cpp (+3/-0)
src/diagnostics/diagnostic_en.xml (+65/-0)
src/diagnostics/pregenerated/diagnostic_list.cpp (+51/-0)
src/diagnostics/pregenerated/dict_en.cpp (+19/-0)
src/diagnostics/qname.cpp (+6/-0)
src/functions/library.cpp (+2/-0)
src/functions/pregenerated/func_json.cpp (+87/-0)
src/functions/pregenerated/func_json.h (+79/-0)
src/functions/pregenerated/function_enum.h (+2/-0)
src/runtime/CMakeLists.txt (+3/-0)
src/runtime/full_text/ft_match.cpp (+2/-3)
src/runtime/json/common.cpp (+62/-0)
src/runtime/json/common.h (+118/-0)
src/runtime/json/json_impl.cpp (+217/-0)
src/runtime/json/jsonml_array.cpp (+285/-0)
src/runtime/json/jsonml_array.h (+41/-0)
src/runtime/json/pregenerated/json.cpp (+94/-0)
src/runtime/json/pregenerated/json.h (+114/-0)
src/runtime/json/snelson.cpp (+512/-0)
src/runtime/json/snelson.h (+41/-0)
src/runtime/spec/json/json.xml (+52/-0)
src/runtime/spec/mappings.xml (+4/-0)
src/runtime/visitors/pregenerated/planiter_visitor.h (+10/-0)
src/runtime/visitors/pregenerated/printer_visitor.cpp (+29/-0)
src/runtime/visitors/pregenerated/printer_visitor.h (+6/-0)
src/unit_tests/CMakeLists.txt (+2/-0)
src/unit_tests/json_parser.cpp (+636/-0)
src/unit_tests/unit_test_list.h (+1/-0)
src/unit_tests/unit_tests.cpp (+1/-0)
src/util/CMakeLists.txt (+2/-0)
src/util/json_parser.cpp (+662/-0)
src/util/json_parser.h (+570/-0)
src/util/mem_streambuf.cpp (+119/-0)
src/util/mem_streambuf.h (+108/-0)
src/util/omanip.h (+205/-57)
src/util/oseparator.h (+17/-5)
src/util/stl_util.h (+51/-2)
src/util/string_util.h (+2/-1)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-02.xml.res (+4/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-04.xml.res (+7/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-05.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-wikipedia.xml.res (+10/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-02.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-indent-wikipedia.xml.res (+11/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-none-wikipedia.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-some-wikipedia.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-null-handling.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-numbers-and-decimals.xml.res (+7/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-01.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-02.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-03.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-04.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-05.xml.res (+4/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-06.xml.res (+8/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-07.xml.res (+10/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-example.xml.res (+14/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-object-01.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-object-02.xml.res (+4/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-serialize.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-02.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-04.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-05.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-12.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-indent-example.xml.res (+11/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-none-example.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-object-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-object-02.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-object-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-parse.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-some-example.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-uncommon-chars.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-utf-8.xml.res (+3/-0)
test/rbkt/Queries/zorba/json/json-invalid-option-parameter.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-invalid-option-parameter.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-01.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-02.xq (+14/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-03.xq (+18/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-04.xq (+27/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-05.xq (+66/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-06.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-06.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-wikipedia.xq (+23/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-01.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-02.xq (+13/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-03.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-indent-wikipedia.xq (+20/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-none-wikipedia.xq (+19/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-some-wikipedia.xq (+20/-0)
test/rbkt/Queries/zorba/json/json-snelson-invalid-json.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-invalid-json.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-snelson-null-handling.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-numbers-and-decimals.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-01.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-02.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-03.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-04.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-05.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-06.xq (+7/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-07.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-empty.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-empty.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-example.xq (+20/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-object-01.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-object-02.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-serialize.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-01.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-02.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-03.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-04.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-05.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-06.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-06.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-07.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-07.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-08.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-08.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-09.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-09.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-10.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-10.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-11.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-11.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-12.xq (+11/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-comment-node.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-comment-node.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-indent-example.xq (+24/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-invalid-value-for-attribute.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-invalid-value-for-attribute.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-none-example.xq (+19/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-object-01.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-object-02.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-object-03.xq (+12/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-parse.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-some-example.xq (+24/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-type-value-missing.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-type-value-missing.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-uncommon-chars.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-utf-8.xq (+5/-0)
test/unit/CMakeLists.txt (+1/-0)
test/update/Queries/zorba/store/sc3.spec (+2/-0)
test/update/Queries/zorba/store/sc3_ex3.xq (+1/-1)
To merge this branch: bzr merge lp:~zorba-coders/zorba/feature-json_parser
Reviewer Review Type Date Requested Status
Sorin Marian Nasoi Needs Fixing
Matthias Brantner Needs Fixing
William Candillon Needs Fixing
Paul J. Lucas Approve
Review via email: mp+89616@code.launchpad.net

This proposal supersedes a proposal from 2012-01-18.

This proposal has been superseded by a proposal from 2012-02-02.

Description of the change

New JSON parser and module.
Fixed the missing quote in the documentation.
Added checking of stream state for manipulators.

To post a comment you must log in.
Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

I have resubmitted the merge proposal after committing the fix for the JSON tests.

Revision history for this message
William Candillon (wcandillon) wrote : Posted in a previous version of this proposal

The documentation is missing some examples.
The example section at http://docs.basex.org/wiki/JSON_Module is a good place to get inspired.

Why A chars have a backslash in error code descriptions:
ZJSE0001 if \a $xml is not a document or element node.
ZJSE0002 if \a $xml contains an element that is missing a required attribute.
ZJSE0003 if \a $xml contains an attribute having an illegal value.
ZJSE0004 if \a $xml contains an illegal element.
ZJSE0005 if \a $xml contains an illegal child element for a JSON type.
ZJSE0006 if \a $xml contains an illegal child element.
ZJSE0007 if \a $xml contains an illegal text node.
ZJSE0008 if \a $xml contains an illegal value for a JSON type.

review: Needs Fixing
Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

The \a is the doxygen way to say the following thing is an argument and should be italicized.

Revision history for this message
William Candillon (wcandillon) wrote : Posted in a previous version of this proposal

Doesn't seem to work for xqdoc

Revision history for this message
Paul J. Lucas (paul-lucas) :
review: Approve
Revision history for this message
William Candillon (wcandillon) wrote :

The module works great.
There is room for improvement regarding its documentation.
"There are many ways to represent JSON data in XML" -> There are two ways to represent JSON data in XML:
- bullet 1 (John Snelson's format)
- bullet 2 (JSON)
Then add two examples, you can take some inspiration at http://docs.basex.org/wiki/JSON_Module

For parse#1, add a small example.
For parse#2, add two small examples and some description of what is excepted for the option element (which namespace, example of schema instance instance)
For serialize#1, please elaborate on what the following means: "Serializes an XDM into JSON using one of the representations described above." and add two small examples.
For serialize#2, same comment than for serialize#1 + add some explanation of what is excepted for the option element.

review: Needs Fixing
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

No, the statement "There are many ways to represent JSON data in XML" is correct as it is. There *are* *many* ways: Zorba only implements *two* of those *many* ways.

Adding *two* examples is too much. API documentation isn't supposed to be the primary documentation.

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

I think William is right. We should have a couple of examples which help the user to get started with this module. Those could either be inline in the text or links generated using the @example xqdoc tag.

The serialize functions should be annotated %ann:streamable because they return a streamable string (see modules/com/zorba-xquery/www/modules/fetch.xq).

Some error codes lack a prefix (e.g. ZJPE0001). If I understood correctly, it should be zerr:ZJPE0001. Otherwise, the user doesn't know how to catch the error.

Under which circumstances does json:parse return the empty sequence? What does json:serialize return if the input is the empty sequence?

review: Needs Fixing
10617. By Paul J. Lucas

1. Added yet more documentation.
2. Added %ann:streamable.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote :

1) Should be possible to call json:serialize(json:parse(VALID_JSON))
where VALID_JSON is any valid JSON string
see failing test test/rbkt/zorba/json/json-snelson-serialize-parse
added bug lp:920717

2) array and object closed prematurely in json:parse
see failing tests test/rbkt/zorba/json/json-snelson-parse-array-06 and test/rbkt/zorba/json/json-snelson-parse-array-07
added bug lp:920719

3) When trying to parsing a valid JSON string with invalid JSON option parameter the error raised is:
http://www.w3.org/2005/xqt-errors:XPST0003
Please add a more useful error message: see failing test test/rbkt/zorba/json/json-invalid-option-parameter
added bug lp:920720

4) Parsing an empty value has wrong behavior for both JSON mappings (Snelson and JSON-ML).
Please see failing tests:
- test/rbkt/zorba/json/json-snelson-empty-value
- test/rbkt/zorba/json/json-jsonml-empty-value
added bug lp:920721

5) Comment nodes should be ignored by json:serialize
Please see failing tests:
- test/rbkt/zorba/json/json-snelson-serialize-object-03
- test/rbkt/zorba/json/json-snelson-serialize-array-12
added bug lp:920722

6) XQDoc issues:
- there is no @project tag for the new json.xq module: as a result, in the XQDoc documentation is generated in the www.zorba-xquery.com/modules/converters instead of data processing/data converters
Please add a @project data processing/data converters in the module description in order to fix this.

- first parse function does not state
zerr:ZJPE0006 as a possible error condition if the passes JSON string is invalid
See added test json-snelson-invalid-json.xq
Added bug lp:920724

review: Needs Fixing
10618. By Sorin Marian Nasoi <email address hidden>

Added some tests that are marked as EXPECTED_FAILURES with their corresponding Launchpad bugs.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote :

r10618 contains all the tests mentioned above.

The tests are passing because they are marked as EXPECTED_FAILURES.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

Why did you mark them as EXPECTED_FAILURE if they're not expected to fail? Presumably, you *want* them fixed, right?

10619. By Sorin Marian Nasoi <email address hidden>

Fixed the following XQDoc issues:
- added module Author
- added correct category
- added examples for all 4 functions
- added zerr:ZJPE0006

Revision history for this message
Chris Hillery (ceejatec) wrote :

EXPECTED_FAILURE does *not* mean "negative test". It means "this is broken, we know it's broken, and we're working on it". That's why you have to supply a bug number to the macro. It probably should be renamed "KNOWN_BUG".

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

It means "known bug" for things that are in the trunk. If it's on a branch (as this is), then either (1) it will be fixed before it's merged into the trunk (at which point the EXPECTED_FAILURE will have to be removed since it will no longer be failing -- which begs my question of, "Why put it in in the first place?") or (2) the test will be deemed invalid and removed.

10620. By Paul J. Lucas

Merge from trunk.

Revision history for this message
Chris Hillery (ceejatec) wrote :

Hmm... valid points. I think there's some value in using EXPECTED_FAILURE() anyway, since it documents the relationship to new bugs that are filed.

It does introduce the possibility of unintentionally merging a new bug onto the trunk, but the diff will clearly show a new EXPECTED_FAILURE() marker and I would hope that any reviewer would raise a serious question about that.

I guess I would say that it's probably unnecessary to add EXPECTED_FAILURE()s (and associated bugs) for small review comments. However, for anything which might reasonably take more than a few hours to fix, IMHO it's probably good practice just to help ensure that no issues get forgotten. In general I would leave that decision up to the team working on the branch in question, though.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote :

> It means "known bug" for things that are in the trunk. If it's on a branch
> (as this is), then either (1) it will be fixed before it's merged into the
> trunk (at which point the EXPECTED_FAILURE will have to be removed since it
> will no longer be failing -- which begs my question of, "Why put it in in the
> first place?") or (2) the test will be deemed invalid and removed.

Paul, IMO:
- adding some bugs on Launchpad
- adding tests that fail and marking them as KNOWN_ISSUE (a.k.a. EXPECTED_FAILURES)
was far better than adding a comment in your merge proposal.

Keep in mind that I have spent my time in order to give you a hand in pointing out the issues I found.

Adding separate bugs allows us to:
- discussed the raised issues separately
- make commits to fix them separately into the branch

And to prove my point: you already wrote 2 comments *without* even looking over *any* of the issues I have raised.

Anyway, I already committed a fix in the branch for bug lp:920724.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

Chris Hillery wrote: "I think there's some value in using EXPECTED_FAILURE() anyway, since it documents the relationship to new bugs that are filed."

The relationship to new bugs *from* _______?

Revision history for this message
Chris Hillery (ceejatec) wrote :

Documents the relationship of the newly-added failing tests to the bugs tracking those issues.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

IMO, you didn't *need* to add any comment to the merge proposal other than "Bugs filed" -- I can read the bugs myself.

Sorin wrote: "Keep in mind that I have spent my time in order to give you a hand in pointing out the issues I found."

It's all of our job to review each others' code under the "New Order" so please don't act like it was a personal favor.

I never questioned your adding separate bugs. For the record, I *like* separate bugs. I *only* questioned why you added EXPECTED_FAILURE for those bugs that, as I've pointed out, is unnecessary and not only creates more work for you (because you put them in) but more work for me (because I have to take them out).

Sorin wrote: "And to prove my point: you already wrote 2 comments *without* even looking over *any* of the issues I have raised."

*My* point has *nothing* to do with any bugs in particular. Chris understands my point. Do you?

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

@Chris: Hmmm... again, because this is a branch and no Zorba user will ever see this stuff, it's at best only marginally useful.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote :

Paul, let me ask you something: are you sure you are not missing the point?

IMHO the point is to fix the issues that were raised ASAP.

I have spent my time in order to give as many details as possible in order to fix them ASAP.
Also I have committed a fix in the branch for one of the opened bugs.

Please look over the bugs and let's try to fix them in order to commit the new improved JSON 2.0 module to the trunk.

If you think that the decision I took while reviewing your merge deserves further discussion let's put it on the next weekly conference agenda.

10621. By Paul J. Lucas

Removed virtually useless and potentially wrong EXPECTED_FAILURES.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote :

> IMO, you didn't *need* to add any comment to the merge proposal other than
> "Bugs filed" -- I can read the bugs myself.
FYI: Matthias asked me (in a separate email discussion) to add a small review of the issues I fount in the merge proposal: please take this issue with him.

> Sorin wrote: "Keep in mind that I have spent my time in order to give you a
> hand in pointing out the issues I found."
>
> It's all of our job to review each others' code under the "New Order" so
> please don't act like it was a personal favor.
OK, next time I will review your code I will simply add a one sentence in the merge proposal and keep adding comments because you clearly think this is better.

> I never questioned your adding separate bugs. For the record, I *like*
> separate bugs. I *only* questioned why you added EXPECTED_FAILURE for those
> bugs that, as I've pointed out, is unnecessary and not only creates more work
> for you (because you put them in) but more work for me (because I have to take
> them out).
I will never do this again.

> Sorin wrote: "And to prove my point: you already wrote 2 comments *without*
> even looking over *any* of the issues I have raised."
>
> *My* point has *nothing* to do with any bugs in particular. Chris understands
> my point. Do you?
No, I do not. IMHO you are missing the point, that is: let's try to fix the *bugs* and discuss the procedures later.
Do you understand my point?

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

No, the point is *not* to fix this issues ASAP. The feature is not a high-priority feature, so there's no reason to do this ASAP. My *only* point was why you added EXPECTED_FAILURE lines to the CMakeLists.txt file -- that's it.

But it's moot now since I've removed all the new EXPECTED_FAILURES that you've added. They're of no use to me. Now when I run the test suite and it reports 100% tests passed, I can be confident that I've fixed all the bugs and not have an errant EXPECTED_FAILURE in there.

In the future, please don't add EXPECTED_FAILURE lines to code on a *branch* -- thanks.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote :

> No, the point is *not* to fix this issues ASAP. The feature is not a high-
> priority feature, so there's no reason to do this ASAP. My *only* point was
> why you added EXPECTED_FAILURE lines to the CMakeLists.txt file -- that's it.
>
> But it's moot now since I've removed all the new EXPECTED_FAILURES that you've
> added. They're of no use to me. Now when I run the test suite and it reports
> 100% tests passed, I can be confident that I've fixed all the bugs and not
> have an errant EXPECTED_FAILURE in there.
>
> In the future, please don't add EXPECTED_FAILURE lines to code on a *branch*
> -- thanks.
My 2 cents: no matter what the brief description of the "ctest -R SOME_TEST" shows, one should *always* check out the details inside the ctest logs.

http://en.wikipedia.org/wiki/The_Devil_is_in_the_details

10622. By Paul J. Lucas

Added missing "return".

10623. By Paul J. Lucas

Added missing "return".

10624. By Paul J. Lucas

Renamed tests to conform to rest; fixed missing "return" (again).

10625. By Paul J. Lucas

Merge from trunk.

10626. By Paul J. Lucas

Re-enabled test.

10627. By Paul J. Lucas

Updated test for new JSON module options schema.

10628. By Paul J. Lucas

1. Added ZJPE0009 error ("illegal empty string").
2. Now checking for empty strings in module parse() functions.
3. Added json::parser::peek().

10629. By Paul J. Lucas

Test clean-up.

10630. By Paul J. Lucas

Fixed more bugs.

10631. By Paul J. Lucas

Merge from trunk.

10632. By Paul J. Lucas

Added T_const_ptr.

10633. By Paul J. Lucas

s/optimal_arg/call_traits/

10634. By Paul J. Lucas

Added T_const_ptr.

10635. By Sorin Marian Nasoi <email address hidden>

The new core JSON module version 2.0 also fixes a bug reported against the deprecated version 1.0 external JSON module:
lp:878508.

10636. By Paul J. Lucas

Merge from trunk.

10637. By Markos Zaharioudakis

fixed bug #920717

10638. By Paul J. Lucas

Clean-up.

10639. By Paul J. Lucas

Merge from trunk.

10640. By Paul J. Lucas

Yet another fix.

10641. By Paul J. Lucas

Yet another bug fixed.

10642. By Paul J. Lucas

Merge from trunk.

10643. By Paul J. Lucas

Merge from trunk.

10644. By Paul J. Lucas

More documentation.

10645. By Paul J. Lucas

Merge from trunk.

10646. By Paul J. Lucas

Added yet more examples to JSON documentation.

10647. By Paul J. Lucas

Clean-up; new test.

10648. By Paul J. Lucas

Fixed bug 930573.

10649. By Paul J. Lucas

Merge from trunk.

10650. By Paul J. Lucas

Removed data-converters tag.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2012-02-02 09:56:52 +0000
3+++ ChangeLog 2012-02-02 21:41:24 +0000
4@@ -34,6 +34,7 @@
5 * Fixed bug #918211 (xqueryx fulltext w3c conformance generation)
6 * Fixed bug #918157 (Add XQFTTS to validation queue)
7 * Fixed bug with unversioned modules with C++ external functions
8+ * Fixed bug #878508 (JSON Module not escaping escape characters)
9
10 version 2.1
11
12
13=== modified file 'include/zorba/diagnostic.h'
14--- include/zorba/diagnostic.h 2011-07-01 16:07:54 +0000
15+++ include/zorba/diagnostic.h 2012-02-02 21:41:24 +0000
16@@ -239,7 +239,10 @@
17 ZORBA_DEBUGGER, // Zorba Debugger
18 ZORBA_OS, // Operating System
19 ZORBA_SERIALIZATION,
20- ZORBA_STORE
21+ ZORBA_STORE,
22+
23+ JSON_PARSER,
24+ JSON_SERIALIZATION
25 };
26
27 /**
28
29=== modified file 'include/zorba/internal/ztd.h'
30--- include/zorba/internal/ztd.h 2011-08-23 13:32:16 +0000
31+++ include/zorba/internal/ztd.h 2012-02-02 21:41:24 +0000
32@@ -355,7 +355,8 @@
33 template<typename T> inline
34 typename std::enable_if<ZORBA_TR1_NS::is_pointer<T>::value,std::string>::type
35 to_string( T p ) {
36- return p ? to_string( *p ) : "<null>";
37+ typedef typename ZORBA_TR1_NS::remove_pointer<T>::type const* T_const_ptr;
38+ return p ? to_string( *static_cast<T_const_ptr>( p ) ) : "<null>";
39 }
40
41 /**
42
43=== modified file 'include/zorba/pregenerated/diagnostic_list.h'
44--- include/zorba/pregenerated/diagnostic_list.h 2011-12-21 14:40:33 +0000
45+++ include/zorba/pregenerated/diagnostic_list.h 2012-02-02 21:41:24 +0000
46@@ -746,6 +746,40 @@
47
48 extern ZORBA_DLL_PUBLIC ZorbaErrorCode XSST0010;
49
50+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0001_ILLEGAL_CHARACTER;
51+
52+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0002_ILLEGAL_CODEPOINT;
53+
54+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0003_ILLEGAL_ESCAPE;
55+
56+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0004_ILLEGAL_LITERAL;
57+
58+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0005_ILLEGAL_NUMBER;
59+
60+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0006_UNEXPECTED_TOKEN;
61+
62+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0007_UNTERMINATED_STRING;
63+
64+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0008_ILLEGAL_QNAME;
65+
66+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0009_ILLEGAL_EMPTY_STRING;
67+
68+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE;
69+
70+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0002_ELEMENT_MISSING_ATTRIBUTE;
71+
72+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0003_BAD_ATTRIBUTE_VALUE;
73+
74+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0004_BAD_ELEMENT;
75+
76+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0005_BAD_CHILD_ELEMENT;
77+
78+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0006_NO_ELEMENT_CHILD;
79+
80+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0007_NO_TEXT_CHILD;
81+
82+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0008_BAD_VALUE;
83+
84 } // namespace zerr
85
86 namespace zwarn {
87
88=== modified file 'modules/com/zorba-xquery/www/modules/CMakeLists.txt'
89--- modules/com/zorba-xquery/www/modules/CMakeLists.txt 2012-01-11 17:30:25 +0000
90+++ modules/com/zorba-xquery/www/modules/CMakeLists.txt 2012-02-02 21:41:24 +0000
91@@ -68,6 +68,10 @@
92 # Subdirectories
93 DECLARE_ZORBA_MODULE(FILE converters/base64.xq VERSION 2.0
94 URI "http://www.zorba-xquery.com/modules/converters/base64")
95+DECLARE_ZORBA_MODULE(FILE converters/json.xq VERSION 2.0
96+ URI "http://www.zorba-xquery.com/modules/converters/json")
97+DECLARE_ZORBA_SCHEMA(FILE converters/json-options.xsd
98+ URI "http://www.zorba-xquery.com/modules/converters/json-options")
99 DECLARE_ZORBA_MODULE(FILE introspection/sctx.xq VERSION 2.0
100 URI "http://www.zorba-xquery.com/modules/introspection/sctx")
101 DECLARE_ZORBA_MODULE(FILE xqdoc2xhtml/error.xq VERSION 2.0
102
103=== added file 'modules/com/zorba-xquery/www/modules/converters/json-options.xsd'
104--- modules/com/zorba-xquery/www/modules/converters/json-options.xsd 1970-01-01 00:00:00 +0000
105+++ modules/com/zorba-xquery/www/modules/converters/json-options.xsd 2012-02-02 21:41:24 +0000
106@@ -0,0 +1,60 @@
107+<!--
108+ ! Copyright 2006-2008 The FLWOR Foundation.
109+ !
110+ ! Licensed under the Apache License, Version 2.0 (the "License");
111+ ! you may not use this file except in compliance with the License.
112+ ! You may obtain a copy of the License at
113+ !
114+ ! http://www.apache.org/licenses/LICENSE-2.0
115+ !
116+ ! Unless required by applicable law or agreed to in writing, software
117+ ! distributed under the License is distributed on an "AS IS" BASIS,
118+ ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
119+ ! See the License for the specific language governing permissions and
120+ ! limitations under the License.
121+-->
122+
123+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
124+ xmlns:json="http://www.zorba-xquery.com/modules/converters/json-options"
125+ targetNamespace="http://www.zorba-xquery.com/modules/converters/json-options"
126+ elementFormDefault="qualified"
127+ attributeFormDefault="unqualified">
128+
129+ <xs:element name="options">
130+ <xs:complexType>
131+ <xs:all>
132+
133+ <xs:element name="json-format" minOccurs="1" maxOccurs="1">
134+ <xs:complexType>
135+ <xs:attribute name="value" use="required">
136+ <xs:simpleType>
137+ <xs:restriction base="xs:string">
138+ <xs:enumeration value="Snelson"/>
139+ <xs:enumeration value="JsonML-array"/>
140+ <xs:enumeration value="JsonML-object"/>
141+ </xs:restriction>
142+ </xs:simpleType>
143+ </xs:attribute>
144+ </xs:complexType>
145+ </xs:element>
146+
147+ <xs:element name="whitespace" minOccurs="0" maxOccurs="1">
148+ <xs:complexType>
149+ <xs:attribute name="value" use="required">
150+ <xs:simpleType>
151+ <xs:restriction base="xs:string">
152+ <xs:enumeration value="none"/>
153+ <xs:enumeration value="some"/>
154+ <xs:enumeration value="indent"/>
155+ </xs:restriction>
156+ </xs:simpleType>
157+ </xs:attribute>
158+ </xs:complexType>
159+ </xs:element>
160+
161+ </xs:all>
162+ </xs:complexType>
163+ </xs:element>
164+</xs:schema>
165+
166+<!-- vim:set et sw=2 ts=2: -->
167
168=== added file 'modules/com/zorba-xquery/www/modules/converters/json.xq'
169--- modules/com/zorba-xquery/www/modules/converters/json.xq 1970-01-01 00:00:00 +0000
170+++ modules/com/zorba-xquery/www/modules/converters/json.xq 2012-02-02 21:41:24 +0000
171@@ -0,0 +1,217 @@
172+(:
173+ : Copyright 2006-2009 The FLWOR Foundation.
174+ :
175+ : Licensed under the Apache License, Version 2.0 (the "License");
176+ : you may not use this file except in compliance with the License.
177+ : You may obtain a copy of the License at
178+ :
179+ : http://www.apache.org/licenses/LICENSE-2.0
180+ :
181+ : Unless required by applicable law or agreed to in writing, software
182+ : distributed under the License is distributed on an "AS IS" BASIS,
183+ : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184+ : See the License for the specific language governing permissions and
185+ : limitations under the License.
186+ :)
187+
188+xquery version "3.0";
189+
190+ (:~
191+ : Using this module, you can parse JSON data into XML, manipulate it like any
192+ : other XML data using XQuery, and serialize the result back as JSON.
193+ :
194+ : There are many ways to represent JSON data in XML, some loss-less ("round
195+ : tripable") and some lossy ("one way"). Loss-less representations preserve
196+ : the JSON data types <i>boolean</i>, <i>number</i>, and <i>null</i>; lossy
197+ : representations convert all data to strings.
198+ :
199+ : For a loss-less representation, Zorba implements that proposed by
200+ : <a href="http://john.snelson.org.uk/parsing-json-into-xquery">John Snelson</a>.
201+ : For example:
202+ : <pre>
203+ : &lt;json type="object"&gt;
204+ : &lt;pair name="firstName" type="string"&gt;John&lt;/pair&gt;
205+ : &lt;pair name="lastName" type="string"&gt;Smith&lt;/pair&gt;
206+ : &lt;pair name="address" type="object"&gt;
207+ : &lt;pair name="streetAddress" type="string"&gt;21 2nd Street&lt;/pair&gt;
208+ : &lt;pair name="city" type="string"&gt;New York&lt;/pair&gt;
209+ : &lt;pair name="state" type="string"&gt;NY&lt;/pair&gt;
210+ : &lt;pair name="postalCode" type="number"&gt;10021&lt;/pair&gt;
211+ : &lt;/pair&gt;
212+ : &lt;pair name="phoneNumbers" type="array"&gt;
213+ : &lt;item type="string"&gt;212 732-1234&lt;/item&gt;
214+ : &lt;item type="string"&gt;646 123-4567&lt;/item&gt;
215+ : &lt;/pair&gt;
216+ : &lt;/json&gt;
217+ : </pre>
218+ : For a lossy representation, Zorba implements
219+ : <a href="http://jsonml.org/">JsonML</a> (the array form).
220+ : For example:
221+ : <pre>
222+ : &lt;person created="2006-11-11T19:23" modified="2006-12-31T23:59"&gt;
223+ : &lt;firstName&gt;Robert&lt;/firstName&gt;
224+ : &lt;lastName&gt;Smith&lt;/lastName&gt;
225+ : &lt;address type="home"&gt;
226+ : &lt;street&gt;12345 Sixth Ave&lt;/street&gt;
227+ : &lt;city&gt;Anytown&lt;/city&gt;
228+ : &lt;state&gt;CA&lt;/state&gt;
229+ : &lt;postalCode&gt;98765-4321&lt;/postalCode&gt;
230+ : &lt;/address&gt;
231+ : &lt;/person&gt;
232+ : </pre>
233+ :
234+ : @author Paul J. Lucas
235+ : @project data processing/data converters
236+ :)
237+module namespace json = "http://www.zorba-xquery.com/modules/converters/json";
238+
239+import module namespace schema = "http://www.zorba-xquery.com/modules/schema";
240+
241+import schema namespace json-options =
242+ "http://www.zorba-xquery.com/modules/converters/json-options";
243+
244+declare namespace ann = "http://www.zorba-xquery.com/annotations";
245+declare namespace err = "http://www.w3.org/2005/xqt-errors";
246+declare namespace zerr = "http://www.zorba-xquery.com/errors";
247+
248+declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
249+declare option ver:module-version "2.0";
250+
251+(:~
252+ : Parses JSON data from a string and returns an XDM instance using one of the
253+ : representations described above.
254+ :
255+ : @param $json The JSON data to parse.
256+ : @param $options The parsing options.
257+ : @return said XDM instance.
258+ : @error err:XQDY0027 if $options can not be validated against the
259+ : json-options schema.
260+ : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
261+ : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
262+ : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
263+ : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
264+ : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
265+ : @error zerr:ZJPE0006 if $json is not a valid JSON string.
266+ : @error zerr:ZJPE0007 if $json contains an unterminated string.
267+ : @error zerr:ZJPE0008 if $json contains an illegal QName.
268+ : @example test/rbkt/Queries/zorba/json/json-jsonml_array-parse-01.xq
269+ :)
270+declare function json:parse(
271+ $json as xs:string?,
272+ $options as element(json-options:options)
273+) as element(*,xs:untyped)*
274+{
275+ let $validated-options := if ( schema:is-validated( $options ) ) then
276+ $options
277+ else
278+ validate { $options }
279+ return json:parse-internal( $json, $validated-options )
280+};
281+
282+(:~
283+ : Parses JSON data from a string and returns an XDM instance using the Snelson
284+ : representation described above.
285+ :
286+ : @param $json The JSON data to parse.
287+ : @return said XDM instance.
288+ : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
289+ : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
290+ : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
291+ : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
292+ : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
293+ : @error zerr:ZJPE0006 if $json is not a valid JSON string.
294+ : @error zerr:ZJPE0007 if $json contains an unterminated string.
295+ : @error zerr:ZJPE0008 if $json contains an illegal QName.
296+ : @example test/rbkt/Queries/zorba/json/json-snelson-parse-array-01.xq
297+ :)
298+declare function json:parse(
299+ $json as xs:string?
300+) as element(*,xs:untyped)*
301+{
302+ json:parse-internal(
303+ $json,
304+ validate {
305+ <options xmlns="http://www.zorba-xquery.com/modules/converters/json-options" >
306+ <json-format value="Snelson"/>
307+ </options>
308+ }
309+ )
310+};
311+
312+(:~
313+ : Serializes an XDM into JSON using one of the representations described
314+ : above.
315+ :
316+ : @param $xml The XDM to serialize.
317+ : @param $options The serializing options.
318+ : @return a JSON string.
319+ : @error err:XQDY0027 if $options can not be validated against the
320+ : json-options schema.
321+ : @error zerr:ZJSE0001 if $xml is not a document or element node.
322+ : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
323+ : attribute.
324+ : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
325+ : @error zerr:ZJSE0004 if $xml contains an illegal element.
326+ : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
327+ : type.
328+ : @error zerr:ZJSE0006 if $xml contains an illegal child element.
329+ : @error zerr:ZJSE0007 if $xml contains an illegal text node.
330+ : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
331+ : @example test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-01.xq
332+ :)
333+declare function json:serialize(
334+ $xml as item()*,
335+ $options as element(json-options:options)
336+) as xs:string
337+{
338+ let $validated-options := if ( schema:is-validated( $options ) ) then
339+ $options
340+ else
341+ validate { $options }
342+ return json:serialize-internal( $xml, $validated-options )
343+};
344+
345+(:~
346+ : Serializes an XDM into JSON using one of the representations described
347+ : above.
348+ :
349+ : @param $xml The XDM to serialize.
350+ : @return a JSON string.
351+ : @error zerr:ZJSE0001 if $xml is not a document or element node.
352+ : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
353+ : attribute.
354+ : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
355+ : @error zerr:ZJSE0004 if $xml contains an illegal element.
356+ : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
357+ : type.
358+ : @error zerr:ZJSE0006 if $xml contains an illegal child element.
359+ : @error zerr:ZJSE0007 if $xml contains an illegal text node.
360+ : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
361+ : @example test/rbkt/Queries/zorba/json/json-snelson-serialize-array-01.xq
362+ :)
363+declare function json:serialize(
364+ $xml as item()*
365+) as xs:string
366+{
367+ json:serialize-internal($xml,
368+ validate {
369+ <options xmlns="http://www.zorba-xquery.com/modules/converters/json-options" >
370+ <json-format value="Snelson"/>
371+ </options>
372+ }
373+ )
374+};
375+
376+(:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
377+
378+declare %private function json:parse-internal(
379+ $json as xs:string?,
380+ $options as item()?
381+) as element()* external;
382+
383+declare %ann:streamable %private function json:serialize-internal(
384+ $xml as item()*,
385+ $options as item()?
386+) as xs:string external;
387+
388+(: vim:set et sw=2 ts=2: :)
389
390=== modified file 'modules/com/zorba-xquery/www/modules/pregenerated/errors.xq'
391--- modules/com/zorba-xquery/www/modules/pregenerated/errors.xq 2011-12-21 14:40:33 +0000
392+++ modules/com/zorba-xquery/www/modules/pregenerated/errors.xq 2012-02-02 21:41:24 +0000
393@@ -784,4 +784,72 @@
394
395 (:~
396 :)
397-declare variable $zerr:XSST0010 as xs:QName := fn:QName($zerr:NS, "zerr:XSST0010");
398\ No newline at end of file
399+declare variable $zerr:XSST0010 as xs:QName := fn:QName($zerr:NS, "zerr:XSST0010");
400+
401+(:~
402+:)
403+declare variable $zerr:ZJPE0001 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0001");
404+
405+(:~
406+:)
407+declare variable $zerr:ZJPE0002 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0002");
408+
409+(:~
410+:)
411+declare variable $zerr:ZJPE0003 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0003");
412+
413+(:~
414+:)
415+declare variable $zerr:ZJPE0004 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0004");
416+
417+(:~
418+:)
419+declare variable $zerr:ZJPE0005 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0005");
420+
421+(:~
422+:)
423+declare variable $zerr:ZJPE0006 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0006");
424+
425+(:~
426+:)
427+declare variable $zerr:ZJPE0007 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0007");
428+
429+(:~
430+:)
431+declare variable $zerr:ZJPE0008 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0008");
432+
433+(:~
434+:)
435+declare variable $zerr:ZJPE0009 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0009");
436+
437+(:~
438+:)
439+declare variable $zerr:ZJSE0001 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0001");
440+
441+(:~
442+:)
443+declare variable $zerr:ZJSE0002 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0002");
444+
445+(:~
446+:)
447+declare variable $zerr:ZJSE0003 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0003");
448+
449+(:~
450+:)
451+declare variable $zerr:ZJSE0004 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0004");
452+
453+(:~
454+:)
455+declare variable $zerr:ZJSE0005 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0005");
456+
457+(:~
458+:)
459+declare variable $zerr:ZJSE0006 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0006");
460+
461+(:~
462+:)
463+declare variable $zerr:ZJSE0007 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0007");
464+
465+(:~
466+:)
467+declare variable $zerr:ZJSE0008 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0008");
468\ No newline at end of file
469
470=== modified file 'src/context/static_context.cpp'
471--- src/context/static_context.cpp 2012-02-02 09:56:52 +0000
472+++ src/context/static_context.cpp 2012-02-02 21:41:24 +0000
473@@ -280,6 +280,11 @@
474 static_context::ZORBA_BASE64_FN_NS =
475 "http://www.zorba-xquery.com/modules/converters/base64";
476
477+
478+const char*
479+static_context::ZORBA_JSON_FN_NS =
480+"http://www.zorba-xquery.com/modules/converters/json";
481+
482 const char*
483 static_context::ZORBA_NODEREF_FN_NS =
484 "http://www.zorba-xquery.com/modules/node-reference";
485@@ -436,6 +441,7 @@
486 ns == ZORBA_REFLECTION_FN_NS ||
487 ns == ZORBA_SCRIPTING_FN_NS ||
488 ns == ZORBA_STRING_FN_NS ||
489+ ns == ZORBA_JSON_FN_NS ||
490 ns == ZORBA_FETCH_FN_NS ||
491 ns == ZORBA_NODE_FN_NS ||
492 ns == ZORBA_XML_FN_NS);
493@@ -482,6 +488,7 @@
494 {
495 return (ns == ZORBA_MATH_FN_NS ||
496 ns == ZORBA_INTROSP_SCTX_FN_NS ||
497+ ns == ZORBA_JSON_FN_NS ||
498 ns == ZORBA_RANDOM_FN_NS);
499 }
500
501
502=== modified file 'src/context/static_context.h'
503--- src/context/static_context.h 2012-02-02 09:56:52 +0000
504+++ src/context/static_context.h 2012-02-02 21:41:24 +0000
505@@ -446,6 +446,7 @@
506 // Namespaces of external modules declaring zorba builtin functions
507 static const char* ZORBA_MATH_FN_NS;
508 static const char* ZORBA_BASE64_FN_NS;
509+ static const char* ZORBA_JSON_FN_NS;
510 static const char* ZORBA_NODEREF_FN_NS;
511 static const char* ZORBA_NODEPOS_FN_NS;
512 static const char* ZORBA_STORE_DYNAMIC_COLLECTIONS_DDL_FN_NS;
513
514=== modified file 'src/diagnostics/diagnostic.cpp'
515--- src/diagnostics/diagnostic.cpp 2011-07-11 21:01:20 +0000
516+++ src/diagnostics/diagnostic.cpp 2012-02-02 21:41:24 +0000
517@@ -134,6 +134,9 @@
518 case ZORBA_STORE : o << "Zorba store" ; break;
519 case ZORBA_XQP : o << "Zorba" ; break;
520
521+ case JSON_PARSER : o << "JSON parser" ; break;
522+ case JSON_SERIALIZATION : o << "JSON serialization" ; break;
523+
524 default : /* suppresses warning */ break;
525 }
526 return o;
527
528=== modified file 'src/diagnostics/diagnostic_en.xml'
529--- src/diagnostics/diagnostic_en.xml 2011-12-21 14:40:33 +0000
530+++ src/diagnostics/diagnostic_en.xml 2012-02-02 21:41:24 +0000
531@@ -2307,6 +2307,63 @@
532 <value>"continue loop" statement not inside while statement</value>
533 </diagnostic>
534
535+ <!--////////// JSON Parse Errors ////////////////////////////////////////-->
536+
537+ <diagnostic code="ZJPE0001" name="ILLEGAL_CHARACTER">
538+ <value>'$1': illegal JSON character</value>
539+ </diagnostic>
540+ <diagnostic code="ZJPE0002" name="ILLEGAL_CODEPOINT">
541+ <value>"$1": illegal Unicode code-point</value>
542+ </diagnostic>
543+ <diagnostic code="ZJPE0003" name="ILLEGAL_ESCAPE">
544+ <value>'\\$1': illegal JSON character escape</value>
545+ </diagnostic>
546+ <diagnostic code="ZJPE0004" name="ILLEGAL_LITERAL">
547+ <value>illegal JSON literal</value>
548+ </diagnostic>
549+ <diagnostic code="ZJPE0005" name="ILLEGAL_NUMBER">
550+ <value>illegal JSON number</value>
551+ </diagnostic>
552+ <diagnostic code="ZJPE0006" name="UNEXPECTED_TOKEN">
553+ <value>"$1": unexpected JSON token</value>
554+ </diagnostic>
555+ <diagnostic code="ZJPE0007" name="UNTERMINATED_STRING">
556+ <value>unterminated JSON string</value>
557+ </diagnostic>
558+ <diagnostic code="ZJPE0008" name="ILLEGAL_QNAME">
559+ <value>"$1": illegal QName</value>
560+ </diagnostic>
561+ <diagnostic code="ZJPE0009" name="ILLEGAL_EMPTY_STRING">
562+ <value>illegal empty string</value>
563+ </diagnostic>
564+
565+ <!--////////// JSON Serialization Errors ////////////////////////////////-->
566+
567+ <diagnostic code="ZJSE0001" name="NOT_DOCUMENT_OR_ELEMENT_NODE">
568+ <value>JSON serialization requires document or element node</value>
569+ </diagnostic>
570+ <diagnostic code="ZJSE0002" name="ELEMENT_MISSING_ATTRIBUTE">
571+ <value>"$1" element missing required "$2" attribute</value>
572+ </diagnostic>
573+ <diagnostic code="ZJSE0003" name="BAD_ATTRIBUTE_VALUE">
574+ <value>"$1": illegal value for attribute "$2"</value>
575+ </diagnostic>
576+ <diagnostic code="ZJSE0004" name="BAD_ELEMENT">
577+ <value>"$1": illegal element${; must be "2"}${ or "3"}</value>
578+ </diagnostic>
579+ <diagnostic code="ZJSE0005" name="BAD_CHILD_ELEMENT">
580+ <value>"$1": illegal child element of "$2" type; must be "$3"</value>
581+ </diagnostic>
582+ <diagnostic code="ZJSE0006" name="NO_ELEMENT_CHILD">
583+ <value>JSON type "$1" can not have a child element node</value>
584+ </diagnostic>
585+ <diagnostic code="ZJSE0007" name="NO_TEXT_CHILD">
586+ <value>JSON type "$1" can not have a child text node</value>
587+ </diagnostic>
588+ <diagnostic code="ZJSE0008" name="BAD_VALUE">
589+ <value>"$1": illegal value for JSON type "$2"</value>
590+ </diagnostic>
591+
592 </namespace>
593
594 <!--////////// Zorba Warnings ////////////////////////////////////////////-->
595@@ -3264,6 +3321,14 @@
596 <value>Zorba warning</value>
597 </entry>
598
599+ <entry key="JSON parser error">
600+ <value>JSON parser error</value>
601+ </entry>
602+
603+ <entry key="JSON serialization error">
604+ <value>JSON serialization error</value>
605+ </entry>
606+
607 <entry key="dynamic error">
608 <value>dynamic error</value>
609 </entry>
610
611=== modified file 'src/diagnostics/pregenerated/diagnostic_list.cpp'
612--- src/diagnostics/pregenerated/diagnostic_list.cpp 2011-12-21 14:40:33 +0000
613+++ src/diagnostics/pregenerated/diagnostic_list.cpp 2012-02-02 21:41:24 +0000
614@@ -1097,6 +1097,57 @@
615 ZorbaErrorCode XSST0010( "XSST0010" );
616
617
618+ZorbaErrorCode ZJPE0001_ILLEGAL_CHARACTER( "ZJPE0001" );
619+
620+
621+ZorbaErrorCode ZJPE0002_ILLEGAL_CODEPOINT( "ZJPE0002" );
622+
623+
624+ZorbaErrorCode ZJPE0003_ILLEGAL_ESCAPE( "ZJPE0003" );
625+
626+
627+ZorbaErrorCode ZJPE0004_ILLEGAL_LITERAL( "ZJPE0004" );
628+
629+
630+ZorbaErrorCode ZJPE0005_ILLEGAL_NUMBER( "ZJPE0005" );
631+
632+
633+ZorbaErrorCode ZJPE0006_UNEXPECTED_TOKEN( "ZJPE0006" );
634+
635+
636+ZorbaErrorCode ZJPE0007_UNTERMINATED_STRING( "ZJPE0007" );
637+
638+
639+ZorbaErrorCode ZJPE0008_ILLEGAL_QNAME( "ZJPE0008" );
640+
641+
642+ZorbaErrorCode ZJPE0009_ILLEGAL_EMPTY_STRING( "ZJPE0009" );
643+
644+
645+ZorbaErrorCode ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE( "ZJSE0001" );
646+
647+
648+ZorbaErrorCode ZJSE0002_ELEMENT_MISSING_ATTRIBUTE( "ZJSE0002" );
649+
650+
651+ZorbaErrorCode ZJSE0003_BAD_ATTRIBUTE_VALUE( "ZJSE0003" );
652+
653+
654+ZorbaErrorCode ZJSE0004_BAD_ELEMENT( "ZJSE0004" );
655+
656+
657+ZorbaErrorCode ZJSE0005_BAD_CHILD_ELEMENT( "ZJSE0005" );
658+
659+
660+ZorbaErrorCode ZJSE0006_NO_ELEMENT_CHILD( "ZJSE0006" );
661+
662+
663+ZorbaErrorCode ZJSE0007_NO_TEXT_CHILD( "ZJSE0007" );
664+
665+
666+ZorbaErrorCode ZJSE0008_BAD_VALUE( "ZJSE0008" );
667+
668+
669 } // namespace zerr
670
671 namespace zwarn {
672
673=== modified file 'src/diagnostics/pregenerated/dict_en.cpp'
674--- src/diagnostics/pregenerated/dict_en.cpp 2011-12-21 14:40:33 +0000
675+++ src/diagnostics/pregenerated/dict_en.cpp 2012-02-02 21:41:24 +0000
676@@ -332,6 +332,23 @@
677 #if defined(ZORBA_WITH_DEBUGGER)
678 { "ZGDB0001", "" },
679 #endif
680+ { "ZJPE0001", "'$1': illegal JSON character" },
681+ { "ZJPE0002", "\"$1\": illegal Unicode code-point" },
682+ { "ZJPE0003", "'\\$1': illegal JSON character escape" },
683+ { "ZJPE0004", "illegal JSON literal" },
684+ { "ZJPE0005", "illegal JSON number" },
685+ { "ZJPE0006", "\"$1\": unexpected JSON token" },
686+ { "ZJPE0007", "unterminated JSON string" },
687+ { "ZJPE0008", "\"$1\": illegal QName" },
688+ { "ZJPE0009", "illegal empty string" },
689+ { "ZJSE0001", "JSON serialization requires document or element node" },
690+ { "ZJSE0002", "\"$1\" element missing required \"$2\" attribute" },
691+ { "ZJSE0003", "\"$1\": illegal value for attribute \"$2\"" },
692+ { "ZJSE0004", "\"$1\": illegal element${; must be \"2\"}${ or \"3\"}" },
693+ { "ZJSE0005", "\"$1\": illegal child element of \"$2\" type; must be \"$3\"" },
694+ { "ZJSE0006", "JSON type \"$1\" can not have a child element node" },
695+ { "ZJSE0007", "JSON type \"$1\" can not have a child text node" },
696+ { "ZJSE0008", "\"$1\": illegal value for JSON type \"$2\"" },
697 { "ZOSE0001", "\"$1\": file not found" },
698 { "ZOSE0002", "\"$1\": not plain file" },
699 { "ZOSE0003", "stream read failure" },
700@@ -494,6 +511,8 @@
701 { "~HexBinaryMustBeEven", "HexBinary value must contain an even number of characters" },
702 { "~IncompleteKeyInIndexBuild", "incomplete key during index build" },
703 { "~IncompleteKeyInIndexRefresh", "incomplete key during index refresh" },
704+ { "~JSON parser error", "JSON parser error" },
705+ { "~JSON serialization error", "JSON serialization error" },
706 { "~LibModVersionMismatch_3", "XQuery library version can not be imported by a $3 version module" },
707 { "~ModuleDeclNotInMain", "module declaration must not be in main module" },
708 { "~ModuleNotFound", "module not found" },
709
710=== modified file 'src/diagnostics/qname.cpp'
711--- src/diagnostics/qname.cpp 2011-07-01 16:07:54 +0000
712+++ src/diagnostics/qname.cpp 2012-02-02 21:41:24 +0000
713@@ -79,9 +79,15 @@
714 case 'C': return ZORBA_SERIALIZATION;
715 case 'D': return ZORBA_DDF;
716 case 'G': return ZORBA_DEBUGGER;
717+ case 'J': switch ( name[2] ) {
718+ case 'P': return JSON_PARSER;
719+ case 'S': return JSON_SERIALIZATION;
720+ default : ZORBA_ASSERT( false );
721+ }
722 case 'O': return ZORBA_OS;
723 case 'S': return ZORBA_STORE;
724 case 'X': return ZORBA_XQP;
725+
726 default : ZORBA_ASSERT( false );
727 }
728 }
729
730=== modified file 'src/functions/library.cpp'
731--- src/functions/library.cpp 2011-10-14 07:35:51 +0000
732+++ src/functions/library.cpp 2012-02-02 21:41:24 +0000
733@@ -57,6 +57,7 @@
734 #include "functions/func_sequences.h"
735 #include "functions/func_sequences_impl.h"
736 #include "functions/func_strings.h"
737+#include "functions/func_json.h"
738 #include "functions/func_var_decl.h"
739 #include "functions/func_xqdoc.h"
740 #include "functions/func_documents.h"
741@@ -107,6 +108,7 @@
742 populate_context_fnput(sctx);
743 populate_context_index_ddl(sctx);
744 populate_context_ic_ddl(sctx);
745+ populate_context_json(sctx);
746 populate_context_maths(sctx);
747 populate_context_nodes(sctx);
748 populate_context_node_position(sctx);
749
750=== added file 'src/functions/pregenerated/func_json.cpp'
751--- src/functions/pregenerated/func_json.cpp 1970-01-01 00:00:00 +0000
752+++ src/functions/pregenerated/func_json.cpp 2012-02-02 21:41:24 +0000
753@@ -0,0 +1,87 @@
754+/*
755+ * Copyright 2006-2008 The FLWOR Foundation.
756+ *
757+ * Licensed under the Apache License, Version 2.0 (the "License");
758+ * you may not use this file except in compliance with the License.
759+ * You may obtain a copy of the License at
760+ *
761+ * http://www.apache.org/licenses/LICENSE-2.0
762+ *
763+ * Unless required by applicable law or agreed to in writing, software
764+ * distributed under the License is distributed on an "AS IS" BASIS,
765+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
766+ * See the License for the specific language governing permissions and
767+ * limitations under the License.
768+ */
769+
770+// ******************************************
771+// * *
772+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
773+// * SEE .xml FILE WITH SAME NAME *
774+// * *
775+// ******************************************
776+
777+
778+#include "stdafx.h"
779+#include "runtime/json/json.h"
780+#include "functions/func_json.h"
781+
782+
783+namespace zorba{
784+
785+
786+
787+PlanIter_t fn_zorba_json_parse_internal::codegen(
788+ CompilerCB*,
789+ static_context* sctx,
790+ const QueryLoc& loc,
791+ std::vector<PlanIter_t>& argv,
792+ AnnotationHolder& ann) const
793+{
794+ return new JSONParseInternal(sctx, loc, argv);
795+}
796+
797+PlanIter_t fn_zorba_json_serialize_internal::codegen(
798+ CompilerCB*,
799+ static_context* sctx,
800+ const QueryLoc& loc,
801+ std::vector<PlanIter_t>& argv,
802+ AnnotationHolder& ann) const
803+{
804+ return new JSONSerializeInternal(sctx, loc, argv);
805+}
806+
807+void populate_context_json(static_context* sctx)
808+{
809+ {
810+
811+
812+ DECL_WITH_KIND(sctx, fn_zorba_json_parse_internal,
813+ (createQName("http://www.zorba-xquery.com/modules/converters/json","","parse-internal"),
814+ GENV_TYPESYSTEM.STRING_TYPE_ONE,
815+ GENV_TYPESYSTEM.ITEM_TYPE_QUESTION,
816+ GENV_TYPESYSTEM.ELEMENT_TYPE_STAR),
817+ FunctionConsts::FN_ZORBA_JSON_PARSE_INTERNAL_2);
818+
819+ }
820+
821+
822+ {
823+
824+
825+ DECL_WITH_KIND(sctx, fn_zorba_json_serialize_internal,
826+ (createQName("http://www.zorba-xquery.com/modules/converters/json","","serialize-internal"),
827+ GENV_TYPESYSTEM.ITEM_TYPE_STAR,
828+ GENV_TYPESYSTEM.ITEM_TYPE_QUESTION,
829+ GENV_TYPESYSTEM.STRING_TYPE_ONE),
830+ FunctionConsts::FN_ZORBA_JSON_SERIALIZE_INTERNAL_2);
831+
832+ }
833+
834+}
835+
836+
837+}
838+
839+
840+
841
842=== added file 'src/functions/pregenerated/func_json.h'
843--- src/functions/pregenerated/func_json.h 1970-01-01 00:00:00 +0000
844+++ src/functions/pregenerated/func_json.h 2012-02-02 21:41:24 +0000
845@@ -0,0 +1,79 @@
846+/*
847+ * Copyright 2006-2008 The FLWOR Foundation.
848+ *
849+ * Licensed under the Apache License, Version 2.0 (the "License");
850+ * you may not use this file except in compliance with the License.
851+ * You may obtain a copy of the License at
852+ *
853+ * http://www.apache.org/licenses/LICENSE-2.0
854+ *
855+ * Unless required by applicable law or agreed to in writing, software
856+ * distributed under the License is distributed on an "AS IS" BASIS,
857+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
858+ * See the License for the specific language governing permissions and
859+ * limitations under the License.
860+ */
861+
862+// ******************************************
863+// * *
864+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
865+// * SEE .xml FILE WITH SAME NAME *
866+// * *
867+// ******************************************
868+
869+
870+#ifndef ZORBA_FUNCTIONS_JSON_H
871+#define ZORBA_FUNCTIONS_JSON_H
872+
873+
874+#include "common/shared_types.h"
875+#include "functions/function_impl.h"
876+
877+
878+namespace zorba {
879+
880+
881+void populate_context_json(static_context* sctx);
882+
883+
884+
885+
886+//fn-zorba-json:parse-internal
887+class fn_zorba_json_parse_internal : public function
888+{
889+public:
890+ fn_zorba_json_parse_internal(const signature& sig, FunctionConsts::FunctionKind kind)
891+ :
892+ function(sig, kind)
893+ {
894+
895+ }
896+
897+ CODEGEN_DECL();
898+};
899+
900+
901+//fn-zorba-json:serialize-internal
902+class fn_zorba_json_serialize_internal : public function
903+{
904+public:
905+ fn_zorba_json_serialize_internal(const signature& sig, FunctionConsts::FunctionKind kind)
906+ :
907+ function(sig, kind)
908+ {
909+
910+ }
911+
912+ CODEGEN_DECL();
913+};
914+
915+
916+} //namespace zorba
917+
918+
919+#endif
920+/*
921+ * Local variables:
922+ * mode: c++
923+ * End:
924+ */
925
926=== modified file 'src/functions/pregenerated/function_enum.h'
927--- src/functions/pregenerated/function_enum.h 2012-01-11 17:30:25 +0000
928+++ src/functions/pregenerated/function_enum.h 2012-02-02 21:41:24 +0000
929@@ -166,6 +166,8 @@
930 FN_ZORBA_INTROSPECT_SCTX_IN_SCOPE_ATTRIBUTE_GROUPS_0,
931 FN_ZORBA_INTROSPECT_SCTX_OPTION_1,
932 FN_ZORBA_INTROSPECT_SCTX_FUNCTION_ANNOTATIONS_2,
933+ FN_ZORBA_JSON_PARSE_INTERNAL_2,
934+ FN_ZORBA_JSON_SERIALIZE_INTERNAL_2,
935 MATH_SQRT_1,
936 MATH_EXP_1,
937 MATH_EXP10_1,
938
939=== modified file 'src/runtime/CMakeLists.txt'
940--- src/runtime/CMakeLists.txt 2011-06-01 13:16:28 +0000
941+++ src/runtime/CMakeLists.txt 2012-02-02 21:41:24 +0000
942@@ -115,6 +115,9 @@
943 durations_dates_times/DurationsDatesTimesImpl.cpp
944 indexing/doc_indexer.cpp
945 indexing/index_ddl.cpp
946+ json/common.cpp
947+ json/jsonml_array.cpp
948+ json/snelson.cpp
949 numerics/NumericsImpl.cpp
950 numerics/format_integer_impl.cpp
951 sequences/SequencesImpl.cpp
952
953=== modified file 'src/runtime/full_text/ft_match.cpp'
954--- src/runtime/full_text/ft_match.cpp 2011-06-14 17:26:33 +0000
955+++ src/runtime/full_text/ft_match.cpp 2012-02-02 21:41:24 +0000
956@@ -31,7 +31,7 @@
957 return o << "0x" << hex << reinterpret_cast<unsigned long>( obj ) << dec;
958 }
959
960-DEF_OMANIP1( print_addr, void const*, obj )
961+DEF_OMANIP1( print_addr, void const* )
962
963 ostream& operator<<( ostream &o, ft_string_match const &sm ) {
964 return o << "{SM: "
965@@ -52,8 +52,7 @@
966 return o;
967 }
968
969-DEF_OMANIP2( print_string_matches, char const*, label,
970- ft_string_matches const&, sms )
971+DEF_OMANIP2( print_string_matches, char const*, ft_string_matches const& )
972
973 ostream& operator<<( ostream &o, ft_match const &m ) {
974 return o << indent << "ft_match @ " << print_addr( &m ) << '\n'
975
976=== added directory 'src/runtime/json'
977=== added file 'src/runtime/json/common.cpp'
978--- src/runtime/json/common.cpp 1970-01-01 00:00:00 +0000
979+++ src/runtime/json/common.cpp 2012-02-02 21:41:24 +0000
980@@ -0,0 +1,62 @@
981+/*
982+ * Copyright 2006-2011 The FLWOR Foundation.
983+ *
984+ * Licensed under the Apache License, Version 2.0 (the "License");
985+ * you may not use this file except in compliance with the License.
986+ * You may obtain a copy of the License at
987+ *
988+ * http://www.apache.org/licenses/LICENSE-2.0
989+ *
990+ * Unless required by applicable law or agreed to in writing, software
991+ * distributed under the License is distributed on an "AS IS" BASIS,
992+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
993+ * See the License for the specific language governing permissions and
994+ * limitations under the License.
995+ */
996+#include "stdafx.h"
997+
998+#include "store/api/iterator.h"
999+
1000+#include "common.h"
1001+
1002+using namespace std;
1003+
1004+namespace zorba {
1005+
1006+///////////////////////////////////////////////////////////////////////////////
1007+
1008+bool get_attribute_value( store::Item_t const &element, char const *att_name,
1009+ zstring *att_value ) {
1010+ store::Iterator_t i( element->getAttributes() );
1011+ bool found = false;
1012+ i->open();
1013+ store::Item_t att_item;
1014+ while ( i->next( att_item ) ) {
1015+ if ( att_item->getNodeName()->getStringValue() == att_name ) {
1016+ att_item->getStringValue2( *att_value );
1017+ found = true;
1018+ break;
1019+ }
1020+ }
1021+ i->close();
1022+ return found;
1023+}
1024+
1025+///////////////////////////////////////////////////////////////////////////////
1026+
1027+#if ZORBA_DEBUG_JSON
1028+
1029+ostream& operator<<( ostream &o, parse_state s ) {
1030+ static char const *const string_of[] = {
1031+ "in_array",
1032+ "in_object"
1033+ };
1034+ return o << string_of[ s ];
1035+}
1036+
1037+#endif /* ZORBA_DEBUG_JSON */
1038+
1039+///////////////////////////////////////////////////////////////////////////////
1040+
1041+} // namespace zorba
1042+/* vim:set et sw=2 ts=2: */
1043
1044=== added file 'src/runtime/json/common.h'
1045--- src/runtime/json/common.h 1970-01-01 00:00:00 +0000
1046+++ src/runtime/json/common.h 2012-02-02 21:41:24 +0000
1047@@ -0,0 +1,118 @@
1048+/*
1049+ * Copyright 2006-2011 The FLWOR Foundation.
1050+ *
1051+ * Licensed under the Apache License, Version 2.0 (the "License");
1052+ * you may not use this file except in compliance with the License.
1053+ * You may obtain a copy of the License at
1054+ *
1055+ * http://www.apache.org/licenses/LICENSE-2.0
1056+ *
1057+ * Unless required by applicable law or agreed to in writing, software
1058+ * distributed under the License is distributed on an "AS IS" BASIS,
1059+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1060+ * See the License for the specific language governing permissions and
1061+ * limitations under the License.
1062+ */
1063+
1064+#ifndef ZORBA_RUNTIME_JSON_COMMON_H
1065+#define ZORBA_RUNTIME_JSON_COMMON_H
1066+
1067+#include <iostream>
1068+#include <stack>
1069+
1070+#include "store/api/item.h"
1071+#include "store/api/item_factory.h"
1072+#include "util/indent.h"
1073+#include "util/omanip.h"
1074+#include "zorbatypes/zstring.h"
1075+
1076+#define ZORBA_DEBUG_JSON 0
1077+
1078+namespace zorba {
1079+
1080+///////////////////////////////////////////////////////////////////////////////
1081+
1082+typedef std::stack<store::Item*> item_stack_type;
1083+
1084+enum parse_state {
1085+ in_array,
1086+ in_object
1087+};
1088+
1089+typedef std::stack<int> state_stack_type;
1090+
1091+namespace whitespace {
1092+ enum type {
1093+ none,
1094+ some,
1095+ indent
1096+ };
1097+}
1098+
1099+///////////////////////////////////////////////////////////////////////////////
1100+
1101+bool get_attribute_value( store::Item_t const &element, char const *att_name,
1102+ zstring *att_value );
1103+
1104+typedef std::ostream& (*std_omanip_type)(std::ostream&);
1105+
1106+inline std::ostream& if_do( std::ostream &o, bool expr, std_omanip_type fn ) {
1107+ if ( expr )
1108+ o << fn;
1109+ return o;
1110+}
1111+DEF_OMANIP2( if_do, bool, std_omanip_type )
1112+
1113+#define if_indent(WS,FN) if_do( (WS) == whitespace::indent, FN )
1114+
1115+inline std::ostream& if_emit( std::ostream &o, bool expr, char c ) {
1116+ if ( expr )
1117+ o << c;
1118+ return o;
1119+}
1120+DEF_OMANIP2( if_emit, bool, char )
1121+
1122+///////////////////////////////////////////////////////////////////////////////
1123+
1124+#if ZORBA_DEBUG_JSON
1125+
1126+std::ostream& operator<<( std::ostream &o, parse_state s );
1127+
1128+# define PUSH_ITEM(I) \
1129+ do { \
1130+ cout << __LINE__ << ":PUSH_ITEM( " << (I)->show() << " )" << endl; \
1131+ item_stack.push( (I).getp() ); \
1132+ } while (0)
1133+
1134+# define POP_ITEM() \
1135+ do { \
1136+ cout << __LINE__ << ":POP_ITEM()" << endl; \
1137+ cur_item = ztd::pop_stack( item_stack ); \
1138+ } while (0)
1139+
1140+# define PUSH_STATE(S) \
1141+ do { \
1142+ cout << __LINE__ << ":PUSH_STATE( " << (S) << " )" << endl; \
1143+ state_stack.push( S ); \
1144+ } while (0)
1145+
1146+# define POP_STATE() \
1147+ do { \
1148+ cout << __LINE__ << ":POP_STATE()" << endl; \
1149+ state_stack.pop(); \
1150+ } while (0) \
1151+
1152+#else
1153+
1154+# define PUSH_ITEM(I) item_stack.push( (I).getp() )
1155+# define POP_ITEM() cur_item = ztd::pop_stack( item_stack )
1156+# define PUSH_STATE(S) state_stack.push( S )
1157+# define POP_STATE() state_stack.pop()
1158+
1159+#endif /* ZORBA_DEBUG_JSON */
1160+
1161+///////////////////////////////////////////////////////////////////////////////
1162+
1163+} // namespace zorba
1164+#endif /* ZORBA_RUNTIME_JSON_COMMON_H */
1165+/* vim:set et sw=2 ts=2: */
1166
1167=== added file 'src/runtime/json/json_impl.cpp'
1168--- src/runtime/json/json_impl.cpp 1970-01-01 00:00:00 +0000
1169+++ src/runtime/json/json_impl.cpp 2012-02-02 21:41:24 +0000
1170@@ -0,0 +1,217 @@
1171+/*
1172+ * Copyright 2006-2011 The FLWOR Foundation.
1173+ *
1174+ * Licensed under the Apache License, Version 2.0 (the "License");
1175+ * you may not use this file except in compliance with the License.
1176+ * You may obtain a copy of the License at
1177+ *
1178+ * http://www.apache.org/licenses/LICENSE-2.0
1179+ *
1180+ * Unless required by applicable law or agreed to in writing, software
1181+ * distributed under the License is distributed on an "AS IS" BASIS,
1182+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1183+ * See the License for the specific language governing permissions and
1184+ * limitations under the License.
1185+ */
1186+#include "stdafx.h"
1187+
1188+#include <map>
1189+#include <sstream>
1190+
1191+#include <zorba/diagnostic_list.h>
1192+
1193+#include "runtime/json/json.h"
1194+#include "store/api/item_factory.h"
1195+#include "system/globalenv.h"
1196+#include "util/mem_streambuf.h"
1197+
1198+#include "jsonml_array.h"
1199+#include "snelson.h"
1200+
1201+using namespace std;
1202+
1203+namespace zorba {
1204+
1205+///////////////////////////////////////////////////////////////////////////////
1206+
1207+typedef map<zstring,zstring> options_type;
1208+
1209+static void get_options( store::Item_t const &options_element,
1210+ options_type *options ) {
1211+ ZORBA_ASSERT( options_element->getNodeKind() ==
1212+ store::StoreConsts::elementNode );
1213+ store::Iterator_t i = options_element->getChildren();
1214+ i->open();
1215+ store::Item_t option_item;
1216+ while ( i->next( option_item ) ) {
1217+ if ( option_item->getNodeKind() == store::StoreConsts::elementNode ) {
1218+ zstring const name( option_item->getNodeName()->getStringValue() );
1219+ zstring value;
1220+ get_attribute_value( option_item, "value", &value );
1221+ (*options)[ name ] = value;
1222+ }
1223+ }
1224+ i->close();
1225+}
1226+
1227+///////////////////////////////////////////////////////////////////////////////
1228+
1229+bool JSONParseInternal::nextImpl( store::Item_t& result,
1230+ PlanState &planState ) const {
1231+ store::Item_t cur_item;
1232+ options_type options;
1233+ istringstream iss;
1234+ mem_streambuf buf;
1235+
1236+ PlanIteratorState *state;
1237+ DEFAULT_STACK_INIT( PlanIteratorState, state, planState );
1238+
1239+ ZORBA_ASSERT( theChildren.size() == 2 );
1240+ consumeNext( cur_item, theChildren[1], planState );
1241+ get_options( cur_item, &options );
1242+
1243+ consumeNext( cur_item, theChildren[0], planState );
1244+ result = nullptr;
1245+
1246+ istream *is;
1247+ if ( cur_item->isStreamable() ) {
1248+ is = &cur_item->getStream();
1249+ } else {
1250+ zstring s;
1251+ cur_item->getStringValue2( s );
1252+ // Doing it this way uses the string data in-place with no copy.
1253+ buf.set( s.data(), s.size() );
1254+ iss.ios::rdbuf( &buf );
1255+ is = &iss;
1256+ }
1257+
1258+ try {
1259+ json::parser p( *is );
1260+ p.set_loc(
1261+ loc.getFilename().c_str(), loc.getLineBegin(), loc.getColumnBegin()
1262+ );
1263+
1264+ options_type::mapped_type const &format = options[ "json-format" ];
1265+ ZORBA_ASSERT( !format.empty() );
1266+ if ( format == "Snelson" )
1267+ snelson::parse( p, &result );
1268+ else if ( format == "JsonML-array" )
1269+ jsonml_array::parse( p, &result );
1270+ else
1271+ ZORBA_ASSERT( false );
1272+ }
1273+ catch ( json::illegal_character const &e ) {
1274+ throw XQUERY_EXCEPTION(
1275+ zerr::ZJPE0001_ILLEGAL_CHARACTER,
1276+ ERROR_PARAMS( e.get_char() ),
1277+ ERROR_LOC( e.get_loc() )
1278+ );
1279+ }
1280+ catch ( json::illegal_codepoint const &e ) {
1281+ throw XQUERY_EXCEPTION(
1282+ zerr::ZJPE0002_ILLEGAL_CODEPOINT,
1283+ ERROR_PARAMS( e.get_codepoint() ),
1284+ ERROR_LOC( e.get_loc() )
1285+ );
1286+ }
1287+ catch ( json::illegal_escape const &e ) {
1288+ throw XQUERY_EXCEPTION(
1289+ zerr::ZJPE0003_ILLEGAL_ESCAPE,
1290+ ERROR_PARAMS( e.get_escape() ),
1291+ ERROR_LOC( e.get_loc() )
1292+ );
1293+ }
1294+ catch ( json::illegal_literal const &e ) {
1295+ throw XQUERY_EXCEPTION(
1296+ zerr::ZJPE0004_ILLEGAL_LITERAL,
1297+ ERROR_LOC( e.get_loc() )
1298+ );
1299+ }
1300+ catch ( json::illegal_number const &e ) {
1301+ throw XQUERY_EXCEPTION(
1302+ zerr::ZJPE0005_ILLEGAL_NUMBER,
1303+ ERROR_LOC( e.get_loc() )
1304+ );
1305+ }
1306+ catch ( json::unexpected_token const &e ) {
1307+ throw XQUERY_EXCEPTION(
1308+ zerr::ZJPE0006_UNEXPECTED_TOKEN,
1309+ ERROR_PARAMS( e.get_token() ),
1310+ ERROR_LOC( e.get_loc() )
1311+ );
1312+ }
1313+ catch ( json::unterminated_string const &e ) {
1314+ throw XQUERY_EXCEPTION(
1315+ zerr::ZJPE0007_UNTERMINATED_STRING,
1316+ ERROR_LOC( e.get_loc() )
1317+ );
1318+ }
1319+
1320+ STACK_PUSH( !!result, state );
1321+ STACK_END( state );
1322+}
1323+
1324+///////////////////////////////////////////////////////////////////////////////
1325+
1326+bool JSONSerializeInternal::nextImpl( store::Item_t& result,
1327+ PlanState &planState ) const {
1328+ store::Item_t cur_item;
1329+ options_type options;
1330+
1331+ PlanIteratorState *state;
1332+ DEFAULT_STACK_INIT( PlanIteratorState, state, planState );
1333+
1334+ ZORBA_ASSERT( theChildren.size() == 2 );
1335+ consumeNext( cur_item, theChildren[1], planState );
1336+ get_options( cur_item, &options );
1337+
1338+ consumeNext( cur_item, theChildren[0], planState );
1339+ try {
1340+ options_type::mapped_type const &format_opt = options[ "json-format" ];
1341+ ZORBA_ASSERT( !format_opt.empty() );
1342+
1343+ whitespace::type ws;
1344+ options_type::mapped_type const &whitespace_opt = options[ "whitespace" ];
1345+ if ( whitespace_opt.empty() || whitespace_opt == "none" )
1346+ ws = whitespace::none;
1347+ else if ( whitespace_opt == "some" )
1348+ ws = whitespace::some;
1349+ else if ( whitespace_opt == "indent" )
1350+ ws = whitespace::indent;
1351+ else
1352+ ZORBA_ASSERT( false );
1353+
1354+ ostringstream oss;
1355+ switch ( cur_item->getNodeKind() ) {
1356+ case store::StoreConsts::documentNode:
1357+ case store::StoreConsts::elementNode:
1358+ if ( format_opt == "Snelson" )
1359+ snelson::serialize( oss, cur_item, ws );
1360+ else if ( format_opt == "JsonML-array" )
1361+ jsonml_array::serialize( oss, cur_item, ws );
1362+ else
1363+ ZORBA_ASSERT( false );
1364+ break;
1365+ default:
1366+ throw XQUERY_EXCEPTION(
1367+ zerr::ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE,
1368+ ERROR_LOC( loc )
1369+ );
1370+ }
1371+ // This string copying is inefficient, but I can't see another way.
1372+ zstring temp( oss.str() );
1373+ GENV_ITEMFACTORY->createString( result, temp );
1374+ }
1375+ catch ( ZorbaException &e ) {
1376+ set_source( e, loc );
1377+ throw;
1378+ }
1379+
1380+ STACK_PUSH( !!result, state );
1381+ STACK_END( state );
1382+}
1383+
1384+///////////////////////////////////////////////////////////////////////////////
1385+
1386+} // namespace zorba
1387+/* vim:set et sw=2 ts=2: */
1388
1389=== added file 'src/runtime/json/jsonml_array.cpp'
1390--- src/runtime/json/jsonml_array.cpp 1970-01-01 00:00:00 +0000
1391+++ src/runtime/json/jsonml_array.cpp 2012-02-02 21:41:24 +0000
1392@@ -0,0 +1,285 @@
1393+/*
1394+ * Copyright 2006-2011 The FLWOR Foundation.
1395+ *
1396+ * Licensed under the Apache License, Version 2.0 (the "License");
1397+ * you may not use this file except in compliance with the License.
1398+ * You may obtain a copy of the License at
1399+ *
1400+ * http://www.apache.org/licenses/LICENSE-2.0
1401+ *
1402+ * Unless required by applicable law or agreed to in writing, software
1403+ * distributed under the License is distributed on an "AS IS" BASIS,
1404+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1405+ * See the License for the specific language governing permissions and
1406+ * limitations under the License.
1407+ */
1408+#include "stdafx.h"
1409+
1410+#include <sstream>
1411+
1412+#include <zorba/diagnostic_list.h>
1413+
1414+#include "runtime/json/json.h"
1415+#include "store/api/item_factory.h"
1416+#include "system/globalenv.h"
1417+#include "types/root_typemanager.h"
1418+#include "util/ascii_util.h"
1419+#include "util/cxx_util.h"
1420+#include "util/json_parser.h"
1421+#include "util/mem_streambuf.h"
1422+#include "util/omanip.h"
1423+#include "util/oseparator.h"
1424+#include "util/stl_util.h"
1425+
1426+#include "jsonml_array.h"
1427+
1428+using namespace std;
1429+
1430+namespace zorba {
1431+
1432+///////////////////////////////////////////////////////////////////////////////
1433+
1434+static void split_name( zstring const &name, zstring *prefix, zstring *local ) {
1435+ zstring::size_type const colon = name.find( ':' );
1436+ if ( colon != zstring::npos ) {
1437+ *prefix = name.substr( 0, colon );
1438+ *local = name.substr( colon + 1 );
1439+ if ( prefix->empty() || local->empty() )
1440+ throw XQUERY_EXCEPTION(
1441+ zerr::ZJPE0008_ILLEGAL_QNAME,
1442+ ERROR_PARAMS( name )
1443+ );
1444+ } else {
1445+ prefix->clear();
1446+ *local = name;
1447+ }
1448+}
1449+
1450+namespace expect {
1451+ enum type {
1452+ none,
1453+ element_name,
1454+ attribute_name,
1455+ attribute_value
1456+ };
1457+}
1458+
1459+///////////////////////////////////////////////////////////////////////////////
1460+
1461+namespace jsonml_array {
1462+
1463+void parse( json::parser &p, store::Item_t *result ) {
1464+ ZORBA_ASSERT( result );
1465+
1466+ state_stack_type state_stack;
1467+
1468+ store::Item_t cur_item, junk_item, value_item;
1469+ store::Item_t att_name, element_name, type_name;
1470+
1471+ zstring base_uri;
1472+ bool got_something = false;
1473+ item_stack_type item_stack;
1474+ expect::type expect_what = expect::none;
1475+ store::NsBindings ns_bindings;
1476+ zstring value;
1477+
1478+ json::token token;
1479+ while ( p.next( &token ) ) {
1480+ got_something = true;
1481+ switch ( token.get_type() ) {
1482+
1483+ case '[':
1484+ PUSH_STATE( in_array );
1485+ expect_what = expect::element_name;
1486+ break;
1487+
1488+ case '{':
1489+ PUSH_STATE( in_object );
1490+ expect_what = expect::attribute_name;
1491+ break;
1492+
1493+ case ']':
1494+ POP_ITEM();
1495+ // no break;
1496+ case '}':
1497+ POP_STATE();
1498+ expect_what = expect::none;
1499+ break;
1500+
1501+ case ',':
1502+ expect_what = state_stack.top() == in_object ?
1503+ expect::attribute_name : expect::none;
1504+ break;
1505+
1506+ case ':':
1507+ expect_what = expect::attribute_value;
1508+ break;
1509+
1510+ case json::token::number:
1511+ case 'F':
1512+ case 'T':
1513+ case json::token::json_null:
1514+ case json::token::string: {
1515+ value = token.get_value();
1516+ zstring prefix, local;
1517+ switch ( expect_what ) {
1518+ case expect::element_name:
1519+ split_name( value, &prefix, &local );
1520+ GENV_ITEMFACTORY->createQName( element_name, "", prefix, local );
1521+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
1522+ GENV_ITEMFACTORY->createElementNode(
1523+ cur_item,
1524+ item_stack.empty() ? nullptr : item_stack.top(),
1525+ element_name, type_name, false, false, ns_bindings, base_uri
1526+ );
1527+ PUSH_ITEM( cur_item );
1528+ if ( !*result )
1529+ *result = cur_item;
1530+ break;
1531+ case expect::attribute_name:
1532+ split_name( value, &prefix, &local );
1533+ GENV_ITEMFACTORY->createQName( att_name, "", prefix, local );
1534+ break;
1535+ case expect::attribute_value:
1536+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
1537+ GENV_ITEMFACTORY->createString( value_item, value );
1538+ GENV_ITEMFACTORY->createAttributeNode(
1539+ junk_item, cur_item, att_name, type_name, value_item
1540+ );
1541+ break;
1542+ case expect::none:
1543+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
1544+ break;
1545+ }
1546+ break;
1547+ }
1548+
1549+ case json::token::none:
1550+ break;
1551+
1552+ default:
1553+ assert( false );
1554+ } // switch
1555+ } // while
1556+ if ( !got_something )
1557+ throw XQUERY_EXCEPTION( zerr::ZJPE0009_ILLEGAL_EMPTY_STRING );
1558+}
1559+
1560+} // namespace jsonml_array
1561+
1562+///////////////////////////////////////////////////////////////////////////////
1563+
1564+static ostream& serialize_attributes( ostream &o, store::Item_t const &element,
1565+ oseparator &sep, whitespace::type ws ) {
1566+ bool emitted_attributes = false;
1567+ oseparator att_sep;
1568+ switch ( ws ) {
1569+ case whitespace::none : att_sep.sep( "," ); break;
1570+ case whitespace::some : att_sep.sep( ", " ); break;
1571+ case whitespace::indent: att_sep.sep( ",\n" ); break;
1572+ }
1573+
1574+ store::Iterator_t i( element->getAttributes() );
1575+ i->open();
1576+ store::Item_t att_item;
1577+ while ( i->next( att_item ) ) {
1578+ zstring const att_name( att_item->getNodeName()->getStringValue() );
1579+ if ( att_name == "xmlns" )
1580+ continue;
1581+ if ( !emitted_attributes ) {
1582+ o << sep
1583+ << if_emit( ws == whitespace::indent, '\n' )
1584+ << if_indent( ws, indent ) << '{'
1585+ << if_indent( ws, inc_indent );
1586+ emitted_attributes = true;
1587+ }
1588+ bool const was_printing = att_sep.printing();
1589+ o << att_sep;
1590+ if ( was_printing )
1591+ o << if_indent( ws, indent );
1592+ else
1593+ o << if_emit( ws, ' ' );
1594+
1595+ o << '"' << att_name << '"'
1596+ << if_emit( ws, ' ' ) << ':' << if_emit( ws, ' ' )
1597+ << '"' << att_item->getStringValue() << '"';
1598+ }
1599+ i->close();
1600+ if ( emitted_attributes )
1601+ o << if_emit( ws, ' ' ) << '}' << if_indent( ws, dec_indent );
1602+ return o;
1603+}
1604+DEF_OMANIP3( serialize_attributes, store::Item_t const&, oseparator&,
1605+ whitespace::type )
1606+
1607+static ostream& serialize_children( ostream&, store::Item_t const &parent,
1608+ oseparator&, whitespace::type );
1609+DEF_OMANIP3( serialize_children, store::Item_t const&, oseparator&,
1610+ whitespace::type )
1611+
1612+static ostream& serialize_element( ostream &o, store::Item_t const &element,
1613+ oseparator &sep, whitespace::type ws ) {
1614+ if ( sep.printing() )
1615+ o << if_emit( ws == whitespace::indent, '\n' );
1616+ sep.printing( true );
1617+ o << if_indent( ws, indent ) << '[' << if_emit( ws, ' ' )
1618+ << '"' << element->getNodeName()->getStringValue() << '"'
1619+ << if_indent( ws, inc_indent )
1620+ << serialize_attributes( element, sep, ws )
1621+ << serialize_children( element, sep, ws )
1622+ << if_emit( ws, ' ' ) << ']'
1623+ << if_indent( ws, dec_indent );
1624+ return o;
1625+}
1626+DEF_OMANIP3( serialize_element, store::Item_t const&, oseparator&,
1627+ whitespace::type )
1628+
1629+static ostream& serialize_children( ostream &o, store::Item_t const &parent,
1630+ oseparator &sep, whitespace::type ws ) {
1631+ store::Iterator_t i( parent->getChildren() );
1632+ i->open();
1633+ store::Item_t child;
1634+ while ( i->next( child ) ) {
1635+ switch ( child->getNodeKind() ) {
1636+ case store::StoreConsts::elementNode:
1637+ o << sep << serialize_element( child, sep, ws );
1638+ break;
1639+ case store::StoreConsts::textNode:
1640+ o << sep << '"' << child->getStringValue() << '"';
1641+ break;
1642+ default:
1643+ break;
1644+ }
1645+ }
1646+ i->close();
1647+ return o;
1648+}
1649+
1650+///////////////////////////////////////////////////////////////////////////////
1651+
1652+namespace jsonml_array {
1653+
1654+void serialize( ostream &o, store::Item_t const &item, whitespace::type ws ) {
1655+ oseparator sep;
1656+ if ( ws )
1657+ sep.sep( ", " );
1658+ else
1659+ sep.sep( "," );
1660+ switch ( item->getNodeKind() ) {
1661+ case store::StoreConsts::documentNode:
1662+ o << serialize_children( item, sep, ws );
1663+ break;
1664+ case store::StoreConsts::elementNode:
1665+ o << serialize_element( item, sep, ws );
1666+ break;
1667+ default:
1668+ throw XQUERY_EXCEPTION( zerr::ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE );
1669+ }
1670+}
1671+
1672+} // namespace jsonml_array
1673+
1674+///////////////////////////////////////////////////////////////////////////////
1675+
1676+} // namespace zorba
1677+/* vim:set et sw=2 ts=2: */
1678
1679=== added file 'src/runtime/json/jsonml_array.h'
1680--- src/runtime/json/jsonml_array.h 1970-01-01 00:00:00 +0000
1681+++ src/runtime/json/jsonml_array.h 2012-02-02 21:41:24 +0000
1682@@ -0,0 +1,41 @@
1683+/*
1684+ * Copyright 2006-2011 The FLWOR Foundation.
1685+ *
1686+ * Licensed under the Apache License, Version 2.0 (the "License");
1687+ * you may not use this file except in compliance with the License.
1688+ * You may obtain a copy of the License at
1689+ *
1690+ * http://www.apache.org/licenses/LICENSE-2.0
1691+ *
1692+ * Unless required by applicable law or agreed to in writing, software
1693+ * distributed under the License is distributed on an "AS IS" BASIS,
1694+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1695+ * See the License for the specific language governing permissions and
1696+ * limitations under the License.
1697+ */
1698+#include "stdafx.h"
1699+
1700+#ifndef ZORBA_RUNTIME_JSON_JSONML_ARRAY_H
1701+#define ZORBA_RUNTIME_JSON_JSONML_ARRAY_H
1702+
1703+#include <iostream>
1704+
1705+#include "store/api/item.h"
1706+#include "util/json_parser.h"
1707+
1708+#include "common.h"
1709+
1710+namespace zorba {
1711+namespace jsonml_array {
1712+
1713+///////////////////////////////////////////////////////////////////////////////
1714+
1715+void parse( json::parser &p, store::Item_t *result );
1716+void serialize( std::ostream&, store::Item_t const &item, whitespace::type );
1717+
1718+///////////////////////////////////////////////////////////////////////////////
1719+
1720+} // namespace jsonml_array
1721+} // namespace zorba
1722+#endif /* ZORBA_RUNTIME_JSON_JSONML_ARRAY_H */
1723+/* vim:set et sw=2 ts=2: */
1724
1725=== added directory 'src/runtime/json/pregenerated'
1726=== added file 'src/runtime/json/pregenerated/json.cpp'
1727--- src/runtime/json/pregenerated/json.cpp 1970-01-01 00:00:00 +0000
1728+++ src/runtime/json/pregenerated/json.cpp 2012-02-02 21:41:24 +0000
1729@@ -0,0 +1,94 @@
1730+/*
1731+ * Copyright 2006-2008 The FLWOR Foundation.
1732+ *
1733+ * Licensed under the Apache License, Version 2.0 (the "License");
1734+ * you may not use this file except in compliance with the License.
1735+ * You may obtain a copy of the License at
1736+ *
1737+ * http://www.apache.org/licenses/LICENSE-2.0
1738+ *
1739+ * Unless required by applicable law or agreed to in writing, software
1740+ * distributed under the License is distributed on an "AS IS" BASIS,
1741+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1742+ * See the License for the specific language governing permissions and
1743+ * limitations under the License.
1744+ */
1745+
1746+// ******************************************
1747+// * *
1748+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
1749+// * SEE .xml FILE WITH SAME NAME *
1750+// * *
1751+// ******************************************
1752+
1753+#include "stdafx.h"
1754+#include "zorbatypes/rchandle.h"
1755+#include "zorbatypes/zstring.h"
1756+#include "runtime/visitors/planiter_visitor.h"
1757+#include "runtime/json/json.h"
1758+#include "system/globalenv.h"
1759+
1760+
1761+
1762+namespace zorba {
1763+
1764+// <JSONParseInternal>
1765+const char* JSONParseInternal::class_name_str = "JSONParseInternal";
1766+JSONParseInternal::class_factory<JSONParseInternal>
1767+JSONParseInternal::g_class_factory;
1768+
1769+const serialization::ClassVersion
1770+JSONParseInternal::class_versions[] ={{ 1, 0x000905, false}};
1771+
1772+const int JSONParseInternal::class_versions_count =
1773+sizeof(JSONParseInternal::class_versions)/sizeof(struct serialization::ClassVersion);
1774+
1775+void JSONParseInternal::accept(PlanIterVisitor& v) const {
1776+ v.beginVisit(*this);
1777+
1778+ std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
1779+ std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
1780+ for ( ; lIter != lEnd; ++lIter ){
1781+ (*lIter)->accept(v);
1782+ }
1783+
1784+ v.endVisit(*this);
1785+}
1786+
1787+JSONParseInternal::~JSONParseInternal() {}
1788+
1789+// </JSONParseInternal>
1790+
1791+
1792+// <JSONSerializeInternal>
1793+const char* JSONSerializeInternal::class_name_str = "JSONSerializeInternal";
1794+JSONSerializeInternal::class_factory<JSONSerializeInternal>
1795+JSONSerializeInternal::g_class_factory;
1796+
1797+const serialization::ClassVersion
1798+JSONSerializeInternal::class_versions[] ={{ 1, 0x000905, false}};
1799+
1800+const int JSONSerializeInternal::class_versions_count =
1801+sizeof(JSONSerializeInternal::class_versions)/sizeof(struct serialization::ClassVersion);
1802+
1803+void JSONSerializeInternal::accept(PlanIterVisitor& v) const {
1804+ v.beginVisit(*this);
1805+
1806+ std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
1807+ std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
1808+ for ( ; lIter != lEnd; ++lIter ){
1809+ (*lIter)->accept(v);
1810+ }
1811+
1812+ v.endVisit(*this);
1813+}
1814+
1815+JSONSerializeInternal::~JSONSerializeInternal() {}
1816+
1817+// </JSONSerializeInternal>
1818+
1819+
1820+
1821+}
1822+
1823+
1824
1825=== added file 'src/runtime/json/pregenerated/json.h'
1826--- src/runtime/json/pregenerated/json.h 1970-01-01 00:00:00 +0000
1827+++ src/runtime/json/pregenerated/json.h 2012-02-02 21:41:24 +0000
1828@@ -0,0 +1,114 @@
1829+/*
1830+ * Copyright 2006-2008 The FLWOR Foundation.
1831+ *
1832+ * Licensed under the Apache License, Version 2.0 (the "License");
1833+ * you may not use this file except in compliance with the License.
1834+ * You may obtain a copy of the License at
1835+ *
1836+ * http://www.apache.org/licenses/LICENSE-2.0
1837+ *
1838+ * Unless required by applicable law or agreed to in writing, software
1839+ * distributed under the License is distributed on an "AS IS" BASIS,
1840+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1841+ * See the License for the specific language governing permissions and
1842+ * limitations under the License.
1843+ */
1844+
1845+// ******************************************
1846+// * *
1847+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
1848+// * SEE .xml FILE WITH SAME NAME *
1849+// * *
1850+// ******************************************
1851+#ifndef ZORBA_RUNTIME_JSON_JSON_H
1852+#define ZORBA_RUNTIME_JSON_JSON_H
1853+
1854+
1855+#include "common/shared_types.h"
1856+
1857+
1858+
1859+#include "runtime/base/narybase.h"
1860+
1861+
1862+namespace zorba {
1863+
1864+/**
1865+ *
1866+ * function for parsing strings into json-xdm
1867+ *
1868+ * Author: Zorba Team
1869+ */
1870+class JSONParseInternal : public NaryBaseIterator<JSONParseInternal, PlanIteratorState>
1871+{
1872+public:
1873+ SERIALIZABLE_CLASS(JSONParseInternal);
1874+
1875+ SERIALIZABLE_CLASS_CONSTRUCTOR2T(JSONParseInternal,
1876+ NaryBaseIterator<JSONParseInternal, PlanIteratorState>);
1877+
1878+ void serialize( ::zorba::serialization::Archiver& ar)
1879+ {
1880+ serialize_baseclass(ar,
1881+ (NaryBaseIterator<JSONParseInternal, PlanIteratorState>*)this);
1882+ }
1883+
1884+ JSONParseInternal(
1885+ static_context* sctx,
1886+ const QueryLoc& loc,
1887+ std::vector<PlanIter_t>& children)
1888+ :
1889+ NaryBaseIterator<JSONParseInternal, PlanIteratorState>(sctx, loc, children)
1890+ {}
1891+
1892+ virtual ~JSONParseInternal();
1893+
1894+ void accept(PlanIterVisitor& v) const;
1895+
1896+ bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
1897+};
1898+
1899+
1900+/**
1901+ *
1902+ * Function to serialize json/jsonml xdm to string
1903+ *
1904+ * Author: Zorba Team
1905+ */
1906+class JSONSerializeInternal : public NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>
1907+{
1908+public:
1909+ SERIALIZABLE_CLASS(JSONSerializeInternal);
1910+
1911+ SERIALIZABLE_CLASS_CONSTRUCTOR2T(JSONSerializeInternal,
1912+ NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>);
1913+
1914+ void serialize( ::zorba::serialization::Archiver& ar)
1915+ {
1916+ serialize_baseclass(ar,
1917+ (NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>*)this);
1918+ }
1919+
1920+ JSONSerializeInternal(
1921+ static_context* sctx,
1922+ const QueryLoc& loc,
1923+ std::vector<PlanIter_t>& children)
1924+ :
1925+ NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>(sctx, loc, children)
1926+ {}
1927+
1928+ virtual ~JSONSerializeInternal();
1929+
1930+ void accept(PlanIterVisitor& v) const;
1931+
1932+ bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
1933+};
1934+
1935+
1936+}
1937+#endif
1938+/*
1939+ * Local variables:
1940+ * mode: c++
1941+ * End:
1942+ */
1943
1944=== added file 'src/runtime/json/snelson.cpp'
1945--- src/runtime/json/snelson.cpp 1970-01-01 00:00:00 +0000
1946+++ src/runtime/json/snelson.cpp 2012-02-02 21:41:24 +0000
1947@@ -0,0 +1,512 @@
1948+/*
1949+ * Copyright 2006-2011 The FLWOR Foundation.
1950+ *
1951+ * Licensed under the Apache License, Version 2.0 (the "License");
1952+ * you may not use this file except in compliance with the License.
1953+ * You may obtain a copy of the License at
1954+ *
1955+ * http://www.apache.org/licenses/LICENSE-2.0
1956+ *
1957+ * Unless required by applicable law or agreed to in writing, software
1958+ * distributed under the License is distributed on an "AS IS" BASIS,
1959+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1960+ * See the License for the specific language governing permissions and
1961+ * limitations under the License.
1962+ */
1963+#include "stdafx.h"
1964+
1965+#include <sstream>
1966+
1967+#include <zorba/diagnostic_list.h>
1968+
1969+#include "runtime/json/json.h"
1970+#include "store/api/item_factory.h"
1971+#include "system/globalenv.h"
1972+#include "types/root_typemanager.h"
1973+#include "util/ascii_util.h"
1974+#include "util/cxx_util.h"
1975+#include "util/indent.h"
1976+#include "util/json_parser.h"
1977+#include "util/mem_streambuf.h"
1978+#include "util/omanip.h"
1979+#include "util/oseparator.h"
1980+#include "util/stl_util.h"
1981+
1982+#include "snelson.h"
1983+
1984+#define SNELSON_NS "http://john.snelson.org.uk/parsing-json-into-xquery"
1985+
1986+using namespace std;
1987+
1988+namespace zorba {
1989+
1990+///////////////////////////////////////////////////////////////////////////////
1991+
1992+static void add_type_attribute( store::Item *parent, char const *value ) {
1993+ store::Item_t junk_item, att_name, type_name, value_item;
1994+ GENV_ITEMFACTORY->createQName( att_name, "", "", "type" );
1995+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
1996+ zstring value_string( value );
1997+ GENV_ITEMFACTORY->createString( value_item, value_string );
1998+ GENV_ITEMFACTORY->createAttributeNode(
1999+ junk_item, parent, att_name, type_name, value_item
2000+ );
2001+}
2002+
2003+#define ADD_TYPE_ATTRIBUTE(T) \
2004+ do { \
2005+ if ( needs_type_attribute ) { \
2006+ add_type_attribute( cur_item, T ); \
2007+ needs_type_attribute = false; \
2008+ } \
2009+ } while (0)
2010+
2011+static void add_item_element( item_stack_type &item_stack,
2012+ state_stack_type &state_stack,
2013+ store::Item_t &cur_item,
2014+ char const *type ) {
2015+ store::Item_t element_name, type_name;
2016+ zstring base_uri;
2017+ store::NsBindings ns_bindings;
2018+ GENV_ITEMFACTORY->createQName( element_name, SNELSON_NS, "", "item" );
2019+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2020+ GENV_ITEMFACTORY->createElementNode(
2021+ cur_item, item_stack.top(),
2022+ element_name, type_name, false, false, ns_bindings, base_uri
2023+ );
2024+ add_type_attribute( cur_item.getp(), type );
2025+ PUSH_ITEM( cur_item );
2026+}
2027+
2028+#define ADD_ITEM_ELEMENT(T) \
2029+ if ( !ztd::top_stack_equals( state_stack, in_array ) ) ; else \
2030+ add_item_element( item_stack, state_stack, cur_item, T )
2031+
2032+#define POP_ITEM_ELEMENT() \
2033+ if ( !ztd::top_stack_equals( state_stack, in_array ) ) ; else \
2034+ POP_ITEM()
2035+
2036+static void escape_json_chars( zstring *s ) {
2037+ ascii::replace_all( *s, "\"", 1, "\\\"", 2 );
2038+ ascii::replace_all( *s, "\\", 1, "\\\\", 2 );
2039+ ascii::replace_all( *s, "\b", 1, "\\b", 2 );
2040+ ascii::replace_all( *s, "\f", 1, "\\f", 2 );
2041+ ascii::replace_all( *s, "\n", 1, "\\n", 2 );
2042+ ascii::replace_all( *s, "\r", 1, "\\r", 2 );
2043+ ascii::replace_all( *s, "\t", 1, "\\t", 2 );
2044+}
2045+
2046+///////////////////////////////////////////////////////////////////////////////
2047+
2048+namespace snelson {
2049+
2050+void parse( json::parser &p, store::Item_t *result ) {
2051+ ZORBA_ASSERT( result );
2052+
2053+ store::Item_t cur_item, junk_item, value_item;
2054+ store::Item_t att_name, element_name, type_name;
2055+
2056+ zstring base_uri;
2057+ bool got_something = false;
2058+ item_stack_type item_stack;
2059+ bool needs_type_attribute = false;
2060+ bool next_string_is_key = false;
2061+ store::NsBindings ns_bindings;
2062+ state_stack_type state_stack;
2063+ zstring value;
2064+
2065+ json::token token;
2066+ while ( p.next( &token ) ) {
2067+ got_something = true;
2068+
2069+ if ( !*result ) {
2070+ GENV_ITEMFACTORY->createQName( element_name, SNELSON_NS, "", "json" );
2071+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2072+ GENV_ITEMFACTORY->createElementNode(
2073+ cur_item, nullptr,
2074+ element_name, type_name, false, false, ns_bindings, base_uri
2075+ );
2076+ *result = cur_item;
2077+ needs_type_attribute = true;
2078+ }
2079+
2080+ switch ( token.get_type() ) {
2081+
2082+ case '[':
2083+ PUSH_ITEM( cur_item );
2084+ ADD_TYPE_ATTRIBUTE( "array" );
2085+ ADD_ITEM_ELEMENT( "array" );
2086+ PUSH_STATE( in_array );
2087+ break;
2088+
2089+ case '{':
2090+ PUSH_ITEM( cur_item );
2091+ ADD_TYPE_ATTRIBUTE( "object" );
2092+ ADD_ITEM_ELEMENT( "object" );
2093+ PUSH_STATE( in_object );
2094+ next_string_is_key = true;
2095+ break;
2096+
2097+ case ']':
2098+ case '}':
2099+ POP_ITEM();
2100+ POP_STATE();
2101+ POP_ITEM_ELEMENT();
2102+ break;
2103+
2104+ case ',':
2105+ next_string_is_key = (state_stack.top() == in_object);
2106+ break;
2107+
2108+ case json::token::number:
2109+ ADD_TYPE_ATTRIBUTE( "number" );
2110+ ADD_ITEM_ELEMENT( "number" );
2111+ value = token.get_value();
2112+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
2113+ POP_ITEM_ELEMENT();
2114+ break;
2115+
2116+ case json::token::string:
2117+ ADD_TYPE_ATTRIBUTE( "string" );
2118+ value = token.get_value();
2119+#if 0
2120+ escape_json_chars( &value );
2121+#endif
2122+
2123+ if ( next_string_is_key ) {
2124+ // <pair name="..." ...>
2125+ GENV_ITEMFACTORY->createQName( element_name, SNELSON_NS, "", "pair" );
2126+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2127+ GENV_ITEMFACTORY->createElementNode(
2128+ cur_item, item_stack.top(),
2129+ element_name, type_name, false, false, ns_bindings, base_uri
2130+ );
2131+
2132+ GENV_ITEMFACTORY->createQName( att_name, "", "", "name" );
2133+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2134+ GENV_ITEMFACTORY->createString( value_item, value );
2135+ GENV_ITEMFACTORY->createAttributeNode(
2136+ junk_item, cur_item, att_name, type_name, value_item
2137+ );
2138+
2139+ needs_type_attribute = true;
2140+ next_string_is_key = false;
2141+ } else {
2142+ ADD_ITEM_ELEMENT( "string" );
2143+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
2144+ POP_ITEM_ELEMENT();
2145+ }
2146+ break;
2147+
2148+ case 'F':
2149+ case 'T':
2150+ ADD_TYPE_ATTRIBUTE( "boolean" );
2151+ ADD_ITEM_ELEMENT( "boolean" );
2152+ value = token.get_type() == 'F' ? "false" : "true";
2153+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
2154+ POP_ITEM_ELEMENT();
2155+ break;
2156+
2157+ case json::token::json_null:
2158+ ADD_TYPE_ATTRIBUTE( "null" );
2159+ ADD_ITEM_ELEMENT( "null" );
2160+ POP_ITEM_ELEMENT();
2161+ break;
2162+
2163+ case ':':
2164+ case json::token::none:
2165+ break;
2166+
2167+ default:
2168+ assert( false );
2169+ } // switch
2170+ } // while
2171+ if ( !got_something )
2172+ throw XQUERY_EXCEPTION( zerr::ZJPE0009_ILLEGAL_EMPTY_STRING );
2173+}
2174+
2175+} // namespace snelson
2176+
2177+///////////////////////////////////////////////////////////////////////////////
2178+
2179+static void assert_json_type( json::type t, zstring const &s ) {
2180+ // Doing it this way uses the string data in-place with no copy.
2181+ mem_streambuf::char_type *const p =
2182+ const_cast<mem_streambuf::char_type*>( s.data() );
2183+ mem_streambuf buf( p, s.size() );
2184+ istringstream iss;
2185+ iss.ios::rdbuf( &buf );
2186+
2187+ json::lexer lex( iss );
2188+ json::token token;
2189+ try {
2190+ if ( lex.next( &token ) && json::map_type( token.get_type() ) == t )
2191+ return;
2192+ }
2193+ catch ( json::exception const& ) {
2194+ // do nothing
2195+ }
2196+ throw XQUERY_EXCEPTION(
2197+ zerr::ZJSE0008_BAD_VALUE,
2198+ ERROR_PARAMS( s, t )
2199+ );
2200+}
2201+
2202+static void require_attribute_value( store::Item_t const &element,
2203+ char const *att_name,
2204+ zstring *att_value ) {
2205+ if ( !get_attribute_value( element, att_name, att_value ) )
2206+ throw XQUERY_EXCEPTION(
2207+ zerr::ZJSE0002_ELEMENT_MISSING_ATTRIBUTE,
2208+ ERROR_PARAMS( element->getNodeName()->getStringValue(), att_name )
2209+ );
2210+}
2211+
2212+static json::type get_json_type( store::Item_t const &element,
2213+ bool allow_all_types = true ) {
2214+ zstring att_value;
2215+ require_attribute_value( element, "type", &att_value );
2216+ if ( att_value == "array" )
2217+ return json::array;
2218+ if ( att_value == "object" )
2219+ return json::object;
2220+ if ( allow_all_types ) {
2221+ if ( att_value == "boolean" )
2222+ return json::boolean;
2223+ if ( att_value == "null" )
2224+ return json::null;
2225+ if ( att_value == "number" )
2226+ return json::number;
2227+ if ( att_value == "string" )
2228+ return json::string;
2229+ }
2230+ throw XQUERY_EXCEPTION(
2231+ zerr::ZJSE0003_BAD_ATTRIBUTE_VALUE,
2232+ ERROR_PARAMS( att_value, "type" )
2233+ );
2234+}
2235+
2236+inline std::ostream& if_space_or_newline( std::ostream &o,
2237+ whitespace::type ws ) {
2238+ if ( ws == whitespace::some )
2239+ o << ' ';
2240+ else
2241+ o << if_emit( ws == whitespace::indent, '\n' );
2242+ return o;
2243+}
2244+DEF_OMANIP1( if_space_or_newline, whitespace::type )
2245+
2246+static ostream& serialize_begin( ostream &o, json::type t,
2247+ whitespace::type ws ) {
2248+ switch ( t ) {
2249+ case json::array :
2250+ o << '[' << if_emit( ws, ' ' );
2251+ break;
2252+ case json::object:
2253+ o << '{' << if_space_or_newline( ws ) << if_indent( ws, inc_indent );
2254+ break;
2255+ default:
2256+ /* suppress warning */;
2257+ }
2258+ return o;
2259+}
2260+DEF_OMANIP2( serialize_begin, json::type, whitespace::type )
2261+
2262+static ostream& serialize_end( ostream &o, json::type t, whitespace::type ws ) {
2263+ switch ( t ) {
2264+ case json::array:
2265+ o << if_emit( ws, ' ' ) << ']';
2266+ break;
2267+ case json::object:
2268+ o << if_space_or_newline( ws ) << if_indent( ws, dec_indent )
2269+ << if_indent( ws, indent ) << '}';
2270+ break;
2271+ default:
2272+ /* suppress warning */;
2273+ }
2274+ return o;
2275+}
2276+DEF_OMANIP2( serialize_end, json::type, whitespace::type )
2277+
2278+static ostream& serialize_boolean( ostream &o, zstring const &s ) {
2279+ assert_json_type( json::boolean, s );
2280+ return o << s;
2281+}
2282+DEF_OMANIP1( serialize_boolean, zstring const& )
2283+
2284+static ostream& serialize_number( ostream &o, zstring const &s ) {
2285+ assert_json_type( json::number, s );
2286+ return o << s;
2287+}
2288+DEF_OMANIP1( serialize_number, zstring const& )
2289+
2290+static ostream& serialize_string( ostream &o, zstring const &s ) {
2291+ zstring temp( s );
2292+ escape_json_chars( &temp );
2293+ temp.insert( (zstring::size_type)0, 1, '"' );
2294+ temp.append( 1, '"' );
2295+ assert_json_type( json::string, temp );
2296+ return o << temp;
2297+}
2298+DEF_OMANIP1( serialize_string, zstring const& )
2299+
2300+static ostream& serialize_children( ostream&, store::Item_t const&, json::type,
2301+ whitespace::type );
2302+DEF_OMANIP3( serialize_children, store::Item_t const&, json::type,
2303+ whitespace::type )
2304+
2305+static ostream& serialize_json_element( ostream &o,
2306+ store::Item_t const &element,
2307+ whitespace::type ws ) {
2308+ zstring const element_name( element->getNodeName()->getStringValue() );
2309+ if ( element_name != "json" )
2310+ throw XQUERY_EXCEPTION(
2311+ zerr::ZJSE0004_BAD_ELEMENT,
2312+ ERROR_PARAMS( element_name, "json" )
2313+ );
2314+
2315+ json::type const t = get_json_type( element, false );
2316+
2317+ return o
2318+ << serialize_begin( t, ws )
2319+ << serialize_children( element, t, ws )
2320+ << serialize_end( t, ws );
2321+}
2322+DEF_OMANIP2( serialize_json_element, store::Item_t const&, whitespace::type )
2323+
2324+static ostream& serialize_item_element( ostream &o,
2325+ store::Item_t const &element,
2326+ whitespace::type ws ) {
2327+ zstring const element_name( element->getNodeName()->getStringValue() );
2328+ if ( element_name != "item" )
2329+ throw XQUERY_EXCEPTION(
2330+ zerr::ZJSE0005_BAD_CHILD_ELEMENT,
2331+ ERROR_PARAMS( element_name, "array", "item" )
2332+ );
2333+
2334+ json::type const t = get_json_type( element );
2335+
2336+ return o
2337+ << serialize_begin( t, ws )
2338+ << serialize_children( element, t, ws )
2339+ << serialize_end( t, ws );
2340+}
2341+DEF_OMANIP2( serialize_item_element, store::Item_t const&, whitespace::type )
2342+
2343+static ostream& serialize_pair_element( ostream &o,
2344+ store::Item_t const &element,
2345+ whitespace::type ws ) {
2346+ zstring const element_name( element->getNodeName()->getStringValue() );
2347+ if ( element_name != "pair" )
2348+ throw XQUERY_EXCEPTION(
2349+ zerr::ZJSE0005_BAD_CHILD_ELEMENT,
2350+ ERROR_PARAMS( element_name, "object", "pair" )
2351+ );
2352+
2353+ zstring name_att_value;
2354+ require_attribute_value( element, "name", &name_att_value );
2355+ json::type const t = get_json_type( element );
2356+
2357+ return o
2358+ << if_indent( ws, indent ) << serialize_string( name_att_value )
2359+ << if_emit( ws, ' ' ) << ':' << if_emit( ws, ' ' )
2360+ << serialize_begin( t, ws )
2361+ << serialize_children( element, t, ws )
2362+ << serialize_end( t, ws );
2363+}
2364+DEF_OMANIP2( serialize_pair_element, store::Item_t const&, whitespace::type )
2365+
2366+static ostream& serialize_children( ostream &o, store::Item_t const &parent,
2367+ json::type parent_type,
2368+ whitespace::type ws ) {
2369+ if ( parent_type == json::null )
2370+ o << "null";
2371+ else {
2372+ oseparator sep;
2373+ if ( ws == whitespace::none )
2374+ sep.sep( "," );
2375+ else if ( ws == whitespace::some || parent_type == json::array )
2376+ sep.sep( ", " );
2377+ else
2378+ sep.sep( ",\n" );
2379+
2380+ store::Iterator_t i = parent->getChildren();
2381+ i->open();
2382+ store::Item_t child;
2383+ while ( i->next( child ) ) {
2384+
2385+ switch ( child->getNodeKind() ) {
2386+
2387+ case store::StoreConsts::elementNode:
2388+ o << sep;
2389+ switch ( parent_type ) {
2390+ case json::none:
2391+ o << serialize_json_element( child, ws );
2392+ break;
2393+ case json::array:
2394+ o << serialize_item_element( child, ws );
2395+ break;
2396+ case json::object:
2397+ o << serialize_pair_element( child, ws );
2398+ break;
2399+ default:
2400+ throw XQUERY_EXCEPTION(
2401+ zerr::ZJSE0006_NO_ELEMENT_CHILD,
2402+ ERROR_PARAMS( json::type_string_of[ parent_type ] )
2403+ );
2404+ }
2405+ break;
2406+
2407+ case store::StoreConsts::textNode:
2408+ o << sep;
2409+ switch ( parent_type ) {
2410+ case json::boolean:
2411+ o << serialize_boolean( child->getStringValue() );
2412+ break;
2413+ case json::number:
2414+ o << serialize_number( child->getStringValue() );
2415+ break;
2416+ case json::string:
2417+ o << serialize_string( child->getStringValue() );
2418+ break;
2419+ default:
2420+ throw XQUERY_EXCEPTION(
2421+ zerr::ZJSE0007_NO_TEXT_CHILD,
2422+ ERROR_PARAMS( json::type_string_of[ parent_type ] )
2423+ );
2424+ }
2425+ break;
2426+
2427+ default:
2428+ // do nothing
2429+ break;
2430+ } // switch
2431+ } // while
2432+ i->close();
2433+ }
2434+ return o;
2435+}
2436+
2437+///////////////////////////////////////////////////////////////////////////////
2438+
2439+namespace snelson {
2440+
2441+void serialize( ostream &o, store::Item_t const &item, whitespace::type ws ) {
2442+ switch ( item->getNodeKind() ) {
2443+ case store::StoreConsts::documentNode:
2444+ o << serialize_children( item, json::none, ws );
2445+ break;
2446+ case store::StoreConsts::elementNode:
2447+ o << serialize_json_element( item, ws );
2448+ break;
2449+ default:
2450+ throw XQUERY_EXCEPTION( zerr::ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE );
2451+ }
2452+}
2453+
2454+} // namespace snelson
2455+
2456+///////////////////////////////////////////////////////////////////////////////
2457+
2458+} // namespace zorba
2459+/* vim:set et sw=2 ts=2: */
2460
2461=== added file 'src/runtime/json/snelson.h'
2462--- src/runtime/json/snelson.h 1970-01-01 00:00:00 +0000
2463+++ src/runtime/json/snelson.h 2012-02-02 21:41:24 +0000
2464@@ -0,0 +1,41 @@
2465+/*
2466+ * Copyright 2006-2011 The FLWOR Foundation.
2467+ *
2468+ * Licensed under the Apache License, Version 2.0 (the "License");
2469+ * you may not use this file except in compliance with the License.
2470+ * You may obtain a copy of the License at
2471+ *
2472+ * http://www.apache.org/licenses/LICENSE-2.0
2473+ *
2474+ * Unless required by applicable law or agreed to in writing, software
2475+ * distributed under the License is distributed on an "AS IS" BASIS,
2476+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2477+ * See the License for the specific language governing permissions and
2478+ * limitations under the License.
2479+ */
2480+#include "stdafx.h"
2481+
2482+#ifndef ZORBA_RUNTIME_JSON_SNELSON_H
2483+#define ZORBA_RUNTIME_JSON_SNELSON_H
2484+
2485+#include <iostream>
2486+
2487+#include "store/api/item.h"
2488+#include "util/json_parser.h"
2489+
2490+#include "common.h"
2491+
2492+namespace zorba {
2493+namespace snelson {
2494+
2495+///////////////////////////////////////////////////////////////////////////////
2496+
2497+void parse( json::parser &p, store::Item_t *result );
2498+void serialize( std::ostream&, store::Item_t const &item, whitespace::type );
2499+
2500+///////////////////////////////////////////////////////////////////////////////
2501+
2502+} // namespace snelson
2503+} // namespace zorba
2504+#endif /* ZORBA_RUNTIME_JSON_SNELSON_H */
2505+/* vim:set et sw=2 ts=2: */
2506
2507=== added directory 'src/runtime/spec/json'
2508=== added file 'src/runtime/spec/json/json.xml'
2509--- src/runtime/spec/json/json.xml 1970-01-01 00:00:00 +0000
2510+++ src/runtime/spec/json/json.xml 2012-02-02 21:41:24 +0000
2511@@ -0,0 +1,52 @@
2512+<?xml version="1.0" encoding="UTF-8"?>
2513+
2514+<!--
2515+////////////////////////////////////////////////////////////////////////////////
2516+////////////////////////////////////////////////////////////////////////////////
2517+-->
2518+<zorba:iterators
2519+ xmlns:zorba="http://www.zorba-xquery.com"
2520+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2521+ xsi:schemaLocation="http://www.zorba-xquery.com ../runtime.xsd">
2522+
2523+<!--
2524+/*******************************************************************************
2525+*******************************************************************************/
2526+-->
2527+<zorba:iterator name="JSONParseInternal" arity="nary">
2528+
2529+ <zorba:description author="Zorba Team">
2530+ function for parsing strings into json-xdm
2531+ </zorba:description>
2532+
2533+ <zorba:function isDeterministic="true">
2534+ <zorba:signature localname="parse-internal" prefix="fn-zorba-json">
2535+ <zorba:param>xs:string</zorba:param>
2536+ <zorba:param>item()?</zorba:param>
2537+ <zorba:output>element()*</zorba:output>
2538+ </zorba:signature>
2539+ </zorba:function>
2540+
2541+</zorba:iterator>
2542+
2543+<!--
2544+/*******************************************************************************
2545+*******************************************************************************/
2546+-->
2547+<zorba:iterator name="JSONSerializeInternal" arity="nary">
2548+
2549+ <zorba:description author="Zorba Team">
2550+ Function to serialize json/jsonml xdm to string
2551+ </zorba:description>
2552+
2553+ <zorba:function isDeterministic="true">
2554+ <zorba:signature localname="serialize-internal" prefix="fn-zorba-json">
2555+ <zorba:param>item()*</zorba:param>
2556+ <zorba:param>item()?</zorba:param>
2557+ <zorba:output>xs:string</zorba:output>
2558+ </zorba:signature>
2559+ </zorba:function>
2560+
2561+</zorba:iterator>
2562+
2563+</zorba:iterators>
2564
2565=== modified file 'src/runtime/spec/mappings.xml'
2566--- src/runtime/spec/mappings.xml 2011-10-14 07:35:51 +0000
2567+++ src/runtime/spec/mappings.xml 2012-02-02 21:41:24 +0000
2568@@ -106,6 +106,10 @@
2569 define="ZORBA_STRING_FN_NS"
2570 prefix="fn-zorba-string"/>
2571
2572+ <zorba:namespace uri="http://www.zorba-xquery.com/modules/converters/json"
2573+ define="ZORBA_JSON_FN_NS"
2574+ prefix="fn-zorba-json"/>
2575+
2576 <zorba:namespace uri="http://www.zorba-xquery.com/modules/fetch"
2577 define="ZORBA_FETCH_FN_NS"
2578 prefix="fn-zorba-fetch"/>
2579
2580=== modified file 'src/runtime/visitors/pregenerated/planiter_visitor.h'
2581--- src/runtime/visitors/pregenerated/planiter_visitor.h 2012-01-11 17:30:25 +0000
2582+++ src/runtime/visitors/pregenerated/planiter_visitor.h 2012-02-02 21:41:24 +0000
2583@@ -251,6 +251,10 @@
2584
2585 class FunctionAnnotationsIterator;
2586
2587+ class JSONParseInternal;
2588+
2589+ class JSONSerializeInternal;
2590+
2591 class SqrtIterator;
2592
2593 class ExpIterator;
2594@@ -929,6 +933,12 @@
2595 virtual void beginVisit ( const FunctionAnnotationsIterator& ) = 0;
2596 virtual void endVisit ( const FunctionAnnotationsIterator& ) = 0;
2597
2598+ virtual void beginVisit ( const JSONParseInternal& ) = 0;
2599+ virtual void endVisit ( const JSONParseInternal& ) = 0;
2600+
2601+ virtual void beginVisit ( const JSONSerializeInternal& ) = 0;
2602+ virtual void endVisit ( const JSONSerializeInternal& ) = 0;
2603+
2604 virtual void beginVisit ( const SqrtIterator& ) = 0;
2605 virtual void endVisit ( const SqrtIterator& ) = 0;
2606
2607
2608=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.cpp'
2609--- src/runtime/visitors/pregenerated/printer_visitor.cpp 2012-01-11 17:30:25 +0000
2610+++ src/runtime/visitors/pregenerated/printer_visitor.cpp 2012-02-02 21:41:24 +0000
2611@@ -50,6 +50,7 @@
2612 #include "runtime/function_item/function_item_iter.h"
2613 #include "runtime/indexing/ic_ddl.h"
2614 #include "runtime/introspection/sctx.h"
2615+#include "runtime/json/json.h"
2616 #include "runtime/maths/maths.h"
2617 #include "runtime/nodes/node_position.h"
2618 #include "runtime/nodes/nodes.h"
2619@@ -1650,6 +1651,34 @@
2620 // </FunctionAnnotationsIterator>
2621
2622
2623+// <JSONParseInternal>
2624+void PrinterVisitor::beginVisit ( const JSONParseInternal& a) {
2625+ thePrinter.startBeginVisit("JSONParseInternal", ++theId);
2626+ printCommons( &a, theId );
2627+ thePrinter.endBeginVisit( theId );
2628+}
2629+
2630+void PrinterVisitor::endVisit ( const JSONParseInternal& ) {
2631+ thePrinter.startEndVisit();
2632+ thePrinter.endEndVisit();
2633+}
2634+// </JSONParseInternal>
2635+
2636+
2637+// <JSONSerializeInternal>
2638+void PrinterVisitor::beginVisit ( const JSONSerializeInternal& a) {
2639+ thePrinter.startBeginVisit("JSONSerializeInternal", ++theId);
2640+ printCommons( &a, theId );
2641+ thePrinter.endBeginVisit( theId );
2642+}
2643+
2644+void PrinterVisitor::endVisit ( const JSONSerializeInternal& ) {
2645+ thePrinter.startEndVisit();
2646+ thePrinter.endEndVisit();
2647+}
2648+// </JSONSerializeInternal>
2649+
2650+
2651 // <SqrtIterator>
2652 void PrinterVisitor::beginVisit ( const SqrtIterator& a) {
2653 thePrinter.startBeginVisit("SqrtIterator", ++theId);
2654
2655=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.h'
2656--- src/runtime/visitors/pregenerated/printer_visitor.h 2012-01-11 17:30:25 +0000
2657+++ src/runtime/visitors/pregenerated/printer_visitor.h 2012-02-02 21:41:24 +0000
2658@@ -379,6 +379,12 @@
2659 void beginVisit( const FunctionAnnotationsIterator& );
2660 void endVisit ( const FunctionAnnotationsIterator& );
2661
2662+ void beginVisit( const JSONParseInternal& );
2663+ void endVisit ( const JSONParseInternal& );
2664+
2665+ void beginVisit( const JSONSerializeInternal& );
2666+ void endVisit ( const JSONSerializeInternal& );
2667+
2668 void beginVisit( const SqrtIterator& );
2669 void endVisit ( const SqrtIterator& );
2670
2671
2672=== modified file 'src/unit_tests/CMakeLists.txt'
2673--- src/unit_tests/CMakeLists.txt 2012-02-02 09:56:52 +0000
2674+++ src/unit_tests/CMakeLists.txt 2012-02-02 21:41:24 +0000
2675@@ -19,6 +19,8 @@
2676 test_uri.cpp
2677 unique_ptr.cpp
2678 unit_tests.cpp
2679+ test_uri.cpp
2680+ json_parser.cpp
2681 )
2682
2683 IF (NOT ZORBA_NO_FULL_TEXT)
2684
2685=== added file 'src/unit_tests/json_parser.cpp'
2686--- src/unit_tests/json_parser.cpp 1970-01-01 00:00:00 +0000
2687+++ src/unit_tests/json_parser.cpp 2012-02-02 21:41:24 +0000
2688@@ -0,0 +1,636 @@
2689+/*
2690+ * Copyright 2006-2008 The FLWOR Foundation.
2691+ *
2692+ * Licensed under the Apache License, Version 2.0 (the "License");
2693+ * you may not use this file except in compliance with the License.
2694+ * You may obtain a copy of the License at
2695+ *
2696+ * http://www.apache.org/licenses/LICENSE-2.0
2697+ *
2698+ * Unless required by applicable law or agreed to in writing, software
2699+ * distributed under the License is distributed on an "AS IS" BASIS,
2700+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2701+ * See the License for the specific language governing permissions and
2702+ * limitations under the License.
2703+ */
2704+
2705+#include <sstream>
2706+
2707+#include "util/json_parser.h"
2708+
2709+using namespace std;
2710+using namespace zorba;
2711+using namespace zorba::json;
2712+
2713+///////////////////////////////////////////////////////////////////////////////
2714+
2715+static int failures;
2716+
2717+static bool assert_true( char const *expr, int line, bool result ) {
2718+ if ( !result ) {
2719+ cout << "FAILED, line " << line << ": " << expr << endl;
2720+ ++failures;
2721+ }
2722+ return result;
2723+}
2724+
2725+static void print_exception( char const *expr, int line,
2726+ std::exception const &e ) {
2727+ assert_true( expr, line, false );
2728+ cout << "+ exception: ";
2729+ if ( json::exception const *j = dynamic_cast<json::exception const*>( &e ) ) {
2730+ json::location const &loc = j->get_loc();
2731+ if ( loc.file() && *loc.file() )
2732+ cout << '"' << loc.file() << "\": ";
2733+ cout << loc.line();
2734+ if ( loc.column() )
2735+ cout << ',' << loc.column();
2736+ cout << ": ";
2737+ }
2738+ cout << e.what() << endl;
2739+}
2740+
2741+#define ASSERT_TRUE( EXPR ) assert_true( #EXPR, __LINE__, !!(EXPR) )
2742+
2743+#define ASSERT_EXCEPTION( EXPR, EXCEPTION ) \
2744+ try { EXPR; assert_true( #EXPR, __LINE__, false ); } \
2745+ catch ( EXCEPTION const& ) { }
2746+
2747+#define ASSERT_NO_EXCEPTION( EXPR ) \
2748+ try { EXPR; } \
2749+ catch ( std::exception const &e ) { print_exception( #EXPR, __LINE__, e ); } \
2750+ catch ( ... ) { assert_true( #EXPR, __LINE__, false ); }
2751+
2752+#define ASSERT_TRUE_AND_NO_EXCEPTION( EXPR ) \
2753+ try { ASSERT_TRUE( EXPR ); } \
2754+ catch ( std::exception const &e ) { print_exception( #EXPR, __LINE__, e ); } \
2755+ catch ( ... ) { assert_true( #EXPR, __LINE__, false ); }
2756+
2757+///////////////////////////////////////////////////////////////////////////////
2758+
2759+static void test_empty_stream() {
2760+ char const source[] = "";
2761+ istringstream iss( source );
2762+ parser p( iss );
2763+ token t;
2764+ ASSERT_NO_EXCEPTION( p.next( &t ) );
2765+}
2766+
2767+static void test_illegal_character() {
2768+ char const source[] = " x ";
2769+ istringstream iss( source );
2770+ lexer lex( iss );
2771+ token t;
2772+ ASSERT_EXCEPTION( lex.next( &t ), illegal_character );
2773+}
2774+
2775+static void test_illegal_codepoint() {
2776+ static char const *const sources[] = {
2777+ " \" \\u \" ",
2778+ " \" \\u0 \" ",
2779+ " \" \\u00 \" ",
2780+ " \" \\u000 \" ",
2781+ " \" \\uG \" ",
2782+ " \" \\u\" ",
2783+ 0
2784+ };
2785+
2786+ for ( char const *const *s = sources; *s; ++s ) {
2787+ istringstream iss( *s );
2788+ lexer lex( iss );
2789+ token t;
2790+ ASSERT_EXCEPTION( lex.next( &t ), illegal_codepoint );
2791+ }
2792+}
2793+
2794+static void test_illegal_escape() {
2795+ char const source[] = " \" \\x \" ";
2796+ istringstream iss( source );
2797+ lexer lex( iss );
2798+ token t;
2799+ ASSERT_EXCEPTION( lex.next( &t ), illegal_escape );
2800+}
2801+
2802+static void test_illegal_literal() {
2803+ static char const *const sources[] = {
2804+ " f ",
2805+ " fa ",
2806+ " fal ",
2807+ " fals ",
2808+ " falsee ",
2809+ " t ",
2810+ " tr ",
2811+ " tru ",
2812+ " truee ",
2813+ " n ",
2814+ " nu ",
2815+ " nul ",
2816+ " nulll ",
2817+ 0
2818+ };
2819+
2820+ for ( char const *const *s = sources; *s; ++s ) {
2821+ istringstream iss( *s );
2822+ lexer lex( iss );
2823+ token t;
2824+ ASSERT_EXCEPTION( lex.next( &t ), illegal_literal );
2825+ }
2826+}
2827+
2828+static void test_illegal_number() {
2829+}
2830+
2831+static void test_json_org_example() {
2832+ char const source[] =
2833+/* 1 */ "{" "\n"
2834+/* 2 */ " \"glossary\": {" "\n"
2835+/* 3 */ " \"title\": \"example glossary\"," "\n"
2836+/* 4 */ " \"GlossDiv\": {" "\n"
2837+/* 5 */ " \"title\": \"S\"," "\n"
2838+/* 6 */ " \"GlossList\": {" "\n"
2839+/* 7 */ " \"GlossEntry\": {" "\n"
2840+/* 8 */ " \"ID\": \"SGML\"," "\n"
2841+/* 9 */ " \"SortAs\": \"SGML\"," "\n"
2842+/* 10 */ " \"GlossTerm\": \"Standard Generalized Markup Language\"," "\n"
2843+/* 11 */ " \"Acronym\": \"SGML\"," "\n"
2844+/* 12 */ " \"Abbrev\": \"ISO 8879:1986\"," "\n"
2845+/* 13 */ " \"GlossDef\": {" "\n"
2846+/* 14 */ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\"," "\n"
2847+/* 15 */ " \"GlossSeeAlso\": [\"GML\", \"XML\"]" "\n"
2848+/* 16 */ " }," "\n"
2849+/* 17 */ " \"GlossSee\": \"markup\"" "\n"
2850+/* 18 */ " }" "\n"
2851+/* 19 */ " }" "\n"
2852+/* 20 */ " }" "\n"
2853+/* 21 */ " }" "\n"
2854+/* 22 */ "}" "\n" ;
2855+
2856+ istringstream iss( source );
2857+ parser p( iss );
2858+ token t;
2859+
2860+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 1: {
2861+ ASSERT_TRUE( t == token::begin_object );
2862+
2863+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 2: "glossary"
2864+ ASSERT_TRUE( t == token::string );
2865+
2866+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 2: :
2867+ ASSERT_TRUE( t == token::name_separator );
2868+
2869+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 2: {
2870+ ASSERT_TRUE( t == token::begin_object );
2871+
2872+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: "title"
2873+ ASSERT_TRUE( t == token::string );
2874+
2875+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: :
2876+ ASSERT_TRUE( t == token::name_separator );
2877+
2878+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: "example glossary"
2879+ ASSERT_TRUE( t == token::string );
2880+
2881+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: ,
2882+ ASSERT_TRUE( t == token::value_separator );
2883+
2884+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 4: "GlossDiv"
2885+ ASSERT_TRUE( t == token::string );
2886+
2887+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 4: :
2888+ ASSERT_TRUE( t == token::name_separator );
2889+
2890+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 4: {
2891+ ASSERT_TRUE( t == token::begin_object );
2892+
2893+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: "title"
2894+ ASSERT_TRUE( t == token::string );
2895+
2896+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: :
2897+ ASSERT_TRUE( t == token::name_separator );
2898+
2899+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: "S"
2900+ ASSERT_TRUE( t == token::string );
2901+
2902+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: ,
2903+ ASSERT_TRUE( t == token::value_separator );
2904+
2905+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 6: "GlossList"
2906+ ASSERT_TRUE( t == token::string );
2907+
2908+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 6: :
2909+ ASSERT_TRUE( t == token::name_separator );
2910+
2911+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 6: {
2912+ ASSERT_TRUE( t == token::begin_object );
2913+
2914+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 7: "GlossEntry"
2915+ ASSERT_TRUE( t == token::string );
2916+
2917+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 7: :
2918+ ASSERT_TRUE( t == token::name_separator );
2919+
2920+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 7: {
2921+ ASSERT_TRUE( t == token::begin_object );
2922+
2923+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: "ID"
2924+ ASSERT_TRUE( t == token::string );
2925+
2926+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: :
2927+ ASSERT_TRUE( t == token::name_separator );
2928+
2929+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: "SGML"
2930+ ASSERT_TRUE( t == token::string );
2931+
2932+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: ,
2933+ ASSERT_TRUE( t == token::value_separator );
2934+
2935+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: "SortAs"
2936+ ASSERT_TRUE( t == token::string );
2937+
2938+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: :
2939+ ASSERT_TRUE( t == token::name_separator );
2940+
2941+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: "SGML"
2942+ ASSERT_TRUE( t == token::string );
2943+
2944+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: ,
2945+ ASSERT_TRUE( t == token::value_separator );
2946+
2947+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: "GlossTerm"
2948+ ASSERT_TRUE( t == token::string );
2949+
2950+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: :
2951+ ASSERT_TRUE( t == token::name_separator );
2952+
2953+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: "Standard ..."
2954+ ASSERT_TRUE( t == token::string );
2955+
2956+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: ,
2957+ ASSERT_TRUE( t == token::value_separator );
2958+
2959+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: "Acronym"
2960+ ASSERT_TRUE( t == token::string );
2961+
2962+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: :
2963+ ASSERT_TRUE( t == token::name_separator );
2964+
2965+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: "SGML"
2966+ ASSERT_TRUE( t == token::string );
2967+
2968+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: ,
2969+ ASSERT_TRUE( t == token::value_separator );
2970+
2971+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: "Abbrev"
2972+ ASSERT_TRUE( t == token::string );
2973+
2974+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: :
2975+ ASSERT_TRUE( t == token::name_separator );
2976+
2977+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: "ISO 8879:1986"
2978+ ASSERT_TRUE( t == token::string );
2979+
2980+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: ,
2981+ ASSERT_TRUE( t == token::value_separator );
2982+
2983+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 13: "GlossDef"
2984+ ASSERT_TRUE( t == token::string );
2985+
2986+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 13: :
2987+ ASSERT_TRUE( t == token::name_separator );
2988+
2989+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 13: {
2990+ ASSERT_TRUE( t == token::begin_object );
2991+
2992+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: "para"
2993+ ASSERT_TRUE( t == token::string );
2994+
2995+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: :
2996+ ASSERT_TRUE( t == token::name_separator );
2997+
2998+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: "A meta-markup ..."
2999+ ASSERT_TRUE( t == token::string );
3000+
3001+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: ,
3002+ ASSERT_TRUE( t == token::value_separator );
3003+
3004+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: "GlossSeeAlso"
3005+ ASSERT_TRUE( t == token::string );
3006+
3007+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: :
3008+ ASSERT_TRUE( t == token::name_separator );
3009+
3010+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: [
3011+ ASSERT_TRUE( t == token::begin_array );
3012+
3013+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: "GML"
3014+ ASSERT_TRUE( t == token::string );
3015+
3016+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: ,
3017+ ASSERT_TRUE( t == token::value_separator );
3018+
3019+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: "XML"
3020+ ASSERT_TRUE( t == token::string );
3021+
3022+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: ]
3023+ ASSERT_TRUE( t == token::end_array );
3024+
3025+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 16: }
3026+ ASSERT_TRUE( t == token::end_object );
3027+
3028+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 16: ,
3029+ ASSERT_TRUE( t == token::value_separator );
3030+
3031+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 17: "GlossSee"
3032+ ASSERT_TRUE( t == token::string );
3033+
3034+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 17: :
3035+ ASSERT_TRUE( t == token::name_separator );
3036+
3037+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 17: "markup"
3038+ ASSERT_TRUE( t == token::string );
3039+
3040+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 18: }
3041+ ASSERT_TRUE( t == token::end_object );
3042+
3043+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 19: }
3044+ ASSERT_TRUE( t == token::end_object );
3045+
3046+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 20: }
3047+ ASSERT_TRUE( t == token::end_object );
3048+
3049+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 21: }
3050+ ASSERT_TRUE( t == token::end_object );
3051+
3052+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 22: }
3053+ ASSERT_TRUE( t == token::end_object );
3054+
3055+ ASSERT_TRUE( !p.next( &t ) );
3056+}
3057+
3058+static void test_lexer_array() {
3059+ char const source[] = "[ 1, \"2\", false, true, null ]";
3060+ istringstream iss( source );
3061+ lexer lex( iss );
3062+ token t;
3063+
3064+ ASSERT_TRUE( lex.next( &t ) );
3065+ ASSERT_TRUE( t == token::begin_array );
3066+
3067+ ASSERT_TRUE( lex.next( &t ) );
3068+ ASSERT_TRUE( t == token::number );
3069+ ASSERT_TRUE( t.get_value() == "1" );
3070+
3071+ ASSERT_TRUE( lex.next( &t ) );
3072+ ASSERT_TRUE( t == token::value_separator );
3073+
3074+ ASSERT_TRUE( lex.next( &t ) );
3075+ ASSERT_TRUE( t == token::string );
3076+ ASSERT_TRUE( t.get_value() == "2" );
3077+
3078+ ASSERT_TRUE( lex.next( &t ) );
3079+ ASSERT_TRUE( t == token::value_separator );
3080+
3081+ ASSERT_TRUE( lex.next( &t ) );
3082+ ASSERT_TRUE( t == token::json_false );
3083+
3084+ ASSERT_TRUE( lex.next( &t ) );
3085+ ASSERT_TRUE( t == token::value_separator );
3086+
3087+ ASSERT_TRUE( lex.next( &t ) );
3088+ ASSERT_TRUE( t == token::json_true );
3089+
3090+ ASSERT_TRUE( lex.next( &t ) );
3091+ ASSERT_TRUE( t == token::value_separator );
3092+
3093+ ASSERT_TRUE( lex.next( &t ) );
3094+ ASSERT_TRUE( t == token::json_null );
3095+
3096+ ASSERT_TRUE( lex.next( &t ) );
3097+ ASSERT_TRUE( t == token::end_array );
3098+
3099+ ASSERT_TRUE( !lex.next( &t ) );
3100+}
3101+
3102+static void test_lexer_object() {
3103+ char const source[] = "{ \"a\" : 1, \"b\" : \"2\" }";
3104+ istringstream iss( source );
3105+ lexer lex( iss );
3106+ token t;
3107+
3108+ ASSERT_TRUE( lex.next( &t ) );
3109+ ASSERT_TRUE( t == token::begin_object );
3110+
3111+ ASSERT_TRUE( lex.next( &t ) );
3112+ ASSERT_TRUE( t == token::string );
3113+
3114+ ASSERT_TRUE( lex.next( &t ) );
3115+ ASSERT_TRUE( t == token::name_separator );
3116+
3117+ ASSERT_TRUE( lex.next( &t ) );
3118+ ASSERT_TRUE( t == token::number );
3119+
3120+ ASSERT_TRUE( lex.next( &t ) );
3121+ ASSERT_TRUE( t == token::value_separator );
3122+
3123+ ASSERT_TRUE( lex.next( &t ) );
3124+ ASSERT_TRUE( t == token::string );
3125+
3126+ ASSERT_TRUE( lex.next( &t ) );
3127+ ASSERT_TRUE( t == token::name_separator );
3128+
3129+ ASSERT_TRUE( lex.next( &t ) );
3130+ ASSERT_TRUE( t == token::string );
3131+
3132+ ASSERT_TRUE( lex.next( &t ) );
3133+ ASSERT_TRUE( t == token::end_object );
3134+
3135+ ASSERT_TRUE( !lex.next( &t ) );
3136+}
3137+
3138+static void test_parser_array() {
3139+ char const source[] = "[ 1, \"2\", false, true, null ]";
3140+ istringstream iss( source );
3141+ parser p( iss );
3142+ token t;
3143+
3144+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3145+ ASSERT_TRUE( t == token::begin_array );
3146+
3147+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3148+ ASSERT_TRUE( t == token::number );
3149+ ASSERT_TRUE( t.get_value() == "1" );
3150+
3151+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3152+ ASSERT_TRUE( t == token::value_separator );
3153+
3154+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3155+ ASSERT_TRUE( t == token::string );
3156+ ASSERT_TRUE( t.get_value() == "2" );
3157+
3158+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3159+ ASSERT_TRUE( t == token::value_separator );
3160+
3161+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3162+ ASSERT_TRUE( t == token::json_false );
3163+
3164+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3165+ ASSERT_TRUE( t == token::value_separator );
3166+
3167+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3168+ ASSERT_TRUE( t == token::json_true );
3169+
3170+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3171+ ASSERT_TRUE( t == token::value_separator );
3172+
3173+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3174+ ASSERT_TRUE( t == token::json_null );
3175+
3176+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3177+ ASSERT_TRUE( t == token::end_array );
3178+
3179+ ASSERT_TRUE( !p.next( &t ) );
3180+}
3181+
3182+static void test_parser_object() {
3183+ char const source[] = "{ \"a\" : 1, \"b\" : \"2\" }";
3184+ istringstream iss( source );
3185+ parser p( iss );
3186+ token t;
3187+
3188+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3189+ ASSERT_TRUE( t == token::begin_object );
3190+
3191+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3192+ ASSERT_TRUE( t == token::string );
3193+
3194+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3195+ ASSERT_TRUE( t == token::name_separator );
3196+
3197+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3198+ ASSERT_TRUE( t == token::number );
3199+
3200+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3201+ ASSERT_TRUE( t == token::value_separator );
3202+
3203+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3204+ ASSERT_TRUE( t == token::string );
3205+
3206+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3207+ ASSERT_TRUE( t == token::name_separator );
3208+
3209+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3210+ ASSERT_TRUE( t == token::string );
3211+
3212+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3213+ ASSERT_TRUE( t == token::end_object );
3214+
3215+ ASSERT_TRUE( !p.next( &t ) );
3216+}
3217+
3218+static void test_unexpected_token() {
3219+ token t;
3220+ {
3221+ char const source[] = "{ 1 }";
3222+ istringstream iss( source );
3223+ parser p( iss );
3224+
3225+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3226+ ASSERT_TRUE( t == token::begin_object );
3227+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3228+ }
3229+ {
3230+ char const source[] = "{ \"a\" : 1, }";
3231+ istringstream iss( source );
3232+ parser p( iss );
3233+
3234+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3235+ ASSERT_TRUE( t == token::begin_object );
3236+
3237+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3238+ ASSERT_TRUE( t == token::string );
3239+
3240+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3241+ ASSERT_TRUE( t == token::name_separator );
3242+
3243+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3244+ ASSERT_TRUE( t == token::number );
3245+
3246+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3247+ ASSERT_TRUE( t == token::value_separator );
3248+
3249+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3250+ }
3251+ {
3252+ char const source[] = "{ \"t\" : true \"f\" : false }";
3253+ istringstream iss( source );
3254+ parser p( iss );
3255+
3256+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3257+ ASSERT_TRUE( t == token::begin_object );
3258+
3259+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3260+ ASSERT_TRUE( t == token::string );
3261+
3262+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3263+ ASSERT_TRUE( t == token::name_separator );
3264+
3265+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3266+ ASSERT_TRUE( t == token::json_true );
3267+
3268+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3269+ }
3270+ {
3271+ char const source[] = "[ 1";
3272+ istringstream iss( source );
3273+ parser p( iss );
3274+
3275+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3276+ ASSERT_TRUE( t == token::begin_array );
3277+
3278+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3279+ ASSERT_TRUE( t == token::number );
3280+
3281+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3282+ }
3283+}
3284+
3285+static void test_unterminated_string() {
3286+ char const source[] = " \"hello ";
3287+ istringstream iss( source );
3288+ lexer lex( iss );
3289+ token t;
3290+
3291+ ASSERT_EXCEPTION( lex.next( &t ), unterminated_string );
3292+}
3293+
3294+///////////////////////////////////////////////////////////////////////////////
3295+
3296+namespace zorba {
3297+namespace UnitTests {
3298+
3299+int json_parser( int, char*[] ) {
3300+
3301+ // lexer-only tests
3302+ test_lexer_array();
3303+ test_lexer_object();
3304+ test_illegal_character();
3305+ test_illegal_codepoint();
3306+ test_illegal_escape();
3307+ test_illegal_literal();
3308+ test_illegal_number();
3309+ test_unterminated_string();
3310+
3311+ // parser tests
3312+ test_empty_stream();
3313+ test_parser_array();
3314+ test_parser_object();
3315+ test_unexpected_token();
3316+ test_json_org_example();
3317+
3318+ cout << failures << " test(s) failed\n";
3319+ return failures ? 1 : 0;
3320+}
3321+
3322+} // namespace UnitTests
3323+} // namespace zorba
3324+/* vim:set et sw=2 ts=2: */
3325
3326=== modified file 'src/unit_tests/unit_test_list.h'
3327--- src/unit_tests/unit_test_list.h 2012-02-02 09:56:52 +0000
3328+++ src/unit_tests/unit_test_list.h 2012-02-02 21:41:24 +0000
3329@@ -34,6 +34,7 @@
3330 /**
3331 * ADD NEW UNIT TESTS HERE
3332 */
3333+ int json_parser( int, char*[] );
3334
3335 void initializeTestList();
3336 };
3337
3338=== modified file 'src/unit_tests/unit_tests.cpp'
3339--- src/unit_tests/unit_tests.cpp 2012-02-02 09:56:52 +0000
3340+++ src/unit_tests/unit_tests.cpp 2012-02-02 21:41:24 +0000
3341@@ -39,6 +39,7 @@
3342 void initializeTestList() {
3343 libunittests["string"] = test_string;
3344 libunittests["uri"] = runUriTest;
3345+ libunittests["json_parser"] = json_parser;
3346 libunittests["unique_ptr"] = test_unique_ptr;
3347 #ifndef ZORBA_NO_FULL_TEXT
3348 libunittests["stemmer"] = test_stemmer;
3349
3350=== modified file 'src/util/CMakeLists.txt'
3351--- src/util/CMakeLists.txt 2011-07-18 14:25:21 +0000
3352+++ src/util/CMakeLists.txt 2012-02-02 21:41:24 +0000
3353@@ -20,6 +20,8 @@
3354 dir.cpp
3355 fs_util.cpp
3356 indent.cpp
3357+ json_parser.cpp
3358+ mem_streambuf.cpp
3359 regex.cpp
3360 string_util.cpp
3361 unicode_util.cpp
3362
3363=== added file 'src/util/json_parser.cpp'
3364--- src/util/json_parser.cpp 1970-01-01 00:00:00 +0000
3365+++ src/util/json_parser.cpp 2012-02-02 21:41:24 +0000
3366@@ -0,0 +1,662 @@
3367+/*
3368+ * Copyright 2006-2008 The FLWOR Foundation.
3369+ *
3370+ * Licensed under the Apache License, Version 2.0 (the "License");
3371+ * you may not use this file except in compliance with the License.
3372+ * You may obtain a copy of the License at
3373+ *
3374+ * http://www.apache.org/licenses/LICENSE-2.0
3375+ *
3376+ * Unless required by applicable law or agreed to in writing, software
3377+ * distributed under the License is distributed on an "AS IS" BASIS,
3378+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3379+ * See the License for the specific language governing permissions and
3380+ * limitations under the License.
3381+ */
3382+
3383+#include "diagnostics/assert.h"
3384+
3385+#include "ascii_util.h"
3386+#include "stl_util.h"
3387+#include "string_util.h"
3388+#include "utf8_util.h"
3389+
3390+#define DEBUG_JSON_PARSER 0
3391+
3392+#if DEBUG_JSON_PARSER
3393+# include "indent.h"
3394+#endif /* DEBUG_JSON_PARSER */
3395+
3396+#include "json_parser.h"
3397+
3398+using namespace std;
3399+
3400+namespace zorba {
3401+namespace json {
3402+
3403+///////////////////////////////////////////////////////////////////////////////
3404+
3405+char const *const type_string_of[] = {
3406+ "none",
3407+ "array",
3408+ "boolean",
3409+ "null",
3410+ "number",
3411+ "object",
3412+ "string"
3413+};
3414+
3415+type map_type( token::type tt ) {
3416+ switch ( tt ) {
3417+ case token::string:
3418+ return string;
3419+ case token::number:
3420+ return number;
3421+ case token::json_false:
3422+ case token::json_true:
3423+ return boolean;
3424+ case token::json_null:
3425+ return null;
3426+ default:
3427+ return none;
3428+ }
3429+}
3430+
3431+///////////////////////////////////////////////////////////////////////////////
3432+
3433+exception::exception( location const &loc, std::string const &message ) :
3434+ loc_( loc ), message_( message )
3435+{
3436+}
3437+
3438+exception::~exception() throw() {
3439+ // out-of-line since it's virtual
3440+}
3441+
3442+char const* exception::what() const throw() {
3443+ return message_.c_str();
3444+}
3445+
3446+illegal_character::illegal_character( location const &loc, char c ) :
3447+ exception( loc, BUILD_STRING( '\'', c, "': illegal character" ) ),
3448+ c_( c )
3449+{
3450+}
3451+
3452+illegal_character::~illegal_character() throw() {
3453+ // out-of-line since it's virtual
3454+}
3455+
3456+illegal_codepoint::illegal_codepoint( location const &loc,
3457+ token::value_type const &cp ) :
3458+ exception( loc, BUILD_STRING( '"', cp, "\": illegal codepoint" ) ),
3459+ codepoint_( cp )
3460+{
3461+}
3462+
3463+illegal_codepoint::~illegal_codepoint() throw() {
3464+ // out-of-line since it's virtual
3465+}
3466+
3467+illegal_escape::illegal_escape( location const &loc, char c ) :
3468+ exception( loc, BUILD_STRING( "\"\\", c, "\": illegal character escape" ) ),
3469+ esc_( c )
3470+{
3471+}
3472+
3473+illegal_escape::~illegal_escape() throw() {
3474+ // out-of-line since it's virtual
3475+}
3476+
3477+illegal_literal::illegal_literal( location const &loc ) :
3478+ exception( loc, "illegal literal" )
3479+{
3480+}
3481+
3482+illegal_literal::~illegal_literal() throw() {
3483+ // out-of-line since it's virtual
3484+}
3485+
3486+illegal_number::illegal_number( location const &loc ) :
3487+ exception( loc, "illegal number" )
3488+{
3489+}
3490+
3491+illegal_number::~illegal_number() throw() {
3492+ // out-of-line since it's virtual
3493+}
3494+
3495+unexpected_token::unexpected_token( token const &t ) :
3496+ exception( t.get_loc(), BUILD_STRING( '"', t, "\": unexpected token" ) ),
3497+ token_( t )
3498+{
3499+}
3500+
3501+unexpected_token::~unexpected_token() throw() {
3502+ // out-of-line since it's virtual
3503+}
3504+
3505+unterminated_string::unterminated_string( location const &loc ) :
3506+ exception( loc, "unterminated string" )
3507+{
3508+}
3509+
3510+unterminated_string::~unterminated_string() throw() {
3511+ // out-of-line since it's virtual
3512+}
3513+
3514+///////////////////////////////////////////////////////////////////////////////
3515+
3516+token::token() :
3517+ type_( none )
3518+{
3519+}
3520+
3521+ostream& operator<<( ostream &o, token::type tt ) {
3522+ switch ( tt ) {
3523+ case token::string : o << "string"; break;
3524+ case token::number : o << "number"; break;
3525+ case token::json_false: o << "false" ; break;
3526+ case token::json_null : o << "null" ; break;
3527+ case token::json_true : o << "true" ; break;
3528+ case token::none : o << "<none>"; break;
3529+ default : o << static_cast<char>( tt );
3530+ }
3531+ return o;
3532+}
3533+
3534+ostream& operator<<( ostream &o, token const &t ) {
3535+ switch ( t.get_type() ) {
3536+ case token::string: o << '"' << t.get_value() << '"'; break;
3537+ case token::number: o << t.get_value() ; break;
3538+ default : o << t.get_type() ;
3539+ }
3540+ return o;
3541+}
3542+
3543+///////////////////////////////////////////////////////////////////////////////
3544+
3545+lexer::lexer( istream &in ) :
3546+ in_( &in ),
3547+ line_( 1 ),
3548+ col_( 1 )
3549+{
3550+}
3551+
3552+bool lexer::get_char( char *c ) {
3553+ char const temp = in_->get();
3554+ if ( in_->good() ) {
3555+ if ( temp == '\n' )
3556+ ++line_, col_ = 1;
3557+ else
3558+ ++col_;
3559+ if ( c )
3560+ *c = temp;
3561+ return true;
3562+ }
3563+ return false;
3564+}
3565+
3566+bool lexer::peek_char( char *c ) {
3567+ *c = in_->peek();
3568+ return in_->good();
3569+}
3570+
3571+bool lexer::next( token *t ) {
3572+ while ( true ) {
3573+ cur_loc_ = cur_loc();
3574+ char c;
3575+ if ( !get_char( &c ) )
3576+ return false;
3577+ switch ( c ) {
3578+ case ' ':
3579+ case '\n':
3580+ case '\r':
3581+ case '\t':
3582+ continue;
3583+ case '"':
3584+ t->type_ = token::string;
3585+ t->loc_ = cur_loc_;
3586+ parse_string( &t->value_ );
3587+ return true;
3588+ case '-':
3589+ case '0':
3590+ case '1':
3591+ case '2':
3592+ case '3':
3593+ case '4':
3594+ case '5':
3595+ case '6':
3596+ case '7':
3597+ case '8':
3598+ case '9':
3599+ t->type_ = token::number;
3600+ t->loc_ = cur_loc_;
3601+ parse_number( c, &t->value_ );
3602+ return true;
3603+ case 'f':
3604+ case 'n':
3605+ case 't':
3606+ t->type_ = parse_literal( c, &t->value_ );
3607+ t->loc_ = cur_loc_;
3608+ return true;
3609+ case '[':
3610+ case '{':
3611+ case ']':
3612+ case '}':
3613+ case ':':
3614+ case ',':
3615+ t->type_ = static_cast<token::type>( c );
3616+ t->loc_ = cur_loc_;
3617+ return true;
3618+ default:
3619+ throw illegal_character( cur_loc_, c );
3620+ }
3621+ } // while
3622+}
3623+
3624+unicode::code_point lexer::parse_codepoint() {
3625+ static char const hex_digits[] = "0123456789ABCDEF";
3626+
3627+ zstring cp_string( "\\u" ); // needed only for error message
3628+
3629+ unicode::code_point cp = 0;
3630+ for ( int i = 1; i <= 4; ++i ) {
3631+ char c;
3632+ if ( !get_char( &c ) || !ascii::is_xdigit( c ) )
3633+ throw illegal_codepoint( cur_loc_, cp_string );
3634+ cp_string += c;
3635+ c = ascii::to_upper( c );
3636+ char const *const p = std::strchr( hex_digits, c );
3637+ assert( p );
3638+ cp = (cp << 4) | (p - hex_digits);
3639+ }
3640+ return cp;
3641+}
3642+
3643+token::type lexer::parse_literal( char first_c, token::value_type *value ) {
3644+ static token::value_type const false_value( "false" );
3645+ static token::value_type const null_value ( "null" );
3646+ static token::value_type const true_value ( "true" );
3647+
3648+ token::type tt;
3649+ switch ( first_c ) {
3650+ case 'f': *value = false_value; tt = token::json_false; break;
3651+ case 'n': *value = null_value ; tt = token::json_null ; break;
3652+ case 't': *value = true_value ; tt = token::json_true ; break;
3653+ default : assert( false );
3654+ }
3655+
3656+ char c;
3657+ for ( char const *s = value->c_str(); *++s; ) {
3658+ if ( !get_char( &c ) || c != *s )
3659+ throw illegal_literal( cur_loc_ );
3660+ }
3661+ if ( peek_char( &c ) && ascii::is_alnum( c ) )
3662+ throw illegal_literal( cur_loc_ );
3663+
3664+ return tt;
3665+}
3666+
3667+void lexer::parse_number( char first_c, token::value_type *value ) {
3668+ value->clear();
3669+
3670+ // <number> ::= [-] <int> [<frac>] [<exp>]
3671+ char c = first_c;
3672+ if ( c == '-' ) {
3673+ *value += c;
3674+ if ( !get_char( &c ) )
3675+ throw illegal_number( cur_loc_ );
3676+ }
3677+
3678+ // <int> := '0' | <1-9> <digit>*
3679+ if ( !ascii::is_digit( c ) )
3680+ throw illegal_number( cur_loc_ );
3681+ *value += c;
3682+ if ( c == '0' ) {
3683+ if ( !get_char( &c ) )
3684+ return;
3685+ } else {
3686+ while ( true ) {
3687+ if ( !get_char( &c ) )
3688+ return;
3689+ if ( !ascii::is_digit( c ) )
3690+ break;
3691+ *value += c;
3692+ }
3693+ }
3694+
3695+ // <frac> ::= '.' <digit>+
3696+ if ( c == '.' ) {
3697+ *value += c;
3698+ if ( !get_char( &c ) || !ascii::is_digit( c ) )
3699+ throw illegal_number( cur_loc_ );
3700+ *value += c;
3701+ while ( true ) {
3702+ if ( !get_char( &c ) )
3703+ return;
3704+ if ( !ascii::is_digit( c ) )
3705+ break;
3706+ *value += c;
3707+ }
3708+ }
3709+
3710+ // <exp> ::= <e> [<sign>] <digit>+
3711+ // <e> ::= 'e' | 'E'
3712+ // <sign> ::= '-' | '+'
3713+ if ( c == 'e' || c == 'E' ) {
3714+ *value += c;
3715+ if ( !get_char( &c ) )
3716+ throw illegal_number( cur_loc_ );
3717+ if ( c == '+' || c == '-' ) {
3718+ *value += c;
3719+ if ( !get_char( &c ) )
3720+ throw illegal_number( cur_loc_ );
3721+ }
3722+ if ( !ascii::is_digit( c ) )
3723+ throw illegal_number( cur_loc_ );
3724+ *value += c;
3725+ while ( true ) {
3726+ if ( !get_char( &c ) )
3727+ return;
3728+ if ( !ascii::is_digit( c ) )
3729+ break;
3730+ *value += c;
3731+ }
3732+ }
3733+
3734+ in_->putback( c );
3735+}
3736+
3737+void lexer::parse_string( token::value_type *value ) {
3738+ value->clear();
3739+ bool got_backslash = false;
3740+ location const start_loc( cur_loc_ );
3741+
3742+ while ( true ) {
3743+ cur_loc_ = cur_loc();
3744+ char c;
3745+ if ( !get_char( &c ) )
3746+ throw unterminated_string( start_loc );
3747+ if ( got_backslash ) {
3748+ got_backslash = false;
3749+ switch ( c ) {
3750+ case '"':
3751+ case '/':
3752+ case '\\':
3753+ *value += c;
3754+ break;
3755+ case 'b':
3756+ *value += '\b';
3757+ break;
3758+ case 'f':
3759+ *value += '\f';
3760+ break;
3761+ case 'n':
3762+ *value += '\n';
3763+ break;
3764+ case 'r':
3765+ *value += '\r';
3766+ break;
3767+ case 't':
3768+ *value += '\t';
3769+ break;
3770+ case 'u':
3771+ utf8::encode( parse_codepoint(), value );
3772+ break;
3773+ default:
3774+ throw illegal_escape( cur_loc_, c );
3775+ }
3776+ continue;
3777+ }
3778+
3779+ switch ( c ) {
3780+ case '\\':
3781+ got_backslash = true;
3782+ break;
3783+ case '"':
3784+ return;
3785+ default:
3786+ *value += c;
3787+ }
3788+ } // while
3789+}
3790+
3791+void lexer::set_loc( char const *file, line_type line, column_type col ) {
3792+ if ( file )
3793+ file_ = file;
3794+ line_ = line;
3795+ col_ = col;
3796+}
3797+
3798+///////////////////////////////////////////////////////////////////////////////
3799+
3800+#if DEBUG_JSON_PARSER
3801+
3802+ostream& operator<<( ostream &o, parser::state s ) {
3803+ static char const *const string_of[] = {
3804+ "A0", "A1", "A2",
3805+ "E0", "E1",
3806+ "J0", "J1",
3807+ "M0", "M1",
3808+ "O0", "O1", "O2",
3809+ "P0", "P1",
3810+ "V0"
3811+ };
3812+ return o << string_of[ s ];
3813+}
3814+
3815+static void throw_unexpected_token( int line, token const &t ) {
3816+ try {
3817+ throw unexpected_token( t );
3818+ }
3819+ catch ( exception const &e ) {
3820+ cerr << line << ": " << e.what() << endl;
3821+ throw;
3822+ }
3823+}
3824+
3825+bool parser::get_token_debug( int line, token *t ) {
3826+ bool const got_token = get_token( t );
3827+ cout << line << ": get_token => " << *t << endl;
3828+ return got_token;
3829+}
3830+
3831+bool parser::matches_token_debug( int line, token::type tt, token *t ) {
3832+ bool const matched = matches_token( tt, t );
3833+ cout << line << ": token " << *t << " matches " << tt << " => " << (matched ? 'T' : 'F') << endl;
3834+ return matched;
3835+}
3836+
3837+token::type parser::peek_token_debug( int line ) {
3838+ token::type const tt = peek_token();
3839+ cout << line << ": peek_token => " << peeked_token_ << endl;
3840+ return tt;
3841+}
3842+
3843+void parser::require_token_debug( int line, token::type tt, token *t ) {
3844+ if ( !get_token_debug( line, t ) || t->get_type() != tt )
3845+ throw_unexpected_token( line, *t );
3846+}
3847+
3848+# define GET_TOKEN(T) get_token_debug( __LINE__, T )
3849+# define MATCHES_TOKEN(TT,T) matches_token_debug( __LINE__, TT, T )
3850+# define PEEK_TOKEN() peek_token_debug( __LINE__ )
3851+# define REQUIRE_TOKEN(TT,T) require_token_debug( __LINE__, TT, T )
3852+# define THROW_UNEXPECTED_TOKEN(T) throw_unexpected_token( __LINE__, T )
3853+
3854+# define GOTO_STATE(S) \
3855+ if (0) ; else { \
3856+ state_ = (S); \
3857+ cout << __LINE__ << ':' << indent << "GOTO_STATE( " << state_ << " )" << endl; \
3858+ continue; \
3859+ }
3860+
3861+# define PUSH_STATE(S) \
3862+ if (0) ; else { \
3863+ state_stack_.push(S); \
3864+ cout << __LINE__ << ':' << indent << "PUSH_STATE( " << (S) << " )" << endl << inc_indent; \
3865+ }
3866+
3867+# define POP_STATE() \
3868+ if (0) ; else { \
3869+ state_ = ztd::pop_stack( state_stack_ ); \
3870+ cout << __LINE__ << ':' << indent << "POP_STATE() => " << state_ << endl << dec_indent; \
3871+ }
3872+
3873+#else
3874+
3875+# define GET_TOKEN(T) get_token( T )
3876+# define MATCHES_TOKEN(TT,T) matches_token( TT, T )
3877+# define PEEK_TOKEN() peek_token()
3878+# define REQUIRE_TOKEN(TT,T) require_token( TT, T )
3879+# define THROW_UNEXPECTED_TOKEN(T) throw unexpected_token( T )
3880+
3881+# define GOTO_STATE(S) { state_ = (S); continue; }
3882+# define PUSH_STATE(S) state_stack_.push(S)
3883+# define POP_STATE() state_ = ztd::pop_stack( state_stack_ )
3884+
3885+#endif /* DEBUG_JSON_PARSER */
3886+
3887+///////////////////////////////////////////////////////////////////////////////
3888+
3889+parser::parser( istream &in ) : lexer_( in ) {
3890+#if DEBUG_JSON_PARSER
3891+ get_indent( cout ) = 0;
3892+#endif /* DEBUG_JSON_PARSER */
3893+ PUSH_STATE( J0 );
3894+}
3895+
3896+bool parser::get_token( token *t ) {
3897+ if ( peeked_token_ ) {
3898+ *t = peeked_token_;
3899+ peeked_token_.clear();
3900+ return true;
3901+ }
3902+ t->clear();
3903+ return lexer_.next( t );
3904+}
3905+
3906+bool parser::matches_token( token::type tt, token *t ) {
3907+ if ( peek_token() == tt )
3908+ return get_token( t );
3909+ *t = peeked_token_;
3910+ return false;
3911+}
3912+
3913+token::type parser::peek_token() {
3914+ if ( !peeked_token_ )
3915+ lexer_.next( &peeked_token_ );
3916+ return peeked_token_.get_type();
3917+}
3918+
3919+#if ! DEBUG_JSON_PARSER
3920+void parser::require_token( token::type tt, token *t ) {
3921+ if ( !get_token( t ) || t->get_type() != tt )
3922+ THROW_UNEXPECTED_TOKEN( *t );
3923+}
3924+#endif /* DEBUG_JSON_PARSER */
3925+
3926+bool parser::next( token *t ) {
3927+ if ( state_stack_.empty() )
3928+ return false;
3929+ POP_STATE();
3930+ while ( true ) {
3931+ switch ( state_ ) {
3932+
3933+ // <JSON> ::= <Array> | <Object>
3934+ case J0: PUSH_STATE( J1 );
3935+ switch ( PEEK_TOKEN() ) {
3936+ case token::begin_array : GOTO_STATE( A0 );
3937+ case token::begin_object: GOTO_STATE( O0 );
3938+ case token::none : break;
3939+ default: THROW_UNEXPECTED_TOKEN( peeked_token_ );
3940+ }
3941+ case J1: return false;
3942+
3943+ // <Array> ::= '[' <Element>* ']'
3944+ case A0: REQUIRE_TOKEN( token::begin_array, t );
3945+ PUSH_STATE( A1 );
3946+ return true;
3947+ case A1: if ( MATCHES_TOKEN( token::end_array, t ) )
3948+ return true;
3949+ PUSH_STATE( A2 );
3950+ GOTO_STATE( E0 );
3951+ case A2: REQUIRE_TOKEN( token::end_array, t );
3952+ return true;
3953+
3954+ // <Element> ::= <Value> [ ',' <Element> ]
3955+ case E0: PUSH_STATE( E1 );
3956+ GOTO_STATE( V0 );
3957+ case E1: if ( MATCHES_TOKEN( token::value_separator, t ) ) {
3958+ PUSH_STATE( E0 );
3959+ return true;
3960+ }
3961+ POP_STATE();
3962+ continue;
3963+
3964+ // <Object> ::= '{' <Member>* '}'
3965+ case O0: REQUIRE_TOKEN( token::begin_object, t );
3966+ PUSH_STATE( O1 );
3967+ return true;
3968+ case O1: if ( MATCHES_TOKEN( token::end_object, t ) )
3969+ return true;
3970+ PUSH_STATE( O2 );
3971+ GOTO_STATE( M0 );
3972+ case O2: REQUIRE_TOKEN( token::end_object, t );
3973+ return true;
3974+
3975+ // <Member> ::= <Pair> [ ',' <Member> ]
3976+ case M0: PUSH_STATE( M1 );
3977+ GOTO_STATE( P0 );
3978+ case M1: if ( MATCHES_TOKEN( token::value_separator, t ) ) {
3979+ PUSH_STATE( M0 );
3980+ return true;
3981+ }
3982+ POP_STATE();
3983+ continue;
3984+
3985+ // <Pair> ::= <String> ':' <Value>
3986+ case P0: REQUIRE_TOKEN( token::string, t );
3987+ PUSH_STATE( P1 );
3988+ return true;
3989+ case P1: REQUIRE_TOKEN( token::name_separator, t );
3990+ PUSH_STATE( V0 );
3991+ return true;
3992+
3993+ // <Value> ::= <Array> | <Object> | <String> | <Number>
3994+ // | false | null | true
3995+ case V0: switch ( PEEK_TOKEN() ) {
3996+ case token::begin_array:
3997+ GOTO_STATE( A0 );
3998+ case token::begin_object:
3999+ GOTO_STATE( O0 );
4000+ case token::string:
4001+ case token::number:
4002+ case token::json_false:
4003+ case token::json_null:
4004+ case token::json_true:
4005+ GET_TOKEN( t );
4006+ return true;
4007+ default:
4008+ THROW_UNEXPECTED_TOKEN( peeked_token_ );
4009+ }
4010+ } // switch ( state_ )
4011+ } // while
4012+}
4013+
4014+token::type parser::peek( token *t ) {
4015+ if ( token::type const tt = PEEK_TOKEN() ) {
4016+ if ( t )
4017+ *t = peeked_token_;
4018+ return tt;
4019+ }
4020+ return token::none;
4021+}
4022+
4023+///////////////////////////////////////////////////////////////////////////////
4024+
4025+} // namespace json
4026+} // namespace zorba
4027+
4028+/* vim:set et sw=2 ts=2: */
4029
4030=== added file 'src/util/json_parser.h'
4031--- src/util/json_parser.h 1970-01-01 00:00:00 +0000
4032+++ src/util/json_parser.h 2012-02-02 21:41:24 +0000
4033@@ -0,0 +1,570 @@
4034+/*
4035+ * Copyright 2006-2008 The FLWOR Foundation.
4036+ *
4037+ * Licensed under the Apache License, Version 2.0 (the "License");
4038+ * you may not use this file except in compliance with the License.
4039+ * You may obtain a copy of the License at
4040+ *
4041+ * http://www.apache.org/licenses/LICENSE-2.0
4042+ *
4043+ * Unless required by applicable law or agreed to in writing, software
4044+ * distributed under the License is distributed on an "AS IS" BASIS,
4045+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4046+ * See the License for the specific language governing permissions and
4047+ * limitations under the License.
4048+ */
4049+
4050+#ifndef ZORBA_JSON_PARSER_H
4051+#define ZORBA_JSON_PARSER_H
4052+
4053+#include <zorba/config.h>
4054+
4055+#include <exception>
4056+#include <iostream>
4057+#include <stack>
4058+#include <string>
4059+
4060+#include <zorba/internal/diagnostic.h>
4061+
4062+#include "zorbatypes/zstring.h"
4063+
4064+#include "cxx_util.h"
4065+#include "unicode_util.h"
4066+
4067+namespace zorba {
4068+namespace json {
4069+
4070+///////////////////////////////////////////////////////////////////////////////
4071+
4072+typedef internal::diagnostic::location location;
4073+
4074+///////////////////////////////////////////////////////////////////////////////
4075+
4076+/**
4077+ * A JSON %type is the type of JSON data. This isn't used by the lexer or
4078+ * parser implementation at all, but it's handy.
4079+ */
4080+enum type {
4081+ none, // meaning "not set" as opposed to "null"
4082+ array,
4083+ boolean,
4084+ null,
4085+ number,
4086+ object,
4087+ string
4088+};
4089+extern char const *const type_string_of[];
4090+
4091+inline std::ostream& operator<<( std::ostream &o, type t ) {
4092+ return o << type_string_of[ t ];
4093+}
4094+
4095+/**
4096+ * A JSON %token. Tokens have a type, location at which they were found, and
4097+ * sometimes a value.
4098+ *
4099+ * See: "RFC 4627: The application/json Media Type for JavaScript Object
4100+ * Notation (JSON)."
4101+ */
4102+class token {
4103+ // see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2333.html
4104+ struct pointer_conversion { int valid; };
4105+ typedef int pointer_conversion::*explicit_bool;
4106+public:
4107+ typedef zstring value_type;
4108+
4109+ /**
4110+ * The types of tokens in JSON. The first 6 constants have values that
4111+ * correspond to the actual structural characters used by JSON; the rest were
4112+ * assigned non-standard, mnemonic values for convenience.
4113+ */
4114+ enum type {
4115+ none,
4116+ begin_array = '[',
4117+ begin_object = '{',
4118+ end_array = ']',
4119+ end_object = '}',
4120+ name_separator = ':',
4121+ value_separator = ',',
4122+ string = 'S',
4123+ number = 'N',
4124+ json_false = 'F',
4125+ json_null = '0',
4126+ json_true = 'T',
4127+ };
4128+
4129+ /**
4130+ * Default constructor.
4131+ */
4132+ token();
4133+
4134+ /**
4135+ * Clears this %token.
4136+ */
4137+ void clear() {
4138+ type_ = none;
4139+ value_.clear();
4140+ }
4141+
4142+ /**
4143+ * Gets the location at which this %token was found.
4144+ *
4145+ * @return Returns said location.
4146+ */
4147+ location const& get_loc() const {
4148+ return loc_;
4149+ }
4150+
4151+ /**
4152+ * Gets the type of this %token.
4153+ *
4154+ * @return Returns said type.
4155+ */
4156+ type get_type() const {
4157+ return type_;
4158+ }
4159+
4160+ /**
4161+ * Gets the value of this %token, if any. Only %token types string, number,
4162+ * false, null, and true have a value.
4163+ *
4164+ * @return Returns said value or the empty string.
4165+ */
4166+ value_type const& get_value() const {
4167+ return value_;
4168+ }
4169+
4170+ /**
4171+ * Conversion to \c bool.
4172+ *
4173+ * @return Returns \c true only if this token's type is not \c none.
4174+ */
4175+ operator explicit_bool() const {
4176+ return type_ ? &pointer_conversion::valid : nullptr;
4177+ }
4178+
4179+private:
4180+ location loc_;
4181+ type type_;
4182+ value_type value_;
4183+
4184+ friend class lexer;
4185+};
4186+
4187+/**
4188+ * Map a token's type to a JSON type.
4189+ *
4190+ * @param tt The token::type to map.
4191+ * @return Returns the corresponding JSON type or \c none if \a tt doesn't map.
4192+ */
4193+type map_type( token::type tt );
4194+
4195+/**
4196+ * Emits the given token type to an ostream.
4197+ *
4198+ * @param o The ostream to emit to.
4199+ * @param tt The token type to emit.
4200+ * @return Returns \a o.
4201+ */
4202+std::ostream& operator<<( std::ostream &o, token::type tt );
4203+
4204+/**
4205+ * Emits the given token to an ostream.
4206+ *
4207+ * @param o The ostream to emit to.
4208+ * @param t The token to emit.
4209+ * @return Returns \a o.
4210+ */
4211+std::ostream& operator<<( std::ostream &o, token const &t );
4212+
4213+/**
4214+ * Compares two tokens for equality.
4215+ *
4216+ * @param t1 The first token.
4217+ * @param t2 The second token.
4218+ * @return Returns \c true only if the two tokens' types and values are equal.
4219+ */
4220+inline bool operator==( token const &t1, token const &t2 ) {
4221+ return t1.get_type() == t2.get_type() && t1.get_value() == t2.get_value();
4222+}
4223+
4224+/**
4225+ * Compares a token's type to another type for equality.
4226+ *
4227+ * @param t The token whose type to compare.
4228+ * @param tt The type to compare to.
4229+ * @return Returns \c true only if the token's type equals \a tt.
4230+ */
4231+inline bool operator==( token const &t, token::type tt ) {
4232+ return t.get_type() == tt;
4233+}
4234+
4235+/**
4236+ * Compares a token's type to another type for equality.
4237+ *
4238+ * @param tt The type to compare.
4239+ * @param t The token whose type to compare to.
4240+ * @return Returns \c true only if \a tt equals the token's type.
4241+ */
4242+inline bool operator==( token::type tt, token const &t ) {
4243+ return t == tt;
4244+}
4245+
4246+/**
4247+ * Compares a token's value to a C string for equality.
4248+ *
4249+ * @param t The token whose value to compare.
4250+ * @param value The value to compare to.
4251+ * @return Returns \c true only if the token's value equals \a value.
4252+ */
4253+inline bool operator==( token const &t, char const *value ) {
4254+ return t.get_value() == value;
4255+}
4256+
4257+/**
4258+ * Compares a C string to a token's value for equality.
4259+ *
4260+ * @param value The value to compare.
4261+ * @param t The token whose value to compare to.
4262+ * @return Returns \c true only if \a value equals the token's value.
4263+ */
4264+inline bool operator==( char const *value, token const &t ) {
4265+ return t == value;
4266+}
4267+
4268+/**
4269+ * Compares two tokens for inequality.
4270+ *
4271+ * @param t1 The first token.
4272+ * @param t2 The second token.
4273+ * @return Returns \c true if either the two tokens' types or values are not
4274+ * equal.
4275+ */
4276+inline bool operator!=( token const &t1, token const &t2 ) {
4277+ return !(t1 == t2);
4278+}
4279+
4280+/**
4281+ * Compares a token's type to another type for inequality.
4282+ *
4283+ * @param t The token whose type to compare.
4284+ * @param tt The type to compare to.
4285+ * @return Returns \c true only if the token's type is not equal to \a tt.
4286+ */
4287+inline bool operator!=( token const &t, token::type tt ) {
4288+ return !(t == tt);
4289+}
4290+
4291+/**
4292+ * Compares a token's type to another type for inequality.
4293+ *
4294+ * @param tt The type to compare.
4295+ * @param t The token whose type to compare to.
4296+ * @return Returns \c true only if \a tt is not equal to the token's type.
4297+ */
4298+inline bool operator!=( token::type tt, token const &t ) {
4299+ return !(tt == t);
4300+}
4301+
4302+/**
4303+ * Compares a token's value to a C string for inequality.
4304+ *
4305+ * @param t The token whose value to compare.
4306+ * @param value The value to compare to.
4307+ * @return Returns \c true only if the token's value is not equal to \a value.
4308+ */
4309+inline bool operator!=( token const &t, char const *value ) {
4310+ return !(t == value);
4311+}
4312+
4313+/**
4314+ * Compares a token's value to a C string for inequality.
4315+ *
4316+ * @param value The value to compare.
4317+ * @param t The token whose value to compare to.
4318+ * @return Returns \c true only if \a value is not equal to the token's value.
4319+ */
4320+inline bool operator!=( char const *value, token const &t ) {
4321+ return !(value == t);
4322+}
4323+
4324+///////////////////////////////////////////////////////////////////////////////
4325+
4326+/**
4327+ * An %exception is the root of the JSON %exception hierarchy.
4328+ */
4329+class exception : public std::exception {
4330+public:
4331+ ~exception() throw();
4332+
4333+ /**
4334+ * Gets the location in the JSON source whence this exception was thrown.
4335+ */
4336+ location const& get_loc() const {
4337+ return loc_;
4338+ }
4339+
4340+ // inherited
4341+ char const* what() const throw();
4342+
4343+protected:
4344+ exception( location const &loc, std::string const &message );
4345+
4346+private:
4347+ location loc_;
4348+ std::string message_;
4349+};
4350+
4351+/**
4352+ * This exception is thrown when an illegal character is encountered in a JSON
4353+ * data stream.
4354+ */
4355+class illegal_character : public exception {
4356+public:
4357+ illegal_character( location const &loc, char c );
4358+ ~illegal_character() throw();
4359+
4360+ /**
4361+ * Gets the illegal character.
4362+ *
4363+ * @return Returns said character.
4364+ */
4365+ char get_char() const {
4366+ return c_;
4367+ }
4368+
4369+private:
4370+ char c_;
4371+};
4372+
4373+/**
4374+ * This exception is thrown when an illegal Unicode code-point escape sequence
4375+ * (\uHHHH) is encountered.
4376+ */
4377+class illegal_codepoint : public exception {
4378+public:
4379+ illegal_codepoint( location const &loc, token::value_type const &cp );
4380+ ~illegal_codepoint() throw();
4381+
4382+ /**
4383+ * Gets the illegal code-point.
4384+ *
4385+ * @return Returns said code-point.
4386+ */
4387+ token::value_type get_codepoint() const {
4388+ return codepoint_;
4389+ }
4390+
4391+private:
4392+ token::value_type codepoint_;
4393+};
4394+
4395+/**
4396+ * This exception is thrown when an illegal character follows a backslash
4397+ * (escape) within a string literal. The legal escape characters are:
4398+ * ["/\bfnrtu].
4399+ */
4400+class illegal_escape : public exception {
4401+public:
4402+ illegal_escape( location const &loc, char escape );
4403+ ~illegal_escape() throw();
4404+
4405+ /**
4406+ * Gets the illegal escape character.
4407+ *
4408+ * @return Returns said character.
4409+ */
4410+ char get_escape() const {
4411+ return esc_;
4412+ }
4413+
4414+private:
4415+ char esc_;
4416+};
4417+
4418+/**
4419+ * This exception is thrown when a literal other than \c false, \c null, or
4420+ * \c true is encountered.
4421+ */
4422+class illegal_literal : public exception {
4423+public:
4424+ illegal_literal( location const &loc );
4425+ ~illegal_literal() throw();
4426+};
4427+
4428+/**
4429+ * This exception is thrown when an illegal number is encountered.
4430+ */
4431+class illegal_number : public exception {
4432+public:
4433+ illegal_number( location const &loc );
4434+ ~illegal_number() throw();
4435+};
4436+
4437+/**
4438+ * This exception is thrown when an unexpected token is encountered.
4439+ */
4440+class unexpected_token : public exception {
4441+public:
4442+ unexpected_token( token const &t );
4443+ ~unexpected_token() throw();
4444+
4445+ /**
4446+ * Gets the unexpected token
4447+ *
4448+ * @return Returns said token.
4449+ */
4450+ token const& get_token() const {
4451+ return token_;
4452+ }
4453+
4454+private:
4455+ token token_;
4456+};
4457+
4458+/**
4459+ * This exception is thrown when an EOF is encountered before a string's
4460+ * terminating quote.
4461+ */
4462+class unterminated_string : public exception {
4463+public:
4464+ unterminated_string( location const &loc );
4465+ ~unterminated_string() throw();
4466+};
4467+
4468+///////////////////////////////////////////////////////////////////////////////
4469+
4470+/**
4471+ * A %lexer extracts JSON tokens from an istream.
4472+ */
4473+class lexer {
4474+public:
4475+ typedef location::line_type line_type;
4476+ typedef location::column_type column_type;
4477+
4478+ /**
4479+ * Constructs a %lexer on the given istream.
4480+ *
4481+ * @param in The istream to read from.
4482+ */
4483+ lexer( std::istream &in );
4484+
4485+ /**
4486+ * Gets the next token, if any.
4487+ *
4488+ * @param result A pointer to the token to get into.
4489+ * @return Returns \c true only if there was a next token.
4490+ * @throws exception upon error.
4491+ */
4492+ bool next( token *result );
4493+
4494+ /**
4495+ * Sets the file location.
4496+ *
4497+ * @param file The source file name.
4498+ * @param line The source line number.
4499+ * @param col The source column number.
4500+ */
4501+ void set_loc( char const *file, line_type line, column_type col );
4502+
4503+private:
4504+ location cur_loc() const {
4505+ return location( file_, line_, col_ );
4506+ }
4507+
4508+ bool get_char( char* = nullptr );
4509+ bool peek_char( char* );
4510+ unicode::code_point parse_codepoint();
4511+ token::type parse_literal( char, token::value_type* );
4512+ void parse_number( char, token::value_type* );
4513+ void parse_string( token::value_type* );
4514+
4515+ std::istream *in_;
4516+ std::string file_;
4517+ line_type line_;
4518+ column_type col_;
4519+ location cur_loc_;
4520+};
4521+
4522+///////////////////////////////////////////////////////////////////////////////
4523+
4524+/**
4525+ * A %parser extracts JSON tokens from an istream while checking to ensure the
4526+ * token sequence is valid.
4527+ */
4528+class parser {
4529+public:
4530+ typedef lexer::line_type line_type;
4531+ typedef lexer::column_type column_type;
4532+
4533+ /**
4534+ * Constructs a %parser on the given istream.
4535+ *
4536+ * @param in The istream to read from.
4537+ */
4538+ parser( std::istream &in );
4539+
4540+ /**
4541+ * Gets the next token, if any.
4542+ *
4543+ * @param result A pointer to the token to receive the token.
4544+ * @return Returns \c true only if there was a next token.
4545+ * @throws exception upon error.
4546+ */
4547+ bool next( token *result );
4548+
4549+ /**
4550+ * Peeks at the next token, if any.
4551+ *
4552+ * @param result A pointer to the token to receive the token, if any.
4553+ * @return Returns the type of the peeked token.
4554+ * @throws exception upon error.
4555+ */
4556+ token::type peek( token *result = nullptr );
4557+
4558+ /**
4559+ * Sets the file location.
4560+ *
4561+ * @param file The source file name.
4562+ * @param line The source line number.
4563+ * @param col The source column number.
4564+ */
4565+ void set_loc( char const *file, line_type line, column_type col ) {
4566+ lexer_.set_loc( file, line, col );
4567+ }
4568+
4569+private:
4570+ enum state {
4571+ A0, A1, A2, // Array
4572+ E0, E1, // Element
4573+ J0, J1, // JSON
4574+ M0, M1, // Member
4575+ O0, O1, O2, // Object
4576+ P0, P1, // Pair
4577+ V0 // Value
4578+ };
4579+
4580+ friend std::ostream& operator<<( std::ostream&, state );
4581+
4582+ bool get_token( token* );
4583+ bool get_token_debug( int, token* );
4584+ bool matches_token( token::type, token* );
4585+ bool matches_token_debug( int, token::type, token* );
4586+ token::type peek_token();
4587+ token::type peek_token_debug( int );
4588+ void require_token( token::type, token* );
4589+ void require_token_debug( int, token::type, token* );
4590+
4591+ lexer lexer_;
4592+ token peeked_token_;
4593+ std::stack<state> state_stack_;
4594+ state state_;
4595+};
4596+
4597+///////////////////////////////////////////////////////////////////////////////
4598+
4599+} // namespace json
4600+} // namespace zorba
4601+
4602+#endif /* ZORBA_JSON_PARSER_H */
4603+/* vim:set et sw=2 ts=2: */
4604
4605=== added file 'src/util/mem_streambuf.cpp'
4606--- src/util/mem_streambuf.cpp 1970-01-01 00:00:00 +0000
4607+++ src/util/mem_streambuf.cpp 2012-02-02 21:41:24 +0000
4608@@ -0,0 +1,119 @@
4609+/*
4610+ * Copyright 2006-2008 The FLWOR Foundation.
4611+ *
4612+ * Licensed under the Apache License, Version 2.0 (the "License");
4613+ * you may not use this file except in compliance with the License.
4614+ * You may obtain a copy of the License at
4615+ *
4616+ * http://www.apache.org/licenses/LICENSE-2.0
4617+ *
4618+ * Unless required by applicable law or agreed to in writing, software
4619+ * distributed under the License is distributed on an "AS IS" BASIS,
4620+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4621+ * See the License for the specific language governing permissions and
4622+ * limitations under the License.
4623+ */
4624+
4625+#include <cstring> /* for memcpy(3) */
4626+
4627+#include "diagnostics/assert.h"
4628+
4629+#include "cxx_util.h"
4630+#include "mem_streambuf.h"
4631+
4632+using namespace std;
4633+
4634+namespace zorba {
4635+
4636+///////////////////////////////////////////////////////////////////////////////
4637+
4638+mem_streambuf::mem_streambuf() {
4639+ set( nullptr, nullptr );
4640+}
4641+
4642+mem_streambuf::mem_streambuf( char_type *begin, char_type *end ) {
4643+ set( begin, end );
4644+}
4645+
4646+mem_streambuf::mem_streambuf( char_type *begin, off_type size ) {
4647+ set( begin, size );
4648+}
4649+
4650+mem_streambuf::int_type mem_streambuf::overflow( int_type c ) {
4651+ if ( traits_type::eq_int_type( c, traits_type::eof() ) )
4652+ return traits_type::not_eof( c );
4653+ if ( pptr() >= epptr() )
4654+ return traits_type::eof();
4655+ *pptr() = traits_type::to_char_type( c );
4656+ pbump( 1 );
4657+ return c;
4658+}
4659+
4660+mem_streambuf::int_type mem_streambuf::pbackfail( int_type c ) {
4661+ if ( !traits_type::eq_int_type( c, traits_type::eof() ) ) {
4662+ *pptr() = traits_type::to_int_type( c );
4663+ pbump( -1 );
4664+ }
4665+ return traits_type::to_int_type( *pptr() );
4666+}
4667+
4668+mem_streambuf::pos_type mem_streambuf::seekoff( off_type off,
4669+ ios_base::seekdir dir,
4670+ ios_base::openmode ) {
4671+ switch ( dir ) {
4672+ case ios_base::beg:
4673+ our_setg( begin_ + off );
4674+ break;
4675+ case ios_base::cur:
4676+ our_setg( gptr() + off );
4677+ break;
4678+ case ios_base::end:
4679+ our_setg( end_ + off );
4680+ break;
4681+ default:
4682+ ZORBA_ASSERT( false );
4683+ }
4684+ return off;
4685+}
4686+
4687+mem_streambuf::pos_type mem_streambuf::seekpos( pos_type pos,
4688+ ios_base::openmode mode ) {
4689+ return seekoff( pos, ios_base::beg, mode );
4690+}
4691+
4692+void mem_streambuf::set( char_type *begin, char_type *end ) {
4693+ begin_ = begin;
4694+ end_ = end;
4695+ our_setg( begin );
4696+ our_setp( end );
4697+}
4698+
4699+streamsize mem_streambuf::showmanyc() {
4700+ return egptr() - gptr();
4701+}
4702+
4703+mem_streambuf::int_type mem_streambuf::underflow() {
4704+ return gptr() < egptr() ?
4705+ traits_type::to_int_type( *gptr() ) : traits_type::eof();
4706+}
4707+
4708+streamsize mem_streambuf::xsgetn( char_type *buf, std::streamsize size ) {
4709+ streamsize const remaining = showmanyc();
4710+ if ( size > remaining )
4711+ size = remaining;
4712+ ::memcpy( buf, gptr(), size );
4713+ return size;
4714+}
4715+
4716+streamsize mem_streambuf::xsputn( char_type const *buf, streamsize size ) {
4717+ streamsize const remaining = epptr() - pptr();
4718+ if ( size > remaining )
4719+ size = remaining;
4720+ ::memcpy( pptr(), buf, size );
4721+ return size;
4722+}
4723+
4724+///////////////////////////////////////////////////////////////////////////////
4725+
4726+} // namespace zorba
4727+/* vim:set et sw=2 ts=2: */
4728
4729=== added file 'src/util/mem_streambuf.h'
4730--- src/util/mem_streambuf.h 1970-01-01 00:00:00 +0000
4731+++ src/util/mem_streambuf.h 2012-02-02 21:41:24 +0000
4732@@ -0,0 +1,108 @@
4733+/*
4734+ * Copyright 2006-2008 The FLWOR Foundation.
4735+ *
4736+ * Licensed under the Apache License, Version 2.0 (the "License");
4737+ * you may not use this file except in compliance with the License.
4738+ * You may obtain a copy of the License at
4739+ *
4740+ * http://www.apache.org/licenses/LICENSE-2.0
4741+ *
4742+ * Unless required by applicable law or agreed to in writing, software
4743+ * distributed under the License is distributed on an "AS IS" BASIS,
4744+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4745+ * See the License for the specific language governing permissions and
4746+ * limitations under the License.
4747+ */
4748+
4749+#ifndef ZORBA_MMAP_STREAMBUF_H
4750+#define ZORBA_MMAP_STREAMBUF_H
4751+
4752+#include <streambuf>
4753+
4754+namespace zorba {
4755+
4756+///////////////////////////////////////////////////////////////////////////////
4757+
4758+/**
4759+ * A %mem_streambuf is-a std::streambuf for a fixed-size chunk of memory.
4760+ */
4761+class mem_streambuf : public std::streambuf {
4762+public:
4763+ typedef std::streambuf::char_type char_type;
4764+ typedef std::streambuf::int_type int_type;
4765+ typedef std::streambuf::off_type off_type;
4766+ typedef std::streambuf::pos_type pos_type;
4767+ typedef std::streambuf::traits_type traits_type;
4768+
4769+ /**
4770+ * Default constructor.
4771+ */
4772+ mem_streambuf();
4773+
4774+ /**
4775+ * Constructs a %mem_streambuf.
4776+ *
4777+ * @param begin A pointer to the beginning of the memory chunk.
4778+ * @param end A pointer to one past the end of the memory chunk.
4779+ */
4780+ mem_streambuf( char_type *begin, char_type *end );
4781+
4782+ /**
4783+ * Constructs a %mem_streambuf.
4784+ *
4785+ * @param begin A pointer to the beginning of the memory chunk.
4786+ * @param size The size of the memory chunk.
4787+ */
4788+ mem_streambuf( char_type *begin, off_type size );
4789+
4790+ /**
4791+ * Sets the memory chunk.
4792+ *
4793+ * @param begin A pointer to the beginning of the memory chunk.
4794+ * @param end A pointer to one past the end of the memory chunk.
4795+ */
4796+ void set( char_type *begin, char_type *end );
4797+
4798+ /**
4799+ * Sets the memory chunk.
4800+ *
4801+ * @param begin A pointer to the beginning of the memory chunk.
4802+ * @param size The size of the memory chunk.
4803+ */
4804+ void set( char_type *begin, off_type size );
4805+
4806+protected:
4807+ int_type overflow( int_type c );
4808+ int_type pbackfail( int_type c );
4809+ pos_type seekoff( off_type, std::ios_base::seekdir, std::ios_base::openmode );
4810+ pos_type seekpos( pos_type, std::ios_base::openmode );
4811+ std::streamsize showmanyc();
4812+ int_type underflow();
4813+ std::streamsize xsgetn( char_type*, std::streamsize );
4814+ std::streamsize xsputn( char_type const*, std::streamsize );
4815+
4816+private:
4817+ char_type *begin_, *end_;
4818+
4819+ void our_setg( char_type *ptr ) {
4820+ setg( begin_, ptr, end_ );
4821+ }
4822+
4823+ void our_setp( char_type *ptr ) {
4824+ setp( ptr, end_ );
4825+ }
4826+
4827+ // forbid
4828+ mem_streambuf( mem_streambuf const& );
4829+ mem_streambuf& operator=( mem_streambuf const& );
4830+};
4831+
4832+inline void mem_streambuf::set( char_type *begin, off_type size ) {
4833+ set( begin, begin + size );
4834+}
4835+
4836+///////////////////////////////////////////////////////////////////////////////
4837+
4838+} // namespace zorba
4839+#endif /* ZORBA_MMAP_STREAMBUF_H */
4840+/* vim:set et sw=2 ts=2: */
4841
4842=== modified file 'src/util/omanip.h'
4843--- src/util/omanip.h 2011-06-14 17:26:33 +0000
4844+++ src/util/omanip.h 2012-02-02 21:41:24 +0000
4845@@ -25,127 +25,275 @@
4846 ///////////////////////////////////////////////////////////////////////////////
4847
4848 /**
4849- * An omanip1 is a class for assisting in the creation of ostream manipulators
4850+ * An %omanip1 is a class for assisting in the creation of ostream manipulators
4851 * by storing a pointer to a function and its argument to be called later via
4852 * operator<<().
4853 *
4854 * See also: "Standard C++ IOStreams and Locales," Angelika Langer and Klaus
4855 * Kreft, Addison-Wesley, pp. 179-191.
4856 */
4857-template<typename Arg1Type> class omanip1 {
4858+template<typename Arg1Type>
4859+class omanip1 {
4860 public:
4861
4862 /**
4863- * The signature of functions this omanip1 can handle.
4864+ * The signature of functions this %omanip1 can handle.
4865 */
4866 typedef std::ostream& (*func_type)( std::ostream&, Arg1Type );
4867
4868 /**
4869- * Constructs an omanip1.
4870+ * Constructs an %omanip1.
4871 *
4872- * @param f The function to call when this omanip1 is inserted into an
4873- * ostream.
4874- * @param arg1 The argument to be passed to the function.
4875+ * @param f The function to call when this %omanip1 is inserted into an
4876+ * ostream.
4877+ * @param a1 The argument to be passed to the function.
4878 */
4879- omanip1( func_type f, Arg1Type arg1 ) :
4880- f_( f ), arg1_( arg1 )
4881+ omanip1( func_type f, Arg1Type a1 ) :
4882+ f_( f ), a1_( a1 )
4883 {
4884 }
4885
4886 /**
4887- * Inserts the given omanip1 into the given ostream. This has the effect of
4888- * calling the function and argument bound to the omanip1 at the time of its
4889+ * Inserts the given %omanip1 into the given ostream. This has the effect of
4890+ * calling the function and argument bound to the %omanip1 at the time of its
4891 * construction.
4892 *
4893 * @param o The ostream to insert into.
4894- * @param m The omanip1 to insert.
4895+ * @param m The %omanip1 to insert.
4896 */
4897 friend std::ostream& operator<<( std::ostream &o, omanip1 const &m ) {
4898- return (*m.f_)( o, m.arg1_ );
4899+ if ( o.good() )
4900+ (*m.f_)( o, m.a1_ );
4901+ return o;
4902 }
4903
4904 private:
4905 func_type const f_;
4906- Arg1Type const arg1_;
4907+ Arg1Type const a1_;
4908 };
4909
4910 /**
4911 * Defines an ostream manipulator "thunk" function that calls an existing
4912 * non-manipulator function having the same name.
4913 *
4914- * @param FN_NAME The name of the existing function.
4915- * @param ARG1_TYPE The type of the non-ostream argument.
4916- * @param ARG1_NAME The name of the non-ostream argument.
4917+ * @param FN_NAME The name of the existing function.
4918+ * @param ARG1_T The type of the non-ostream argument.
4919 */
4920-#define DEF_OMANIP1(FN_NAME,ARG1_TYPE,ARG1_NAME) \
4921- inline omanip1<ARG1_TYPE> \
4922- FN_NAME( ARG1_TYPE ARG1_NAME ) { \
4923- return omanip1<ARG1_TYPE>( FN_NAME, ARG1_NAME ); \
4924+#define DEF_OMANIP1(FN_NAME,ARG1_T) \
4925+ inline omanip1<ARG1_T> \
4926+ FN_NAME( ARG1_T a1 ) { \
4927+ return omanip1<ARG1_T>( FN_NAME, a1 ); \
4928 }
4929
4930 ///////////////////////////////////////////////////////////////////////////////
4931
4932 /**
4933- * An omanip2 is a class for assisting in the creation of ostream manipulators
4934+ * An %omanip2 is a class for assisting in the creation of ostream manipulators
4935 * by storing a pointer to a function and its arguments to be called later via
4936 * operator<<().
4937 *
4938 * See also: "Standard C++ IOStreams and Locales," Angelika Langer and Klaus
4939 * Kreft, Addison-Wesley, pp. 179-191.
4940 */
4941-template<typename Arg1Type, typename Arg2Type> class omanip2 {
4942+template<typename Arg1Type,typename Arg2Type>
4943+class omanip2 {
4944 public:
4945
4946 /**
4947- * The signature of functions this omanip2 can handle.
4948+ * The signature of functions this %omanip2 can handle.
4949 */
4950 typedef std::ostream& (*func_type)( std::ostream&, Arg1Type, Arg2Type );
4951
4952 /**
4953- * Constructs an omanip2.
4954+ * Constructs an %omanip2.
4955 *
4956- * @param f The function to call when this omanip2 is inserted into an
4957- * ostream.
4958- * @param arg1 The first argument to be passed to the function.
4959- * @param arg2 The second argument to be passed to the function.
4960+ * @param f The function to call when this %omanip2 is inserted into an
4961+ * ostream.
4962+ * @param a1 The first argument to be passed to the function.
4963+ * @param a2 The second argument to be passed to the function.
4964 */
4965- omanip2( func_type f, Arg1Type arg1, Arg2Type arg2 ) :
4966- f_( f ), arg1_( arg1 ), arg2_( arg2 )
4967+ omanip2( func_type f, Arg1Type a1, Arg2Type a2 ) :
4968+ f_( f ), a1_( a1 ), a2_( a2 )
4969 {
4970 }
4971
4972 /**
4973- * Inserts the given omanip2 into the given ostream. This has the effect of
4974- * calling the function and argument bound to the omanip2 at the time of its
4975+ * Inserts the given %omanip2 into the given ostream. This has the effect of
4976+ * calling the function and argument bound to the %omanip2 at the time of its
4977 * construction.
4978 *
4979 * @param o The ostream to insert into.
4980- * @param m The omanip2 to insert.
4981+ * @param m The %omanip2 to insert.
4982 */
4983 friend std::ostream& operator<<( std::ostream &o, omanip2 const &m ) {
4984- return (*m.f_)( o, m.arg1_, m.arg2_ );
4985- }
4986-
4987-private:
4988- func_type const f_;
4989- Arg1Type const arg1_;
4990- Arg2Type const arg2_;
4991-};
4992-
4993-/**
4994- * Defines an ostream manipulator "thunk" function that calls an existing
4995- * non-manipulator function having the same name.
4996- *
4997- * @param FN_NAME The name of the existing function.
4998- * @param ARG1_TYPE The type of the first non-ostream argument.
4999- * @param ARG1_NAME The name of the first non-ostream argument.
5000- * @param ARG2_TYPE The type of the second non-ostream argument.
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches