-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlexerWrapper.js
100 lines (83 loc) · 3.19 KB
/
lexerWrapper.js
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
'use strict';
const antlr4 = require('antlr4');
function LexerWrapper(lexerFactory) {
this._lexerFactory = lexerFactory;
this._cachedLexer = null;
}
LexerWrapper.prototype.tokenizeNonDefaultChannel = function (input) {
const result = this._tokenize(input);
result.tokens = result.tokens.filter((token) => token.channel === 0);
return result;
};
LexerWrapper.prototype.getEmptyTokenStream = function() {
return new antlr4.CommonTokenStream(this._getCachedLexer());
};
LexerWrapper.prototype._tokenize = function (input) {
const lexer = this._createLexer(input);
lexer.removeErrorListeners();
const result = {untokenizedText: ''};
const newErrorListener = Object.create(antlr4.error.ErrorListener);
newErrorListener.syntaxError = function (recognizer, offendingSymbol, line, column, msg, e) {
result.untokenizedText = input.substring(column);
};
lexer.addErrorListener(newErrorListener);
result.tokens = lexer.getAllTokens();
return result;
};
LexerWrapper.prototype.findStateByRuleNumber = function (ruleNumber) {
return this._getCachedLexer().atn.ruleToStartState.slice(ruleNumber, ruleNumber + 1)[0];
};
LexerWrapper.prototype._getCachedLexer = function () {
if (this._cachedLexer === null) {
this._cachedLexer = this._createEmptyLexer();
}
return this._cachedLexer;
};
LexerWrapper.prototype._createEmptyLexer = function (lexerInput) {
return this._createLexer('');
};
LexerWrapper.prototype._createLexer = function (lexerInput) {
const inputStream = new antlr4.InputStream(lexerInput);
const lexer = this._lexerFactory.createLexer(inputStream);
return lexer;
};
module.exports.LexerWrapper = LexerWrapper;
/*
public String[] getRuleNames() {
return getCachedLexer().getRuleNames();
}
public Vocabulary getVocabulary() {
return getCachedLexer().getVocabulary();
}
private TokenizationResult tokenize(String input) {
Lexer lexer = this.createLexer(input);
lexer.removeErrorListeners();
final TokenizationResult result = new TokenizationResult();
ANTLRErrorListener newErrorListener = new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
result.untokenizedText = input.substring(charPositionInLine); // intended side effect
}
};
lexer.addErrorListener(newErrorListener);
result.tokens = lexer.getAllTokens();
return result;
}
private Lexer createLexer(CharStream input) {
return this.lexerFactory.createLexer(input);
}
private Lexer createLexer(String lexerInput) {
return this.createLexer(toCharStream(lexerInput));
}
private static CharStream toCharStream(String text) {
CharStream inputStream;
try {
inputStream = CharStreams.fromReader(new StringReader(text));
} catch (IOException e) {
throw new IllegalStateException("Unexpected while reading input string", e);
}
return inputStream;
}
}
*/