1 | # llparse
|
2 | [![Build Status](https://secure.travis-ci.org/nodejs/llparse.svg)](http://travis-ci.org/nodejs/llparse)
|
3 | [![NPM version](https://badge.fury.io/js/llparse.svg)](https://badge.fury.io/js/llparse)
|
4 |
|
5 | An API for compiling an incremental parser into C output and/or
|
6 | [LLVM bitcode][3].
|
7 |
|
8 | ## Usage
|
9 |
|
10 | ```ts
|
11 | import { LLParse } from 'llparse';
|
12 |
|
13 | const p = new LLParse('http_parser');
|
14 |
|
15 | const method = p.node('method');
|
16 | const beforeUrl = p.node('before_url');
|
17 | const urlSpan = p.span(p.code.span('on_url'));
|
18 | const url = p.node('url');
|
19 | const http = p.node('http');
|
20 |
|
21 | // Add custom uint8_t property to the state
|
22 | p.property('i8', 'method');
|
23 |
|
24 | // Store method inside a custom property
|
25 | const onMethod = p.invoke(p.code.store('method'), beforeUrl);
|
26 |
|
27 | // Invoke custom C function
|
28 | const complete = p.invoke(p.code.match('on_complete'), {
|
29 | // Restart
|
30 | 0: method
|
31 | }, p.error(4, '`on_complete` error'));
|
32 |
|
33 | method
|
34 | .select({
|
35 | 'HEAD': 0, 'GET': 1, 'POST': 2, 'PUT': 3,
|
36 | 'DELETE': 4, 'OPTIONS': 5, 'CONNECT': 6,
|
37 | 'TRACE': 7, 'PATCH': 8
|
38 | }, onMethod)
|
39 | .otherwise(p.error(5, 'Expected method'));
|
40 |
|
41 | beforeUrl
|
42 | .match(' ', beforeUrl)
|
43 | .otherwise(urlSpan.start(url));
|
44 |
|
45 | url
|
46 | .peek(' ', urlSpan.end(http))
|
47 | .skipTo(url);
|
48 |
|
49 | http
|
50 | .match(' HTTP/1.1\r\n\r\n', complete)
|
51 | .otherwise(p.error(6, 'Expected HTTP/1.1 and two newlines'));
|
52 |
|
53 | const artifacts = p.build(method);
|
54 | console.log('----- BITCODE -----');
|
55 | console.log(artifacts.bitcode); // buffer
|
56 | console.log('----- BITCODE END -----');
|
57 | console.log('----- HEADER -----');
|
58 | console.log(artifacts.header);
|
59 | console.log('----- HEADER END -----');
|
60 | ```
|
61 |
|
62 | #### LICENSE
|
63 |
|
64 | This software is licensed under the MIT License.
|
65 |
|
66 | Copyright Fedor Indutny, 2018.
|
67 |
|
68 | Permission is hereby granted, free of charge, to any person obtaining a
|
69 | copy of this software and associated documentation files (the
|
70 | "Software"), to deal in the Software without restriction, including
|
71 | without limitation the rights to use, copy, modify, merge, publish,
|
72 | distribute, sublicense, and/or sell copies of the Software, and to permit
|
73 | persons to whom the Software is furnished to do so, subject to the
|
74 | following conditions:
|
75 |
|
76 | The above copyright notice and this permission notice shall be included
|
77 | in all copies or substantial portions of the Software.
|
78 |
|
79 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
80 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
81 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
82 | NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
83 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
84 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
85 | USE OR OTHER DEALINGS IN THE SOFTWARE.
|
86 |
|
87 | [3]: https://llvm.org/docs/LangRef.html
|