UNPKG

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