on 4acde7e help sign in
27 files changed+199−1407
since splitsnapshots
unifiedsplit
.ghci+1−1 .github/workflows/ci.yml−33 .gitignore−5 .travis.yml+10 CHANGES.txt−16 LICENSE+3−190 PULL_REQUEST_TEMPLATE.md−5 README.md+3−9 afl.sh−40 appveyor.yml+18 cbits/fuzz.c−26 cbits/hexml.c+80−87 cbits/hexml.h+14−18 gen/.ghci−3 gen/.ghcid−1 gen/Lib.hs−201 gen/Main.hs−70 gen/Parser.hs−76 gen/test.c−488 ghci.bat+1−2 hexml.cabal+8−9 src/Main.hs+14−25 src/Text/XML/Hexml.hs+41−65 vs/hexml.vcxproj+1−1 cbits/main.c → vs/main.c+5−11 xml/example.xml−23 xml/simple.xml−2
.ghci+1−1
@@ -1,4 +1,4 @@
1−:set -Wunused-binds -Wunused-imports -Worphans
1+:set -fwarn-unused-binds -fwarn-unused-imports
22:set -isrc
33:load Main
44
.github/workflows/ci.ymldeleted−33
@@ -1,33 +0,0 @@
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−5
@@ -1,11 +1,6 @@
11/dist/
22*.o
33*.prof
4−*.suo
5−*.opendb
6−*.db
7−*.user
84/.stack-work/
95/issues/
106/vs/x64/
11−/afl-results/
.travis.ymlnew file+10
@@ -0,0 +1,10 @@
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,19 +1,3 @@
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
193 Initial version
LICENSE+3−190
@@ -1,20 +1,9 @@
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
1+Copyright Neil Mitchell 2016.
132All rights reserved.
143
154Redistribution and use in source and binary forms, with or without
16−modification, are permitted provided that the following conditions
17−are met:
5+modification, are permitted provided that the following conditions are
6+met:
187
198 * Redistributions of source code must retain the above copyright
209 notice, this list of conditions and the following disclaimer.
@@ -39,179 +28,3 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3928THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4029(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4130OF 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.mddeleted−5
@@ -1,5 +0,0 @@
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+3−9
@@ -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/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)
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)
22
3−An XML DOM-style parser, that only parses a subset of XML, but is designed to be fast. In particular:
3+A bad XML DOM-style parser, that only parses a subset of XML, but is designed to be fast. In particular:
44
55* Entities, e.g. `&amp;`, are not expanded.
66* Not all the validity conditions are checked.
@@ -8,10 +8,4 @@ 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](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.
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.
afl.shdeleted−40
@@ -1,40 +0,0 @@
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.ymlnew file+18
@@ -0,0 +1,18 @@
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.cdeleted−26
@@ -1,26 +0,0 @@
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+80−87
@@ -1,5 +1,5 @@
11#include "hexml.h"
2−#include <stdlib.h>
2+#include <malloc.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−static inline int end(str s) { return s.start + s.length; }
13+int static inline end(str s) { return s.start + s.length; }
1414
15−static inline str start_length(int32_t start, int32_t length)
15+str static inline start_length(int32_t start, int32_t length)
1616{
17− assert(start >= 0 && length >= 0);
17+ if (start < 0 || length < 0) assert(0);
1818 str res;
1919 res.start = start;
2020 res.length = length;
2121 return res;
2222}
2323
24−static inline str start_end(int32_t start, int32_t end)
24+str static inline 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− const char* body; // pointer to initial argument, not owned by us
54+ char* body; // pointer to initial argument, not owned by us
5555
5656 // things only used while parsing
57− const char* cursor; // pointer to where we are in body
58− const char* end; // pointer to one past the last char
57+ char* cursor; // pointer to where we are in body
58+ 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−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); }
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); }
6868
6969
7070/////////////////////////////////////////////////////////////////////
@@ -72,45 +72,49 @@ static inline int doc_position(const document* d) { return (int) (d->cursor - d-
7272
7373typedef struct
7474{
75− const document* d;
75+ document* d;
7676 char* buffer;
7777 int length;
7878 int cursor;
7979} render;
8080
81−static inline void render_char(render* r, char c)
81+void static inline 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−static inline void bound(int idx, int mn, int mx)
88+void static inline bound(char* msg, int index, int mn, int mx)
8989{
90− assert(idx >= mn && idx <= mx);
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+ }
9195}
9296
93−static inline void bound_str(str s, int mn, int mx)
97+void static inline bound_str(char* msg, str s, int mn, int mx)
9498{
95− assert(s.length >= 0);
96− bound(s.start, mn, mx);
97− bound(end(s), mn, mx);
99+ if (s.length < 0) assert(0);
100+ bound(msg, s.start, mn, mx);
101+ bound(msg, end(s), mn, mx);
98102}
99103
100−static void render_str(render* r, str s)
104+void render_str(render* r, str s)
101105{
102− bound_str(s, 0, doc_length(r->d));
106+ bound_str("render_str", s, 0, doc_length(r->d));
103107 for (int i = 0; i < s.length; i++)
104108 render_char(r, r->d->body[s.start + i]);
105109}
106110
107−static void render_tag(render* r, const node* n);
111+void render_tag(render* r, node* n);
108112
109−static void render_content(render* r, const node* n)
113+void render_content(render* r, node* n)
110114{
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);
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);
114118
115119 int done = n->inner.start;
116120 for (int i = 0; i < n->nodes.length; i++)
@@ -123,7 +127,7 @@ static void render_content(render* r, const node* n)
123127 render_str(r, start_end(done, end(n->inner)));
124128}
125129
126−static void render_tag(render* r, const node* n)
130+void render_tag(render* r, node* n)
127131{
128132 render_char(r, '<');
129133 render_str(r, n->name);
@@ -145,7 +149,7 @@ static void render_tag(render* r, const node* n)
145149 render_char(r, '>');
146150}
147151
148−int hexml_node_render(const document* d, const node* n, char* buffer, int length)
152+int node_render(document* d, node* n, char* buffer, int length)
149153{
150154 render r;
151155 r.d = d;
@@ -164,10 +168,10 @@ int hexml_node_render(const document* d, const node* n, char* buffer, int length
164168/////////////////////////////////////////////////////////////////////
165169// NOT THE PARSER
166170
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];}
171+char* document_error(document* d){return d->error_message;}
172+node* document_node(document* d){return &d->nodes.nodes[0];}
169173
170−void hexml_document_free(document* d)
174+void document_free(document* d)
171175{
172176 free(d->error_message);
173177 free(d->nodes.alloc);
@@ -175,20 +179,20 @@ void hexml_document_free(document* d)
175179 free(d);
176180}
177181
178−node* hexml_node_children(const document* d, const node* n, int* res)
182+node* node_children(document* d, node* n, int* res)
179183{
180184 *res = n->nodes.length;
181185 return &d->nodes.nodes[n->nodes.start];
182186}
183187
184−attr* hexml_node_attributes(const document* d, const node* n, int* res)
188+attr* node_attributes(document* d, node* n, int* res)
185189{
186190 *res = n->attrs.length;
187191 return &d->attrs.attrs[n->attrs.start];
188192}
189193
190194
191−attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
195+attr* node_attributeBy(document* d, node* n, char* s, int slen)
192196{
193197 if (slen == -1) slen = (int) strlen(s);
194198 const int limit = end(n->attrs);
@@ -202,7 +206,7 @@ attr* hexml_node_attribute(const document* d, const node* n, const char* s, int
202206}
203207
204208// Search for given strings within a node
205−node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
209+node* node_childBy(document* d, node* parent, node* prev, char* s, int slen)
206210{
207211 if (slen == -1) slen = (int) strlen(s);
208212 int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
@@ -220,49 +224,49 @@ node* hexml_node_child(const document* d, const node* parent, const node* prev,
220224/////////////////////////////////////////////////////////////////////
221225// PARSE TABLE
222226
223−static const char tag_name1 = 0x1;
224−static const char tag_name = 0x2;
225−static const char tag_space = 0x4;
227+const char tag_name1 = 0x1;
228+const char tag_name = 0x2;
229+const char tag_space = 0x4;
226230
227−static char parse_table[256];
231+char parse_table[256];
228232
229−static void init_parse_table()
233+void init_parse_table()
230234{
231235 static bool done = 0;
232236 if (done) return;
233237 for (int i = 0; i < 256; i++)
234238 {
235239 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
236− bool name = name1 || i == '-' || i == '.' || (i >= '0' && i <= '9');
240+ bool name = name1 || i == '-' || (i >= '0' && i <= '9');
237241 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
238242 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
239243 }
240244 done = 1;
241245}
242246
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); }
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); }
247251
248252
249253/////////////////////////////////////////////////////////////////////
250254// PARSER COMBINATORS
251255
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; }
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; }
256260
257261// Remove whitespace characters from the cursor while they are still whitespace
258−static inline void trim(document* d)
262+void static inline trim(document* d)
259263{
260264 while (is_space(peek(d)))
261265 skip(d, 1);
262266}
263267
264268// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
265−static bool find(document* d, char c)
269+bool find(document* d, char c)
266270{
267271 char* x = memchr(d->cursor, c, d->end - d->cursor);
268272 if (x == NULL)
@@ -281,13 +285,12 @@ static bool find(document* d, char c)
281285/////////////////////////////////////////////////////////////////////
282286// PARSING CODE
283287
284−static inline void node_alloc(node_buffer* b, int ask)
288+void static inline node_alloc(node_buffer* b, int ask)
285289{
286290 int space = b->size - b->used_back - b->used_front;
287291 if (space >= ask) return;
288292 int size2 = (b->size + 1000 + ask) * 2;
289293 node* buf2 = malloc(size2 * sizeof(node));
290− assert(buf2);
291294 memcpy(buf2, b->nodes, b->used_front * sizeof(node));
292295 memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
293296 free(b->alloc);
@@ -296,13 +299,12 @@ static inline void node_alloc(node_buffer* b, int ask)
296299 b->alloc = buf2;
297300}
298301
299−static inline void attr_alloc(attr_buffer* b, int ask)
302+void static inline attr_alloc(attr_buffer* b, int ask)
300303{
301304 int space = b->size - b->used;
302305 if (space >= ask) return;
303306 int size2 = (b->size + 1000 + ask) * 2;
304307 attr* buf2 = malloc(size2 * sizeof(attr));
305− assert(buf2);
306308 memcpy(buf2, b->attrs, b->used * sizeof(attr));
307309 free(b->alloc);
308310 b->size = size2;
@@ -310,17 +312,16 @@ static inline void attr_alloc(attr_buffer* b, int ask)
310312 b->alloc = buf2;
311313}
312314
313−static void set_error(document* d, const char* msg)
315+void set_error(document* d, char* msg)
314316{
315317 if (d->error_message != NULL) return; // keep the first error message
316318 d->error_message = malloc(strlen(msg)+1);
317− assert(d->error_message);
318319 strcpy(d->error_message, msg);
319320}
320321
321322// you now expect a name, perhaps preceeded by whitespace
322323// the name may be empty
323−static inline str parse_name(document* d)
324+str static inline parse_name(document* d)
324325{
325326 int start = doc_position(d);
326327 if (!is_name1(peek(d)))
@@ -331,15 +332,14 @@ static inline str parse_name(document* d)
331332 return start_end(start, doc_position(d));
332333}
333334
334−static inline str parse_attrval(document* d)
335+str static inline parse_attrval(document* d)
335336{
336337 trim(d);
337− if (peek(d) != '=')
338+ if (get(d) != '=')
338339 {
339340 set_error(d, "Expected = in attribute, but missing");
340341 return start_length(0, 0);
341342 }
342− skip(d, 1);
343343 trim(d);
344344 char c = peek(d);
345345 if (c == '\"' || c == '\'')
@@ -365,10 +365,10 @@ static inline str parse_attrval(document* d)
365365
366366// seen a tag name, now looking for attributes terminated by >
367367// puts the attributes it finds in the attribute buffer
368−static inline str parse_attributes(document* d)
368+str static inline parse_attributes(document* d)
369369{
370370 int start = d->attrs.used;
371− while (d->error_message == NULL)
371+ for (int i = 0; ; i++)
372372 {
373373 trim(d);
374374 str name = parse_name(d);
@@ -381,11 +381,11 @@ static inline str parse_attributes(document* d)
381381 return start_end(start, d->attrs.used);
382382}
383383
384−static str parse_content(document* d);
384+str parse_content(document* d);
385385
386386
387387// Add a new entry into tag, am at a '<'
388−static inline void parse_tag(document* d)
388+void static inline parse_tag(document* d)
389389{
390390 node_alloc(&d->nodes, 1);
391391 d->nodes.used_back++;
@@ -396,18 +396,12 @@ static inline void parse_tag(document* d)
396396 assert(c == '<');
397397 if (peek(d) == '?') skip(d, 1);
398398 me->name = parse_name(d);
399− if (me->name.length == 0)
400− {
401− set_error(d, "Missing tag name");
402− return;
403− }
404399 me->attrs = parse_attributes(d);
405− if (d->error_message != NULL) return;
406400
407− c = peek(d);
408− if ((c == '/' || c == '?') && peek_at(d, 1) == '>')
401+ c = get(d);
402+ if ((c == '/' || c == '?') && peek(d) == '>')
409403 {
410− skip(d, 2);
404+ skip(d, 1);
411405 me->nodes = start_length(0, 0);
412406 me->outer.length = start_end(me->outer.start, doc_position(d)).length;
413407 me->inner = start_length(doc_position(d), 0);
@@ -418,8 +412,6 @@ static inline void parse_tag(document* d)
418412 set_error(d, "Gunk at the end of the tag");
419413 return;
420414 }
421− else
422− skip(d, 1);
423415 me->inner.start = doc_position(d);
424416 str content = parse_content(d);
425417
@@ -429,7 +421,7 @@ static inline void parse_tag(document* d)
429421 me->inner.length = start_end(me->inner.start, doc_position(d)).length;
430422
431423 if (d->error_message != NULL) return;
432− if (peek(d) == '<' && peek_at(d, 1) == '/')
424+ if (peek(d) == '<' && peekAt(d, 1) == '/')
433425 {
434426 skip(d, 2);
435427 if (d->end - d->cursor >= me->name.length &&
@@ -451,7 +443,7 @@ static inline void parse_tag(document* d)
451443
452444// Parser until </, return the index of your node children
453445// Not inline as it is recursive
454−static str parse_content(document* d)
446+str parse_content(document* d)
455447{
456448 int before = d->nodes.used_back;
457449 while (d->error_message == NULL)
@@ -461,22 +453,22 @@ static str parse_content(document* d)
461453 {
462454 break;
463455 }
464− else if (peek_at(d, 1) == '/')
456+ else if (peekAt(d, 1) == '/')
465457 {
466458 // have found a </
467459 break;
468460 }
469− else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
461+ else if (peekAt(d, 1) == '!' && peekAt(d, 2) == '-' && peekAt(d, 3) == '-')
470462 {
471463 skip(d, 3);
472464 // you can't reuse the two '-' characters for the closing as well
473− if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
465+ if (peekAt(d, 0) == '\0' || peekAt(d, 1) == '\0')
474466 {
475467 set_error(d, "Didn't get a closing comment");
476468 return start_end(0, 0);
477469 }
478470 skip(d, 2);
479− for (;;)
471+ while (1)
480472 {
481473 if (!find(d, '>'))
482474 {
@@ -484,7 +476,7 @@ static str parse_content(document* d)
484476 return start_end(0, 0);
485477 }
486478 skip(d, 1);
487− if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
479+ if (peekAt(d, -3) == '-' && peekAt(d, -2))
488480 break;
489481 }
490482 }
@@ -512,14 +504,13 @@ typedef struct
512504 node nodes[500];
513505} buffer;
514506
515−document* hexml_document_parse(const char* s, int slen)
507+document* document_parse(char* s, int slen)
516508{
517509 if (slen == -1) slen = (int) strlen(s);
518510 assert(s[slen] == 0);
519511 init_parse_table();
520512
521513 buffer* buf = malloc(sizeof(buffer));
522− assert(buf);
523514 document* d = &buf->document;
524515 d->body = s;
525516 d->cursor = s;
@@ -539,13 +530,15 @@ document* hexml_document_parse(const char* s, int slen)
539530 d->nodes.nodes[0].outer = start_length(0, slen);
540531 d->nodes.nodes[0].inner = start_length(0, slen);
541532 d->nodes.nodes[0].attrs = start_length(0, 0);
542−
533+
543534 // Introduce an intermediate result, otherwise behaviour is undefined
544535 // because there is no guaranteed ordering between LHS and RHS evaluation
545536 str content = parse_content(d);
546537 d->nodes.nodes[0].nodes = content;
547538
548− if (d->cursor < d->end)
539+ if (d->cursor < d->end && d->error_message == NULL)
540+ {
549541 set_error(d, "Trailing junk at the end of the document");
542+ }
550543 return d;
551544}
cbits/hexml.h+14−18
@@ -21,7 +21,6 @@ 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
2524 str attrs; // all the attributes, in the attribute buffer (not usable)
2625 str nodes; // all the nodes, in the node buffer (not usable)
2726} node;
@@ -29,29 +28,26 @@ typedef struct
2928// Convention: if slen can be -1 for nul-terminated, or given explicitly
3029
3130// Parse a document, returns a potentially invalid document - use document_error to check
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]
34−document* hexml_document_parse(const char* s, int slen);
31+// Must use document_free to release the memory.
32+document* document_parse(char* s, int slen);
3533
3634// Free the memory returned from document_parse.
37−void hexml_document_free(document* d);
35+void document_free(document* d);
3836
39−// Generate a fresh string with the same semantics as the node.
40−// Requires an input buffer, returns the size of the rendered document.
41−// Requires the string passed to document_parse to be valid.
42−int hexml_node_render(const document* d, const node* n, char* buffer, int length);
37+// generate a fresh string with the same semantics as the node
38+// requires an input buffer, returns the size of the rendered document
39+int node_render(document* d, node* n, char* buffer, int length);
4340
44−// Return either NULL (successful parse) or the error message.
45−char* hexml_document_error(const document* d);
41+// return either NULL (successful parse) or the error message
42+char* document_error(document* d);
4643
47−// Return the root node of the document - imagine the document is wrapped in <>$DOC</> tags.
48−node* hexml_document_node(const document* d);
44+// return the root node of the document - imagine the document is wrapped in <>$DOC</> tags
45+node* document_node(document* d);
4946
5047// List all items within a node
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);
48+node* node_children(document* d, node* n, int* res);
49+attr* node_attributes(document* d, node* n, int* res);
5350
5451// Search for given strings within a node, note that prev may be NULL
55−// Requires the string passed to document_parse to be valid.
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);
52+node* node_childBy(document* d, node* parent, node* prev, char* s, int slen);
53+attr* node_attributeBy(document* d, node* n, char* s, int slen);
gen/.ghcideleted−3
@@ -1,3 +0,0 @@
1−:set -fwarn-unused-binds -fwarn-unused-imports -fwarn-orphans
2−:load Main
3−:def test \xs -> return $ ":main" ++ xs
gen/.ghciddeleted−1
@@ -1 +0,0 @@
1−-c ghci --test=:test --reload=test.c
gen/Lib.hsdeleted−201
@@ -1,201 +0,0 @@
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.hsdeleted−70
@@ -1,70 +0,0 @@
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.hsdeleted−76
@@ -1,76 +0,0 @@
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.cdeleted−488
@@ -1,488 +0,0 @@
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+1−2
@@ -1,2 +1 @@
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 %*
1+gcc -O3 -std=c99 -c cbits\hexml.c -o cbits\hexml.o && ghci.exe cbits\hexml.o %*
hexml.cabal+8−9
@@ -1,20 +1,19 @@
11cabal-version: >= 1.18
22build-type: Simple
33name: hexml
4−version: 0.3.4
4+version: 0.1
55license: BSD3
6−x-license: BSD-3-Clause OR Apache-2.0
76license-file: LICENSE
8−category: XML
7+category: Development
98author: Neil Mitchell <ndmitchell@gmail.com>
109maintainer: Neil Mitchell <ndmitchell@gmail.com>
11−copyright: Neil Mitchell 2016-2023
12−synopsis: XML subset DOM parser
10+copyright: Neil Mitchell 2016
11+synopsis: Bad XML parser
1312description:
14− An XML DOM-style parser, that only parses a subset of XML, but is designed to be fast.
13+ A bad XML parser.
1514homepage: https://github.com/ndmitchell/hexml#readme
1615bug-reports: https://github.com/ndmitchell/hexml/issues
17−tested-with: GHC==9.6, GHC==9.4, GHC==9.2, GHC==9.0, GHC==8.10, GHC==8.8
16+tested-with: GHC==8.0.1, GHC==7.10.3, GHC==7.8.4, GHC==7.6.3
1817extra-doc-files:
1918 README.md
2019 CHANGES.txt
@@ -31,9 +30,9 @@ library
3130 default-language: Haskell2010
3231
3332 build-depends:
34− base >= 4.5 && < 5,
33+ base > 4 && < 5,
3534 bytestring,
36− extra >= 0.3
35+ extra
3736
3837 c-sources: cbits/hexml.c
3938 include-dirs: cbits
src/Main.hs+14−25
@@ -7,7 +7,6 @@ import qualified Data.ByteString.Char8 as BS
77import Control.Monad
88import Data.Monoid
99import Data.Char
10−import Prelude
1110
1211
1312examples :: [(Bool, BS.ByteString)]
@@ -19,53 +18,43 @@ examples =
1918 ,(False, "<test></more>")
2019 ,(False, "<test")
2120 ,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
22− ,(True, "<foo bar.baz=\"qux\"></foo>")
2321 ]
2422
2523main :: IO ()
2624main = do
27− forM_ examples $ \(parses, src) ->
25+ forM_ examples $ \(parses, src) -> do
26+ print src
2827 case parse src of
2928 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
3029 Right doc -> do
3130 unless parses $ fail "Unexpected parse success"
32− checkFind doc
3331 let r = render doc
34− r === rerender doc
32+ print r
33+ print $ rerender doc
34+ when (r /= rerender doc) $ fail "Different rerender"
3535 let Right d = parse r
36− r === render d
36+ when (r /= render d) $ fail "Different after rerendering"
3737
38− let Right doc = parse "<test id=\"1\" extra=\"2\" />\n<test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
38+ let Right doc = parse "<test id=\"1\" extra=\"2\" /><test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
3939 map name (children doc) === ["test","test","b","test","test"]
40− location (children doc !! 2) === (2,16)
4140 length (childrenBy doc "test") === 4
4241 length (childrenBy doc "b") === 1
4342 length (childrenBy doc "extra") === 0
4443 attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
4544 map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
4645
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+ Right _ <- return $ parse $ "<test " <> BS.unwords [BS.pack $ "x" ++ show i ++ "='value'" | i <- [1..10000]] <> " />"
47+ Right _ <- return $ parse $ BS.unlines $ replicate 10000 "<test x='value' />"
4948
5049 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"
50+ Right doc <- return $ parse $ "<test " <> BS.unwords [x <> "='" <> x <> "'" | x <- attrs] <> ">middle</test>"
51+ [c] <- return $ childrenBy doc "test"
5352 forM_ attrs $ \a -> attributeBy c a === Just (Attribute a a)
5453 forM_ ["missing","gone","nothing"] $ \a -> attributeBy c a === Nothing
55− putStrLn "\nSuccess"
54+ putStrLn "Done"
5655
5756
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−
68−a === b = if a == b then putChar '.' else fail $ "mismatch, " ++ show a ++ " /= " ++ show b
57+a === b = if a == b then putStrLn "success" else fail "mismatch"
6958
7059rerender :: Node -> BS.ByteString
7160rerender = inside
@@ -76,7 +65,7 @@ rerender = inside
7665 "</" <> name x <> ">"
7766 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
7867
79− validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-.:_" :: String)) x = x
68+ validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-:_" :: String)) x = x
8069 | otherwise = error "Invalid name"
8170 validAttr x | BS.notElem '\"' x = x
8271 | otherwise = error "Invalid attribute"
src/Text/XML/Hexml.hs+41−65
@@ -1,11 +1,11 @@
1−{-# LANGUAGE RecordWildCards, BangPatterns #-}
1+{-# LANGUAGE RecordWildCards #-}
22
33-- | A module for fast first-approximation parsing of XML.
44-- Note that entities, e.g. @&amp;@, are not expanded.
55module Text.XML.Hexml(
66 Node, Attribute(..),
77 parse, render,
8− location, name, inner, outer,
8+ name, inner, outer,
99 attributes, children, contents,
1010 attributeBy, childrenBy
1111 ) where
@@ -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 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)
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)
5454
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)
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)
5757
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)
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)
6060
6161-- | A node in an XML document, created by 'parse', then calling functions such
6262-- as 'children' on that initial 'Node'.
@@ -66,44 +66,36 @@ data Node = Node BS.ByteString (ForeignPtr CDocument) (Ptr CNode)
6666-- @hello=\"world\"@ would produce @Attribute \"hello\" \"world\"@.
6767data Attribute = Attribute
6868 {attributeName :: BS.ByteString
69− ,attributeValue :: BS.ByteString
69+ ,attributeVaule :: BS.ByteString
7070 } deriving (Show, Eq, Ord)
7171
7272instance Show Node where
7373 show d = "Node " ++ show (BS.unpack $ outer d)
7474
7575
76−touchBS :: BS.ByteString -> IO ()
77−touchBS = touchForeignPtr . fst3 . BS.toForeignPtr
78−
79−
8076-- | Parse a ByteString as an XML document, returning a 'Left' error message, or a 'Right' document.
81−-- Note that the returned node will have a 'name' of @\"\"@, no 'attributes', and 'contents' as per the document.
82−-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise an XML node and a single
77+-- Note that the returned node will have a 'name' of @\"\"@, no attributes, and content as per the document.
78+-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise a single
8379-- root element, use @'children' n !! 1@.
8480parse :: BS.ByteString -> Either BS.ByteString Node
85−parse src = do
86− let src0 = src <> BS.singleton '\0'
87− unsafePerformIO $ BS.unsafeUseAsCStringLen src0 $ \(str, len) -> do
88− doc <- hexml_document_parse str (fromIntegral len - 1)
89− err <- hexml_document_error doc
90− if err /= nullPtr then do
91− bs <- BS.packCString =<< hexml_document_error doc
92− hexml_document_free doc
93− pure $ Left bs
94− else do
95− node <- hexml_document_node doc
96− doc <- newForeignPtr hexml_document_free_funptr doc
97− pure $ Right $ Node src0 doc node
81+parse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
82+ doc <- document_parse str (fromIntegral len - 1)
83+ err <- document_error doc
84+ if err /= nullPtr then do
85+ bs <- BS.packCString =<< document_error doc
86+ document_free doc
87+ return $ Left bs
88+ else do
89+ node <- document_node doc
90+ doc <- newForeignPtr document_free_funptr doc
91+ return $ Right $ Node src doc node
9892
9993-- | Given a node, rerender it to something with an equivalent parse tree.
10094-- Mostly useful for debugging - if you want the real source document use 'outer' instead.
10195render :: Node -> BS.ByteString
102−render (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
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
105− touchBS src
106− pure res
96+render (Node _ doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
97+ i <- node_render d n nullPtr 0
98+ BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
10799
108100applyStr :: BS.ByteString -> Str -> BS.ByteString
109101applyStr bs Str{..} = BS.take (fromIntegral strLength) $ BS.drop (fromIntegral strStart) bs
@@ -118,7 +110,7 @@ attrPeek :: BS.ByteString -> ForeignPtr CDocument -> Ptr CAttr -> Attribute
118110attrPeek src doc a = unsafePerformIO $ withForeignPtr doc $ \_ -> do
119111 name <- applyStr src <$> peekElemOff (castPtr a) 0
120112 val <- applyStr src <$> peekElemOff (castPtr a) 1
121− pure $ Attribute name val
113+ return $ Attribute name val
122114
123115-- | Get the name of a node, e.g. @\<test /\>@ produces @\"test\"@.
124116name :: Node -> BS.ByteString
@@ -142,7 +134,7 @@ outer = nodeBS 2
142134contents :: Node -> [Either BS.ByteString Node]
143135contents n@(Node src _ _) = f (strStart inner) outers
144136 where
145− f i [] = string i (strEnd inner)
137+ f i [] = string i (strEnd inner) ++ []
146138 f i ((x, n):xs) = string i (strStart x) ++ Right n : f (strEnd x) xs
147139
148140 string start end | start == end = []
@@ -152,19 +144,19 @@ contents n@(Node src _ _) = f (strStart inner) outers
152144
153145-- | Get the direct child nodes of this node.
154146children :: Node -> [Node]
155−children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d ->
147+children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
156148 alloca $ \count -> do
157− res <- hexml_node_children d n count
149+ res <- node_children d n count
158150 count <- fromIntegral <$> peek count
159− pure [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
151+ return [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
160152
161153-- | Get the attributes of this node.
162154attributes :: Node -> [Attribute]
163−attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d ->
155+attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
164156 alloca $ \count -> do
165− res <- hexml_node_attributes d n count
157+ res <- node_attributes d n count
166158 count <- fromIntegral <$> peek count
167− pure [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
159+ return [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
168160
169161-- | Get the direct children of this node which have a specific name.
170162-- A more efficient version of:
@@ -175,9 +167,8 @@ childrenBy (Node src doc n) str = go nullPtr
175167 where
176168 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
177169 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
178− r <- hexml_node_child d n old bs $ fromIntegral len
179− touchBS src
180− pure $ if r == nullPtr then [] else Node src doc r : go r
170+ r <- node_childBy d n old bs $ fromIntegral len
171+ return $ if r == nullPtr then [] else Node src doc r : go r
181172
182173-- | Get the first attribute of this node which has a specific name, if there is one.
183174-- A more efficient version of:
@@ -186,20 +177,5 @@ childrenBy (Node src doc n) str = go nullPtr
186177attributeBy :: Node -> BS.ByteString -> Maybe Attribute
187178attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
188179 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
189− r <- hexml_node_attribute d n bs $ fromIntegral len
190− touchBS src
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)
180+ r <- node_attributeBy d n bs $ fromIntegral len
181+ return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
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="..\cbits\main.c" />
24+ <ClCompile Include="main.c" />
2525 </ItemGroup>
2626 <PropertyGroup Label="Globals">
2727 <ProjectGuid>{9DA5F6EE-A35E-4ED6-ABC2-A7707392E6EB}</ProjectGuid>
cbits/main.c → vs/main.crenamed+5−11
@@ -12,7 +12,6 @@ char* readFile(char* file)
1212 int len = ftell(f);
1313 fseek(f, 0, SEEK_SET);
1414 char* res = malloc(len + 1);
15− assert(res);
1615 res[len] = 0;
1716 size_t n = fread(res, 1, len, f);
1817 assert(n == len);
@@ -22,25 +21,20 @@ char* readFile(char* file)
2221
2322char* 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 />";
2423
25−int main(int argc, char **argv)
24+void main(int argc, char **argv)
2625{
2726 setbuf(stdout, NULL);
2827 char* body = argv[1] == NULL ? example : readFile(argv[1]);
29− document* doc = hexml_document_parse(body, -1);
30− char* err = hexml_document_error(doc);
28+ document* doc = document_parse(body, -1);
29+ char* err = document_error(doc);
3130 if (err == NULL)
3231 {
33− int len = hexml_node_render(doc, hexml_document_node(doc), NULL, 0);
32+ int len = node_render(doc, document_node(doc), NULL, 0);
3433 char* s = malloc(len + 1);
35− assert(s);
36− hexml_node_render(doc, hexml_document_node(doc), s, len);
34+ node_render(doc, document_node(doc), s, len);
3735 s[len] = 0;
3836 printf("Parse successful\n"); // , %s\n", s);
39− return 0;
4037 }
4138 else
42− {
4339 printf("Parse failed, %s\n", err);
44− return 1;
45− }
4640}
xml/example.xmldeleted−23
@@ -1,23 +0,0 @@
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.xmldeleted−2
@@ -1,2 +0,0 @@
1−<a b="c">d</a>
2−