Commit ff7ad02
authored
Java: Add Herb Java JNI Bindings (#766)
This pull request adds Java bindings for Herb using the JNI (Java Native
Interface), allowing Java applications to parse and analyze ERB
templates with the same implementation as the native C, Ruby
C-Extension, C++ Emscripten WASM and the C++ Node.js NAPI bindings.
Building the Java bindings requires Java and a C compiler:
```bash
cd java
make
```
Once built, you can use the `herb-java` CLI tool:
#### **`herb-java version`**
```
herb java v0.7.5, libprism v1.6.0, libherb v0.7.5 (Java JNI)
```
#### **`herb-java 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-java 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: true
│ │ └── valid: true
│ ├── 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: ""
│
└── @ HTMLTextNode (location: (1:37)-(2:0))
└── content: "\n"
```
Resolves #6481 parent d4c201c commit ff7ad02
File tree
44 files changed
+2578
-5
lines changed- .github
- workflows
- docs
- .vitepress/config
- docs/bindings
- javascript
- java
- ruby
- java
- bin
- org/herb
- ast
- snapshots
- templates/java
- org/herb/ast
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
44 files changed
+2578
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | | - | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
39 | 45 | | |
40 | 46 | | |
41 | 47 | | |
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
92 | 100 | | |
93 | | - | |
94 | 101 | | |
| 102 | + | |
95 | 103 | | |
96 | 104 | | |
97 | 105 | | |
| |||
100 | 108 | | |
101 | 109 | | |
102 | 110 | | |
103 | | - | |
104 | 111 | | |
| 112 | + | |
105 | 113 | | |
106 | 114 | | |
107 | 115 | | |
| |||
114 | 122 | | |
115 | 123 | | |
116 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
117 | 129 | | |
118 | 130 | | |
119 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
93 | 101 | | |
94 | 102 | | |
95 | 103 | | |
| |||
| 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 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
0 commit comments