Commit a248389
authored
Rust: Add Herb Rust FFI Bindings (#767)
This pull request adds Rust bindings for Herb using FFI (Foreign
Function Interface) via `bindgen`, allowing Rust applications to parse
and analyze ERB templates with the same implementation as the native C,
Ruby C-Extension, C++ Emscripten WASM, Java JNI, and the C++ Node.js
NAPI bindings.
Building the Rust bindings is requires a Rust toolchain, Bundler, Prism,
and a C compiler:
```bash
cd rust
make all
```
Once built, you can use the `herb-rust` CLI tool:
#### **`herb-rust version`**
```
herb rust v0.7.5, libprism v1.6.0, libherb v0.7.5 (Rust FFI)
```
#### **`herb-rust lex examples/test.html.erb`**
```js
#<Herb::Token type="TOKEN_HTML_TAG_START" value="<" range=[0, 1] start=(1:0) end=(1:1)>
#<Herb::Token type="TOKEN_IDENTIFIER" value="h1" range=[1, 3] start=(1:1) end=(1:3)>
#<Herb::Token type="TOKEN_WHITESPACE" value=" " range=[3, 4] start=(1:3) end=(1:4)>
#<Herb::Token type="TOKEN_IDENTIFIER" value="class" range=[4, 9] start=(1:4) end=(1:9)>
#<Herb::Token type="TOKEN_EQUALS" value="=" range=[9, 10] start=(1:9) end=(1:10)>
#<Herb::Token type="TOKEN_QUOTE" value="\"" range=[10, 11] start=(1:10) end=(1:11)>
#<Herb::Token type="TOKEN_IDENTIFIER" value="title" range=[11, 16] start=(1:11) end=(1:16)>
#<Herb::Token type="TOKEN_QUOTE" value="\"" range=[16, 17] start=(1:16) end=(1:17)>
#<Herb::Token type="TOKEN_HTML_TAG_END" value=">" range=[17, 18] start=(1:17) end=(1:18)>
#<Herb::Token type="TOKEN_ERB_START" value="<%=" range=[18, 21] start=(1:18) end=(1:21)>
#<Herb::Token type="TOKEN_ERB_CONTENT" value=" content " range=[21, 30] start=(1:21) end=(1:30)>
#<Herb::Token type="TOKEN_ERB_END" value="%>" range=[30, 32] start=(1:30) end=(1:32)>
#<Herb::Token type="TOKEN_HTML_TAG_START_CLOSE" value="</" range=[32, 34] start=(1:32) end=(1:34)>
#<Herb::Token type="TOKEN_IDENTIFIER" value="h1" range=[34, 36] start=(1:34) end=(1:36)>
#<Herb::Token type="TOKEN_HTML_TAG_END" value=">" range=[36, 37] start=(1:36) end=(1:37)>
#<Herb::Token type="TOKEN_NEWLINE" value="\n" range=[37, 38] start=(1:37) end=(2:0)>
#<Herb::Token type="TOKEN_EOF" value="<EOF>" range=[38, 38] start=(2:0) end=(2:0)>
```
#### **`herb-rust parse examples/test.html.erb`**
```js
@ DocumentNode (location: (1:0)-(2:0))
└── children: (2 items)
├── @ HTMLElementNode (location: (1:0)-(1:37))
│ ├── open_tag:
│ │ └── @ HTMLOpenTagNode (location: (1:0)-(1:18))
│ │ ├── tag_opening: "<" (location: (1:0)-(1:1))
│ │ ├── tag_name: "h1" (location: (1:1)-(1:3))
│ │ ├── tag_closing: ">" (location: (1:17)-(1:18))
│ │ ├── children: (1 item)
│ │ │ └── @ HTMLAttributeNode (location: (1:4)-(1:17))
│ │ │ ├── name:
│ │ │ │ └── @ HTMLAttributeNameNode (location: (1:4)-(1:9))
│ │ │ │ └── children: (1 item)
│ │ │ │ └── @ LiteralNode (location: (1:4)-(1:9))
│ │ │ │ └── content: "class"
│ │ │ │
│ │ │ ├── equals: "=" (location: (1:9)-(1:10))
│ │ │ └── value:
│ │ │ └── @ HTMLAttributeValueNode (location: (1:10)-(1:17))
│ │ │ ├── open_quote: "\"" (location: (1:10)-(1:11))
│ │ │ ├── children: (1 item)
│ │ │ │ └── @ LiteralNode (location: (1:11)-(1:16))
│ │ │ │ └── content: "title"
│ │ │ ├── close_quote: "\"" (location: (1:16)-(1:17))
│ │ │ └── quoted: true
│ │ └── is_void: false
│ │
│ ├── tag_name: "h1" (location: (1:1)-(1:3))
│ ├── body: (1 item)
│ │ └── @ ERBContentNode (location: (1:18)-(1:32))
│ │ ├── tag_opening: "<%=" (location: (1:18)-(1:21))
│ │ ├── content: " content " (location: (1:21)-(1:30))
│ │ ├── tag_closing: "%>" (location: (1:30)-(1:32))
│ │ ├── parsed: false
│ │ └── valid: false
│ ├── close_tag:
│ │ └── @ HTMLCloseTagNode (location: (1:32)-(1:37))
│ │ ├── tag_opening: "</" (location: (1:32)-(1:34))
│ │ ├── tag_name: "h1" (location: (1:34)-(1:36))
│ │ ├── children: []
│ │ └── tag_closing: ">" (location: (1:36)-(1:37))
│ │
│ ├── is_void: false
│ └── source: "HTML"
│
└── @ HTMLTextNode (location: (1:37)-(2:0))
└── content: "\n"
```
Resolves #6471 parent 57d8b51 commit a248389
File tree
50 files changed
+2764
-2
lines changed- .github
- workflows
- docs
- .vitepress/config
- docs/bindings
- javascript
- java
- ruby
- rust
- rust
- .cargo
- bin
- src
- ast
- tests
- fixtures
- snapshots
- templates
- rust/src
- ast
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
50 files changed
+2764
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
| |||
126 | 129 | | |
127 | 130 | | |
128 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
129 | 136 | | |
130 | 137 | | |
131 | 138 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| |||
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
| 122 | + | |
121 | 123 | | |
122 | 124 | | |
123 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
0 commit comments