|
| 1 | +# Node.js bindings for `lib-ruby-parser` |
| 2 | + |
| 3 | +It's based on [C++ bindings](https://github.com/lib-ruby-parser/cpp-bindings) and `node-addon-api`. |
| 4 | + |
| 5 | +Each node has its own JavaScript class, so |
| 6 | +1. it's possible to dinstinguish them by checking `instanceof` |
| 7 | +2. they can be extended in pure JavaScript |
| 8 | + |
| 9 | +Here's how it looks like: |
| 10 | + |
| 11 | +```js |
| 12 | +ParserResult { |
| 13 | + ast: Send { |
| 14 | + recv: Int { |
| 15 | + value: '2', |
| 16 | + operator_l: null, |
| 17 | + expression_l: Range { begin_pos: 0, end_pos: 1 } |
| 18 | + }, |
| 19 | + method_name: '+', |
| 20 | + args: [ |
| 21 | + Int { |
| 22 | + value: '3', |
| 23 | + operator_l: null, |
| 24 | + expression_l: Range { begin_pos: 4, end_pos: 5 } |
| 25 | + } |
| 26 | + ], |
| 27 | + dot_l: null, |
| 28 | + selector_l: Range { begin_pos: 2, end_pos: 3 }, |
| 29 | + begin_l: null, |
| 30 | + end_l: null, |
| 31 | + operator_l: null, |
| 32 | + expression_l: Range { begin_pos: 0, end_pos: 5 } |
| 33 | + }, |
| 34 | + tokens: [ |
| 35 | + Token { |
| 36 | + name: 'tINTEGER', |
| 37 | + value: '2', |
| 38 | + loc: Loc { begin: 0, end: 1 } |
| 39 | + }, |
| 40 | + Token { name: 'tPLUS', value: '+', loc: Loc { begin: 2, end: 3 } }, |
| 41 | + Token { |
| 42 | + name: 'tINTEGER', |
| 43 | + value: '3', |
| 44 | + loc: Loc { begin: 4, end: 5 } |
| 45 | + }, |
| 46 | + Token { name: 'EOF', value: '', loc: Loc { begin: 5, end: 5 } } |
| 47 | + ], |
| 48 | + diagnostics: [], |
| 49 | + comments: [], |
| 50 | + magic_comments: [], |
| 51 | + input: '2 + 3' |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## API |
| 56 | + |
| 57 | +tldr; all classes mirror Rust implementation. |
| 58 | + |
| 59 | +TypeScript definition: |
| 60 | + |
| 61 | +```ts |
| 62 | +interface Loc { begin: number, end: number } |
| 63 | +interface Range { begin_pos: number, end_pos: number } |
| 64 | + |
| 65 | +interface Token { |
| 66 | + name: string, |
| 67 | + value: string, |
| 68 | + loc: Loc |
| 69 | +} |
| 70 | + |
| 71 | +interface Args { |
| 72 | + // mirrors https://docs.rs/lib-ruby-parser/0.7.0/lib_ruby_parser/nodes/struct.Args.html |
| 73 | + args: Array<Node>, |
| 74 | + expression_l: Range, |
| 75 | + begin_l: Range | null, |
| 76 | + end_l: Range | null |
| 77 | +} |
| 78 | + |
| 79 | +interface Class { |
| 80 | + // mirrors https://docs.rs/lib-ruby-parser/0.7.0/lib_ruby_parser/nodes/struct.Class.html |
| 81 | + name: Node, |
| 82 | + superclass: Node | null, |
| 83 | + body: Node | null, |
| 84 | + keyword_l: Range, |
| 85 | + operator_l: Range | null, |
| 86 | + end_l: Range, |
| 87 | + expression_l: Range |
| 88 | +} |
| 89 | + |
| 90 | +// other ~100 nodes |
| 91 | + |
| 92 | +type Node = Args | Class | ... /* other nodes */; |
| 93 | + |
| 94 | +function parse(code: String): ParserResult |
| 95 | +``` |
0 commit comments