Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit f330a1d

Browse files
authored
Merge pull request #11 from ts-graphviz/add-location
BRAKING CHANGE: Add location to AST object
2 parents d0c1fde + de82e45 commit f330a1d

File tree

7 files changed

+2736
-963
lines changed

7 files changed

+2736
-963
lines changed

README.md

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -129,83 +129,127 @@ The basic usage is the same as the `parse` function, except that it returns the
129129
import { AST } from '@ts-graphviz/parser';
130130

131131
const ast = AST.parse(`
132-
strict digraph example {
133-
subgraph cluster_0 {
134-
label="Subgraph A";
135-
a -> b -> c -> d;
136-
}
137-
138-
subgraph cluster_1 {
139-
label="Subgraph B";
140-
a -> f;
141-
f -> c;
142-
}
132+
digraph example {
133+
node1 [
134+
label = "My Node",
135+
]
143136
}
144137
`);
145138

146139
console.log(ast);
147140
// {
148141
// type: 'graph',
149-
// id: 'example',
142+
// id: {
143+
// type: 'literal',
144+
// value: 'example',
145+
// quoted: false,
146+
// location: {
147+
// start: { offset: 11, line: 2, column: 11 },
148+
// end: { offset: 18, line: 2, column: 18 }
149+
// }
150+
// },
150151
// directed: true,
151-
// strict: true,
152+
// strict: false,
152153
// body: [
153154
// {
154-
// type: 'subgraph',
155-
// id: 'cluster_0',
156-
// body: [
157-
// { type: 'attribute', key: 'label', value: 'Subgraph A' },
158-
// {
159-
// type: 'edge',
160-
// targets: [ { id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' } ],
161-
// attributes: []
155+
// type: 'node',
156+
// id: {
157+
// type: 'literal',
158+
// value: 'node1',
159+
// quoted: false,
160+
// location: {
161+
// start: { offset: 25, line: 3, column: 5 },
162+
// end: { offset: 30, line: 3, column: 10 }
162163
// }
163-
// ]
164-
// },
165-
// {
166-
// type: 'subgraph',
167-
// id: 'cluster_1',
168-
// body: [
169-
// { type: 'attribute', key: 'label', value: 'Subgraph B' },
164+
// },
165+
// attributes: [
170166
// {
171-
// type: 'edge',
172-
// targets: [ { id: 'a' }, { id: 'f' } ],
173-
// attributes: []
174-
// },
175-
// {
176-
// type: 'edge',
177-
// targets: [ { id: 'f' }, { id: 'c' } ],
178-
// attributes: []
167+
// type: 'attribute',
168+
// key: {
169+
// type: 'literal',
170+
// value: 'label',
171+
// quoted: false,
172+
// location: {
173+
// start: { offset: 39, line: 4, column: 7 },
174+
// end: { offset: 44, line: 4, column: 12 }
175+
// }
176+
// },
177+
// value: {
178+
// type: 'literal',
179+
// value: 'My Node',
180+
// quoted: true,
181+
// location: {
182+
// start: { offset: 47, line: 4, column: 15 },
183+
// end: { offset: 56, line: 4, column: 24 }
184+
// }
185+
// },
186+
// location: {
187+
// start: { offset: 39, line: 4, column: 7 },
188+
// end: { offset: 57, line: 4, column: 25 }
189+
// }
179190
// }
180-
// ]
191+
// ],
192+
// location: {
193+
// start: { offset: 25, line: 3, column: 5 },
194+
// end: { offset: 66, line: 6, column: 3 }
195+
// }
181196
// }
182-
// ]
197+
// ],
198+
// location: {
199+
// start: { offset: 0, line: 1, column: 1 },
200+
// end: { offset: 68, line: 7, column: 1 }
201+
// }
183202
// }
184203
```
185204

186205
##### Example: Specifying the `rule` option
187206

188207
```ts
189-
import { AST } from '@ts-graphviz/parser';
190-
191-
const ast = AST.parse(
192-
`test [
193-
style=filled;
194-
color=lightgrey;
195-
label = "example #1";
196-
];`,
197-
{ rule: 'node' },
198-
);
208+
const ast = AST.parse('test [ style=filled; ];', { rule: 'node' });
199209

200210
console.log(ast);
201211
// {
202212
// type: 'node',
203-
// id: 'test',
213+
// id: {
214+
// type: 'literal',
215+
// value: 'test',
216+
// quoted: false,
217+
// location: {
218+
// start: { offset: 0, line: 1, column: 1 },
219+
// end: { offset: 4, line: 1, column: 5 }
220+
// }
221+
// },
204222
// attributes: [
205-
// { key: 'style', value: 'filled' },
206-
// { key: 'color', value: 'lightgrey' },
207-
// { key: 'label', value: 'example #1' }
208-
// ]
223+
// {
224+
// type: 'attribute',
225+
// key: {
226+
// type: 'literal',
227+
// value: 'style',
228+
// quoted: false,
229+
// location: {
230+
// start: { offset: 7, line: 1, column: 8 },
231+
// end: { offset: 12, line: 1, column: 13 }
232+
// }
233+
// },
234+
// value: {
235+
// type: 'literal',
236+
// value: 'filled',
237+
// quoted: false,
238+
// location: {
239+
// start: { offset: 13, line: 1, column: 14 },
240+
// end: { offset: 19, line: 1, column: 20 }
241+
// }
242+
// },
243+
// location: {
244+
// start: { offset: 7, line: 1, column: 8 },
245+
// end: { offset: 20, line: 1, column: 21 }
246+
// }
247+
// }
248+
// ],
249+
// location: {
250+
// start: { offset: 0, line: 1, column: 1 },
251+
// end: { offset: 23, line: 1, column: 24 }
252+
// }
209253
// }
210254
```
211255

0 commit comments

Comments
 (0)