forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
138 lines (121 loc) · 3.47 KB
/
parser.ts
File metadata and controls
138 lines (121 loc) · 3.47 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type Prettier from "prettier";
import nodeTypeInfo from "tree-sitter-java-orchard/src/node-types.json" with { type: "json" };
import { Language, Parser, type Node } from "web-tree-sitter";
import { determinePrettierIgnoreRanges } from "./comments.ts";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.ts";
export default {
async parse(text) {
const tree = (await parser).parse(text)!;
const { rootNode } = tree;
if (rootNode.hasError) {
throw new Error("Failed to parse: " + rootNode);
}
const ast = processTree(rootNode);
determinePrettierIgnoreRanges(ast);
tree.delete();
return ast;
},
astFormat: "java",
hasPragma(text) {
return /^\/\*\*\n\s+\*\s@(format|prettier)\n\s+\*\//.test(text);
},
locStart(node) {
return node.start.index;
},
locEnd(node) {
return node.end.index;
}
} satisfies Prettier.Parser<SyntaxNode | CommentNode>;
const parser = (async () => {
await Parser.init();
const parser = new Parser();
const Java = await Language.load(
new URL("./tree-sitter-java_orchard.wasm", import.meta.url).pathname
);
parser.setLanguage(Java);
return parser;
})();
const multipleFieldsByType = nodeTypeInfo.reduce((map, nodeInfo) => {
if ("fields" in nodeInfo && nodeInfo.fields) {
const fields = Object.entries(nodeInfo.fields)
.filter(([, { multiple }]) => multiple)
.map(([name]) => name);
if (fields.length) {
map.set(nodeInfo.type, new Set(fields));
}
}
return map;
}, new Map<string, Set<string>>());
function processTree(
node: Node,
fieldName: string | null = null,
comments?: CommentNode[]
) {
const { type, isNamed, text: value, startPosition, endPosition } = node;
const javaNode = {
type,
isNamed,
value,
start: {
index: node.startIndex,
row: startPosition.row,
column: startPosition.column
},
end: {
index: node.endIndex,
row: endPosition.row,
column: endPosition.column
},
children: [],
namedChildren: [],
fieldName
} as SyntaxNode & {
[key: `${string}Node`]: SyntaxNode | undefined;
[key: `${string}Nodes`]: SyntaxNode[];
};
if (!comments) {
comments = javaNode.comments = [];
}
if (!javaNode.isNamed) {
return javaNode;
}
const multipleFields = multipleFieldsByType.get(node.type);
multipleFields?.forEach(name => (javaNode[`${name}Nodes`] = []));
node.children.forEach((child, index) => {
const { type, text: value, startPosition, endPosition } = child;
if (type === SyntaxType.BlockComment || type === SyntaxType.LineComment) {
comments.push({
type,
value,
start: {
index: child.startIndex,
row: startPosition.row,
column: startPosition.column
},
end: {
index: child.endIndex,
row: endPosition.row,
column: endPosition.column
},
leading: false,
trailing: false,
printed: false
});
} else {
const fieldName = node.fieldNameForChild(index);
const javaChild = processTree(child, fieldName, comments);
javaNode.children.push(javaChild);
if (javaChild.isNamed) {
javaNode.namedChildren.push(javaChild);
}
if (fieldName) {
if (multipleFields?.has(fieldName)) {
javaNode[`${fieldName}Nodes`].push(javaChild);
} else {
javaNode[`${fieldName}Node`] = javaChild;
}
}
}
});
return javaNode;
}