on master help sign in
.ghci+1−1
since splitsnapshots
unifiedsplit
@@ -1,4 +1,4 @@
1−:set -fwarn-unused-binds -fwarn-unused-imports
1+:set -Wunused-binds -Wunused-imports -Worphans
2:set -isrc
2:set -isrc
3:load Main
3:load Main
4
4
.github/workflows/ci.ymlnew file+33
@@ -0,0 +1,33 @@
1+name: ci
2+on:
3+ push:
4+ pull_request:
5+ schedule:
6+ - cron: '0 3 * * 6' # 3am Saturday
7+jobs:
8+ test:
9+ runs-on: ${{ matrix.os }}
10+
11+ strategy:
12+ fail-fast: false
13+ matrix:
14+ os: [ubuntu-latest]
15+ ghc: ['9.6', '9.4', '9.2', '9.0', '8.10', '8.8']
16+ include:
17+ - os: windows-latest
18+ - os: macOS-latest
19+
20+ steps:
21+ - run: git config --global core.autocrlf false
22+ - uses: actions/checkout@v2
23+ - uses: haskell/actions/setup@v2
24+ id: setup-haskell
25+ with:
26+ ghc-version: ${{ matrix.ghc }}
27+ - run: cabal v2-freeze --enable-tests
28+ - uses: actions/cache@v2
29+ with:
30+ path: ${{ steps.setup-haskell.outputs.cabal-store }}
31+ key: ${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('cabal.project.freeze') }}
32+ restore-keys: ${{ runner.os }}-${{ matrix.ghc }}-
33+ - uses: ndmitchell/neil@master
.gitignore+6
@@ -1,5 +1,11 @@
1/dist/
1/dist/
2*.o
2*.o
3*.prof
3*.prof
4+*.suo
5+*.opendb
6+*.db
7+*.user
4/.stack-work/
8/.stack-work/
5/issues/
9/issues/
10+/vs/x64/
11+/afl-results/
.travis.ymldeleted−10
@@ -1,10 +0,0 @@
1−sudo: required
2−env:
3− - GHCVER=7.6.3
4− - GHCVER=7.8.4
5− - GHCVER=7.10.3
6− - GHCVER=8.0.1
7− - GHCVER=head
8−
9−script:
10− - wget https://raw.github.com/ndmitchell/neil/master/travis.sh -O - --no-check-certificate --quiet | sh
CHANGES.txt+16
@@ -1,3 +1,19 @@
1Changelog for Hexml
1Changelog for Hexml
2
2
3+0.3.4, released 2018-09-10
4+ #15, licensed under BSD-3-Clause OR Apache-2.0
5+0.3.3, released 2017-10-27
6+ #13, allow the . character in tags and attributes
7+0.3.2, released 2017-05-22
8+ #10, mark the small fast FFI calls as "unsafe" (up to 10x faster)
9+0.3.1, released 2016-12-27
10+ #9, don't walk off the end of the character table
11+0.3, released 2016-12-22
12+ #8, Mac compatibility
13+ Fix a bug when parsing closing comments
14+ #7, prefix all the C functions with hexml_
15+0.2, released 2016-12-14
16+ #6, remove buffer overruns
17+ #5, add lower bounds
18+0.1, released 2016-12-04
3 Initial version
19 Initial version
LICENSE+190−3
@@ -1,9 +1,20 @@
1−Copyright Neil Mitchell 2016.
1+Copyright Neil Mitchell 2016-2023.
2+
3+Licensed under either of:
4+
5+* BSD-3-Clause license (https://opensource.org/licenses/BSD-3-Clause)
6+* Apache License, version 2.0 (https://opensource.org/licenses/Apache-2.0)
7+
8+As a user, you may use this code under either license, at your option.
9+
10+
11+---------------------------------------------------------------------
12+BSD 3-Clause License
2All rights reserved.
13All rights reserved.
3
14
4Redistribution and use in source and binary forms, with or without
15Redistribution and use in source and binary forms, with or without
5−modification, are permitted provided that the following conditions are
16+modification, are permitted provided that the following conditions
6−met:
17+are met:
7
18
8 * Redistributions of source code must retain the above copyright
19 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
20 notice, this list of conditions and the following disclaimer.
@@ -28,3 +39,179 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42+
43+
44+---------------------------------------------------------------------
45+Apache License
46+Version 2.0, January 2004
47+<http://www.apache.org/licenses/>
48+
49+Terms and Conditions for use, reproduction, and distribution
50+1. Definitions
51+"License" shall mean the terms and conditions for use, reproduction,
52+and distribution as defined by Sections 1 through 9 of this document.
53+
54+"Licensor" shall mean the copyright owner or entity authorized by the
55+copyright owner that is granting the License.
56+
57+"Legal Entity" shall mean the union of the acting entity and all
58+other entities that control, are controlled by, or are under common
59+control with that entity. For the purposes of this definition,
60+"control" means (i) the power, direct or indirect, to cause the
61+direction or management of such entity, whether by contract or
62+otherwise, or (ii) ownership of fifty percent (50%) or more of the
63+outstanding shares, or (iii) beneficial ownership of such entity.
64+
65+"You" (or "Your") shall mean an individual or Legal Entity exercising
66+permissions granted by this License.
67+
68+"Source" form shall mean the preferred form for making modifications,
69+including but not limited to software source code, documentation
70+source, and configuration files.
71+
72+"Object" form shall mean any form resulting from mechanical
73+transformation or translation of a Source form, including but not
74+limited to compiled object code, generated documentation, and
75+conversions to other media types.
76+
77+"Work" shall mean the work of authorship, whether in Source or Object
78+form, made available under the License, as indicated by a copyright
79+notice that is included in or attached to the work).
80+
81+"Derivative Works" shall mean any work, whether in Source or Object
82+form, that is based on (or derived from) the Work and for which the
83+editorial revisions, annotations, elaborations, or other
84+modifications represent, as a whole, an original work of authorship.
85+For the purposes of this License, Derivative Works shall not include
86+works that remain separable from, or merely link (or bind by name) to
87+the interfaces of, the Work and Derivative Works thereof.
88+
89+"Contribution" shall mean any work of authorship, including the
90+original version of the Work and any modifications or additions to
91+that Work or Derivative Works thereof, that is intentionally
92+submitted to Licensor for inclusion in the Work by the copyright
93+owner or by an individual or Legal Entity authorized to submit on
94+behalf of the copyright owner. For the purposes of this definition,
95+"submitted" means any form of electronic, verbal, or written
96+communication sent to the Licensor or its representatives, including
97+but not limited to communication on electronic mailing lists, source
98+code control systems, and issue tracking systems that are managed by,
99+or on behalf of, the Licensor for the purpose of discussing and
100+improving the Work, but excluding communication that is conspicuously
101+marked or otherwise designated in writing by the copyright owner as
102+"Not a Contribution."
103+
104+"Contributor" shall mean Licensor and any individual or Legal Entity
105+on behalf of whom a Contribution has been received by Licensor and
106+subsequently incorporated within the Work.
107+
108+2. Grant of Copyright License
109+Subject to the terms and conditions of this License, each Contributor
110+hereby grants to You a perpetual, worldwide, non-exclusive,
111+no-charge, royalty-free, irrevocable copyright license to reproduce,
112+prepare Derivative Works of, publicly display, publicly perform,
113+sublicense, and distribute the Work and such Derivative Works in
114+Source or Object form.
115+
116+3. Grant of Patent License
117+Subject to the terms and conditions of this License, each Contributor
118+hereby grants to You a perpetual, worldwide, non-exclusive,
119+no-charge, royalty-free, irrevocable (except as stated in this
120+section) patent license to make, have made, use, offer to sell, sell,
121+import, and otherwise transfer the Work, where such license applies
122+only to those patent claims licensable by such Contributor that are
123+necessarily infringed by their Contribution(s) alone or by
124+combination of their Contribution(s) with the Work to which such
125+Contribution(s) was submitted. If You institute patent litigation
126+against any entity (including a cross-claim or counterclaim in a
127+lawsuit) alleging that the Work or a Contribution incorporated within
128+the Work constitutes direct or contributory patent infringement, then
129+any patent licenses granted to You under this License for that Work
130+shall terminate as of the date such litigation is filed.
131+
132+4. Redistribution
133+You may reproduce and distribute copies of the Work or Derivative
134+Works thereof in any medium, with or without modifications, and in
135+Source or Object form, provided that You meet the following
136+conditions:
137+
138+(a) You must give any other recipients of the Work or Derivative
139+Works a copy of this License; and
140+(b) You must cause any modified files to carry prominent notices
141+stating that You changed the files; and
142+(c) You must retain, in the Source form of any Derivative Works that
143+You distribute, all copyright, patent, trademark, and attribution
144+notices from the Source form of the Work, excluding those notices
145+that do not pertain to any part of the Derivative Works; and
146+(d) If the Work includes a "NOTICE" text file as part of its
147+distribution, then any Derivative Works that You distribute must
148+include a readable copy of the attribution notices contained within
149+such NOTICE file, excluding those notices that do not pertain to any
150+part of the Derivative Works, in at least one of the following
151+places: within a NOTICE text file distributed as part of the
152+Derivative Works; within the Source form or documentation, if
153+provided along with the Derivative Works; or, within a display
154+generated by the Derivative Works, if and wherever such third-party
155+notices normally appear. The contents of the NOTICE file are for
156+informational purposes only and do not modify the License. You may
157+add Your own attribution notices within Derivative Works that You
158+distribute, alongside or as an addendum to the NOTICE text from the
159+Work, provided that such additional attribution notices cannot be
160+construed as modifying the License.
161+You may add Your own copyright statement to Your modifications and
162+may provide additional or different license terms and conditions for
163+use, reproduction, or distribution of Your modifications, or for any
164+such Derivative Works as a whole, provided Your use, reproduction,
165+and distribution of the Work otherwise complies with the conditions
166+stated in this License.
167+
168+5. Submission of Contributions
169+Unless You explicitly state otherwise, any Contribution intentionally
170+submitted for inclusion in the Work by You to the Licensor shall be
171+under the terms and conditions of this License, without any
172+additional terms or conditions. Notwithstanding the above, nothing
173+herein shall supersede or modify the terms of any separate license
174+agreement you may have executed with Licensor regarding such
175+Contributions.
176+
177+6. Trademarks
178+This License does not grant permission to use the trade names,
179+trademarks, service marks, or product names of the Licensor, except
180+as required for reasonable and customary use in describing the origin
181+of the Work and reproducing the content of the NOTICE file.
182+
183+7. Disclaimer of Warranty
184+Unless required by applicable law or agreed to in writing, Licensor
185+provides the Work (and each Contributor provides its Contributions)
186+on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
187+either express or implied, including, without limitation, any
188+warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY,
189+or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for
190+determining the appropriateness of using or redistributing the Work
191+and assume any risks associated with Your exercise of permissions
192+under this License.
193+
194+8. Limitation of Liability
195+In no event and under no legal theory, whether in tort (including
196+negligence), contract, or otherwise, unless required by applicable
197+law (such as deliberate and grossly negligent acts) or agreed to in
198+writing, shall any Contributor be liable to You for damages,
199+including any direct, indirect, special, incidental, or consequential
200+damages of any character arising as a result of this License or out
201+of the use or inability to use the Work (including but not limited to
202+damages for loss of goodwill, work stoppage, computer failure or
203+malfunction, or any and all other commercial damages or losses), even
204+if such Contributor has been advised of the possibility of such
205+damages.
206+
207+9. Accepting Warranty or Additional Liability
208+While redistributing the Work or Derivative Works thereof, You may
209+choose to offer, and charge a fee for, acceptance of support,
210+warranty, indemnity, or other liability obligations and/or rights
211+consistent with this License. However, in accepting such obligations,
212+You may act only on Your own behalf and on Your sole responsibility,
213+not on behalf of any other Contributor, and only if You agree to
214+indemnify, defend, and hold each Contributor harmless for any
215+liability incurred by, or claims asserted against, such Contributor
216+by reason of your accepting any such warranty or additional
217+liability.
PULL_REQUEST_TEMPLATE.mdnew file+5
@@ -0,0 +1,5 @@
1+Thanks for the pull request!
2+
3+By raising this pull request you confirm you are licensing your contribution under all licenses that apply to this project (see LICENSE) and that you have no patents covering your contribution.
4+
5+If you care, my PR preferences are at https://github.com/ndmitchell/neil#contributions, but they're all guidelines, and I'm not too fussy - you don't have to read them.
README.md+9−3
@@ -1,6 +1,6 @@
1−# Hexml [![Hackage version](https://img.shields.io/hackage/v/hexml.svg?label=Hackage)](https://hackage.haskell.org/package/hexml) [![Stackage version](https://www.stackage.org/package/hexml/badge/lts?label=Stackage)](https://www.stackage.org/package/hexml) [![Linux Build Status](https://img.shields.io/travis/ndmitchell/hexml.svg?label=Linux%20build)](https://travis-ci.org/ndmitchell/hexml) [![Windows Build Status](https://img.shields.io/appveyor/ci/ndmitchell/hexml.svg?label=Windows%20build)](https://ci.appveyor.com/project/ndmitchell/hexml)
1+# Hexml [![Hackage version](https://img.shields.io/hackage/v/hexml.svg?label=Hackage)](https://hackage.haskell.org/package/hexml) [![Stackage version](https://www.stackage.org/package/hexml/badge/nightly?label=Stackage)](https://www.stackage.org/package/hexml) [![Build status](https://img.shields.io/github/actions/workflow/status/ndmitchell/hexml/ci.yml?branch=master)](https://github.com/ndmitchell/hexml/actions)
2
2
3−A bad XML DOM-style parser, that only parses a subset of XML, but is designed to be fast. In particular:
3+An XML DOM-style parser, that only parses a subset of XML, but is designed to be fast. In particular:
4
4
5* Entities, e.g. `&amp;`, are not expanded.
5* Entities, e.g. `&amp;`, are not expanded.
6* Not all the validity conditions are checked.
6* Not all the validity conditions are checked.
@@ -8,4 +8,10 @@ A bad XML DOM-style parser, that only parses a subset of XML, but is designed to
8
8
9The name "hexml" is a combination of "Hex" (a curse) and "XML". The "X" should not be capitalised because the parser is more curse and less XML.
9The name "hexml" is a combination of "Hex" (a curse) and "XML". The "X" should not be capitalised because the parser is more curse and less XML.
10
10
11−Hexml may be suitable if you want to quickly parse XML, from known sources, and a full XML parser has been shown to be a bottleneck. As an alternative to hexml, which supports things like entities but is still pretty fast, see Pugixml.
11+Hexml may be suitable if you want to quickly parse XML, from known sources, and a full XML parser has been shown to be a bottleneck. As an alternative to hexml, which supports things like entities but is still pretty fast, see [Pugixml](http://pugixml.org/) (with a [Haskell binding](https://hackage.haskell.org/package/pugixml) - but be aware the Haskell binding [can segfault](https://github.com/philopon/pugixml-hs/issues/5)).
12+
13+Hexml is tested with [AFL](http://lcamtuf.coredump.cx/afl/).
14+
15+If you want lenses for Hexml, see [hexml-lens](http://hackage.haskell.org/package/hexml-lens).
16+
17+The optimisation work around Hexml spawned [Xeno](http://hackage.haskell.org/package/xeno), a Haskell-only alternative to Hexml. There is a [talk](https://ndmitchell.com/#hexml_12_oct_2017) covering the performance tricks of Hexml and Xeno.
afl.shnew file+40
@@ -0,0 +1,40 @@
1+# Script based on code from https://github.com/ndmitchell/hexml/issues/6
2+# More examples of things to do are at that ticket
3+set -e
4+
5+echo DISABLED DUE TO SEGFAULT IN CLANG WHEN COMPILING AFL
6+echo See https://github.com/ndmitchell/hexml/issues/14
7+exit 0
8+
9+# First install AFL
10+if ! command -v afl-clang-fast > /dev/null ; then
11+ wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz
12+ tar xf afl-latest.tgz
13+ cd afl-*
14+ make
15+ cd llvm_mode && make && cd ..
16+ sudo make install
17+ cd libdislocator && make && sudo make install && cd ..
18+ cd ..
19+fi
20+
21+# Compile it
22+AFL_USE_ASAN=1 afl-clang-fast -O2 -Icbits cbits/fuzz.c -o $PWD/hexml-fuzz
23+
24+# Fuzz it
25+if ! grep -q core /proc/sys/kernel/core_pattern ; then
26+ echo core | sudo tee /proc/sys/kernel/core_pattern
27+fi
28+if [ -z "$CI" ] ; then
29+ afl-fuzz -T hexml -m none -x /usr/local/share/afl/dictionaries/xml.dict -i $PWD/xml -o $PWD/afl-results -- $PWD/hexml-fuzz @@
30+else
31+ AFL_EXIT_WHEN_DONE=1 timeout 5m afl-fuzz -T hexml -m none -x /usr/local/share/afl/dictionaries/xml.dict -i $PWD/xml -o $PWD/afl-results -- $PWD/hexml-fuzz @@ > /dev/null || true
32+ cat afl-results/fuzzer_stats
33+ grep "unique_crashes *: 0" afl-results/fuzzer_stats # fail if there are failures
34+fi
35+
36+# Minimize failures
37+# $ AFL_PRELOAD=/usr/local/lib/afl/libdislocator.so afl-cmin -i results/crashes/ -o results.shrunk -- $PWD/a.out @@
38+# $ mkdir results.min
39+# $ for x in `ls results.shrunk`; do AFL_PRELOAD=/usr/local/lib/afl/libdislocator.so afl-tmin -i results.shrunk/$x -o results.min/$x -- $PWD/a.out @@; done
40+# $ AFL_PRELOAD=/usr/local/lib/afl/libdislocator.so afl-analyze -i results.shrunk/id:000013,sig:11,src:000002,op:havoc,rep:4 -- ./a.out @@
appveyor.ymldeleted−18
@@ -1,18 +0,0 @@
1−cache:
2−- "c:\\sr" # stack root, short paths == less problems
3−
4−build: off
5−
6−before_test:
7−- curl -ostack.zip -L --insecure http://www.stackage.org/stack/windows-i386
8−- 7z x stack.zip stack.exe
9−
10−clone_folder: "c:\\project"
11−environment:
12− global:
13− STACK_ROOT: "c:\\sr"
14−
15−test_script:
16−- stack init
17−- stack setup > nul
18−- echo "" | stack --no-terminal test
cbits/fuzz.cnew file+26
@@ -0,0 +1,26 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+#include <string.h>
4+#include "hexml.c"
5+
6+int main(int ac, char** av)
7+{
8+#ifdef __AFL_HAVE_MANUAL_CONTROL
9+ __AFL_INIT();
10+#endif
11+
12+ FILE *f = fopen(av[1], "rb");
13+ fseek(f, 0, SEEK_END);
14+ size_t fsize = ftell(f);
15+ rewind(f);
16+
17+ char* string = malloc(fsize+1);
18+ memset(string, 0, fsize+1);
19+ (void) fread(string, fsize, 1, f);
20+ fclose(f);
21+
22+ document *doc = hexml_document_parse(string, fsize);
23+ hexml_document_free(doc);
24+ free(string);
25+ return 0;
26+}
cbits/hexml.c+127−107
@@ -1,5 +1,5 @@
1#include "hexml.h"
1#include "hexml.h"
2−#include <malloc.h>
2+#include <stdlib.h>
3#include <string.h>
3#include <string.h>
4#include <assert.h>
4#include <assert.h>
5#include <ctype.h>
5#include <ctype.h>
@@ -10,18 +10,18 @@ typedef int bool;
10/////////////////////////////////////////////////////////////////////
10/////////////////////////////////////////////////////////////////////
11// TYPES
11// TYPES
12
12
13−int static inline end(str s) { return s.start + s.length; }
13+static inline int end(str s) { return s.start + s.length; }
14
14
15−str static inline start_length(int32_t start, int32_t length)
15+static inline str start_length(int32_t start, int32_t length)
16{
16{
17− if (start < 0 || length < 0) assert(0);
17+ assert(start >= 0 && length >= 0);
18 str res;
18 str res;
19 res.start = start;
19 res.start = start;
20 res.length = length;
20 res.length = length;
21 return res;
21 return res;
22}
22}
23
23
24−str static inline start_end(int32_t start, int32_t end)
24+static inline str start_end(int32_t start, int32_t end)
25{
25{
26 return start_length(start, end - start);
26 return start_length(start, end - start);
27}
27}
@@ -40,7 +40,7 @@ typedef struct
40 // have a cursor at the end, which is stack scoped, so children write out, then I do
40 // have a cursor at the end, which is stack scoped, so children write out, then I do
41 // when you commit, you copy over from end to front
41 // when you commit, you copy over from end to front
42
42
43− // nodes
43+ // nodes
44 int size;
44 int size;
45 int used_front; // front entries, stored for good
45 int used_front; // front entries, stored for good
46 int used_back; // back entries, stack based, copied into front
46 int used_back; // back entries, stack based, copied into front
@@ -51,11 +51,11 @@ typedef struct
51
51
52struct document
52struct document
53{
53{
54− char* body; // pointer to initial argument, not owned by us
54+ const char* body; // pointer to initial argument, not owned by us
55
55
56 // things only used while parsing
56 // things only used while parsing
57− char* cursor; // pointer to where we are in body
57+ const char* cursor; // pointer to where we are in body
58− char* end; // pointer to one past the last char
58+ const char* end; // pointer to one past the last char
59 // if cursor is > end we have gone past the end
59 // if cursor is > end we have gone past the end
60
60
61 char* error_message;
61 char* error_message;
@@ -63,8 +63,8 @@ struct document
63 attr_buffer attrs;
63 attr_buffer attrs;
64};
64};
65
65
66−int static inline doc_length(document* d) { return d->end - d->body; }
66+static inline int doc_length(const document* d) { return (int) (d->end - d->body); }
67−int static inline doc_position(document* d) { return d->cursor - d->body; }
67+static inline int doc_position(const document* d) { return (int) (d->cursor - d->body); }
68
68
69
69
70/////////////////////////////////////////////////////////////////////
70/////////////////////////////////////////////////////////////////////
@@ -72,49 +72,45 @@ int static inline doc_position(document* d) { return d->cursor - d->body; }
72
72
73typedef struct
73typedef struct
74{
74{
75− document* d;
75+ const document* d;
76 char* buffer;
76 char* buffer;
77 int length;
77 int length;
78 int cursor;
78 int cursor;
79} render;
79} render;
80
80
81−void static inline render_char(render* r, char c)
81+static inline void render_char(render* r, char c)
82{
82{
83 if (r->cursor < r->length)
83 if (r->cursor < r->length)
84 r->buffer[r->cursor] = c;
84 r->buffer[r->cursor] = c;
85 r->cursor++;
85 r->cursor++;
86}
86}
87
87
88−void static inline bound(char* msg, int index, int mn, int mx)
88+static inline void bound(int idx, int mn, int mx)
89{
89{
90− if (index < mn || index > mx)
91− {
92− //printf("Bounds checking failed %s, got %i, which should be in %i:%i\n", msg, index, mn, mx);
93− assert(0);
90+ assert(idx >= mn && idx <= mx);
94− }
95}
91}
96
92
97−void static inline bound_str(char* msg, str s, int mn, int mx)
93+static inline void bound_str(str s, int mn, int mx)
98{
94{
99− if (s.length < 0) assert(0);
95+ assert(s.length >= 0);
100− bound(msg, s.start, mn, mx);
96+ bound(s.start, mn, mx);
101− bound(msg, end(s), mn, mx);
97+ bound(end(s), mn, mx);
102}
98}
103
99
104−void render_str(render* r, str s)
100+static void render_str(render* r, str s)
105{
101{
106− bound_str("render_str", s, 0, doc_length(r->d));
102+ bound_str(s, 0, doc_length(r->d));
107 for (int i = 0; i < s.length; i++)
103 for (int i = 0; i < s.length; i++)
108 render_char(r, r->d->body[s.start + i]);
104 render_char(r, r->d->body[s.start + i]);
109}
105}
110
106
111−void render_tag(render* r, node* n);
107+static void render_tag(render* r, const node* n);
112
108
113−void render_content(render* r, node* n)
109+static void render_content(render* r, const node* n)
114{
110{
115− bound_str("render_conent inner", n->inner, 0, doc_length(r->d));
111+ bound_str(n->inner, 0, doc_length(r->d));
116− bound_str("render_conent nodes", n->nodes, 0, r->d->nodes.used_front);
112+ bound_str(n->nodes, 0, r->d->nodes.used_front);
117− bound_str("render_conent attrs", n->attrs, 0, r->d->attrs.used);
113+ bound_str(n->attrs, 0, r->d->attrs.used);
118
114
119 int done = n->inner.start;
115 int done = n->inner.start;
120 for (int i = 0; i < n->nodes.length; i++)
116 for (int i = 0; i < n->nodes.length; i++)
@@ -127,7 +123,7 @@ void render_content(render* r, node* n)
127 render_str(r, start_end(done, end(n->inner)));
123 render_str(r, start_end(done, end(n->inner)));
128}
124}
129
125
130−void render_tag(render* r, node* n)
126+static void render_tag(render* r, const node* n)
131{
127{
132 render_char(r, '<');
128 render_char(r, '<');
133 render_str(r, n->name);
129 render_str(r, n->name);
@@ -149,7 +145,7 @@ void render_tag(render* r, node* n)
149 render_char(r, '>');
145 render_char(r, '>');
150}
146}
151
147
152−int node_render(document* d, node* n, char* buffer, int length)
148+int hexml_node_render(const document* d, const node* n, char* buffer, int length)
153{
149{
154 render r;
150 render r;
155 r.d = d;
151 r.d = d;
@@ -168,10 +164,10 @@ int node_render(document* d, node* n, char* buffer, int length)
168/////////////////////////////////////////////////////////////////////
164/////////////////////////////////////////////////////////////////////
169// NOT THE PARSER
165// NOT THE PARSER
170
166
171−char* document_error(document* d){return d->error_message;}
167+char* hexml_document_error(const document* d){return d->error_message;}
172−node* document_node(document* d){return &d->nodes.nodes[0];}
168+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
173
169
174−void document_free(document* d)
170+void hexml_document_free(document* d)
175{
171{
176 free(d->error_message);
172 free(d->error_message);
177 free(d->nodes.alloc);
173 free(d->nodes.alloc);
@@ -179,25 +175,24 @@ void document_free(document* d)
179 free(d);
175 free(d);
180}
176}
181
177
182−node* node_children(document* d, node* n, int* res)
178+node* hexml_node_children(const document* d, const node* n, int* res)
183{
179{
184 *res = n->nodes.length;
180 *res = n->nodes.length;
185 return &d->nodes.nodes[n->nodes.start];
181 return &d->nodes.nodes[n->nodes.start];
186}
182}
187
183
188−attr* node_attributes(document* d, node* n, int* res)
184+attr* hexml_node_attributes(const document* d, const node* n, int* res)
189{
185{
190 *res = n->attrs.length;
186 *res = n->attrs.length;
191 return &d->attrs.attrs[n->attrs.start];
187 return &d->attrs.attrs[n->attrs.start];
192}
188}
193
189
194
190
195−attr* node_attributeBy(document* d, node* n, char* s, int slen)
191+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
196{
192{
197− if (slen == -1) slen = strlen(s);
193+ if (slen == -1) slen = (int) strlen(s);
198− int i = n->attrs.start;
199− int limit = end(n->attrs);
194+ const int limit = end(n->attrs);
200− for (i = n->attrs.start; i < limit; i++)
195+ for (int i = n->attrs.start; i < limit; i++)
201 {
196 {
202 attr* r = &d->attrs.attrs[i];
197 attr* r = &d->attrs.attrs[i];
203 if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
198 if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
@@ -207,11 +202,11 @@ attr* node_attributeBy(document* d, node* n, char* s, int slen)
207}
202}
208
203
209// Search for given strings within a node
204// Search for given strings within a node
210−node* node_childBy(document* d, node* parent, node* prev, char* s, int slen)
205+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
211{
206{
212− if (slen == -1) slen = strlen(s);
207+ if (slen == -1) slen = (int) strlen(s);
213− int i = prev == NULL ? parent->nodes.start : prev + 1 - d->nodes.nodes;
208+ int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
214− int limit = end(parent->nodes);
209+ const int limit = end(parent->nodes);
215 for (; i < limit; i++)
210 for (; i < limit; i++)
216 {
211 {
217 node* r = &d->nodes.nodes[i];
212 node* r = &d->nodes.nodes[i];
@@ -225,49 +220,49 @@ node* node_childBy(document* d, node* parent, node* prev, char* s, int slen)
225/////////////////////////////////////////////////////////////////////
220/////////////////////////////////////////////////////////////////////
226// PARSE TABLE
221// PARSE TABLE
227
222
228−const char tag_name1 = 0x1;
223+static const char tag_name1 = 0x1;
229−const char tag_name = 0x2;
224+static const char tag_name = 0x2;
230−const char tag_space = 0x4;
225+static const char tag_space = 0x4;
231
226
232−char parse_table[256];
227+static char parse_table[256];
233
228
234−void init_parse_table()
229+static void init_parse_table()
235{
230{
236 static bool done = 0;
231 static bool done = 0;
237 if (done) return;
232 if (done) return;
238 for (int i = 0; i < 256; i++)
233 for (int i = 0; i < 256; i++)
239 {
234 {
240 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
235 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
241− bool name = name1 || i == '-' || (i >= '0' && i <= '9');
236+ bool name = name1 || i == '-' || i == '.' || (i >= '0' && i <= '9');
242 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
237 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
243 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
238 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
244 }
239 }
245 done = 1;
240 done = 1;
246}
241}
247
242
248−bool static inline is(char c, char tag) { return parse_table[c] & tag; }
243+static inline bool is(char c, char tag) { return parse_table[(unsigned char) c] & tag; }
249−bool static inline is_name1(char c) { return is(c, tag_name1); }
244+static inline bool is_name1(char c) { return is(c, tag_name1); }
250−bool static inline is_name(char c) { return is(c, tag_name); }
245+static inline bool is_name(char c) { return is(c, tag_name); }
251−bool static inline is_space(char c) { return is(c, tag_space); }
246+static inline bool is_space(char c) { return is(c, tag_space); }
252
247
253
248
254/////////////////////////////////////////////////////////////////////
249/////////////////////////////////////////////////////////////////////
255// PARSER COMBINATORS
250// PARSER COMBINATORS
256
251
257−char static inline peekAt(document* d, int i) { return d->cursor[i]; }
252+static inline char peek_at(const document* d, int i) { return d->cursor[i]; }
253+static inline char peek(const document* d) { return peek_at(d, 0); }
258−void static inline skip(document* d, int i) { d->cursor += i; }
254+static inline void skip(document* d, int i) { d->cursor += i; }
259−char static inline peek(document* d) { return peekAt(d, 0); }
260−char static inline get(document* d) { char c = peek(d); skip(d, 1); return c; }
255+static inline char get(document* d) { char c = peek(d); skip(d, 1); return c; }
261
256
262// Remove whitespace characters from the cursor while they are still whitespace
257// Remove whitespace characters from the cursor while they are still whitespace
263−void static inline trim(document* d)
258+static inline void trim(document* d)
264{
259{
265 while (is_space(peek(d)))
260 while (is_space(peek(d)))
266 skip(d, 1);
261 skip(d, 1);
267}
262}
268
263
269// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
264// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
270−bool find(document* d, char c)
265+static bool find(document* d, char c)
271{
266{
272 char* x = memchr(d->cursor, c, d->end - d->cursor);
267 char* x = memchr(d->cursor, c, d->end - d->cursor);
273 if (x == NULL)
268 if (x == NULL)
@@ -286,35 +281,46 @@ bool find(document* d, char c)
286/////////////////////////////////////////////////////////////////////
281/////////////////////////////////////////////////////////////////////
287// PARSING CODE
282// PARSING CODE
288
283
289−void static inline node_alloc(node_buffer* b, int ask)
284+static inline void node_alloc(node_buffer* b, int ask)
290{
285{
291 int space = b->size - b->used_back - b->used_front;
286 int space = b->size - b->used_back - b->used_front;
292 if (space >= ask) return;
287 if (space >= ask) return;
288+ int size2 = (b->size + 1000 + ask) * 2;
289+ node* buf2 = malloc(size2 * sizeof(node));
293− assert(0);
290+ assert(buf2);
291+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
292+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
293+ free(b->alloc);
294+ b->size = size2;
295+ b->nodes = buf2;
296+ b->alloc = buf2;
294}
297}
295
298
296−void static inline attr_alloc(attr_buffer* b, int ask)
299+static inline void attr_alloc(attr_buffer* b, int ask)
297{
300{
298 int space = b->size - b->used;
301 int space = b->size - b->used;
299 if (space >= ask) return;
302 if (space >= ask) return;
300 int size2 = (b->size + 1000 + ask) * 2;
303 int size2 = (b->size + 1000 + ask) * 2;
301 attr* buf2 = malloc(size2 * sizeof(attr));
304 attr* buf2 = malloc(size2 * sizeof(attr));
305+ assert(buf2);
302 memcpy(buf2, b->attrs, b->used * sizeof(attr));
306 memcpy(buf2, b->attrs, b->used * sizeof(attr));
303 free(b->alloc);
307 free(b->alloc);
308+ b->size = size2;
304 b->attrs = buf2;
309 b->attrs = buf2;
305 b->alloc = buf2;
310 b->alloc = buf2;
306}
311}
307
312
308−void set_error(document* d, char* msg)
313+static void set_error(document* d, const char* msg)
309{
314{
310 if (d->error_message != NULL) return; // keep the first error message
315 if (d->error_message != NULL) return; // keep the first error message
311 d->error_message = malloc(strlen(msg)+1);
316 d->error_message = malloc(strlen(msg)+1);
317+ assert(d->error_message);
312 strcpy(d->error_message, msg);
318 strcpy(d->error_message, msg);
313}
319}
314
320
315// you now expect a name, perhaps preceeded by whitespace
321// you now expect a name, perhaps preceeded by whitespace
316// the name may be empty
322// the name may be empty
317−str static inline parse_name(document* d)
323+static inline str parse_name(document* d)
318{
324{
319 int start = doc_position(d);
325 int start = doc_position(d);
320 if (!is_name1(peek(d)))
326 if (!is_name1(peek(d)))
@@ -325,14 +331,15 @@ str static inline parse_name(document* d)
325 return start_end(start, doc_position(d));
331 return start_end(start, doc_position(d));
326}
332}
327
333
328−str static inline parse_attrval(document* d)
334+static inline str parse_attrval(document* d)
329{
335{
330 trim(d);
336 trim(d);
331− if (get(d) != '=')
337+ if (peek(d) != '=')
332 {
338 {
333 set_error(d, "Expected = in attribute, but missing");
339 set_error(d, "Expected = in attribute, but missing");
334 return start_length(0, 0);
340 return start_length(0, 0);
335 }
341 }
342+ skip(d, 1);
336 trim(d);
343 trim(d);
337 char c = peek(d);
344 char c = peek(d);
338 if (c == '\"' || c == '\'')
345 if (c == '\"' || c == '\'')
@@ -358,11 +365,10 @@ str static inline parse_attrval(document* d)
358
365
359// seen a tag name, now looking for attributes terminated by >
366// seen a tag name, now looking for attributes terminated by >
360// puts the attributes it finds in the attribute buffer
367// puts the attributes it finds in the attribute buffer
361−str static inline parse_attributes(document* d)
368+static inline str parse_attributes(document* d)
362{
369{
363− str res;
364− res.start = d->attrs.used;
370+ int start = d->attrs.used;
365− for (int i = 0; ; i++)
371+ while (d->error_message == NULL)
366 {
372 {
367 trim(d);
373 trim(d);
368 str name = parse_name(d);
374 str name = parse_name(d);
@@ -372,34 +378,39 @@ str static inline parse_attributes(document* d)
372 d->attrs.attrs[d->attrs.used].value = parse_attrval(d);
378 d->attrs.attrs[d->attrs.used].value = parse_attrval(d);
373 d->attrs.used++;
379 d->attrs.used++;
374 }
380 }
375− res.length = d->attrs.used - res.start;
381+ return start_end(start, d->attrs.used);
376− return res;
377}
382}
378
383
379−str parse_content(document* d);
384+static str parse_content(document* d);
380
385
381
386
382// Add a new entry into tag, am at a '<'
387// Add a new entry into tag, am at a '<'
383−void static inline parse_tag(document* d)
388+static inline void parse_tag(document* d)
384{
389{
385 node_alloc(&d->nodes, 1);
390 node_alloc(&d->nodes, 1);
386 d->nodes.used_back++;
391 d->nodes.used_back++;
387− int me = d->nodes.size - d->nodes.used_back;
392+ node* me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back];
388
393
389− d->nodes.nodes[me].outer.start = doc_position(d);
394+ me->outer.start = doc_position(d);
390 char c = get(d);
395 char c = get(d);
391 assert(c == '<');
396 assert(c == '<');
392 if (peek(d) == '?') skip(d, 1);
397 if (peek(d) == '?') skip(d, 1);
393− d->nodes.nodes[me].name = parse_name(d);
398+ me->name = parse_name(d);
399+ if (me->name.length == 0)
400+ {
401+ set_error(d, "Missing tag name");
402+ return;
403+ }
394− d->nodes.nodes[me].attrs = parse_attributes(d);
404+ me->attrs = parse_attributes(d);
405+ if (d->error_message != NULL) return;
395
406
396− c = get(d);
407+ c = peek(d);
397− if ((c == '/' || c == '?') && peek(d) == '>')
408+ if ((c == '/' || c == '?') && peek_at(d, 1) == '>')
398 {
409 {
399− skip(d, 1);
410+ skip(d, 2);
400− d->nodes.nodes[me].nodes = start_length(0, 0);
411+ me->nodes = start_length(0, 0);
401− d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, doc_position(d)).length;
412+ me->outer.length = start_end(me->outer.start, doc_position(d)).length;
402− d->nodes.nodes[me].inner = start_length(doc_position(d), 0);
413+ me->inner = start_length(doc_position(d), 0);
403 return;
414 return;
404 }
415 }
405 else if (c != '>')
416 else if (c != '>')
@@ -407,22 +418,28 @@ void static inline parse_tag(document* d)
407 set_error(d, "Gunk at the end of the tag");
418 set_error(d, "Gunk at the end of the tag");
408 return;
419 return;
409 }
420 }
421+ else
422+ skip(d, 1);
410− d->nodes.nodes[me].inner.start = doc_position(d);
423+ me->inner.start = doc_position(d);
411− d->nodes.nodes[me].nodes = parse_content(d);
424+ str content = parse_content(d);
425+
426+ // parse_content may have allocated more nodes, so recompute me
427+ me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back];
428+ me->nodes = content;
412− d->nodes.nodes[me].inner.length = start_end(d->nodes.nodes[me].inner.start, doc_position(d)).length;
429+ me->inner.length = start_end(me->inner.start, doc_position(d)).length;
413
430
414 if (d->error_message != NULL) return;
431 if (d->error_message != NULL) return;
415− if (peek(d) == '<' && peekAt(d, 1) == '/')
432+ if (peek(d) == '<' && peek_at(d, 1) == '/')
416 {
433 {
417 skip(d, 2);
434 skip(d, 2);
418− if (d->end - d->cursor >= d->nodes.nodes[me].name.length &&
435+ if (d->end - d->cursor >= me->name.length &&
419− memcmp(d->cursor, &d->body[d->nodes.nodes[me].name.start], d->nodes.nodes[me].name.length) == 0)
436+ memcmp(d->cursor, &d->body[me->name.start], me->name.length) == 0)
420 {
437 {
421− skip(d, d->nodes.nodes[me].name.length);
438+ skip(d, me->name.length);
422 trim(d);
439 trim(d);
423 if (get(d) == '>')
440 if (get(d) == '>')
424 {
441 {
425− d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, doc_position(d)).length;
442+ me->outer.length = start_end(me->outer.start, doc_position(d)).length;
426 return;
443 return;
427 }
444 }
428 }
445 }
@@ -434,7 +451,7 @@ void static inline parse_tag(document* d)
434
451
435// Parser until </, return the index of your node children
452// Parser until </, return the index of your node children
436// Not inline as it is recursive
453// Not inline as it is recursive
437−str parse_content(document* d)
454+static str parse_content(document* d)
438{
455{
439 int before = d->nodes.used_back;
456 int before = d->nodes.used_back;
440 while (d->error_message == NULL)
457 while (d->error_message == NULL)
@@ -444,22 +461,22 @@ str parse_content(document* d)
444 {
461 {
445 break;
462 break;
446 }
463 }
447− else if (peekAt(d, 1) == '/')
464+ else if (peek_at(d, 1) == '/')
448 {
465 {
449 // have found a </
466 // have found a </
450 break;
467 break;
451 }
468 }
452− else if (peekAt(d, 1) == '!' && peekAt(d, 2) == '-' && peekAt(d, 3) == '-')
469+ else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
453 {
470 {
454 skip(d, 3);
471 skip(d, 3);
455 // you can't reuse the two '-' characters for the closing as well
472 // you can't reuse the two '-' characters for the closing as well
456− if (peekAt(d, 0) == '\0' || peekAt(d, 1) == '\0')
473+ if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
457 {
474 {
458 set_error(d, "Didn't get a closing comment");
475 set_error(d, "Didn't get a closing comment");
459 return start_end(0, 0);
476 return start_end(0, 0);
460 }
477 }
461 skip(d, 2);
478 skip(d, 2);
462− while (1)
479+ for (;;)
463 {
480 {
464 if (!find(d, '>'))
481 if (!find(d, '>'))
465 {
482 {
@@ -467,7 +484,7 @@ str parse_content(document* d)
467 return start_end(0, 0);
484 return start_end(0, 0);
468 }
485 }
469 skip(d, 1);
486 skip(d, 1);
470− if (peekAt(d, -3) == '-' && peekAt(d, -2))
487+ if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
471 break;
488 break;
472 }
489 }
473 }
490 }
@@ -495,13 +512,14 @@ typedef struct
495 node nodes[500];
512 node nodes[500];
496} buffer;
513} buffer;
497
514
498−document* document_parse(char* s, int slen)
515+document* hexml_document_parse(const char* s, int slen)
499{
516{
500− if (slen == -1) slen = strlen(s);
517+ if (slen == -1) slen = (int) strlen(s);
501 assert(s[slen] == 0);
518 assert(s[slen] == 0);
502 init_parse_table();
519 init_parse_table();
503
520
504 buffer* buf = malloc(sizeof(buffer));
521 buffer* buf = malloc(sizeof(buffer));
522+ assert(buf);
505 document* d = &buf->document;
523 document* d = &buf->document;
506 d->body = s;
524 d->body = s;
507 d->cursor = s;
525 d->cursor = s;
@@ -521,11 +539,13 @@ document* document_parse(char* s, int slen)
521 d->nodes.nodes[0].outer = start_length(0, slen);
539 d->nodes.nodes[0].outer = start_length(0, slen);
522 d->nodes.nodes[0].inner = start_length(0, slen);
540 d->nodes.nodes[0].inner = start_length(0, slen);
523 d->nodes.nodes[0].attrs = start_length(0, 0);
541 d->nodes.nodes[0].attrs = start_length(0, 0);
524− d->nodes.nodes[0].nodes = parse_content(d);
525
542
543+ // Introduce an intermediate result, otherwise behaviour is undefined
544+ // because there is no guaranteed ordering between LHS and RHS evaluation
545+ str content = parse_content(d);
546+ d->nodes.nodes[0].nodes = content;
547+
526− if (d->cursor < d->end && d->error_message == NULL)
548+ if (d->cursor < d->end)
527− {
528 set_error(d, "Trailing junk at the end of the document");
549 set_error(d, "Trailing junk at the end of the document");
529− }
530 return d;
550 return d;
531}
551}
cbits/hexml.h+18−14
@@ -21,6 +21,7 @@ typedef struct
21 str name; // tag name, e.g. <[foo]>
21 str name; // tag name, e.g. <[foo]>
22 str inner; // inner text, <foo>[bar]</foo>
22 str inner; // inner text, <foo>[bar]</foo>
23 str outer; // outer text, [<foo>bar</foo>]
23 str outer; // outer text, [<foo>bar</foo>]
24+ // Not usable, but required to get the correct sizing when returning arrays of nodes
24 str attrs; // all the attributes, in the attribute buffer (not usable)
25 str attrs; // all the attributes, in the attribute buffer (not usable)
25 str nodes; // all the nodes, in the node buffer (not usable)
26 str nodes; // all the nodes, in the node buffer (not usable)
26} node;
27} node;
@@ -28,26 +29,29 @@ typedef struct
28// Convention: if slen can be -1 for nul-terminated, or given explicitly
29// Convention: if slen can be -1 for nul-terminated, or given explicitly
29
30
30// Parse a document, returns a potentially invalid document - use document_error to check
31// Parse a document, returns a potentially invalid document - use document_error to check
31−// Must use document_free to release the memory.
32+// Must use document_free to release the memory (even if invalid).
33+// The string must be nul terminated, e.g. slen != -1 ==> slen[0]
32−document* document_parse(char* s, int slen);
34+document* hexml_document_parse(const char* s, int slen);
33
35
34// Free the memory returned from document_parse.
36// Free the memory returned from document_parse.
35−void document_free(document* d);
37+void hexml_document_free(document* d);
36
38
37−// generate a fresh string with the same semantics as the node
39+// Generate a fresh string with the same semantics as the node.
38−// requires an input buffer, returns the size of the rendered document
40+// Requires an input buffer, returns the size of the rendered document.
41+// Requires the string passed to document_parse to be valid.
39−int node_render(document* d, node* n, char* buffer, int length);
42+int hexml_node_render(const document* d, const node* n, char* buffer, int length);
40
43
41−// return either NULL (successful parse) or the error message
44+// Return either NULL (successful parse) or the error message.
42−char* document_error(document* d);
45+char* hexml_document_error(const document* d);
43
46
44−// return the root node of the document - imagine the document is wrapped in <>$DOC</> tags
47+// Return the root node of the document - imagine the document is wrapped in <>$DOC</> tags.
45−node* document_node(document* d);
48+node* hexml_document_node(const document* d);
46
49
47// List all items within a node
50// List all items within a node
48−node* node_children(document* d, node* n, int* res);
51+node* hexml_node_children(const document* d, const node* n, int* res);
49−attr* node_attributes(document* d, node* n, int* res);
52+attr* hexml_node_attributes(const document* d, const node* n, int* res);
50
53
51// Search for given strings within a node, note that prev may be NULL
54// Search for given strings within a node, note that prev may be NULL
55+// Requires the string passed to document_parse to be valid.
52−node* node_childBy(document* d, node* parent, node* prev, char* s, int slen);
56+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen);
53−attr* node_attributeBy(document* d, node* n, char* s, int slen);
57+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen);
cbits/main.cnew file+46
@@ -0,0 +1,46 @@
1+#include "hexml.h"
2+#include <stdio.h>
3+#include <malloc.h>
4+#include <time.h>
5+#include <string.h>
6+#include <assert.h>
7+
8+char* readFile(char* file)
9+{
10+ FILE* f = fopen(file, "rb");
11+ fseek(f, 0, SEEK_END);
12+ int len = ftell(f);
13+ fseek(f, 0, SEEK_SET);
14+ char* res = malloc(len + 1);
15+ assert(res);
16+ res[len] = 0;
17+ size_t n = fread(res, 1, len, f);
18+ assert(n == len);
19+ fclose(f);
20+ return res;
21+}
22+
23+char* example = "<?xml version=\"1.0\" lang=\"ja\"?><foo><bar baz=\"qux\">quux</bar><bar baz=\"bar\">hoge</bar><piyo>&gt;piyopiyo&lt;</piyo></foo><foo2 />";
24+
25+int main(int argc, char **argv)
26+{
27+ setbuf(stdout, NULL);
28+ char* body = argv[1] == NULL ? example : readFile(argv[1]);
29+ document* doc = hexml_document_parse(body, -1);
30+ char* err = hexml_document_error(doc);
31+ if (err == NULL)
32+ {
33+ int len = hexml_node_render(doc, hexml_document_node(doc), NULL, 0);
34+ char* s = malloc(len + 1);
35+ assert(s);
36+ hexml_node_render(doc, hexml_document_node(doc), s, len);
37+ s[len] = 0;
38+ printf("Parse successful\n"); // , %s\n", s);
39+ return 0;
40+ }
41+ else
42+ {
43+ printf("Parse failed, %s\n", err);
44+ return 1;
45+ }
46+}
gen/.ghcinew file+3
@@ -0,0 +1,3 @@
1+:set -fwarn-unused-binds -fwarn-unused-imports -fwarn-orphans
2+:load Main
3+:def test \xs -> return $ ":main" ++ xs
gen/.ghcidnew file+1
@@ -0,0 +1 @@
1+-c ghci --test=:test --reload=test.c
gen/Lib.hsnew file+201
@@ -0,0 +1,201 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+
3+module Lib(
4+ Parser,
5+ out, many, optional, match, lit, choice, abort, choices,
6+ run,
7+ compile
8+ ) where
9+
10+import Control.Monad.Extra
11+import Control.Monad.Trans.Writer
12+import Data.Maybe
13+import Data.List.Extra
14+import Data.Bits
15+import Data.Monoid
16+import Numeric
17+
18+
19+run :: Parser o () -> String -> ([(Int, o)], (Int, String), Maybe String)
20+run p = runC (toC $ toP p)
21+
22+compile :: Show o => Parser o () -> [String]
23+compile = prettyC . optimiseC . toC . toP
24+
25+---------------------------------------------------------------------
26+-- THE UNDERLYING PARSER TYPE
27+
28+data P o
29+ = Empty
30+ | Seq (P o) (P o)
31+ | Choice (P o) (P o)
32+ | Match CharSet
33+ | Many (P o)
34+ | Out o
35+ | Panic String
36+ deriving Show
37+
38+instance Monoid (P o) where
39+ mempty = Empty
40+ mappend = Seq
41+
42+newtype Parser o a = Parser (Writer (P o) a)
43+ deriving (Functor, Applicative, Monad)
44+
45+toP :: Parser o a -> P o
46+toP (Parser p) = execWriter p
47+
48+out :: o -> Parser o ()
49+out o = Parser $ tell $ Out o
50+
51+abort :: String -> Parser o ()
52+abort x = Parser $ tell $ Panic x
53+
54+many :: Parser o () -> Parser o ()
55+many x = Parser $ tell $ Many $ toP x
56+
57+match :: (Char -> Bool) -> Parser o ()
58+match f = Parser $ tell $ Match $ CharSet f
59+
60+lit :: String -> Parser o ()
61+lit xs = forM_ xs $ \x -> match (== x)
62+
63+choice :: Parser o () -> Parser o () -> Parser o ()
64+choice a b = Parser $ tell $ Choice (toP a) (toP b)
65+
66+choices :: [Parser o ()] -> Parser o ()
67+choices = foldr1 choice
68+
69+optional :: Parser o () -> Parser o ()
70+optional x = choice x $ pure ()
71+
72+
73+---------------------------------------------------------------------
74+-- CHARSET
75+
76+chars = ['\1'..'\xff']
77+
78+newtype CharSet = CharSet {fromCharSet :: Char -> Bool}
79+
80+instance Eq CharSet where
81+ CharSet f1 == CharSet f2 = all (\x -> f1 x == f2 x) chars
82+
83+instance Monoid CharSet where
84+ mempty = CharSet $ const False
85+ mappend (CharSet f1) (CharSet f2) = CharSet $ f1 ||^ f2
86+
87+instance Show CharSet where
88+ show (CharSet p) = "[" ++ f chars ++ "]"
89+ where
90+ showChar x | x `elem` "[]-" = ['\\',x]
91+ | otherwise = init $ tail $ show x
92+ f xs = case dropWhile (not . p) xs of
93+ [] -> []
94+ x:xs -> case span p xs of
95+ ([], rest) -> showChar x ++ f rest :: String
96+ (ys, rest) -> showChar x ++ "-" ++ showChar (last ys) ++ f rest
97+
98+-- | Given we know the first argument are all true, can the second argument ever be False
99+implies :: [CharSet] -> CharSet -> Bool
100+implies known x = all (fromCharSet x) poss
101+ where poss = filter (\c -> all (`fromCharSet` c) known) chars
102+
103+charSetC :: [CharSet] -> ([String], CharSet -> String)
104+charSetC sets =
105+ (["static const char table[256] = {" ++ intercalate "," ("0":map disp chars) ++ "};"]
106+ ,\x -> fromMaybe ("table[(int) *p] & " ++ showBit (bit $ fromJust $ elemIndex x hard)) $ simple x)
107+ where
108+ showBit x = "0x" ++ showHex (x :: Int) ""
109+ disp c = showBit $ foldr (.|.) 0 [bit i | (i, h) <- zipFrom 0 hard, fromCharSet h c]
110+
111+ hard = nub $ filter (isNothing . simple) sets
112+
113+ simple x | x == CharSet (const True) = Just "*p"
114+ | [x] <- filter (fromCharSet x) chars = Just $ "*p == " ++ show x
115+ | otherwise = Nothing
116+
117+---------------------------------------------------------------------
118+-- THE UNDERLYING C TYPE
119+
120+data C o
121+ = While CharSet [C o]
122+ | Stmt o
123+ | Next
124+ | If CharSet [C o] [C o]
125+ | Abort String
126+ deriving Show
127+
128+-- Can you leave this statement successfully having consumed no characters?
129+empty :: P o -> Bool
130+empty Empty = True
131+empty (Choice a b) = empty b
132+empty Many{} = True
133+empty Out{} = True
134+empty Match{} = False
135+empty Panic{} = False
136+empty (Seq a b) = empty a && empty b
137+
138+-- What characters can come next and you commit
139+peek :: P o -> CharSet
140+peek Empty = mempty
141+peek Out{} = mempty
142+peek (Match f) = f
143+peek Panic{} = CharSet $ const True
144+peek (Choice a b) = peek a <> peek b
145+peek (Many x) = peek x
146+peek (Seq a b) = peek a <> (if empty a then peek b else mempty)
147+
148+toC :: P o -> [C o]
149+toC Empty = []
150+toC (Choice a b) = [If (peek a) (toC a) (toC b)]
151+toC (Match f) = [If f [Next] [Abort "Failed to match"]]
152+toC (Many x) = [While (peek x) (toC x)]
153+toC (Out x) = [Stmt x]
154+toC (Panic x) = [Abort x]
155+toC (Seq a b) = toC a ++ toC b
156+
157+runC :: [C o] -> String -> ([(Int, o)], (Int, String), Maybe String)
158+runC = f 0
159+ where
160+ f i [] s = ([], (i, s), Nothing)
161+ f i (Abort x:_) s = ([], (i,s), Just x)
162+ f i (Next:_) [] = ([], (i, []), Just "Next off the end")
163+ f i (Next:r) (x:xs) = f (i+1) r xs
164+ f i (Stmt x:r) xs = let (v1,v2,v3) = f i r xs in ((i,x):v1, v2, v3)
165+ f i (If cond true false : r) xs
166+ | x:_ <- xs, fromCharSet cond x = f i (true ++ r) xs
167+ | otherwise = f i (false ++ r) xs
168+ f i (While cond x:r) xs = f i (If cond (x ++ [While cond x]) [] : r) xs
169+
170+
171+prettyC :: Show o => [C o] -> [String]
172+prettyC xs =
173+ prefix ++
174+ concatMap f xs
175+ where
176+ (prefix, cond) = charSetC $ concatMap charSets xs
177+ charSets (While c x) = c : concatMap charSets x
178+ charSets (If c t f) = c : concatMap charSets t ++ concatMap charSets f
179+ charSets _ = []
180+
181+ fi = map (" "++) . concatMap f
182+ f (Abort x) = ["P_Abort(" ++ show x ++ ");"]
183+ f Next = ["p++;"]
184+ f (Stmt x) = ["P_" ++ show x ++ ";"]
185+ f (If c true []) = ["if (" ++ cond c ++ "){"] ++ fi true ++ ["}"]
186+ f (If c true [false@If{}]) = ["if (" ++ cond c ++ "){"] ++ fi true ++ ["} else " ++ f1] ++ fs
187+ where f1:fs = f false
188+ f (If c true false) = ["if (" ++ cond c ++ "){"] ++ fi true ++ ["} else {"] ++ fi false ++ ["}"]
189+ f (While c x) = ["while (" ++ cond c ++ "){"] ++ fi x ++ ["}"]
190+
191+optimiseC :: [C o] -> [C o]
192+optimiseC = f []
193+ where
194+ f known [] = []
195+ f known (Abort x:_) = [Abort x]
196+ f known (Next:xs) = Next : f [] xs
197+ f known (Stmt x:xs) = Stmt x : f known xs
198+ f known (If cond true false : xs)
199+ | implies known cond = f known $ true ++ xs
200+ | otherwise = If cond (f (cond:known) true) (f known false) : f [] xs -- might be a Next in body
201+ f known (While cond x : xs) = While cond (f (cond:known) x) : f [] xs
gen/Main.hsnew file+70
@@ -0,0 +1,70 @@
1+
2+module Main(main) where
3+
4+import Lib
5+import Parser
6+import Control.Monad.Extra
7+import System.Process.Extra
8+import System.Directory
9+import Data.Maybe
10+import System.IO.Extra
11+import System.FilePath
12+import System.Info.Extra
13+
14+
15+main :: IO ()
16+main = do
17+ forM_ examples $ \(b, src) -> do
18+ putStrLn ""
19+ putStrLn src
20+ let (res, trail, err) = run html src
21+ print res
22+ print (trail, err)
23+ putStrLn $ rerender src res
24+ writeFileChanged "test.h" $ unlines $ compile html
25+ buildRule ["test.h","test.c"] ["gen" <.> (if isWindows then "exe" else "")] $
26+ system_ "gcc test.c -o gen -Werror -Wall -Wextra -DTEST"
27+ system_ "gen"
28+
29+writeFileChanged :: FilePath -> String -> IO ()
30+writeFileChanged file new = do
31+ old <- ifM (doesFileExist file) (readFile' file) (pure "")
32+ when (old /= new) $
33+ writeFile file new
34+
35+buildRule :: [FilePath] -> [FilePath] -> IO () -> IO ()
36+buildRule from to act = do
37+ let modTime x = ifM (doesFileExist x) (Just <$> getModificationTime x) (pure Nothing)
38+ from <- mapM modTime from
39+ to <- mapM modTime to
40+ when (any isNothing (from ++ to) || maximum from >= minimum to)
41+ act
42+
43+examples :: [(Bool, String)]
44+examples = take 1
45+ [(True, "<test foo='123 xyz'><inner /> value</test>")
46+ ,(True, "<test />")
47+ ,(True, "<test id='bob'>here<extra/>there</test>")
48+ ,(True, "<test /><close />")
49+ ,(True, "<test /><!-- comment > --><close />")
50+ ,(True, "<test id=\"bob value\" another-attr=\"test with <\">here </test> more text at the end<close />")
51+ ,(False, "<test></more>")
52+ ,(False, "<test")
53+ ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
54+ ]
55+
56+rerender :: String -> [(Int, Out)] -> String
57+rerender o = f
58+ where
59+ sub s e = take (e - s) $ drop s o
60+
61+ f ((_,Tag):(_,TagComment):xs) = f xs
62+ f ((_,Tag):xs) = "<" ++ f xs
63+ f ((_,TagOpen):xs) = ">" ++ f xs
64+ f ((_,TagOpenClose):xs) = "/>" ++ f xs
65+ f ((s,NameStart):(e,NameEnd):(_,TagClose):xs) = "/" ++ sub s e ++ ">" ++ f xs
66+ f ((s,NameStart):(e,NameEnd):xs) = sub s e ++ " " ++ f xs
67+ f ((s,QuoteStart):(e,QuoteEnd):xs) = "=\"" ++ sub s e ++ "\" " ++ f xs
68+ f ((_,AttribsStart):xs) = f xs
69+ f ((_,AttribsEnd):xs) = f xs
70+ f [] = []
gen/Parser.hsnew file+76
@@ -0,0 +1,76 @@
1+
2+module Parser(Out(..), html) where
3+
4+import Lib
5+
6+data Out
7+ = NameStart | NameEnd
8+ | QuoteStart | QuoteEnd
9+ | AttribsStart | AttribsEnd
10+ | Tag
11+ | TagComment | TagOpen | TagClose | TagOpenClose
12+ deriving Show
13+
14+is lower upper x = x >= lower && x <= upper
15+isName1 x = x == ':' || x == '_' || is 'a' 'z' x || is 'A' 'Z' x
16+isName x = isName1 x || x == '-' || x == '.' || is '0' '9' x
17+isSpace x = x == ' ' || x == '\t' || x == '\r' || x == '\n'
18+
19+whitespace = many $ match isSpace
20+
21+name = do
22+ out NameStart
23+ match isName1
24+ many $ match isName
25+ out NameEnd
26+ whitespace
27+
28+attrib = do
29+ name
30+ lit "=" `choice` abort "Expected = in attribute but missing"
31+ whitespace
32+ let quoted x = do
33+ lit [x]
34+ out QuoteStart
35+ many $ match (/= x)
36+ out QuoteEnd
37+ lit [x] `choice` abort ("Couldn't find closing attribute quote (expected " ++ [x] ++ ")")
38+ whitespace
39+ choices
40+ [quoted '\"'
41+ ,quoted '\''
42+ ,abort "Invalid attribute, expected quote (either ' or \")"]
43+
44+attribs = do
45+ out AttribsStart
46+ many attrib
47+ out AttribsEnd
48+
49+tag = do
50+ out Tag
51+ lit "<"
52+ let name_ = name `choice` abort "Missing tag name"
53+ choices
54+ [do lit "!--"
55+ many $ match (/= '-')
56+ -- FIXME: Not correct!!!
57+ lit "-->"
58+ out TagComment
59+ ,do lit "?"
60+ name_
61+ attribs
62+ lit "?>"
63+ out TagOpenClose
64+ ,do lit "/"
65+ name_
66+ lit ">"
67+ out TagClose
68+ ,do name_
69+ attribs
70+ choice
71+ (lit "/>" >> out TagOpenClose)
72+ (lit ">" >> out TagOpen)
73+ ]
74+
75+
76+html = many $ choice tag $ match $ const True
gen/test.cnew file+488
@@ -0,0 +1,488 @@
1+
2+#include "../cbits/hexml.h"
3+#include <stdlib.h>
4+#include <string.h>
5+#include <assert.h>
6+#include <stdio.h>
7+
8+/////////////////////////////////////////////////////////////////////
9+// TYPES
10+
11+static inline int end(str s) { return s.start + s.length; }
12+
13+static inline str start_length(int32_t start, int32_t length)
14+{
15+ assert(start >= 0 && length >= 0);
16+ str res;
17+ res.start = start;
18+ res.length = length;
19+ return res;
20+}
21+
22+static inline str start_end(int32_t start, int32_t end)
23+{
24+ return start_length(start, end - start);
25+}
26+
27+typedef struct
28+{
29+ int size;
30+ int used;
31+ attr* attrs; // dynamically allocated buffer
32+ attr* alloc; // what to call free on
33+} attr_buffer;
34+
35+typedef struct
36+{
37+ // have a cursor at the front, which is all the stuff I have written out, final
38+ // have a cursor at the end, which is stack scoped, so children write out, then I do
39+ // when you commit, you copy over from end to front
40+
41+ // nodes
42+ int size;
43+ int used_front; // front entries, stored for good
44+ int used_back; // back entries, stack based, copied into front on </close>
45+ // back entries which haven't yet been closed have nodes.length == -1
46+ node* nodes; // dynamically allocated buffer
47+ node* alloc; // what to call free on
48+} node_buffer;
49+
50+
51+struct document
52+{
53+ const char* body; // pointer to initial argument, not owned by us
54+ int body_len; // length of the body
55+
56+ char* error_message;
57+ node_buffer nodes;
58+ attr_buffer attrs;
59+};
60+
61+
62+/////////////////////////////////////////////////////////////////////
63+// RENDER CODE
64+
65+typedef struct
66+{
67+ const document* d; // Document, used to get at the body
68+ char* buffer; // Buffer we are writing too
69+ int length; // Length of buffer, do not write past the end.
70+ // No character is reserved for a NULL terminator.
71+ int cursor; // Cursor of where we are writing now
72+} render;
73+
74+static inline void render_char(render* r, char c)
75+{
76+ if (r->cursor < r->length)
77+ r->buffer[r->cursor] = c;
78+ r->cursor++;
79+}
80+
81+static inline void bound(int idx, int mn, int mx)
82+{
83+ assert(idx >= mn && idx <= mx);
84+}
85+
86+static inline void bound_str(str s, int mn, int mx)
87+{
88+ assert(s.length >= 0);
89+ bound(s.start, mn, mx);
90+ bound(end(s), mn, mx);
91+}
92+
93+static void render_str(render* r, str s)
94+{
95+ bound_str(s, 0, r->d->body_len);
96+ for (int i = 0; i < s.length; i++)
97+ render_char(r, r->d->body[s.start + i]);
98+}
99+
100+static void render_tag(render* r, const node* n);
101+
102+static void render_content(render* r, const node* n)
103+{
104+ bound_str(n->inner, 0, r->d->body_len);
105+ bound_str(n->nodes, 0, r->d->nodes.used_front);
106+ bound_str(n->attrs, 0, r->d->attrs.used);
107+
108+ int done = n->inner.start;
109+ for (int i = 0; i < n->nodes.length; i++)
110+ {
111+ node* x = &r->d->nodes.nodes[n->nodes.start + i];
112+ render_str(r, start_end(done, x->outer.start));
113+ done = end(x->outer);
114+ render_tag(r, x);
115+ }
116+ render_str(r, start_end(done, end(n->inner)));
117+}
118+
119+static void render_tag(render* r, const node* n)
120+{
121+ render_char(r, '<');
122+ render_str(r, n->name);
123+ for (int i = 0; i < n->attrs.length; i++)
124+ {
125+ attr* x = &r->d->attrs.attrs[n->attrs.start + i];
126+ render_char(r, ' ');
127+ render_str(r, x->name);
128+ render_char(r, '=');
129+ render_char(r, '\"');
130+ render_str(r, x->value);
131+ render_char(r, '\"');
132+ }
133+ render_char(r, '>');
134+ render_content(r, n);
135+ render_char(r, '<');
136+ render_char(r, '/');
137+ render_str(r, n->name);
138+ render_char(r, '>');
139+}
140+
141+int hexml_node_render(const document* d, const node* n, char* buffer, int length)
142+{
143+ render r;
144+ r.d = d;
145+ r.buffer = buffer;
146+ r.length = length;
147+ r.cursor = 0;
148+ // The root node (and only the root node) has an empty length, so just render its innards
149+ if (n->name.length == 0)
150+ render_content(&r, n);
151+ else
152+ render_tag(&r, n);
153+ return r.cursor;
154+}
155+
156+
157+/////////////////////////////////////////////////////////////////////
158+// NOT THE PARSER
159+
160+char* hexml_document_error(const document* d){return d->error_message;}
161+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
162+
163+void hexml_document_free(document* d)
164+{
165+ free(d->error_message);
166+ free(d->nodes.alloc);
167+ free(d->attrs.alloc);
168+ free(d);
169+}
170+
171+node* hexml_node_children(const document* d, const node* n, int* res)
172+{
173+ *res = n->nodes.length;
174+ return &d->nodes.nodes[n->nodes.start];
175+}
176+
177+attr* hexml_node_attributes(const document* d, const node* n, int* res)
178+{
179+ *res = n->attrs.length;
180+ return &d->attrs.attrs[n->attrs.start];
181+}
182+
183+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
184+{
185+ if (slen == -1) slen = (int) strlen(s);
186+ const int limit = end(n->attrs);
187+ for (int i = n->attrs.start; i < limit; i++)
188+ {
189+ attr* r = &d->attrs.attrs[i];
190+ if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
191+ return r;
192+ }
193+ return NULL;
194+}
195+
196+// Search for given strings within a node
197+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
198+{
199+ if (slen == -1) slen = (int) strlen(s);
200+ int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
201+ const int limit = end(parent->nodes);
202+ for (; i < limit; i++)
203+ {
204+ node* r = &d->nodes.nodes[i];
205+ if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
206+ return r;
207+ }
208+ return NULL;
209+}
210+
211+/////////////////////////////////////////////////////////////////////
212+// PARSING CODE
213+
214+static inline node* node_alloc(node_buffer* b)
215+{
216+ int space = b->size - b->used_back - b->used_front;
217+ if (space < 1)
218+ {
219+ int size2 = (b->size + 1000) * 2;
220+ node* buf2 = malloc(size2 * sizeof(node));
221+ assert(buf2);
222+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
223+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
224+ free(b->alloc);
225+ b->size = size2;
226+ b->nodes = buf2;
227+ b->alloc = buf2;
228+ }
229+ b->used_back++;
230+ return &b->nodes[b->size - b->used_back];
231+}
232+
233+// buffer[from .. from+n] = reverse (buffer[from .. from+n])
234+// The ranges completely overlap
235+static inline void reverse(node* buffer, int from, int n)
236+{
237+ for (int to = from + n - 1; to > from; from++, to--)
238+ {
239+ node temp = buffer[to];
240+ buffer[to] = buffer[from];
241+ buffer[from] = temp;
242+ }
243+}
244+
245+// buffer[to .. to+n] = reverse (buffer[from .. from+n])
246+// The ranges may not overlap
247+static inline void memcpy_reverse(node* buffer, int from, int to, int n)
248+{
249+ int end = from + n - 1;
250+ for (int i = 0; i < n; i++)
251+ buffer[to+i] = buffer[end-i];
252+}
253+
254+// buffer[to .. to+n] = reverse (buffer[from .. from+n])
255+// The ranges may overlap
256+static inline void memmove_reverse(node* buffer, int from, int to, int n)
257+{
258+ assert(from >= to); // always the case in my usages
259+ if (to + n < from)
260+ memcpy_reverse(buffer, from, to, n);
261+ else
262+ {
263+ int overlap = from - to;
264+ memcpy_reverse(buffer, from, to + n - overlap, overlap);
265+ reverse(buffer, to, n - overlap);
266+ }
267+}
268+
269+static inline str node_commit(node_buffer* b)
270+{
271+ // first search for an "open" node (nodes.length == -1)
272+ int open;
273+ for (open = b->size - b->used_back; open < b->size; open++)
274+ {
275+ if (b->nodes[open].nodes.length == -1)
276+ break;
277+ }
278+
279+ int n = open - (b->size - b->used_back);
280+ memmove_reverse(b->nodes, b->size - b->used_back, b->used_front, n);
281+ b->used_back -= n;
282+ b->used_front += n;
283+ return start_length(b->used_front - n, n);
284+}
285+
286+static inline attr* attr_alloc(attr_buffer* b)
287+{
288+ // Ensure there is at least one node left
289+ int space = b->size - b->used;
290+ if (space < 1)
291+ {
292+ int size2 = (b->size + 1000) * 2;
293+ attr* buf2 = malloc(size2 * sizeof(attr));
294+ assert(buf2);
295+ memcpy(buf2, b->attrs, b->used * sizeof(attr));
296+ free(b->alloc);
297+ b->size = size2;
298+ b->attrs = buf2;
299+ b->alloc = buf2;
300+ }
301+ b->used++;
302+ return &b->attrs[b->used - 1];
303+}
304+
305+static void set_error(document* d, const char* msg)
306+{
307+ if (d->error_message != NULL) return; // keep the first error message
308+ d->error_message = malloc(strlen(msg)+1);
309+ assert(d->error_message);
310+ strcpy(d->error_message, msg);
311+}
312+
313+static inline str gap(const char* ref, const char* start, const char* end)
314+{
315+ return start_end(start - ref, end - ref);
316+}
317+
318+#define P_Abort(x) return x
319+#define P_Tag tag_start = p;
320+#define P_NameStart name_start = p
321+#define P_NameEnd name_end = p
322+#define P_AttribsStart \
323+ me = node_alloc(&d->nodes); \
324+ me->attrs.start = d->attrs.used; \
325+ me->name = gap(d->body, name_start, name_end);
326+#define P_AttribsEnd \
327+ me->attrs = start_end(me->attrs.start, d->attrs.used);
328+#define P_QuoteStart quote_start = p
329+#define P_QuoteEnd \
330+ attr* attr = attr_alloc(&d->attrs); \
331+ attr->name = gap(d->body, name_start, name_end); \
332+ attr->value = gap(d->body, quote_start, p);
333+#define P_TagComment \
334+ // No action required - comments just flow as normal text
335+#define P_TagOpen \
336+ me->nodes.length = -1; \
337+ me->inner = gap(d->body, p, p); \
338+ me->outer = gap(d->body, tag_start, tag_start);
339+#define P_TagClose \
340+ str nodes = node_commit(&d->nodes); \
341+ if (d->nodes.used_back == 0) \
342+ return "Closing tag with no openning"; \
343+ me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back]; \
344+ me->nodes = nodes; \
345+ if (me->name.length != gap(d->body, name_start, name_end).length || \
346+ memcmp(&d->body[me->name.start], name_start, me->name.length) != 0) \
347+ return "Mismatch in closing tag"; \
348+ me->inner = gap(d->body, &d->body[me->inner.start], tag_start); \
349+ me->outer = gap(d->body, &d->body[me->outer.start], p);
350+#define P_TagOpenClose \
351+ me->nodes = start_end(0, 0); \
352+ me->inner = gap(d->body, p, p); \
353+ me->outer = gap(d->body, tag_start, p);
354+
355+// Given the parsed string, return either NULL (success) or an error message (failure)
356+static const char* parser(const char* p, document* d)
357+{
358+ const char *tag_start; // Where the tag starts [<foo
359+ const char *name_start, *name_end; // Where a name is <[foo] or <foo [bar]=
360+ const char *quote_start; // Where an attribute quote is <foo bar='[123'>
361+ node* me; // The current node I'm working on
362+# include "test.h"
363+ return NULL;
364+}
365+
366+// Based on looking at ~50Kb XML documents, they seem to have ~700 attributes
367+// and ~300 nodes, so size appropriately to cope with that.
368+typedef struct
369+{
370+ document document;
371+ attr attrs[1000];
372+ node nodes[500];
373+} buffer;
374+
375+document* hexml_document_parse(const char* s, int slen)
376+{
377+ if (slen == -1) slen = (int) strlen(s);
378+ assert(s[slen] == 0);
379+
380+ buffer* buf = malloc(sizeof(buffer));
381+ assert(buf);
382+ document* d = &buf->document;
383+ d->body = s;
384+ d->body_len = slen;
385+ d->error_message = NULL;
386+ d->attrs.size = 1000;
387+ d->attrs.used = 0;
388+ d->attrs.attrs = buf->attrs;
389+ d->attrs.alloc = NULL;
390+ d->nodes.size = 500;
391+ d->nodes.used_back = 0;
392+ d->nodes.used_front = 1;
393+ d->nodes.nodes = buf->nodes;
394+ d->nodes.alloc = NULL;
395+
396+ d->nodes.nodes[0].name = start_length(0, 0);
397+ d->nodes.nodes[0].outer = start_length(0, slen);
398+ d->nodes.nodes[0].inner = start_length(0, slen);
399+ d->nodes.nodes[0].attrs = start_length(0, 0);
400+
401+ // Introduce an intermediate result, otherwise behaviour is undefined
402+ // because there is no guaranteed ordering between LHS and RHS evaluation
403+ const char* res = parser(s, d);
404+ if (res != NULL)
405+ set_error(d, res);
406+ else
407+ {
408+ d->nodes.nodes[0].nodes = node_commit(&d->nodes);
409+ if (d->nodes.used_back != 0)
410+ d->error_message = strdup("used_back failure");
411+ }
412+ return d;
413+}
414+
415+/////////////////////////////////////////////////////////////////////
416+// TEST HARNESS (temporary)
417+
418+#ifdef TEST
419+
420+static char* resolve(const document* d, str loc)
421+{
422+ char* buf = malloc(loc.length + 3);
423+ memcpy(&buf[1], &d->body[loc.start], loc.length);
424+ buf[0] = '\"';
425+ buf[loc.length+1] = '\"';
426+ buf[loc.length+2] = 0;
427+ return buf;
428+}
429+
430+static char* disp(str loc)
431+{
432+ char* buf = malloc(100);
433+ if (loc.length == 0)
434+ strcpy(buf, "[]");
435+ else
436+ sprintf(buf, "[%i..%i]", loc.start, end(loc)-1);
437+ return buf;
438+}
439+
440+int main()
441+{
442+ setbuf(stdout, NULL);
443+ const char* example = "<test foo='123 xyz' bar='t'><inner /> value</test>";
444+ document* d = hexml_document_parse(example, -1);
445+ if (d->error_message != NULL)
446+ printf("Parse failure: %s\n", d->error_message);
447+ else
448+ {
449+ char buf[1000];
450+ int len = hexml_node_render(d, hexml_document_node(d), buf, sizeof(buf));
451+ buf[len] = 0;
452+ printf("Result = %s\n", buf);
453+ printf("Used nodes = %i, attributes = %i\n", d->nodes.used_front, d->attrs.used);
454+
455+ for (int i = 0; i < d->attrs.used; i++)
456+ printf("Attr %i: %s=%s\n", i, resolve(d, d->attrs.attrs[i].name), resolve(d, d->attrs.attrs[i].value));
457+ for (int i = 0; i < d->nodes.used_front; i++)
458+ {
459+ printf("Node %i: %s attrs=%s, nodes=%s\n", i, resolve(d, d->nodes.nodes[i].name), disp(d->nodes.nodes[i].attrs), disp(d->nodes.nodes[i].nodes));
460+ printf(" inner %s\n outer %s\n", resolve(d, d->nodes.nodes[i].inner), resolve(d, d->nodes.nodes[i].outer));
461+ }
462+ }
463+ hexml_document_free(d);
464+ return 0;
465+}
466+
467+#endif
468+
469+/*
470+void main()
471+{
472+ setbuf(stdout, NULL);
473+ char* body = "<?xml version=\"1.0\" lang=\"ja\"?><foo><bar baz=\"qux\">quux</bar><bar baz=\"bar\">hoge</bar><piyo>&gt;piyopiyo&lt;</piyo></foo><foo2 />";
474+ body = "<test>X</test> ";
475+ document* doc = document_parse(body, -1);
476+ char* err = document_error(doc);
477+ if (err == NULL)
478+ {
479+ int len = document_render(doc, NULL, 0);
480+ char* s = malloc(len + 1);
481+ document_render(doc, s, len);
482+ s[len] = 0;
483+ printf("Parse successful, %s\n", s);
484+ }
485+ else
486+ printf("Parse failed, %s\n", err);
487+}
488+*/
ghci.bat+2−1
@@ -1 +1,2 @@
1−gcc -O3 -std=c99 -c cbits\hexml.c -o cbits\hexml.o && ghci.exe cbits\hexml.o %*
1+gcc -O2 -std=c99 -c cbits\hexml.c -o cbits\hexml.o -Wall -Wextra -Werror && ghci.exe cbits\hexml.o %*
2+rem gcc -O2 -std=c99 -c gen\test.c -o gen\test.o -Wall -Wextra -Werror && ghci.exe gen\test.o %*
go.batdeleted−2
@@ -1,2 +0,0 @@
1−rem sos -p .*.c -c "go 2>&1 | head -n12"
2−gcc -c hexml.c -O3 && gcc -c main.c -O3 && gcc -o hexml hexml.o main.o && hexml
hexml.cabal+9−8
@@ -1,19 +1,20 @@
1cabal-version: >= 1.18
1cabal-version: >= 1.18
2build-type: Simple
2build-type: Simple
3name: hexml
3name: hexml
4−version: 0.1
4+version: 0.3.4
5license: BSD3
5license: BSD3
6+x-license: BSD-3-Clause OR Apache-2.0
6license-file: LICENSE
7license-file: LICENSE
7−category: Development
8+category: XML
8author: Neil Mitchell <ndmitchell@gmail.com>
9author: Neil Mitchell <ndmitchell@gmail.com>
9maintainer: Neil Mitchell <ndmitchell@gmail.com>
10maintainer: Neil Mitchell <ndmitchell@gmail.com>
10−copyright: Neil Mitchell 2016
11+copyright: Neil Mitchell 2016-2023
11−synopsis: Bad XML parser
12+synopsis: XML subset DOM parser
12description:
13description:
13− A bad XML parser.
14+ An XML DOM-style parser, that only parses a subset of XML, but is designed to be fast.
14homepage: https://github.com/ndmitchell/hexml#readme
15homepage: https://github.com/ndmitchell/hexml#readme
15bug-reports: https://github.com/ndmitchell/hexml/issues
16bug-reports: https://github.com/ndmitchell/hexml/issues
16−tested-with: GHC==8.0.1, GHC==7.10.3, GHC==7.8.4, GHC==7.6.3
17+tested-with: GHC==9.6, GHC==9.4, GHC==9.2, GHC==9.0, GHC==8.10, GHC==8.8
17extra-doc-files:
18extra-doc-files:
18 README.md
19 README.md
19 CHANGES.txt
20 CHANGES.txt
@@ -30,9 +31,9 @@ library
30 default-language: Haskell2010
31 default-language: Haskell2010
31
32
32 build-depends:
33 build-depends:
33− base > 4 && < 5,
34+ base >= 4.5 && < 5,
34 bytestring,
35 bytestring,
35− extra
36+ extra >= 0.3
36
37
37 c-sources: cbits/hexml.c
38 c-sources: cbits/hexml.c
38 include-dirs: cbits
39 include-dirs: cbits
main.cdeleted−22
@@ -1,22 +0,0 @@
1−#include "hexml.h"
2−#include <stdio.h>
3−#include <malloc.h>
4−
5−void main()
6−{
7− setbuf(stdout, NULL);
8− char* body = "<?xml version=\"1.0\" lang=\"ja\"?><foo><bar baz=\"qux\">quux</bar><bar baz=\"bar\">hoge</bar><piyo>&gt;piyopiyo&lt;</piyo></foo><foo2 />";
9− body = "<test>X</test> ";
10− document* doc = document_parse(body, -1);
11− char* err = document_error(doc);
12− if (err == NULL)
13− {
14− int len = document_render(doc, NULL, 0);
15− char* s = malloc(len + 1);
16− document_render(doc, s, len);
17− s[len] = 0;
18− printf("Parse successful, %s\n", s);
19− }
20− else
21− printf("Parse failed, %s\n", err);
22−}
src/Main.hs+30−9
@@ -7,6 +7,7 @@ import qualified Data.ByteString.Char8 as BS
7import Control.Monad
7import Control.Monad
8import Data.Monoid
8import Data.Monoid
9import Data.Char
9import Data.Char
10+import Prelude
10
11
11
12
12examples :: [(Bool, BS.ByteString)]
13examples :: [(Bool, BS.ByteString)]
@@ -18,33 +19,53 @@ examples =
18 ,(False, "<test></more>")
19 ,(False, "<test></more>")
19 ,(False, "<test")
20 ,(False, "<test")
20 ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
21 ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
22+ ,(True, "<foo bar.baz=\"qux\"></foo>")
21 ]
23 ]
22
24
23main :: IO ()
25main :: IO ()
24main = do
26main = do
25− forM_ examples $ \(parses, src) -> do
27+ forM_ examples $ \(parses, src) ->
26− print src
27 case parse src of
28 case parse src of
28 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
29 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
29 Right doc -> do
30 Right doc -> do
30 unless parses $ fail "Unexpected parse success"
31 unless parses $ fail "Unexpected parse success"
32+ checkFind doc
31 let r = render doc
33 let r = render doc
32− print r
33− print $ rerender doc
34− when (r /= rerender doc) $ fail "Different rerender"
34+ r === rerender doc
35 let Right d = parse r
35 let Right d = parse r
36− when (r /= render d) $ fail "Different after rerendering"
36+ r === render d
37
37
38− let Right doc = parse "<test id=\"1\" extra=\"2\" /><test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
38+ let Right doc = parse "<test id=\"1\" extra=\"2\" />\n<test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
39 map name (children doc) === ["test","test","b","test","test"]
39 map name (children doc) === ["test","test","b","test","test"]
40+ location (children doc !! 2) === (2,16)
40 length (childrenBy doc "test") === 4
41 length (childrenBy doc "test") === 4
41 length (childrenBy doc "b") === 1
42 length (childrenBy doc "b") === 1
42 length (childrenBy doc "extra") === 0
43 length (childrenBy doc "extra") === 0
43 attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
44 attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
44 map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
45 map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
45
46
47+ Right _ <- pure $ parse $ "<test " <> BS.unwords [BS.pack $ "x" ++ show i ++ "='value'" | i <- [1..10000]] <> " />"
48+ Right _ <- pure $ parse $ BS.unlines $ replicate 10000 "<test x='value' />"
46
49
50+ let attrs = ["usd:jpy","test","extra","more","stuff","jpy:usd","xxx","xxxx"]
51+ Right doc <- pure $ parse $ "<test " <> BS.unwords [x <> "='" <> x <> "'" | x <- attrs] <> ">middle</test>"
52+ [c] <- pure $ childrenBy doc "test"
53+ forM_ attrs $ \a -> attributeBy c a === Just (Attribute a a)
54+ forM_ ["missing","gone","nothing"] $ \a -> attributeBy c a === Nothing
55+ putStrLn "\nSuccess"
56+
57+
58+checkFind :: Node -> IO ()
59+checkFind n = do
60+ forM_ (attributes n) $ \a -> attributeBy n (attributeName a) === Just a
61+ attributeBy n "xxx" === Nothing
62+ let cs = children n
63+ forM_ ("xxx":map name cs) $ \c ->
64+ map outer (filter ((==) c . name) cs) === map outer (childrenBy n c)
65+ mapM_ checkFind $ children n
66+
67+
47−a === b = if a == b then putStrLn "success" else fail "mismatch"
68+a === b = if a == b then putChar '.' else fail $ "mismatch, " ++ show a ++ " /= " ++ show b
48
69
49rerender :: Node -> BS.ByteString
70rerender :: Node -> BS.ByteString
50rerender = inside
71rerender = inside
@@ -55,7 +76,7 @@ rerender = inside
55 "</" <> name x <> ">"
76 "</" <> name x <> ">"
56 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
77 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
57
78
58− validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-:_" :: String)) x = x
79+ validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-.:_" :: String)) x = x
59 | otherwise = error "Invalid name"
80 | otherwise = error "Invalid name"
60 validAttr x | BS.notElem '\"' x = x
81 validAttr x | BS.notElem '\"' x = x
61 | otherwise = error "Invalid attribute"
82 | otherwise = error "Invalid attribute"
src/Text/XML/Hexml.hs+68−41
@@ -1,11 +1,11 @@
1−{-# LANGUAGE RecordWildCards #-}
1+{-# LANGUAGE RecordWildCards, BangPatterns #-}
2
2
3-- | A module for fast first-approximation parsing of XML.
3-- | A module for fast first-approximation parsing of XML.
4-- Note that entities, e.g. @&amp;@, are not expanded.
4-- Note that entities, e.g. @&amp;@, are not expanded.
5module Text.XML.Hexml(
5module Text.XML.Hexml(
6 Node, Attribute(..),
6 Node, Attribute(..),
7 parse, render,
7 parse, render,
8− name, inner, outer,
8+ location, name, inner, outer,
9 attributes, children, contents,
9 attributes, children, contents,
10 attributeBy, childrenBy
10 attributeBy, childrenBy
11 ) where
11 ) where
@@ -45,18 +45,18 @@ instance Storable Str where
45 peek p = Str <$> peekByteOff p 0 <*> peekByteOff p 4
45 peek p = Str <$> peekByteOff p 0 <*> peekByteOff p 4
46 poke p Str{..} = pokeByteOff p 0 strStart >> pokeByteOff p 4 strLength
46 poke p Str{..} = pokeByteOff p 0 strStart >> pokeByteOff p 4 strLength
47
47
48−foreign import ccall document_parse :: CString -> CInt -> IO (Ptr CDocument)
48+foreign import ccall hexml_document_parse :: CString -> CInt -> IO (Ptr CDocument)
49−foreign import ccall document_free :: Ptr CDocument -> IO ()
49+foreign import ccall hexml_document_free :: Ptr CDocument -> IO ()
50−foreign import ccall "&document_free" document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
50+foreign import ccall "&hexml_document_free" hexml_document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
51−foreign import ccall node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
51+foreign import ccall hexml_node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
52−foreign import ccall document_error :: Ptr CDocument -> IO CString
52+foreign import ccall unsafe hexml_document_error :: Ptr CDocument -> IO CString
53−foreign import ccall document_node :: Ptr CDocument -> IO (Ptr CNode)
53+foreign import ccall unsafe hexml_document_node :: Ptr CDocument -> IO (Ptr CNode)
54
54
55−foreign import ccall node_children :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CNode)
55+foreign import ccall unsafe hexml_node_children :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CNode)
56−foreign import ccall node_attributes :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CAttr)
56+foreign import ccall unsafe hexml_node_attributes :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CAttr)
57
57
58−foreign import ccall node_childBy :: Ptr CDocument -> Ptr CNode -> Ptr CNode -> CString -> CInt -> IO (Ptr CNode)
58+foreign import ccall unsafe hexml_node_child :: Ptr CDocument -> Ptr CNode -> Ptr CNode -> CString -> CInt -> IO (Ptr CNode)
59−foreign import ccall node_attributeBy :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO (Ptr CAttr)
59+foreign import ccall unsafe hexml_node_attribute :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO (Ptr CAttr)
60
60
61-- | A node in an XML document, created by 'parse', then calling functions such
61-- | A node in an XML document, created by 'parse', then calling functions such
62-- as 'children' on that initial 'Node'.
62-- as 'children' on that initial 'Node'.
@@ -64,35 +64,46 @@ data Node = Node BS.ByteString (ForeignPtr CDocument) (Ptr CNode)
64
64
65-- | An XML attribute, comprising of a name and a value. As an example,
65-- | An XML attribute, comprising of a name and a value. As an example,
66-- @hello=\"world\"@ would produce @Attribute \"hello\" \"world\"@.
66-- @hello=\"world\"@ would produce @Attribute \"hello\" \"world\"@.
67−data Attribute = Attribute BS.ByteString BS.ByteString deriving (Show, Eq, Ord)
67+data Attribute = Attribute
68+ {attributeName :: BS.ByteString
69+ ,attributeValue :: BS.ByteString
70+ } deriving (Show, Eq, Ord)
68
71
69instance Show Node where
72instance Show Node where
70 show d = "Node " ++ show (BS.unpack $ outer d)
73 show d = "Node " ++ show (BS.unpack $ outer d)
71
74
72
75
76+touchBS :: BS.ByteString -> IO ()
77+touchBS = touchForeignPtr . fst3 . BS.toForeignPtr
78+
79+
73-- | Parse a ByteString as an XML document, returning a 'Left' error message, or a 'Right' document.
80-- | Parse a ByteString as an XML document, returning a 'Left' error message, or a 'Right' document.
74−-- Note that the returned node will have a 'name' of @\"\"@, no attributes, and content as per the document.
81+-- Note that the returned node will have a 'name' of @\"\"@, no 'attributes', and 'contents' as per the document.
75−-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise a single
82+-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise an XML node and a single
76-- root element, use @'children' n !! 1@.
83-- root element, use @'children' n !! 1@.
77parse :: BS.ByteString -> Either BS.ByteString Node
84parse :: BS.ByteString -> Either BS.ByteString Node
78−parse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
85+parse src = do
86+ let src0 = src <> BS.singleton '\0'
87+ unsafePerformIO $ BS.unsafeUseAsCStringLen src0 $ \(str, len) -> do
79− doc <- document_parse str (fromIntegral len - 1)
88+ doc <- hexml_document_parse str (fromIntegral len - 1)
80− err <- document_error doc
89+ err <- hexml_document_error doc
81− if err /= nullPtr then do
90+ if err /= nullPtr then do
82− bs <- BS.packCString =<< document_error doc
91+ bs <- BS.packCString =<< hexml_document_error doc
83− document_free doc
92+ hexml_document_free doc
84− return $ Left bs
93+ pure $ Left bs
85− else do
94+ else do
86− node <- document_node doc
95+ node <- hexml_document_node doc
87− doc <- newForeignPtr document_free_funptr doc
96+ doc <- newForeignPtr hexml_document_free_funptr doc
88− return $ Right $ Node src doc node
97+ pure $ Right $ Node src0 doc node
89
98
90-- | Given a node, rerender it to something with an equivalent parse tree.
99-- | Given a node, rerender it to something with an equivalent parse tree.
91-- Mostly useful for debugging - if you want the real source document use 'outer' instead.
100-- Mostly useful for debugging - if you want the real source document use 'outer' instead.
92render :: Node -> BS.ByteString
101render :: Node -> BS.ByteString
93−render (Node _ doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
102+render (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
94− i <- node_render d n nullPtr 0
103+ i <- hexml_node_render d n nullPtr 0
95− BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
104+ res <- BS.create (fromIntegral i) $ \ptr -> void $ hexml_node_render d n (castPtr ptr) i
105+ touchBS src
106+ pure res
96
107
97applyStr :: BS.ByteString -> Str -> BS.ByteString
108applyStr :: BS.ByteString -> Str -> BS.ByteString
98applyStr bs Str{..} = BS.take (fromIntegral strLength) $ BS.drop (fromIntegral strStart) bs
109applyStr bs Str{..} = BS.take (fromIntegral strLength) $ BS.drop (fromIntegral strStart) bs
@@ -107,7 +118,7 @@ attrPeek :: BS.ByteString -> ForeignPtr CDocument -> Ptr CAttr -> Attribute
107attrPeek src doc a = unsafePerformIO $ withForeignPtr doc $ \_ -> do
118attrPeek src doc a = unsafePerformIO $ withForeignPtr doc $ \_ -> do
108 name <- applyStr src <$> peekElemOff (castPtr a) 0
119 name <- applyStr src <$> peekElemOff (castPtr a) 0
109 val <- applyStr src <$> peekElemOff (castPtr a) 1
120 val <- applyStr src <$> peekElemOff (castPtr a) 1
110− return $ Attribute name val
121+ pure $ Attribute name val
111
122
112-- | Get the name of a node, e.g. @\<test /\>@ produces @\"test\"@.
123-- | Get the name of a node, e.g. @\<test /\>@ produces @\"test\"@.
113name :: Node -> BS.ByteString
124name :: Node -> BS.ByteString
@@ -131,7 +142,7 @@ outer = nodeBS 2
131contents :: Node -> [Either BS.ByteString Node]
142contents :: Node -> [Either BS.ByteString Node]
132contents n@(Node src _ _) = f (strStart inner) outers
143contents n@(Node src _ _) = f (strStart inner) outers
133 where
144 where
134− f i [] = string i (strEnd inner) ++ []
145+ f i [] = string i (strEnd inner)
135 f i ((x, n):xs) = string i (strStart x) ++ Right n : f (strEnd x) xs
146 f i ((x, n):xs) = string i (strStart x) ++ Right n : f (strEnd x) xs
136
147
137 string start end | start == end = []
148 string start end | start == end = []
@@ -141,19 +152,19 @@ contents n@(Node src _ _) = f (strStart inner) outers
141
152
142-- | Get the direct child nodes of this node.
153-- | Get the direct child nodes of this node.
143children :: Node -> [Node]
154children :: Node -> [Node]
144−children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
155+children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d ->
145 alloca $ \count -> do
156 alloca $ \count -> do
146− res <- node_children d n count
157+ res <- hexml_node_children d n count
147 count <- fromIntegral <$> peek count
158 count <- fromIntegral <$> peek count
148− return [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
159+ pure [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
149
160
150-- | Get the attributes of this node.
161-- | Get the attributes of this node.
151attributes :: Node -> [Attribute]
162attributes :: Node -> [Attribute]
152−attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
163+attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d ->
153 alloca $ \count -> do
164 alloca $ \count -> do
154− res <- node_attributes d n count
165+ res <- hexml_node_attributes d n count
155 count <- fromIntegral <$> peek count
166 count <- fromIntegral <$> peek count
156− return [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
167+ pure [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
157
168
158-- | Get the direct children of this node which have a specific name.
169-- | Get the direct children of this node which have a specific name.
159-- A more efficient version of:
170-- A more efficient version of:
@@ -164,8 +175,9 @@ childrenBy (Node src doc n) str = go nullPtr
164 where
175 where
165 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
176 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
166 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
177 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
167− r <- node_childBy d n old bs $ fromIntegral len
178+ r <- hexml_node_child d n old bs $ fromIntegral len
179+ touchBS src
168− return $ if r == nullPtr then [] else Node src doc r : go r
180+ pure $ if r == nullPtr then [] else Node src doc r : go r
169
181
170-- | Get the first attribute of this node which has a specific name, if there is one.
182-- | Get the first attribute of this node which has a specific name, if there is one.
171-- A more efficient version of:
183-- A more efficient version of:
@@ -174,5 +186,20 @@ childrenBy (Node src doc n) str = go nullPtr
174attributeBy :: Node -> BS.ByteString -> Maybe Attribute
186attributeBy :: Node -> BS.ByteString -> Maybe Attribute
175attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
187attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
176 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
188 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
177− r <- node_attributeBy d n bs $ fromIntegral len
189+ r <- hexml_node_attribute d n bs $ fromIntegral len
190+ touchBS src
178− return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
191+ pure $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
192+
193+-- | Find the starting location of a node, the @<@ character.
194+-- The first character will be reported as @(line 1,column 1)@, because thats
195+-- how error messages typically do it.
196+location :: Node -> (Int, Int)
197+location n@(Node src _ _) = BS.foldl' f (pair 1 1) $ BS.take (fromIntegral i) src
198+ where
199+ pair !a !b = (a,b)
200+
201+ i = strStart $ nodeStr 2 n
202+ f (!line, !col) c
203+ | c == '\n' = pair (line+1) 1
204+ | c == '\t' = pair line (col+8)
205+ | otherwise = pair line (col+1)
vs/hexml.slnnew file+28
@@ -0,0 +1,28 @@
1+
2+Microsoft Visual Studio Solution File, Format Version 12.00
3+# Visual Studio 14
4+VisualStudioVersion = 14.0.25420.1
5+MinimumVisualStudioVersion = 10.0.40219.1
6+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hexml", "hexml.vcxproj", "{9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}"
7+EndProject
8+Global
9+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+ Debug|x64 = Debug|x64
11+ Debug|x86 = Debug|x86
12+ Release|x64 = Release|x64
13+ Release|x86 = Release|x86
14+ EndGlobalSection
15+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Debug|x64.ActiveCfg = Debug|x64
17+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Debug|x64.Build.0 = Debug|x64
18+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Debug|x86.ActiveCfg = Debug|Win32
19+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Debug|x86.Build.0 = Debug|Win32
20+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Release|x64.ActiveCfg = Release|x64
21+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Release|x64.Build.0 = Release|x64
22+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Release|x86.ActiveCfg = Release|Win32
23+ {9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}.Release|x86.Build.0 = Release|Win32
24+ EndGlobalSection
25+ GlobalSection(SolutionProperties) = preSolution
26+ HideSolutionNode = FALSE
27+ EndGlobalSection
28+EndGlobal
vs/hexml.vcxprojnew file+153
@@ -0,0 +1,153 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+ <ItemGroup Label="ProjectConfigurations">
4+ <ProjectConfiguration Include="Debug|Win32">
5+ <Configuration>Debug</Configuration>
6+ <Platform>Win32</Platform>
7+ </ProjectConfiguration>
8+ <ProjectConfiguration Include="Release|Win32">
9+ <Configuration>Release</Configuration>
10+ <Platform>Win32</Platform>
11+ </ProjectConfiguration>
12+ <ProjectConfiguration Include="Debug|x64">
13+ <Configuration>Debug</Configuration>
14+ <Platform>x64</Platform>
15+ </ProjectConfiguration>
16+ <ProjectConfiguration Include="Release|x64">
17+ <Configuration>Release</Configuration>
18+ <Platform>x64</Platform>
19+ </ProjectConfiguration>
20+ </ItemGroup>
21+ <ItemGroup>
22+ <ClCompile Include="..\cbits\hexml.c" />
23+ <ClInclude Include="..\cbits\hexml.h" />
24+ <ClCompile Include="..\cbits\main.c" />
25+ </ItemGroup>
26+ <PropertyGroup Label="Globals">
27+ <ProjectGuid>{9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}</ProjectGuid>
28+ <Keyword>Win32Proj</Keyword>
29+ <RootNamespace>hexml</RootNamespace>
30+ <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
31+ </PropertyGroup>
32+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
33+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
34+ <ConfigurationType>Application</ConfigurationType>
35+ <UseDebugLibraries>true</UseDebugLibraries>
36+ <PlatformToolset>v140</PlatformToolset>
37+ <CharacterSet>Unicode</CharacterSet>
38+ </PropertyGroup>
39+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
40+ <ConfigurationType>Application</ConfigurationType>
41+ <UseDebugLibraries>false</UseDebugLibraries>
42+ <PlatformToolset>v140</PlatformToolset>
43+ <WholeProgramOptimization>true</WholeProgramOptimization>
44+ <CharacterSet>Unicode</CharacterSet>
45+ </PropertyGroup>
46+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
47+ <ConfigurationType>Application</ConfigurationType>
48+ <UseDebugLibraries>true</UseDebugLibraries>
49+ <PlatformToolset>v140</PlatformToolset>
50+ <CharacterSet>Unicode</CharacterSet>
51+ </PropertyGroup>
52+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
53+ <ConfigurationType>Application</ConfigurationType>
54+ <UseDebugLibraries>false</UseDebugLibraries>
55+ <PlatformToolset>v140</PlatformToolset>
56+ <WholeProgramOptimization>true</WholeProgramOptimization>
57+ <CharacterSet>Unicode</CharacterSet>
58+ </PropertyGroup>
59+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
60+ <ImportGroup Label="ExtensionSettings">
61+ </ImportGroup>
62+ <ImportGroup Label="Shared">
63+ </ImportGroup>
64+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
65+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
66+ </ImportGroup>
67+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
68+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
69+ </ImportGroup>
70+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
71+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
72+ </ImportGroup>
73+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
74+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
75+ </ImportGroup>
76+ <PropertyGroup Label="UserMacros" />
77+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
78+ <LinkIncremental>true</LinkIncremental>
79+ </PropertyGroup>
80+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
81+ <LinkIncremental>true</LinkIncremental>
82+ </PropertyGroup>
83+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
84+ <LinkIncremental>false</LinkIncremental>
85+ </PropertyGroup>
86+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
87+ <LinkIncremental>false</LinkIncremental>
88+ </PropertyGroup>
89+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
90+ <ClCompile>
91+ <PrecompiledHeader>
92+ </PrecompiledHeader>
93+ <WarningLevel>Level3</WarningLevel>
94+ <Optimization>Disabled</Optimization>
95+ <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);_CRT_SECURE_NO_DEPRECATE</PreprocessorDefinitions>
96+ </ClCompile>
97+ <Link>
98+ <SubSystem>Console</SubSystem>
99+ <GenerateDebugInformation>true</GenerateDebugInformation>
100+ </Link>
101+ </ItemDefinitionGroup>
102+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
103+ <ClCompile>
104+ <PrecompiledHeader>
105+ </PrecompiledHeader>
106+ <WarningLevel>Level3</WarningLevel>
107+ <Optimization>Disabled</Optimization>
108+ <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);_CRT_SECURE_NO_DEPRECATE</PreprocessorDefinitions>
109+ <AdditionalIncludeDirectories>..\cbits</AdditionalIncludeDirectories>
110+ </ClCompile>
111+ <Link>
112+ <SubSystem>Console</SubSystem>
113+ <GenerateDebugInformation>true</GenerateDebugInformation>
114+ </Link>
115+ </ItemDefinitionGroup>
116+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
117+ <ClCompile>
118+ <WarningLevel>Level3</WarningLevel>
119+ <PrecompiledHeader>
120+ </PrecompiledHeader>
121+ <Optimization>MaxSpeed</Optimization>
122+ <FunctionLevelLinking>true</FunctionLevelLinking>
123+ <IntrinsicFunctions>true</IntrinsicFunctions>
124+ <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
125+ </ClCompile>
126+ <Link>
127+ <SubSystem>Console</SubSystem>
128+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
129+ <OptimizeReferences>true</OptimizeReferences>
130+ <GenerateDebugInformation>true</GenerateDebugInformation>
131+ </Link>
132+ </ItemDefinitionGroup>
133+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
134+ <ClCompile>
135+ <WarningLevel>Level3</WarningLevel>
136+ <PrecompiledHeader>
137+ </PrecompiledHeader>
138+ <Optimization>MaxSpeed</Optimization>
139+ <FunctionLevelLinking>true</FunctionLevelLinking>
140+ <IntrinsicFunctions>true</IntrinsicFunctions>
141+ <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
142+ </ClCompile>
143+ <Link>
144+ <SubSystem>Console</SubSystem>
145+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
146+ <OptimizeReferences>true</OptimizeReferences>
147+ <GenerateDebugInformation>true</GenerateDebugInformation>
148+ </Link>
149+ </ItemDefinitionGroup>
150+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
151+ <ImportGroup Label="ExtensionTargets">
152+ </ImportGroup>
153+</Project>
xml/example.xmlnew file+23
@@ -0,0 +1,23 @@
1+<?xml version="1.0"?>
2+<catalog>
3+ <book id="bk101">
4+ <author>Gambardella, Matthew</author>
5+ <title>XML Developer's Guide</title>
6+ <genre>Computer</genre>
7+ <price>44.95</price>
8+ <publish_date>2000-10-01</publish_date>
9+ <description>An in-depth look at creating applications
10+ with XML.</description>
11+ </book>
12+ <!-- more books -->
13+ <book id="bk102">
14+ <author>Ralls, Kim</author>
15+ <title>Midnight Rain</title>
16+ <genre>Fantasy</genre>
17+ <price>5.95</price>
18+ <publish_date>2000-12-16</publish_date>
19+ <description>A former architect battles corporate zombies,
20+ an evil sorceress, and her own childhood to become queen
21+ of the world.</description>
22+ </book>
23+</catalog>
xml/simple.xmlnew file+2
@@ -0,0 +1,2 @@
1+<a b="c">d</a>
2+