File size: 8,848 Bytes
cbc76af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var Parrot;
(function (Parrot) {
    ///<reference path="tokens.ts" />
    (function (Lexer) {
        var Tokenizer = (function () {
            function Tokenizer(source) {
                this._source = source;
                this._currentIndex = 0;
            }
            Tokenizer.prototype.HasAvailableTokens = function () {
                return this._currentIndex < this._source.length;
            };

            Tokenizer.prototype.Peek = function () {
                var character = this._source.charAt(this._currentIndex);
                return character;
            };

            Tokenizer.prototype.Consume = function () {
                if (this._currentIndex >= this._source.length) {
                    throw new EndOfStreamException();
                }

                var character = this._source.charAt(this._currentIndex);
                this._currentIndex++;
                return character;
            };

            Tokenizer.prototype.ConsumeIdentifier = function () {
                var identifier = "";
                var character = this.Peek();
                while (this.IsIdTail(character)) {
                    this.Consume();
                    identifier += character;
                    character = this.Peek();
                }

                return identifier;
            };

            Tokenizer.prototype.ConsumeWhitespace = function () {
                var whitespace = "";
                var character = this.Peek();
                while (this.IsWhitespace(character)) {
                    this.Consume();
                    whitespace += character;
                    character = this.Peek();
                }

                return whitespace;
            };

            Tokenizer.prototype.ConsumeUntilNewLineOrEndOfStream = function () {
                var result = "";
                var character = this.Peek();
                while (!this.IsNewLine(character)) {
                    try  {
                        this.Consume();
                        result += character;
                        character = this.Peek();
                    } catch (EndOfStreamException) {
                        break;
                    }
                }

                return result;
            };

            Tokenizer.prototype.ConsumeQuotedStringLiteral = function (quote) {
                var result = this.Consume();
                ;
                var character = this.Peek();
                while (true) {
                    while (character != quote) {
                        this.Consume();
                        result += character;
                        character = this.Peek();
                    }
                    result += this.Consume();
                    if (this.Peek() != quote) {
                        break;
                    }
                    this.Consume();
                    character = this.Peek();
                }

                //result += this.Consume();
                return result;
            };

            Tokenizer.prototype.IsNewLine = function (character) {
                return character == "\r" || character == "\n";
            };

            Tokenizer.prototype.GetNextToken = function () {
                if (this.HasAvailableTokens()) {
                    var currentCharacter = this.Peek();

                    if (this.IsIdentifierHead(currentCharacter)) {
                        var token = new Parrot.Lexer.IdentifierToken(this._currentIndex, this.ConsumeIdentifier(), 1 /* Identifier */);
                        return token;
                    }

                    if (this.IsWhitespace(currentCharacter)) {
                        return new Parrot.Lexer.WhitespaceToken(this._currentIndex, this.ConsumeWhitespace(), 13 /* Whitespace */);
                    }

                    switch (currentCharacter) {
                        case ',':
                            this.Consume();
                            return new Parrot.Lexer.CommaToken(this._currentIndex);
                        case '(':
                            this.Consume();
                            return new Parrot.Lexer.OpenParenthesisToken(this._currentIndex);
                        case ')':
                            this.Consume();
                            return new Parrot.Lexer.CloseParenthesisToken(this._currentIndex);
                        case '[':
                            this.Consume();
                            return new Parrot.Lexer.OpenBracketToken(this._currentIndex);
                        case ']':
                            this.Consume();
                            return new Parrot.Lexer.CloseBracketToken(this._currentIndex);
                        case '=':
                            this.Consume();
                            return new Parrot.Lexer.EqualToken(this._currentIndex);
                        case '{':
                            this.Consume();
                            return new Parrot.Lexer.OpenBracesToken(this._currentIndex);
                        case '}':
                            this.Consume();
                            return new Parrot.Lexer.CloseBracesToken(this._currentIndex);
                        case '>':
                            this.Consume();
                            return new Parrot.Lexer.GreaterThanToken(this._currentIndex);
                        case '+':
                            this.Consume();
                            return new Parrot.Lexer.PlusToken(this._currentIndex);
                        case '|':
                            return new Parrot.Lexer.StringLiteralPipeToken(this._currentIndex, this.ConsumeUntilNewLineOrEndOfStream());
                        case '"':
                            return new Parrot.Lexer.QuotedStringLiteralToken(this._currentIndex, this.ConsumeQuotedStringLiteral("\""));
                        case '\'':
                            return new Parrot.Lexer.QuotedStringLiteralToken(this._currentIndex, this.ConsumeQuotedStringLiteral("\'"));
                        case '@':
                            this.Consume();
                            return new Parrot.Lexer.AtToken(this._currentIndex);
                        case '^':
                            this.Consume();
                            return new Parrot.Lexer.CarotToken(this._currentIndex);
                        case '\0':
                            return null;
                        default:
                            throw new UnexpectedTokenException("Unexpected token: " + currentCharacter);
                    }
                }

                return null;
            };

            Tokenizer.prototype.IsWhitespace = function (character) {
                return character == "\r" || character == "\n" || character == " " || character == "\f" || character == "\t" || character == "\u000B";
            };

            Tokenizer.prototype.IsIdentifierHead = function (character) {
                var isLetter = /[a-zA-Z]/;
                return character.match(isLetter) != null || character == "_" || character == "#" || character == ".";
            };

            Tokenizer.prototype.IsIdTail = function (character) {
                var isNumber = /[0-9]/;
                return character.match(isNumber) != null || this.IsIdentifierHead(character) || character == ":" || character == "-";
            };

            Tokenizer.prototype.Tokenize = function () {
                var token;
                var _tokens = [];
                while ((token = this.GetNextToken()) != null) {
                    _tokens.push(token);
                }

                return _tokens;
            };

            Tokenizer.prototype.Tokens = function () {
                return this.Tokenize();
            };
            return Tokenizer;
        })();
        Lexer.Tokenizer = Tokenizer;

        var EndOfStreamException = (function () {
            function EndOfStreamException() {
                this.Message = "Unexpected end of stream";
            }
            return EndOfStreamException;
        })();
        Lexer.EndOfStreamException = EndOfStreamException;

        var UnexpectedTokenException = (function () {
            function UnexpectedTokenException(message) {
                this.Message = message;
            }
            return UnexpectedTokenException;
        })();
        Lexer.UnexpectedTokenException = UnexpectedTokenException;
    })(Parrot.Lexer || (Parrot.Lexer = {}));
    var Lexer = Parrot.Lexer;
})(Parrot || (Parrot = {}));
//# sourceMappingURL=tokenizer.js.map