on master help sign in
27 files changed+1337−175
since splitsnapshots
unifiedsplit
.ghci+1−1 .github/workflows/ci.yml+33 .gitignore+1 .travis.yml−10 CHANGES.txt+16 LICENSE+190−3 PULL_REQUEST_TEMPLATE.md+5 README.md+8−2 afl.sh+40 appveyor.yml−18 cbits/fuzz.c+26 cbits/hexml.c+83−80 cbits/hexml.h+10−9 vs/main.c → cbits/main.c+9−5 gen/.ghci+3 gen/.ghcid+1 gen/Lib.hs+201 gen/Main.hs+70 gen/Parser.hs+76 gen/test.c+488 ghci.bat+2−1 hexml.cabal+7−6 src/Main.hs+8−6 src/Text/XML/Hexml.hs+33−33 vs/hexml.vcxproj+1−1 xml/example.xml+23 xml/simple.xml+2
.ghci+1−1
@@ -1,4 +1,4 @@
1−:set -fwarn-unused-binds -fwarn-unused-imports
1+:set -Wunused-binds -Wunused-imports -Worphans
22:set -isrc
33:load Main
44
.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+1
@@ -8,3 +8,4 @@
88/.stack-work/
99/issues/
1010/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 @@
11Changelog for Hexml
22
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
319 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
213All rights reserved.
314
415Redistribution and use in source and binary forms, with or without
5−modification, are permitted provided that the following conditions are
6−met:
16+modification, are permitted provided that the following conditions
17+are met:
718
819 * Redistributions of source code must retain the above copyright
920 notice, this list of conditions and the following disclaimer.
@@ -28,3 +39,179 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2839THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2940(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3041OF 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+8−2
@@ -1,4 +1,4 @@
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)
22
33An XML DOM-style parser, that only parses a subset of XML, but is designed to be fast. In particular:
44
@@ -8,4 +8,10 @@ An XML DOM-style parser, that only parses a subset of XML, but is designed to be
88
99The 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.
1010
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+83−80
@@ -1,5 +1,5 @@
11#include "hexml.h"
2−#include <malloc.h>
2+#include <stdlib.h>
33#include <string.h>
44#include <assert.h>
55#include <ctype.h>
@@ -10,18 +10,18 @@ typedef int bool;
1010/////////////////////////////////////////////////////////////////////
1111// TYPES
1212
13−int static inline end(str s) { return s.start + s.length; }
13+static inline int end(str s) { return s.start + s.length; }
1414
15−str static inline start_length(int32_t start, int32_t length)
15+static inline str start_length(int32_t start, int32_t length)
1616{
17− if (start < 0 || length < 0) assert(0);
17+ assert(start >= 0 && length >= 0);
1818 str res;
1919 res.start = start;
2020 res.length = length;
2121 return res;
2222}
2323
24−str static inline start_end(int32_t start, int32_t end)
24+static inline str start_end(int32_t start, int32_t end)
2525{
2626 return start_length(start, end - start);
2727}
@@ -40,7 +40,7 @@ typedef struct
4040 // have a cursor at the end, which is stack scoped, so children write out, then I do
4141 // when you commit, you copy over from end to front
4242
43− // nodes
43+ // nodes
4444 int size;
4545 int used_front; // front entries, stored for good
4646 int used_back; // back entries, stack based, copied into front
@@ -51,11 +51,11 @@ typedef struct
5151
5252struct document
5353{
54− char* body; // pointer to initial argument, not owned by us
54+ const char* body; // pointer to initial argument, not owned by us
5555
5656 // things only used while parsing
57− char* cursor; // pointer to where we are in body
58− char* end; // pointer to one past the last char
57+ const char* cursor; // pointer to where we are in body
58+ const char* end; // pointer to one past the last char
5959 // if cursor is > end we have gone past the end
6060
6161 char* error_message;
@@ -63,8 +63,8 @@ struct document
6363 attr_buffer attrs;
6464};
6565
66−int static inline doc_length(document* d) { return (int) (d->end - d->body); }
67−int static inline doc_position(document* d) { return (int) (d->cursor - d->body); }
66+static inline int doc_length(const document* d) { return (int) (d->end - d->body); }
67+static inline int doc_position(const document* d) { return (int) (d->cursor - d->body); }
6868
6969
7070/////////////////////////////////////////////////////////////////////
@@ -72,49 +72,45 @@ int static inline doc_position(document* d) { return (int) (d->cursor - d->body)
7272
7373typedef struct
7474{
75− document* d;
75+ const document* d;
7676 char* buffer;
7777 int length;
7878 int cursor;
7979} render;
8080
81−void static inline render_char(render* r, char c)
81+static inline void render_char(render* r, char c)
8282{
8383 if (r->cursor < r->length)
8484 r->buffer[r->cursor] = c;
8585 r->cursor++;
8686}
8787
88−void static inline bound(char* msg, int index, int mn, int mx)
88+static inline void bound(int idx, int mn, int mx)
8989{
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);
94− }
90+ assert(idx >= mn && idx <= mx);
9591}
9692
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)
9894{
99− if (s.length < 0) assert(0);
100− bound(msg, s.start, mn, mx);
101− bound(msg, end(s), mn, mx);
95+ assert(s.length >= 0);
96+ bound(s.start, mn, mx);
97+ bound(end(s), mn, mx);
10298}
10399
104−void render_str(render* r, str s)
100+static void render_str(render* r, str s)
105101{
106− bound_str("render_str", s, 0, doc_length(r->d));
102+ bound_str(s, 0, doc_length(r->d));
107103 for (int i = 0; i < s.length; i++)
108104 render_char(r, r->d->body[s.start + i]);
109105}
110106
111−void render_tag(render* r, node* n);
107+static void render_tag(render* r, const node* n);
112108
113−void render_content(render* r, node* n)
109+static void render_content(render* r, const node* n)
114110{
115− bound_str("render_conent inner", n->inner, 0, doc_length(r->d));
116− bound_str("render_conent nodes", n->nodes, 0, r->d->nodes.used_front);
117− bound_str("render_conent attrs", n->attrs, 0, r->d->attrs.used);
111+ bound_str(n->inner, 0, doc_length(r->d));
112+ bound_str(n->nodes, 0, r->d->nodes.used_front);
113+ bound_str(n->attrs, 0, r->d->attrs.used);
118114
119115 int done = n->inner.start;
120116 for (int i = 0; i < n->nodes.length; i++)
@@ -127,7 +123,7 @@ void render_content(render* r, node* n)
127123 render_str(r, start_end(done, end(n->inner)));
128124}
129125
130−void render_tag(render* r, node* n)
126+static void render_tag(render* r, const node* n)
131127{
132128 render_char(r, '<');
133129 render_str(r, n->name);
@@ -149,7 +145,7 @@ void render_tag(render* r, node* n)
149145 render_char(r, '>');
150146}
151147
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)
153149{
154150 render r;
155151 r.d = d;
@@ -168,10 +164,10 @@ int node_render(document* d, node* n, char* buffer, int length)
168164/////////////////////////////////////////////////////////////////////
169165// NOT THE PARSER
170166
171−char* document_error(document* d){return d->error_message;}
172−node* document_node(document* d){return &d->nodes.nodes[0];}
167+char* hexml_document_error(const document* d){return d->error_message;}
168+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
173169
174−void document_free(document* d)
170+void hexml_document_free(document* d)
175171{
176172 free(d->error_message);
177173 free(d->nodes.alloc);
@@ -179,20 +175,20 @@ void document_free(document* d)
179175 free(d);
180176}
181177
182−node* node_children(document* d, node* n, int* res)
178+node* hexml_node_children(const document* d, const node* n, int* res)
183179{
184180 *res = n->nodes.length;
185181 return &d->nodes.nodes[n->nodes.start];
186182}
187183
188−attr* node_attributes(document* d, node* n, int* res)
184+attr* hexml_node_attributes(const document* d, const node* n, int* res)
189185{
190186 *res = n->attrs.length;
191187 return &d->attrs.attrs[n->attrs.start];
192188}
193189
194190
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)
196192{
197193 if (slen == -1) slen = (int) strlen(s);
198194 const int limit = end(n->attrs);
@@ -206,7 +202,7 @@ attr* node_attributeBy(document* d, node* n, char* s, int slen)
206202}
207203
208204// Search for given strings within a node
209−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)
210206{
211207 if (slen == -1) slen = (int) strlen(s);
212208 int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
@@ -224,49 +220,49 @@ node* node_childBy(document* d, node* parent, node* prev, char* s, int slen)
224220/////////////////////////////////////////////////////////////////////
225221// PARSE TABLE
226222
227−const char tag_name1 = 0x1;
228−const char tag_name = 0x2;
229−const char tag_space = 0x4;
223+static const char tag_name1 = 0x1;
224+static const char tag_name = 0x2;
225+static const char tag_space = 0x4;
230226
231−char parse_table[256];
227+static char parse_table[256];
232228
233−void init_parse_table()
229+static void init_parse_table()
234230{
235231 static bool done = 0;
236232 if (done) return;
237233 for (int i = 0; i < 256; i++)
238234 {
239235 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
240− bool name = name1 || i == '-' || (i >= '0' && i <= '9');
236+ bool name = name1 || i == '-' || i == '.' || (i >= '0' && i <= '9');
241237 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
242238 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
243239 }
244240 done = 1;
245241}
246242
247−bool static inline is(char c, char tag) { return parse_table[c] & tag; }
248−bool static inline is_name1(char c) { return is(c, tag_name1); }
249−bool static inline is_name(char c) { return is(c, tag_name); }
250−bool static inline is_space(char c) { return is(c, tag_space); }
243+static inline bool is(char c, char tag) { return parse_table[(unsigned char) c] & tag; }
244+static inline bool is_name1(char c) { return is(c, tag_name1); }
245+static inline bool is_name(char c) { return is(c, tag_name); }
246+static inline bool is_space(char c) { return is(c, tag_space); }
251247
252248
253249/////////////////////////////////////////////////////////////////////
254250// PARSER COMBINATORS
255251
256−char static inline peekAt(document* d, int i) { return d->cursor[i]; }
257−void static inline skip(document* d, int i) { d->cursor += i; }
258−char static inline peek(document* d) { return peekAt(d, 0); }
259−char static inline get(document* d) { char c = peek(d); skip(d, 1); return c; }
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); }
254+static inline void skip(document* d, int i) { d->cursor += i; }
255+static inline char get(document* d) { char c = peek(d); skip(d, 1); return c; }
260256
261257// Remove whitespace characters from the cursor while they are still whitespace
262−void static inline trim(document* d)
258+static inline void trim(document* d)
263259{
264260 while (is_space(peek(d)))
265261 skip(d, 1);
266262}
267263
268264// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
269−bool find(document* d, char c)
265+static bool find(document* d, char c)
270266{
271267 char* x = memchr(d->cursor, c, d->end - d->cursor);
272268 if (x == NULL)
@@ -285,7 +281,7 @@ bool find(document* d, char c)
285281/////////////////////////////////////////////////////////////////////
286282// PARSING CODE
287283
288−void static inline node_alloc(node_buffer* b, int ask)
284+static inline void node_alloc(node_buffer* b, int ask)
289285{
290286 int space = b->size - b->used_back - b->used_front;
291287 if (space >= ask) return;
@@ -300,7 +296,7 @@ void static inline node_alloc(node_buffer* b, int ask)
300296 b->alloc = buf2;
301297}
302298
303−void static inline attr_alloc(attr_buffer* b, int ask)
299+static inline void attr_alloc(attr_buffer* b, int ask)
304300{
305301 int space = b->size - b->used;
306302 if (space >= ask) return;
@@ -314,7 +310,7 @@ void static inline attr_alloc(attr_buffer* b, int ask)
314310 b->alloc = buf2;
315311}
316312
317−void set_error(document* d, char* msg)
313+static void set_error(document* d, const char* msg)
318314{
319315 if (d->error_message != NULL) return; // keep the first error message
320316 d->error_message = malloc(strlen(msg)+1);
@@ -324,7 +320,7 @@ void set_error(document* d, char* msg)
324320
325321// you now expect a name, perhaps preceeded by whitespace
326322// the name may be empty
327−str static inline parse_name(document* d)
323+static inline str parse_name(document* d)
328324{
329325 int start = doc_position(d);
330326 if (!is_name1(peek(d)))
@@ -335,14 +331,15 @@ str static inline parse_name(document* d)
335331 return start_end(start, doc_position(d));
336332}
337333
338−str static inline parse_attrval(document* d)
334+static inline str parse_attrval(document* d)
339335{
340336 trim(d);
341− if (get(d) != '=')
337+ if (peek(d) != '=')
342338 {
343339 set_error(d, "Expected = in attribute, but missing");
344340 return start_length(0, 0);
345341 }
342+ skip(d, 1);
346343 trim(d);
347344 char c = peek(d);
348345 if (c == '\"' || c == '\'')
@@ -368,10 +365,10 @@ str static inline parse_attrval(document* d)
368365
369366// seen a tag name, now looking for attributes terminated by >
370367// puts the attributes it finds in the attribute buffer
371−str static inline parse_attributes(document* d)
368+static inline str parse_attributes(document* d)
372369{
373370 int start = d->attrs.used;
374− for (int i = 0; ; i++)
371+ while (d->error_message == NULL)
375372 {
376373 trim(d);
377374 str name = parse_name(d);
@@ -384,11 +381,11 @@ str static inline parse_attributes(document* d)
384381 return start_end(start, d->attrs.used);
385382}
386383
387−str parse_content(document* d);
384+static str parse_content(document* d);
388385
389386
390387// Add a new entry into tag, am at a '<'
391−void static inline parse_tag(document* d)
388+static inline void parse_tag(document* d)
392389{
393390 node_alloc(&d->nodes, 1);
394391 d->nodes.used_back++;
@@ -399,12 +396,18 @@ void static inline parse_tag(document* d)
399396 assert(c == '<');
400397 if (peek(d) == '?') skip(d, 1);
401398 me->name = parse_name(d);
399+ if (me->name.length == 0)
400+ {
401+ set_error(d, "Missing tag name");
402+ return;
403+ }
402404 me->attrs = parse_attributes(d);
405+ if (d->error_message != NULL) return;
403406
404− c = get(d);
405− if ((c == '/' || c == '?') && peek(d) == '>')
407+ c = peek(d);
408+ if ((c == '/' || c == '?') && peek_at(d, 1) == '>')
406409 {
407− skip(d, 1);
410+ skip(d, 2);
408411 me->nodes = start_length(0, 0);
409412 me->outer.length = start_end(me->outer.start, doc_position(d)).length;
410413 me->inner = start_length(doc_position(d), 0);
@@ -415,6 +418,8 @@ void static inline parse_tag(document* d)
415418 set_error(d, "Gunk at the end of the tag");
416419 return;
417420 }
421+ else
422+ skip(d, 1);
418423 me->inner.start = doc_position(d);
419424 str content = parse_content(d);
420425
@@ -424,7 +429,7 @@ void static inline parse_tag(document* d)
424429 me->inner.length = start_end(me->inner.start, doc_position(d)).length;
425430
426431 if (d->error_message != NULL) return;
427− if (peek(d) == '<' && peekAt(d, 1) == '/')
432+ if (peek(d) == '<' && peek_at(d, 1) == '/')
428433 {
429434 skip(d, 2);
430435 if (d->end - d->cursor >= me->name.length &&
@@ -446,7 +451,7 @@ void static inline parse_tag(document* d)
446451
447452// Parser until </, return the index of your node children
448453// Not inline as it is recursive
449−str parse_content(document* d)
454+static str parse_content(document* d)
450455{
451456 int before = d->nodes.used_back;
452457 while (d->error_message == NULL)
@@ -456,22 +461,22 @@ str parse_content(document* d)
456461 {
457462 break;
458463 }
459− else if (peekAt(d, 1) == '/')
464+ else if (peek_at(d, 1) == '/')
460465 {
461466 // have found a </
462467 break;
463468 }
464− 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) == '-')
465470 {
466471 skip(d, 3);
467472 // you can't reuse the two '-' characters for the closing as well
468− if (peekAt(d, 0) == '\0' || peekAt(d, 1) == '\0')
473+ if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
469474 {
470475 set_error(d, "Didn't get a closing comment");
471476 return start_end(0, 0);
472477 }
473478 skip(d, 2);
474− while (1)
479+ for (;;)
475480 {
476481 if (!find(d, '>'))
477482 {
@@ -479,7 +484,7 @@ str parse_content(document* d)
479484 return start_end(0, 0);
480485 }
481486 skip(d, 1);
482− if (peekAt(d, -3) == '-' && peekAt(d, -2))
487+ if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
483488 break;
484489 }
485490 }
@@ -507,7 +512,7 @@ typedef struct
507512 node nodes[500];
508513} buffer;
509514
510−document* document_parse(char* s, int slen)
515+document* hexml_document_parse(const char* s, int slen)
511516{
512517 if (slen == -1) slen = (int) strlen(s);
513518 assert(s[slen] == 0);
@@ -534,15 +539,13 @@ document* document_parse(char* s, int slen)
534539 d->nodes.nodes[0].outer = start_length(0, slen);
535540 d->nodes.nodes[0].inner = start_length(0, slen);
536541 d->nodes.nodes[0].attrs = start_length(0, 0);
537−
542+
538543 // Introduce an intermediate result, otherwise behaviour is undefined
539544 // because there is no guaranteed ordering between LHS and RHS evaluation
540545 str content = parse_content(d);
541546 d->nodes.nodes[0].nodes = content;
542547
543− if (d->cursor < d->end && d->error_message == NULL)
544− {
548+ if (d->cursor < d->end)
545549 set_error(d, "Trailing junk at the end of the document");
546− }
547550 return d;
548551}
cbits/hexml.h+10−9
@@ -21,6 +21,7 @@ typedef struct
2121 str name; // tag name, e.g. <[foo]>
2222 str inner; // inner text, <foo>[bar]</foo>
2323 str outer; // outer text, [<foo>bar</foo>]
24+ // Not usable, but required to get the correct sizing when returning arrays of nodes
2425 str attrs; // all the attributes, in the attribute buffer (not usable)
2526 str nodes; // all the nodes, in the node buffer (not usable)
2627} node;
@@ -30,27 +31,27 @@ typedef struct
3031// Parse a document, returns a potentially invalid document - use document_error to check
3132// Must use document_free to release the memory (even if invalid).
3233// The string must be nul terminated, e.g. slen != -1 ==> slen[0]
33−document* document_parse(char* s, int slen);
34+document* hexml_document_parse(const char* s, int slen);
3435
3536// Free the memory returned from document_parse.
36−void document_free(document* d);
37+void hexml_document_free(document* d);
3738
3839// Generate a fresh string with the same semantics as the node.
3940// Requires an input buffer, returns the size of the rendered document.
4041// Requires the string passed to document_parse to be valid.
41−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);
4243
4344// Return either NULL (successful parse) or the error message.
44−char* document_error(document* d);
45+char* hexml_document_error(const document* d);
4546
4647// Return the root node of the document - imagine the document is wrapped in <>$DOC</> tags.
47−node* document_node(document* d);
48+node* hexml_document_node(const document* d);
4849
4950// List all items within a node
50−node* node_children(document* d, node* n, int* res);
51−attr* node_attributes(document* d, node* n, int* res);
51+node* hexml_node_children(const document* d, const node* n, int* res);
52+attr* hexml_node_attributes(const document* d, const node* n, int* res);
5253
5354// Search for given strings within a node, note that prev may be NULL
5455// Requires the string passed to document_parse to be valid.
55−node* node_childBy(document* d, node* parent, node* prev, char* s, int slen);
56−attr* node_attributeBy(document* d, node* n, char* s, int slen);
56+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen);
57+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen);
vs/main.c → cbits/main.crenamed+9−5
@@ -22,21 +22,25 @@ char* readFile(char* file)
2222
2323char* 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 />";
2424
25−void main(int argc, char **argv)
25+int main(int argc, char **argv)
2626{
2727 setbuf(stdout, NULL);
2828 char* body = argv[1] == NULL ? example : readFile(argv[1]);
29− document* doc = document_parse(body, -1);
30− char* err = document_error(doc);
29+ document* doc = hexml_document_parse(body, -1);
30+ char* err = hexml_document_error(doc);
3131 if (err == NULL)
3232 {
33− int len = node_render(doc, document_node(doc), NULL, 0);
33+ int len = hexml_node_render(doc, hexml_document_node(doc), NULL, 0);
3434 char* s = malloc(len + 1);
3535 assert(s);
36− node_render(doc, document_node(doc), s, len);
36+ hexml_node_render(doc, hexml_document_node(doc), s, len);
3737 s[len] = 0;
3838 printf("Parse successful\n"); // , %s\n", s);
39+ return 0;
3940 }
4041 else
42+ {
4143 printf("Parse failed, %s\n", err);
44+ return 1;
45+ }
4246}
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 %*
hexml.cabal+7−6
@@ -1,19 +1,20 @@
11cabal-version: >= 1.18
22build-type: Simple
33name: hexml
4−version: 0.1
4+version: 0.3.4
55license: BSD3
6+x-license: BSD-3-Clause OR Apache-2.0
67license-file: LICENSE
7−category: Development
8+category: XML
89author: Neil Mitchell <ndmitchell@gmail.com>
910maintainer: Neil Mitchell <ndmitchell@gmail.com>
10−copyright: Neil Mitchell 2016
11+copyright: Neil Mitchell 2016-2023
1112synopsis: XML subset DOM parser
1213description:
1314 An XML DOM-style parser, that only parses a subset of XML, but is designed to be fast.
1415homepage: https://github.com/ndmitchell/hexml#readme
1516bug-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
1718extra-doc-files:
1819 README.md
1920 CHANGES.txt
@@ -30,9 +31,9 @@ library
3031 default-language: Haskell2010
3132
3233 build-depends:
33− base > 4 && < 5,
34+ base >= 4.5 && < 5,
3435 bytestring,
35− extra
36+ extra >= 0.3
3637
3738 c-sources: cbits/hexml.c
3839 include-dirs: cbits
src/Main.hs+8−6
@@ -7,6 +7,7 @@ import qualified Data.ByteString.Char8 as BS
77import Control.Monad
88import Data.Monoid
99import Data.Char
10+import Prelude
1011
1112
1213examples :: [(Bool, BS.ByteString)]
@@ -18,11 +19,12 @@ examples =
1819 ,(False, "<test></more>")
1920 ,(False, "<test")
2021 ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
22+ ,(True, "<foo bar.baz=\"qux\"></foo>")
2123 ]
2224
2325main :: IO ()
2426main = do
25− forM_ examples $ \(parses, src) -> do
27+ forM_ examples $ \(parses, src) ->
2628 case parse src of
2729 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
2830 Right doc -> do
@@ -42,12 +44,12 @@ main = do
4244 attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
4345 map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
4446
45− Right _ <- return $ parse $ "<test " <> BS.unwords [BS.pack $ "x" ++ show i ++ "='value'" | i <- [1..10000]] <> " />"
46− Right _ <- return $ parse $ BS.unlines $ replicate 10000 "<test x='value' />"
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' />"
4749
4850 let attrs = ["usd:jpy","test","extra","more","stuff","jpy:usd","xxx","xxxx"]
49− Right doc <- return $ parse $ "<test " <> BS.unwords [x <> "='" <> x <> "'" | x <- attrs] <> ">middle</test>"
50− [c] <- return $ childrenBy doc "test"
51+ Right doc <- pure $ parse $ "<test " <> BS.unwords [x <> "='" <> x <> "'" | x <- attrs] <> ">middle</test>"
52+ [c] <- pure $ childrenBy doc "test"
5153 forM_ attrs $ \a -> attributeBy c a === Just (Attribute a a)
5254 forM_ ["missing","gone","nothing"] $ \a -> attributeBy c a === Nothing
5355 putStrLn "\nSuccess"
@@ -74,7 +76,7 @@ rerender = inside
7476 "</" <> name x <> ">"
7577 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
7678
77− 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
7880 | otherwise = error "Invalid name"
7981 validAttr x | BS.notElem '\"' x = x
8082 | otherwise = error "Invalid attribute"
src/Text/XML/Hexml.hs+33−33
@@ -45,18 +45,18 @@ instance Storable Str where
4545 peek p = Str <$> peekByteOff p 0 <*> peekByteOff p 4
4646 poke p Str{..} = pokeByteOff p 0 strStart >> pokeByteOff p 4 strLength
4747
48−foreign import ccall document_parse :: CString -> CInt -> IO (Ptr CDocument)
49−foreign import ccall document_free :: Ptr CDocument -> IO ()
50−foreign import ccall "&document_free" document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
51−foreign import ccall node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
52−foreign import ccall document_error :: Ptr CDocument -> IO CString
53−foreign import ccall document_node :: Ptr CDocument -> IO (Ptr CNode)
48+foreign import ccall hexml_document_parse :: CString -> CInt -> IO (Ptr CDocument)
49+foreign import ccall hexml_document_free :: Ptr CDocument -> IO ()
50+foreign import ccall "&hexml_document_free" hexml_document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
51+foreign import ccall hexml_node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
52+foreign import ccall unsafe hexml_document_error :: Ptr CDocument -> IO CString
53+foreign import ccall unsafe hexml_document_node :: Ptr CDocument -> IO (Ptr CNode)
5454
55−foreign import ccall 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)
55+foreign import ccall unsafe hexml_node_children :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CNode)
56+foreign import ccall unsafe hexml_node_attributes :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CAttr)
5757
58−foreign import ccall node_childBy :: 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)
58+foreign import ccall unsafe hexml_node_child :: Ptr CDocument -> Ptr CNode -> Ptr CNode -> CString -> CInt -> IO (Ptr CNode)
59+foreign import ccall unsafe hexml_node_attribute :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO (Ptr CAttr)
6060
6161-- | A node in an XML document, created by 'parse', then calling functions such
6262-- as 'children' on that initial 'Node'.
@@ -85,25 +85,25 @@ parse :: BS.ByteString -> Either BS.ByteString Node
8585parse src = do
8686 let src0 = src <> BS.singleton '\0'
8787 unsafePerformIO $ BS.unsafeUseAsCStringLen src0 $ \(str, len) -> do
88− doc <- document_parse str (fromIntegral len - 1)
89− err <- document_error doc
88+ doc <- hexml_document_parse str (fromIntegral len - 1)
89+ err <- hexml_document_error doc
9090 if err /= nullPtr then do
91− bs <- BS.packCString =<< document_error doc
92− document_free doc
93− return $ Left bs
91+ bs <- BS.packCString =<< hexml_document_error doc
92+ hexml_document_free doc
93+ pure $ Left bs
9494 else do
95− node <- document_node doc
96− doc <- newForeignPtr document_free_funptr doc
97− return $ Right $ Node src0 doc node
95+ node <- hexml_document_node doc
96+ doc <- newForeignPtr hexml_document_free_funptr doc
97+ pure $ Right $ Node src0 doc node
9898
9999-- | Given a node, rerender it to something with an equivalent parse tree.
100100-- Mostly useful for debugging - if you want the real source document use 'outer' instead.
101101render :: Node -> BS.ByteString
102102render (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
103− i <- node_render d n nullPtr 0
104− res <- BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
103+ i <- hexml_node_render d n nullPtr 0
104+ res <- BS.create (fromIntegral i) $ \ptr -> void $ hexml_node_render d n (castPtr ptr) i
105105 touchBS src
106− return res
106+ pure res
107107
108108applyStr :: BS.ByteString -> Str -> BS.ByteString
109109applyStr bs Str{..} = BS.take (fromIntegral strLength) $ BS.drop (fromIntegral strStart) bs
@@ -118,7 +118,7 @@ attrPeek :: BS.ByteString -> ForeignPtr CDocument -> Ptr CAttr -> Attribute
118118attrPeek src doc a = unsafePerformIO $ withForeignPtr doc $ \_ -> do
119119 name <- applyStr src <$> peekElemOff (castPtr a) 0
120120 val <- applyStr src <$> peekElemOff (castPtr a) 1
121− return $ Attribute name val
121+ pure $ Attribute name val
122122
123123-- | Get the name of a node, e.g. @\<test /\>@ produces @\"test\"@.
124124name :: Node -> BS.ByteString
@@ -142,7 +142,7 @@ outer = nodeBS 2
142142contents :: Node -> [Either BS.ByteString Node]
143143contents n@(Node src _ _) = f (strStart inner) outers
144144 where
145− f i [] = string i (strEnd inner) ++ []
145+ f i [] = string i (strEnd inner)
146146 f i ((x, n):xs) = string i (strStart x) ++ Right n : f (strEnd x) xs
147147
148148 string start end | start == end = []
@@ -152,19 +152,19 @@ contents n@(Node src _ _) = f (strStart inner) outers
152152
153153-- | Get the direct child nodes of this node.
154154children :: Node -> [Node]
155−children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
155+children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d ->
156156 alloca $ \count -> do
157− res <- node_children d n count
157+ res <- hexml_node_children d n count
158158 count <- fromIntegral <$> peek count
159− 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]]
160160
161161-- | Get the attributes of this node.
162162attributes :: Node -> [Attribute]
163−attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
163+attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d ->
164164 alloca $ \count -> do
165− res <- node_attributes d n count
165+ res <- hexml_node_attributes d n count
166166 count <- fromIntegral <$> peek count
167− 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]]
168168
169169-- | Get the direct children of this node which have a specific name.
170170-- A more efficient version of:
@@ -175,9 +175,9 @@ childrenBy (Node src doc n) str = go nullPtr
175175 where
176176 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
177177 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
178− r <- node_childBy d n old bs $ fromIntegral len
178+ r <- hexml_node_child d n old bs $ fromIntegral len
179179 touchBS src
180− 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
181181
182182-- | Get the first attribute of this node which has a specific name, if there is one.
183183-- A more efficient version of:
@@ -186,9 +186,9 @@ childrenBy (Node src doc n) str = go nullPtr
186186attributeBy :: Node -> BS.ByteString -> Maybe Attribute
187187attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
188188 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
189− r <- node_attributeBy d n bs $ fromIntegral len
189+ r <- hexml_node_attribute d n bs $ fromIntegral len
190190 touchBS src
191− 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
192192
193193-- | Find the starting location of a node, the @<@ character.
194194-- The first character will be reported as @(line 1,column 1)@, because thats
vs/hexml.vcxproj+1−1
@@ -21,7 +21,7 @@
2121 <ItemGroup>
2222 <ClCompile Include="..\cbits\hexml.c" />
2323 <ClInclude Include="..\cbits\hexml.h" />
24− <ClCompile Include="main.c" />
24+ <ClCompile Include="..\cbits\main.c" />
2525 </ItemGroup>
2626 <PropertyGroup Label="Globals">
2727 <ProjectGuid>{9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}</ProjectGuid>
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+