UNPKG

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