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