llparse 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- llparse-0.1.0/LICENSE +21 -0
- llparse-0.1.0/PKG-INFO +129 -0
- llparse-0.1.0/README.md +119 -0
- llparse-0.1.0/llparse/C_compiler.py +204 -0
- llparse-0.1.0/llparse/__init__.py +2 -0
- llparse-0.1.0/llparse/compilator.py +1190 -0
- llparse-0.1.0/llparse/constants.py +48 -0
- llparse-0.1.0/llparse/cython_builder.py +311 -0
- llparse-0.1.0/llparse/debug.py +23 -0
- llparse-0.1.0/llparse/dot.py +213 -0
- llparse-0.1.0/llparse/enumerator.py +20 -0
- llparse-0.1.0/llparse/frontend.py +527 -0
- llparse-0.1.0/llparse/header.py +89 -0
- llparse-0.1.0/llparse/llparse.py +150 -0
- llparse-0.1.0/llparse/pybuilder/__init__.py +2 -0
- llparse-0.1.0/llparse/pybuilder/builder.py +318 -0
- llparse-0.1.0/llparse/pybuilder/loopchecker.py +246 -0
- llparse-0.1.0/llparse/pybuilder/main_code.py +548 -0
- llparse-0.1.0/llparse/pybuilder/parsemap.py +37 -0
- llparse-0.1.0/llparse/pyfront/containers.py +33 -0
- llparse-0.1.0/llparse/pyfront/front.py +189 -0
- llparse-0.1.0/llparse/pyfront/implementation.py +98 -0
- llparse-0.1.0/llparse/pyfront/namespace.py +1 -0
- llparse-0.1.0/llparse/pyfront/nodes.py +243 -0
- llparse-0.1.0/llparse/pyfront/peephole.py +45 -0
- llparse-0.1.0/llparse/pyfront/transform.py +21 -0
- llparse-0.1.0/llparse/settings.py +285 -0
- llparse-0.1.0/llparse/spanalloc.py +176 -0
- llparse-0.1.0/llparse/test.py +232 -0
- llparse-0.1.0/llparse/tire.py +158 -0
- llparse-0.1.0/llparse/trie.py +165 -0
- llparse-0.1.0/llparse.egg-info/PKG-INFO +129 -0
- llparse-0.1.0/llparse.egg-info/SOURCES.txt +35 -0
- llparse-0.1.0/llparse.egg-info/dependency_links.txt +1 -0
- llparse-0.1.0/llparse.egg-info/top_level.txt +1 -0
- llparse-0.1.0/pyproject.toml +11 -0
- llparse-0.1.0/setup.cfg +4 -0
llparse-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Vizonex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
llparse-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llparse
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Parody of llparse written for writing C Parsers with Python
|
|
5
|
+
Author-email: Vizonex <VizonexBusiness@gmail.com>
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# pyllparse
|
|
12
|
+
A python parody of the typescript library llparse.
|
|
13
|
+
|
|
14
|
+
I take no credit for the orginal work done by indutny and I was originally very nervous about making
|
|
15
|
+
this python library that I made public...
|
|
16
|
+
|
|
17
|
+
Links to the original library
|
|
18
|
+
- https://llparse.org
|
|
19
|
+
- https://github.com/nodejs/llparse
|
|
20
|
+
|
|
21
|
+
Unlike the typescript library all 3 of llparse's libraries were combined in this version of the code for the sake of portability...
|
|
22
|
+
I ended up mentioning how I did this a while back and I also had a concept for a C parser as well but I just didn't like it and I ended up using this instead but I also learned typescript as a bonus and It was alot of fun for me. I don't plan to make this into a real pypi library yet but I also didn't want to take away from the magic of the original source code that I borrowed from...
|
|
23
|
+
|
|
24
|
+
# Looking back on my dead work 2 years later
|
|
25
|
+
I had this idea lay somewhat dormant for 2 years and now seeing that I wanted to write a new socks5 server parser writing it in python
|
|
26
|
+
just made a little bit more sense to me, this way I don't have to stress over typescript or needing to relearn things I learned over 2 years ago.
|
|
27
|
+
I have now made this project public on pypi for anybody who wanted to dig into it and access the code
|
|
28
|
+
or generate your own parsers for C.
|
|
29
|
+
|
|
30
|
+
If your better at typescript. I reccommend sticking to the typescript version, no pressure. This library aims to not compete
|
|
31
|
+
with llparse but to act as an alternative for users who prefer python over typescript/node-js, simillar to puppeteer or pyppeteer.
|
|
32
|
+
Personally maybe we should make a rust crate for a rust version or at least someone will do it.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Warnings
|
|
36
|
+
- It should be safe enough to generate code and stress-test it but if something doesn't seem right to you, be sure to throw an issue and share a typescript version of something that works in typescript but not in python, this way something unintended by the typescript developers can be solved.
|
|
37
|
+
- Some code may look unfinished or may need further internal refactoring/polishing and with the other things I want to go out and do
|
|
38
|
+
such as contributions to aiohttp and it's other libraries, cyares, winloop, aiothreading, aiocallback & deprecated-params and I also have an irl Part-time Job, ideas and pull requests are welcomed without hesitation. I maintain other projects listed above to prevent myself from experiencing burnout.
|
|
39
|
+
- Pytest testsuite has not been made yet. If this is a concern to you, please throw an issue on github. (Pytests and new workflows soon!)
|
|
40
|
+
- Some code might be spaghetti-code (because It's been 2 years since I touched much of the code if not at all)
|
|
41
|
+
e.g. tests module should be moved away from the library into the github repo.
|
|
42
|
+
- There's some research portions such as tools to help generate things for cython or tools to make mirrors of llhttp's code
|
|
43
|
+
such as it's native c code (I was looking to see if it could be theoretically autogenerated). Feel free to use them but
|
|
44
|
+
do it at your own risk. They maybe incompleted or not throughly stress-tested.
|
|
45
|
+
|
|
46
|
+
## New Features
|
|
47
|
+
- Throw me an issue if typescript llparse introduces something new that you want for me or another contributor to try and implement
|
|
48
|
+
just seeing llparse add new features is nothing but exciting to me.
|
|
49
|
+
|
|
50
|
+
- If you want a feature that typescript llparse doesn't have, be sure to try making a pull request over there as well and not just here,
|
|
51
|
+
there's a good chance they will appericate you for helping over there too and your helping make llhttp better by doing so. :)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# Why Did I Translate llparse to python?
|
|
55
|
+
- I wanted to work with a langauge I was more familiar with
|
|
56
|
+
- Better educate myself and others on how these great libraries like llhttp are made
|
|
57
|
+
- Write faster C code that could do more than just a simple split function or a regex...
|
|
58
|
+
- Make it easy for me or someone else to find a problem and solve it in typescript after testing it in python
|
|
59
|
+
- Typescript takes 2 commands to run a script with node-js it while python only takes one cutting the time required tremendously...
|
|
60
|
+
- The orginal project was MIT licensed.
|
|
61
|
+
- I wanted to write my own C Parsering tool with llhttp styled callbacks of my own using a language I was the most comfortable with using.
|
|
62
|
+
- I didn't like __Lemon Parser__ or __Yacc__ all that much and a good ide for handling them in Visual Studio Code with error checking to my knowlegde does not exist.
|
|
63
|
+
- The closest thing I got to what I wanted was a project named __NMFU__ shorthand for no memory for you and even I had problems with writing things using that library...
|
|
64
|
+
|
|
65
|
+
This was the Code that inspired me to try and make a new pyi writer branch for cython and if it wasn't for llhttp
|
|
66
|
+
existing as well as it's magical experience I would've never done what I did.
|
|
67
|
+
|
|
68
|
+
Unlike llparse in typescript, this version has more integrated and experimental features like automatically building the api seen in llhttp and
|
|
69
|
+
I've added a few other things like the dot compiler from llparse_dot and I also made a brand new cython compiler
|
|
70
|
+
for it making easy and simple to make pxd files to port your projects to cython
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## How to use
|
|
75
|
+
```python
|
|
76
|
+
# The good old http_parser was borrowed from llparse.org to demonstrate this for you :)
|
|
77
|
+
from llparse import LLParse
|
|
78
|
+
|
|
79
|
+
p = LLParse("http_parser")
|
|
80
|
+
method = p.node("method")
|
|
81
|
+
beforeUrl = p.node("before_url")
|
|
82
|
+
urlSpan = p.span(p.code.span("on_url"))
|
|
83
|
+
url = p.node("url")
|
|
84
|
+
http = p.node("http")
|
|
85
|
+
|
|
86
|
+
# Add custom uint8_t property to the state
|
|
87
|
+
p.property("i8", "method")
|
|
88
|
+
|
|
89
|
+
# Store method inside a custom property
|
|
90
|
+
onMethod = p.invoke(p.code.store("method"), beforeUrl)
|
|
91
|
+
|
|
92
|
+
# Invoke custom C function
|
|
93
|
+
complete = p.invoke(
|
|
94
|
+
p.code.match("on_complete"),
|
|
95
|
+
{
|
|
96
|
+
# Restart
|
|
97
|
+
0: method
|
|
98
|
+
},
|
|
99
|
+
p.error(4, "`on_complete` error"),
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
method.select(
|
|
103
|
+
{
|
|
104
|
+
"HEAD": 0,
|
|
105
|
+
"GET": 1,
|
|
106
|
+
"POST": 2,
|
|
107
|
+
"PUT": 3,
|
|
108
|
+
"DELETE": 4,
|
|
109
|
+
"OPTIONS": 5,
|
|
110
|
+
"CONNECT": 6,
|
|
111
|
+
"TRACE": 7,
|
|
112
|
+
"PATCH": 8,
|
|
113
|
+
},
|
|
114
|
+
onMethod,
|
|
115
|
+
).otherwise(p.error(5, "Expected method"))
|
|
116
|
+
|
|
117
|
+
beforeUrl.match(" ", beforeUrl).otherwise(urlSpan.start(url))
|
|
118
|
+
|
|
119
|
+
url.peek(" ", urlSpan.end(http)).skipTo(url)
|
|
120
|
+
|
|
121
|
+
http.match(" HTTP/1.1\r\n\r\n", complete).otherwise(
|
|
122
|
+
p.error(6, "Expected HTTP/1.1 and two newlines")
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
c = p.build(method)
|
|
126
|
+
print(c.c)
|
|
127
|
+
open("http_parser.c", "w").write(c.c)
|
|
128
|
+
open("http_parser.h", "w").write(c.header)
|
|
129
|
+
```
|
llparse-0.1.0/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# pyllparse
|
|
2
|
+
A python parody of the typescript library llparse.
|
|
3
|
+
|
|
4
|
+
I take no credit for the orginal work done by indutny and I was originally very nervous about making
|
|
5
|
+
this python library that I made public...
|
|
6
|
+
|
|
7
|
+
Links to the original library
|
|
8
|
+
- https://llparse.org
|
|
9
|
+
- https://github.com/nodejs/llparse
|
|
10
|
+
|
|
11
|
+
Unlike the typescript library all 3 of llparse's libraries were combined in this version of the code for the sake of portability...
|
|
12
|
+
I ended up mentioning how I did this a while back and I also had a concept for a C parser as well but I just didn't like it and I ended up using this instead but I also learned typescript as a bonus and It was alot of fun for me. I don't plan to make this into a real pypi library yet but I also didn't want to take away from the magic of the original source code that I borrowed from...
|
|
13
|
+
|
|
14
|
+
# Looking back on my dead work 2 years later
|
|
15
|
+
I had this idea lay somewhat dormant for 2 years and now seeing that I wanted to write a new socks5 server parser writing it in python
|
|
16
|
+
just made a little bit more sense to me, this way I don't have to stress over typescript or needing to relearn things I learned over 2 years ago.
|
|
17
|
+
I have now made this project public on pypi for anybody who wanted to dig into it and access the code
|
|
18
|
+
or generate your own parsers for C.
|
|
19
|
+
|
|
20
|
+
If your better at typescript. I reccommend sticking to the typescript version, no pressure. This library aims to not compete
|
|
21
|
+
with llparse but to act as an alternative for users who prefer python over typescript/node-js, simillar to puppeteer or pyppeteer.
|
|
22
|
+
Personally maybe we should make a rust crate for a rust version or at least someone will do it.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Warnings
|
|
26
|
+
- It should be safe enough to generate code and stress-test it but if something doesn't seem right to you, be sure to throw an issue and share a typescript version of something that works in typescript but not in python, this way something unintended by the typescript developers can be solved.
|
|
27
|
+
- Some code may look unfinished or may need further internal refactoring/polishing and with the other things I want to go out and do
|
|
28
|
+
such as contributions to aiohttp and it's other libraries, cyares, winloop, aiothreading, aiocallback & deprecated-params and I also have an irl Part-time Job, ideas and pull requests are welcomed without hesitation. I maintain other projects listed above to prevent myself from experiencing burnout.
|
|
29
|
+
- Pytest testsuite has not been made yet. If this is a concern to you, please throw an issue on github. (Pytests and new workflows soon!)
|
|
30
|
+
- Some code might be spaghetti-code (because It's been 2 years since I touched much of the code if not at all)
|
|
31
|
+
e.g. tests module should be moved away from the library into the github repo.
|
|
32
|
+
- There's some research portions such as tools to help generate things for cython or tools to make mirrors of llhttp's code
|
|
33
|
+
such as it's native c code (I was looking to see if it could be theoretically autogenerated). Feel free to use them but
|
|
34
|
+
do it at your own risk. They maybe incompleted or not throughly stress-tested.
|
|
35
|
+
|
|
36
|
+
## New Features
|
|
37
|
+
- Throw me an issue if typescript llparse introduces something new that you want for me or another contributor to try and implement
|
|
38
|
+
just seeing llparse add new features is nothing but exciting to me.
|
|
39
|
+
|
|
40
|
+
- If you want a feature that typescript llparse doesn't have, be sure to try making a pull request over there as well and not just here,
|
|
41
|
+
there's a good chance they will appericate you for helping over there too and your helping make llhttp better by doing so. :)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Why Did I Translate llparse to python?
|
|
45
|
+
- I wanted to work with a langauge I was more familiar with
|
|
46
|
+
- Better educate myself and others on how these great libraries like llhttp are made
|
|
47
|
+
- Write faster C code that could do more than just a simple split function or a regex...
|
|
48
|
+
- Make it easy for me or someone else to find a problem and solve it in typescript after testing it in python
|
|
49
|
+
- Typescript takes 2 commands to run a script with node-js it while python only takes one cutting the time required tremendously...
|
|
50
|
+
- The orginal project was MIT licensed.
|
|
51
|
+
- I wanted to write my own C Parsering tool with llhttp styled callbacks of my own using a language I was the most comfortable with using.
|
|
52
|
+
- I didn't like __Lemon Parser__ or __Yacc__ all that much and a good ide for handling them in Visual Studio Code with error checking to my knowlegde does not exist.
|
|
53
|
+
- The closest thing I got to what I wanted was a project named __NMFU__ shorthand for no memory for you and even I had problems with writing things using that library...
|
|
54
|
+
|
|
55
|
+
This was the Code that inspired me to try and make a new pyi writer branch for cython and if it wasn't for llhttp
|
|
56
|
+
existing as well as it's magical experience I would've never done what I did.
|
|
57
|
+
|
|
58
|
+
Unlike llparse in typescript, this version has more integrated and experimental features like automatically building the api seen in llhttp and
|
|
59
|
+
I've added a few other things like the dot compiler from llparse_dot and I also made a brand new cython compiler
|
|
60
|
+
for it making easy and simple to make pxd files to port your projects to cython
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
## How to use
|
|
65
|
+
```python
|
|
66
|
+
# The good old http_parser was borrowed from llparse.org to demonstrate this for you :)
|
|
67
|
+
from llparse import LLParse
|
|
68
|
+
|
|
69
|
+
p = LLParse("http_parser")
|
|
70
|
+
method = p.node("method")
|
|
71
|
+
beforeUrl = p.node("before_url")
|
|
72
|
+
urlSpan = p.span(p.code.span("on_url"))
|
|
73
|
+
url = p.node("url")
|
|
74
|
+
http = p.node("http")
|
|
75
|
+
|
|
76
|
+
# Add custom uint8_t property to the state
|
|
77
|
+
p.property("i8", "method")
|
|
78
|
+
|
|
79
|
+
# Store method inside a custom property
|
|
80
|
+
onMethod = p.invoke(p.code.store("method"), beforeUrl)
|
|
81
|
+
|
|
82
|
+
# Invoke custom C function
|
|
83
|
+
complete = p.invoke(
|
|
84
|
+
p.code.match("on_complete"),
|
|
85
|
+
{
|
|
86
|
+
# Restart
|
|
87
|
+
0: method
|
|
88
|
+
},
|
|
89
|
+
p.error(4, "`on_complete` error"),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
method.select(
|
|
93
|
+
{
|
|
94
|
+
"HEAD": 0,
|
|
95
|
+
"GET": 1,
|
|
96
|
+
"POST": 2,
|
|
97
|
+
"PUT": 3,
|
|
98
|
+
"DELETE": 4,
|
|
99
|
+
"OPTIONS": 5,
|
|
100
|
+
"CONNECT": 6,
|
|
101
|
+
"TRACE": 7,
|
|
102
|
+
"PATCH": 8,
|
|
103
|
+
},
|
|
104
|
+
onMethod,
|
|
105
|
+
).otherwise(p.error(5, "Expected method"))
|
|
106
|
+
|
|
107
|
+
beforeUrl.match(" ", beforeUrl).otherwise(urlSpan.start(url))
|
|
108
|
+
|
|
109
|
+
url.peek(" ", urlSpan.end(http)).skipTo(url)
|
|
110
|
+
|
|
111
|
+
http.match(" HTTP/1.1\r\n\r\n", complete).otherwise(
|
|
112
|
+
p.error(6, "Expected HTTP/1.1 and two newlines")
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
c = p.build(method)
|
|
116
|
+
print(c.c)
|
|
117
|
+
open("http_parser.c", "w").write(c.c)
|
|
118
|
+
open("http_parser.h", "w").write(c.header)
|
|
119
|
+
```
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
from .compilator import Compilation, ICompilerOptions, Node
|
|
4
|
+
from .constants import *
|
|
5
|
+
from .frontend import IFrontendResult
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CCompiler:
|
|
9
|
+
"""The Final HeadPeice where the Main C-Code gets compiled to..."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self, header: Optional[str] = None, debug: Optional[str] = None
|
|
13
|
+
) -> None:
|
|
14
|
+
# NOTE Unlike in typescript llparse Containers are not Required since I'm using a different methoad to translate those parts...
|
|
15
|
+
self.options = ICompilerOptions(debug, header)
|
|
16
|
+
|
|
17
|
+
def compile(self, info: IFrontendResult):
|
|
18
|
+
compilation = Compilation(
|
|
19
|
+
info.prefix,
|
|
20
|
+
info.properties,
|
|
21
|
+
list(info.resumptionTargets),
|
|
22
|
+
options=self.options,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
out: list[str] = []
|
|
26
|
+
|
|
27
|
+
out.append("#include <stdlib.h>")
|
|
28
|
+
out.append("#include <stdint.h>")
|
|
29
|
+
out.append("#include <string.h>")
|
|
30
|
+
out.append("")
|
|
31
|
+
# Seems LLParse was updated from /* UNREACHABLE */ abort(); to a Macro, Intresting...
|
|
32
|
+
out.append('#ifdef __SSE4_2__')
|
|
33
|
+
out.append(' #ifdef _MSC_VER')
|
|
34
|
+
out.append(' #include <nmmintrin.h>')
|
|
35
|
+
out.append(' #else /* !_MSC_VER */')
|
|
36
|
+
out.append(' #include <x86intrin.h>')
|
|
37
|
+
out.append(' #endif /* _MSC_VER */')
|
|
38
|
+
out.append('#endif /* __SSE4_2__ */')
|
|
39
|
+
out.append('')
|
|
40
|
+
|
|
41
|
+
out.append('#ifdef __ARM_NEON__')
|
|
42
|
+
out.append(' #include <arm_neon.h>')
|
|
43
|
+
out.append('#endif /* __ARM_NEON__ */')
|
|
44
|
+
out.append('')
|
|
45
|
+
|
|
46
|
+
out.append('#ifdef __wasm__')
|
|
47
|
+
out.append(' #include <wasm_simd128.h>')
|
|
48
|
+
out.append('#endif /* __wasm__ */')
|
|
49
|
+
out.append('')
|
|
50
|
+
|
|
51
|
+
out.append('#ifdef _MSC_VER')
|
|
52
|
+
out.append(' #define ALIGN(n) _declspec(align(n))')
|
|
53
|
+
out.append(' #define UNREACHABLE __assume(0)')
|
|
54
|
+
out.append('#else /* !_MSC_VER */')
|
|
55
|
+
out.append(' #define ALIGN(n) __attribute__((aligned(n)))')
|
|
56
|
+
out.append(' #define UNREACHABLE __builtin_unreachable()')
|
|
57
|
+
out.append('#endif /* _MSC_VER */')
|
|
58
|
+
|
|
59
|
+
out.append("")
|
|
60
|
+
out.append(
|
|
61
|
+
f'#include "{self.options.header if self.options.header else info.prefix}.h"'
|
|
62
|
+
)
|
|
63
|
+
out.append("")
|
|
64
|
+
out.append(f"typedef int (*{info.prefix}__span_cb)(")
|
|
65
|
+
out.append(f" {info.prefix}_t*, const char*, const char*);")
|
|
66
|
+
out.append("")
|
|
67
|
+
|
|
68
|
+
# Start Queuing span callbacks
|
|
69
|
+
# otherwise we will have nothing
|
|
70
|
+
# but mess which is not what we want - Vizonex
|
|
71
|
+
compilation.reserveSpans(info.spans)
|
|
72
|
+
|
|
73
|
+
rootState: Node = compilation.unwrapNode(info.root)
|
|
74
|
+
rootName = rootState.build(compilation)
|
|
75
|
+
# Bring in the rest of the variables...
|
|
76
|
+
compilation.buildGlobals(out)
|
|
77
|
+
out.append("")
|
|
78
|
+
|
|
79
|
+
out.append(f"int {info.prefix}_init({info.prefix}_t* {ARG_STATE}) " + "{")
|
|
80
|
+
out.append(f" memset({ARG_STATE}, 0, sizeof(*{ARG_STATE}));")
|
|
81
|
+
out.append(f" {ARG_STATE}->_current = (void*) (intptr_t) {rootName};")
|
|
82
|
+
out.append(" return 0;")
|
|
83
|
+
out.append("}")
|
|
84
|
+
out.append("")
|
|
85
|
+
|
|
86
|
+
# TODO (Vizonex) Make llparse_state_t's Name Optional and alterable incase mixed with
|
|
87
|
+
# llhttp or another parser
|
|
88
|
+
out.append(f"static llparse_state_t {info.prefix}__run(")
|
|
89
|
+
out.append(f" {info.prefix}_t* {ARG_STATE},")
|
|
90
|
+
out.append(f" const unsigned char* {ARG_POS},")
|
|
91
|
+
out.append(f" const unsigned char* {ARG_ENDPOS}) " + "{")
|
|
92
|
+
out.append(f" int {VAR_MATCH};")
|
|
93
|
+
out.append(
|
|
94
|
+
" switch ((llparse_state_t) (intptr_t) "
|
|
95
|
+
+ f"{compilation.currentField()}) "
|
|
96
|
+
+ "{"
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# Now build resumption states... These are states what will have a 'case block' next to them...
|
|
100
|
+
# However I'm not refering to the characters those will be handles in thier inner switches,
|
|
101
|
+
# I'm talking about the major states...
|
|
102
|
+
tmp = []
|
|
103
|
+
compilation.buildResumptionStates(tmp)
|
|
104
|
+
compilation.indent(out, tmp, " ")
|
|
105
|
+
|
|
106
|
+
# Final Resumption State... Very important!
|
|
107
|
+
out.append(" default:")
|
|
108
|
+
out.append(" UNREACHABLE;")
|
|
109
|
+
out.append(" }")
|
|
110
|
+
|
|
111
|
+
tmp = []
|
|
112
|
+
compilation.buildInternalStates(tmp)
|
|
113
|
+
compilation.indent(out, tmp, " ")
|
|
114
|
+
|
|
115
|
+
out.append("}")
|
|
116
|
+
out.append("")
|
|
117
|
+
|
|
118
|
+
out.append(
|
|
119
|
+
f"int {info.prefix}_execute({info.prefix}_t* {ARG_STATE}, "
|
|
120
|
+
+ f"const char* {ARG_POS}, const char* {ARG_ENDPOS}) "
|
|
121
|
+
+ "{"
|
|
122
|
+
)
|
|
123
|
+
out.append(" llparse_state_t next;")
|
|
124
|
+
out.append("")
|
|
125
|
+
|
|
126
|
+
out.append(" /* check lingering errors */")
|
|
127
|
+
out.append(f" if ({compilation.errorField()} != 0) " + "{")
|
|
128
|
+
out.append(f" return {compilation.errorField()};")
|
|
129
|
+
out.append(" }")
|
|
130
|
+
out.append("")
|
|
131
|
+
|
|
132
|
+
tmp = []
|
|
133
|
+
self.restartSpans(compilation, info, tmp)
|
|
134
|
+
compilation.indent(out, tmp, " ")
|
|
135
|
+
args = [
|
|
136
|
+
compilation.stateArg(),
|
|
137
|
+
f"(const unsigned char*) {compilation.posArg()}",
|
|
138
|
+
f"(const unsigned char*) {compilation.endPosArg()}",
|
|
139
|
+
]
|
|
140
|
+
out.append(f" next = {info.prefix}__run({(', ').join(args)});")
|
|
141
|
+
out.append(f" if (next == {STATE_ERROR}) " + "{")
|
|
142
|
+
out.append(f" return {compilation.errorField()};")
|
|
143
|
+
out.append(" }")
|
|
144
|
+
out.append(f" {compilation.currentField()} = (void*) (intptr_t) next;")
|
|
145
|
+
out.append("")
|
|
146
|
+
|
|
147
|
+
tmp = []
|
|
148
|
+
self.executeSpans(compilation, info, tmp)
|
|
149
|
+
compilation.indent(out, tmp, " ")
|
|
150
|
+
|
|
151
|
+
out.append(" return 0;")
|
|
152
|
+
out.append("}")
|
|
153
|
+
|
|
154
|
+
# JOIN ALL OF THEM!
|
|
155
|
+
return "\n".join(out)
|
|
156
|
+
|
|
157
|
+
def restartSpans(self, ctx: Compilation, info: IFrontendResult, out: list[str]):
|
|
158
|
+
if len(info.spans) == 0:
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
out.append("/* restart spans */")
|
|
162
|
+
for span in info.spans:
|
|
163
|
+
posField = ctx.spanPosField(span.index)
|
|
164
|
+
|
|
165
|
+
out.append(f"if ({posField} != NULL) " + "{")
|
|
166
|
+
out.append(f" {posField} = (void*) {ctx.posArg()};")
|
|
167
|
+
out.append("}")
|
|
168
|
+
out.append("")
|
|
169
|
+
|
|
170
|
+
def executeSpans(self, ctx: Compilation, info: IFrontendResult, out: list[str]):
|
|
171
|
+
if not info.spans:
|
|
172
|
+
return
|
|
173
|
+
|
|
174
|
+
out.append("/* execute spans */")
|
|
175
|
+
for span in info.spans:
|
|
176
|
+
posField = ctx.spanPosField(span.index)
|
|
177
|
+
|
|
178
|
+
if len(span.callbacks) == 1:
|
|
179
|
+
cb = ctx.unwrapCode(span.callbacks[0], True)
|
|
180
|
+
callback = ctx.buildCode(cb)
|
|
181
|
+
|
|
182
|
+
else:
|
|
183
|
+
# TODO (Vizonex) Merge lines 139 & 140 together in a future update
|
|
184
|
+
callback = (
|
|
185
|
+
f"({info.prefix}__span_cb)"
|
|
186
|
+
+ ctx.spanCbField(span.index)
|
|
187
|
+
+ f"({callback})"
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
args = [ctx.stateArg(), posField, f"(const char*) {ctx.endPosArg()}"]
|
|
191
|
+
|
|
192
|
+
out.append(f"if ({posField} != NULL) " + "{")
|
|
193
|
+
out.append(" int error;")
|
|
194
|
+
out.append("")
|
|
195
|
+
out.append(f" error = {callback}({', '.join(args)});")
|
|
196
|
+
|
|
197
|
+
# TODO (Vizonex): Deduplicate when indutny updates his side so we can all make our changes accordingly...
|
|
198
|
+
out.append(" if (error != 0) {")
|
|
199
|
+
out.append(f" {ctx.errorField()} = error;")
|
|
200
|
+
out.append(f" {ctx.errorPosField()} = {ctx.endPosArg()};")
|
|
201
|
+
out.append(" return error;")
|
|
202
|
+
out.append(" }")
|
|
203
|
+
out.append("}")
|
|
204
|
+
out.append("")
|