UNPKG

5.53 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2016 The ANTLR Project. All rights reserved.
4 * Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
5 */
6var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10 return c > 3 && r && Object.defineProperty(target, key, r), r;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.CodePointCharStream = void 0;
14const assert = require("assert");
15const IntStream_1 = require("./IntStream");
16const Interval_1 = require("./misc/Interval");
17const Decorators_1 = require("./Decorators");
18/**
19 * Alternative to {@link ANTLRInputStream} which treats the input
20 * as a series of Unicode code points, instead of a series of UTF-16
21 * code units.
22 *
23 * Use this if you need to parse input which potentially contains
24 * Unicode values > U+FFFF.
25 */
26class CodePointCharStream {
27 // Use the factory method {@link #fromBuffer(CodePointBuffer)} to
28 // construct instances of this type.
29 constructor(array, position, remaining, name) {
30 // TODO
31 assert(position === 0);
32 this._array = array;
33 this._size = remaining;
34 this._name = name;
35 this._position = 0;
36 }
37 get internalStorage() {
38 return this._array;
39 }
40 static fromBuffer(codePointBuffer, name) {
41 if (name === undefined || name.length === 0) {
42 name = IntStream_1.IntStream.UNKNOWN_SOURCE_NAME;
43 }
44 // Java lacks generics on primitive types.
45 //
46 // To avoid lots of calls to virtual methods in the
47 // very hot codepath of LA() below, we construct one
48 // of three concrete subclasses.
49 //
50 // The concrete subclasses directly access the code
51 // points stored in the underlying array (byte[],
52 // char[], or int[]), so we can avoid lots of virtual
53 // method calls to ByteBuffer.get(offset).
54 return new CodePointCharStream(codePointBuffer.array(), codePointBuffer.position, codePointBuffer.remaining, name);
55 }
56 consume() {
57 if (this._size - this._position === 0) {
58 assert(this.LA(1) === IntStream_1.IntStream.EOF);
59 throw new RangeError("cannot consume EOF");
60 }
61 this._position++;
62 }
63 get index() {
64 return this._position;
65 }
66 get size() {
67 return this._size;
68 }
69 /** mark/release do nothing; we have entire buffer */
70 mark() {
71 return -1;
72 }
73 release(marker) {
74 // No default implementation since this stream buffers the entire input
75 }
76 seek(index) {
77 this._position = index;
78 }
79 get sourceName() {
80 return this._name;
81 }
82 toString() {
83 return this.getText(Interval_1.Interval.of(0, this.size - 1));
84 }
85 LA(i) {
86 let offset;
87 switch (Math.sign(i)) {
88 case -1:
89 offset = this.index + i;
90 if (offset < 0) {
91 return IntStream_1.IntStream.EOF;
92 }
93 return this._array[offset];
94 case 0:
95 // Undefined
96 return 0;
97 case 1:
98 offset = this.index + i - 1;
99 if (offset >= this.size) {
100 return IntStream_1.IntStream.EOF;
101 }
102 return this._array[offset];
103 }
104 throw new RangeError("Not reached");
105 }
106 /** Return the UTF-16 encoded string for the given interval */
107 getText(interval) {
108 const startIdx = Math.min(interval.a, this.size);
109 const len = Math.min(interval.b - interval.a + 1, this.size - startIdx);
110 if (this._array instanceof Int32Array) {
111 return String.fromCodePoint(...Array.from(this._array.subarray(startIdx, startIdx + len)));
112 }
113 else {
114 return String.fromCharCode(...Array.from(this._array.subarray(startIdx, startIdx + len)));
115 }
116 }
117}
118__decorate([
119 Decorators_1.Override
120], CodePointCharStream.prototype, "consume", null);
121__decorate([
122 Decorators_1.Override
123], CodePointCharStream.prototype, "index", null);
124__decorate([
125 Decorators_1.Override
126], CodePointCharStream.prototype, "size", null);
127__decorate([
128 Decorators_1.Override
129], CodePointCharStream.prototype, "mark", null);
130__decorate([
131 Decorators_1.Override
132], CodePointCharStream.prototype, "release", null);
133__decorate([
134 Decorators_1.Override
135], CodePointCharStream.prototype, "seek", null);
136__decorate([
137 Decorators_1.Override
138], CodePointCharStream.prototype, "sourceName", null);
139__decorate([
140 Decorators_1.Override
141], CodePointCharStream.prototype, "toString", null);
142__decorate([
143 Decorators_1.Override
144], CodePointCharStream.prototype, "LA", null);
145__decorate([
146 Decorators_1.Override
147], CodePointCharStream.prototype, "getText", null);
148exports.CodePointCharStream = CodePointCharStream;
149//# sourceMappingURL=CodePointCharStream.js.map
\No newline at end of file