forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (56 loc) · 1.66 KB
/
index.js
File metadata and controls
67 lines (56 loc) · 1.66 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
"use strict";
const JavaLexer = require("./lexer");
const JavaParser = require("./parser");
const { attachComments, matchFormatterOffOnPairs } = require("./comments");
const parser = new JavaParser();
const BaseJavaCstVisitor = parser.getBaseCstVisitorConstructor();
const BaseJavaCstVisitorWithDefaults =
parser.getBaseCstVisitorConstructorWithDefaults();
function parse(inputText, entryPoint = "compilationUnit") {
// Lex
const lexResult = JavaLexer.tokenize(inputText);
if (lexResult.errors.length > 0) {
const firstError = lexResult.errors[0];
throw Error(
"Sad sad panda, lexing errors detected in line: " +
firstError.line +
", column: " +
firstError.column +
"!\n" +
firstError.message
);
}
parser.input = lexResult.tokens;
parser.mostEnclosiveCstNodeByStartOffset = {};
parser.mostEnclosiveCstNodeByEndOffset = {};
parser.setOnOffCommentPairs(
matchFormatterOffOnPairs(lexResult.groups.comments)
);
// Automatic CST created when parsing
const cst = parser[entryPoint]();
if (parser.errors.length > 0) {
const error = parser.errors[0];
throw Error(
"Sad sad panda, parsing errors detected in line: " +
error.token.startLine +
", column: " +
error.token.startColumn +
"!\n" +
error.message +
"!\n\t->" +
error.context.ruleStack.join("\n\t->")
);
}
attachComments(
lexResult.tokens,
lexResult.groups.comments,
parser.mostEnclosiveCstNodeByStartOffset,
parser.mostEnclosiveCstNodeByEndOffset
);
return cst;
}
module.exports = {
parse,
BaseJavaCstVisitor,
BaseJavaCstVisitorWithDefaults
};