|
| 1 | +import UVLJavaScriptLexer from './lib/UVLJavaScriptLexer.js'; |
| 2 | +import UVLJavaScriptParser from './lib/UVLJavaScriptParser.js'; |
| 3 | +import antlr4 from 'antlr4'; |
| 4 | + |
| 5 | +export default class UVLJavaScriptCustomLexer extends UVLJavaScriptLexer { |
| 6 | + |
| 7 | + constructor(input_stream) { |
| 8 | + super(input_stream); |
| 9 | + this.tokens = []; |
| 10 | + this.indents = []; |
| 11 | + this.opened = 0; |
| 12 | + this.lastToken = null; |
| 13 | + } |
| 14 | + |
| 15 | + emitToken(t) { |
| 16 | + super.emitToken(t); |
| 17 | + this.tokens.push(t); |
| 18 | + } |
| 19 | + |
| 20 | + nextToken() { |
| 21 | + if (this._input.LA(1) === antlr4.Token.EOF && this.indents.length !== 0) { |
| 22 | + while (this.tokens.length > 0 && this.tokens[this.tokens.length - 1].type === antlr4.Token.EOF) { |
| 23 | + this.tokens.pop(); |
| 24 | + } |
| 25 | + |
| 26 | + this.emitToken(this.commonToken(UVLJavaScriptLexer.NEWLINE, "\n")); |
| 27 | + |
| 28 | + while (this.indents.length !== 0) { |
| 29 | + this.emitToken(this.createDedent()); |
| 30 | + this.indents.pop(); |
| 31 | + } |
| 32 | + |
| 33 | + this.emitToken(this.commonToken(antlr4.Token.EOF, "<EOF>")); |
| 34 | + } |
| 35 | + |
| 36 | + const nextToken = super.nextToken(); |
| 37 | + |
| 38 | + if (nextToken.channel === antlr4.Token.DEFAULT_CHANNEL) { |
| 39 | + this.lastToken = nextToken; |
| 40 | + } |
| 41 | + |
| 42 | + return this.tokens.length > 0 ? this.tokens.shift() : nextToken; |
| 43 | + } |
| 44 | + |
| 45 | + createDedent() { |
| 46 | + const dedent = this.commonToken(UVLJavaScriptLexer.DEDENT, ""); |
| 47 | + dedent.line = this.lastToken.line; |
| 48 | + return dedent; |
| 49 | + } |
| 50 | + |
| 51 | + commonToken(type, text) { |
| 52 | + const stop = this.getCharIndex() - 1; |
| 53 | + const start = text ? stop - text.length + 1 : stop; |
| 54 | + return new antlr4.CommonToken(this._tokenFactorySourcePair, type, antlr4.Token.DEFAULT_CHANNEL, start, stop); |
| 55 | + } |
| 56 | + |
| 57 | + static getIndentationCount(spaces) { |
| 58 | + let count = 0; |
| 59 | + for (const ch of spaces) { |
| 60 | + if (ch === '\t') { |
| 61 | + count += 8 - (count % 8); |
| 62 | + } else { |
| 63 | + count += 1; |
| 64 | + } |
| 65 | + } |
| 66 | + return count; |
| 67 | + } |
| 68 | + |
| 69 | + skipToken() { |
| 70 | + this.skip(); |
| 71 | + } |
| 72 | + |
| 73 | + atStartOfInput() { |
| 74 | + return this._interp.column === 0 && this._interp.line === 1; |
| 75 | + } |
| 76 | + |
| 77 | + handleNewline() { |
| 78 | + const newLine = this._interp.getText(this._input).replace(/[^\r\n\f]+/g, ""); |
| 79 | + const spaces = this._interp.getText(this._input).replace(/[\r\n\f]+/g, ""); |
| 80 | + const next = String.fromCharCode(this._input.LA(1)); |
| 81 | + |
| 82 | + if (this.opened > 0 || next === '\r' || next === '\n' || next === '\f' || next === '#') { |
| 83 | + this.skip(); |
| 84 | + } else { |
| 85 | + this.emitToken(this.commonToken(UVLJavaScriptLexer.NEWLINE, newLine)); |
| 86 | + |
| 87 | + const indent = UVLJavaScriptCustomLexer.getIndentationCount(spaces); |
| 88 | + const previous = this.indents.length === 0 ? 0 : this.indents[this.indents.length - 1]; |
| 89 | + |
| 90 | + if (indent === previous) { |
| 91 | + this.skip(); |
| 92 | + } else if (indent > previous) { |
| 93 | + this.indents.push(indent); |
| 94 | + this.emitToken(this.commonToken(UVLJavaScriptParser.INDENT, spaces)); |
| 95 | + } else { |
| 96 | + while (this.indents.length > 0 && this.indents[this.indents.length - 1] > indent) { |
| 97 | + this.emitToken(this.createDedent()); |
| 98 | + this.indents.pop(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments