UNPKG

psl-parser

Version:

A parser for the Profile Scripting Language

498 lines 14.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function* getTokens(documentContents) { const t = new Tokenizer(); for (const char of documentContents) { t.charType = getType(char); if (t.tokenType === 0) { t.tokenType = t.charType; } while (!t.parsed) { if (t.parseCharacter(char)) { yield t.token; } } t.parsed = false; } if (t.tokenType !== 0) { // if there is an unfinished token left t.finalizeToken(0); yield t.token; } } exports.getTokens = getTokens; class Token { constructor(type, value, position) { this.position = position; this.value = value; this.type = type; } getRange() { const startPosition = this.position; const endPosition = { line: this.position.line, character: this.position.character + this.value.length }; return new Range(startPosition, endPosition); } isWhiteSpace() { return this.type === 32 /* Space */ || this.type === 11 /* Tab */ || this.type === 13 /* NewLine */ || this.type === -1 /* Undefined */; } isAlphanumeric() { return this.type === 1 /* Alphanumeric */; } isNumeric() { return this.type === 2 /* Numeric */; } isLineComment() { return this.type === 3 /* LineComment */; } isBlockComment() { return this.type === 4 /* BlockComment */; } isString() { return this.type === 5 /* String */; } isLineCommentInit() { return this.type === 6 /* LineCommentInit */; } isBlockCommentInit() { return this.type === 7 /* BlockCommentInit */; } isBlockCommentTerm() { return this.type === 8 /* BlockCommentTerm */; } isDoubleQuotes() { return this.type === 9 /* DoubleQuotes */; } isSlash() { return this.type === 10 /* Slash */; } isTab() { return this.type === 11 /* Tab */; } isNewLine() { return this.type === 13 /* NewLine */; } isSpace() { return this.type === 32 /* Space */; } isExclamationMark() { return this.type === 33 /* ExclamationMark */; } isNumberSign() { return this.type === 35 /* NumberSign */; } isDollarSign() { return this.type === 36 /* DollarSign */; } isAmpersand() { return this.type === 38 /* Ampersand */; } isSingleQuote() { return this.type === 39 /* SingleQuote */; } isOpenParen() { return this.type === 40 /* OpenParen */; } isCloseParen() { return this.type === 41 /* CloseParen */; } isAsterisk() { return this.type === 42 /* Asterisk */; } isPlusSign() { return this.type === 43 /* PlusSign */; } isComma() { return this.type === 44 /* Comma */; } isMinusSign() { return this.type === 45 /* MinusSign */; } isPeriod() { return this.type === 46 /* Period */; } isColon() { return this.type === 58 /* Colon */; } isSemiColon() { return this.type === 59 /* SemiColon */; } isLessThan() { return this.type === 60 /* LessThan */; } isEqualSign() { return this.type === 61 /* EqualSign */; } isGreaterThan() { return this.type === 62 /* GreaterThan */; } isQuestionMark() { return this.type === 63 /* QuestionMark */; } isAtSymbol() { return this.type === 64 /* AtSymbol */; } isOpenBracket() { return this.type === 91 /* OpenBracket */; } isBackslash() { return this.type === 92 /* Backslash */; } isCloseBracket() { return this.type === 93 /* CloseBracket */; } isCaret() { return this.type === 94 /* Caret */; } isUnderscore() { return this.type === 95 /* Underscore */; } isBackQuote() { return this.type === 96 /* BackQuote */; } isOpenBrace() { return this.type === 123 /* OpenBrace */; } isPipe() { return this.type === 124 /* Pipe */; } isCloseBrace() { return this.type === 125 /* CloseBrace */; } isTilde() { return this.type === 126 /* Tilde */; } } exports.Token = Token; class Range { constructor(a, b, c, d) { if (typeof a === 'number' && typeof b === 'number' && typeof c === 'number' && typeof d === 'number') { this.start = new Position(a, b); this.end = new Position(c, d); } else { this.start = a; this.end = b; } } } exports.Range = Range; class Position { /** * @param line A zero-based line value. * @param character A zero-based character value. */ constructor(line, character) { this.line = line; this.character = character; } } exports.Position = Position; class Tokenizer { constructor() { this.documentLine = 0; this.documentColumn = 0; this.charType = 0; this.tokenType = 0; this.tokenValue = ''; this.tokenPosition = { line: this.documentLine, character: this.documentColumn }; this.parsed = false; this.stringOpen = false; this.firstSlash = false; this.asterisk = false; } parseCharacter(char) { if (this.tokenType === 1 /* Alphanumeric */) { if (this.charType === 1 /* Alphanumeric */ || this.charType === 2 /* Numeric */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; return false; } else { this.finalizeToken(this.charType); return true; } } else if (this.tokenType === 2 /* Numeric */) { if (this.charType === 2 /* Numeric */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; return false; } else { this.finalizeToken(this.charType); return true; } } else if (this.tokenType === 3 /* LineComment */) { if (this.charType === 13 /* NewLine */) { this.finalizeToken(13 /* NewLine */); return true; } else { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; return false; } } else if (this.tokenType === 4 /* BlockComment */) { if (this.asterisk) { // the previous char is * this.asterisk = false; if (this.charType === 10 /* Slash */) { // the last two chars are * / this.finalizeToken(8 /* BlockCommentTerm */); this.tokenValue = this.tokenValue + '*'; // add the * that was not yet added to the token this.documentColumn++; return true; } else { this.tokenValue = this.tokenValue + '*'; // add the * that was not yet added to the token this.documentColumn++; } } // do not add a * to the token immediately, it could be the end of a block comment if (this.charType === 42 /* Asterisk */) { this.asterisk = true; } else { this.tokenValue = this.tokenValue + char; if (this.charType === 13 /* NewLine */) { this.documentLine++; this.documentColumn = 0; } else { this.documentColumn++; } } this.parsed = true; return false; } else if (this.tokenType === 5 /* String */) { if (this.charType === 9 /* DoubleQuotes */) { this.finalizeToken(9 /* DoubleQuotes */); return true; } else { this.tokenValue = this.tokenValue + char; this.parsed = true; if (this.charType === 13 /* NewLine */) { this.documentLine++; this.documentColumn = 0; } else { this.documentColumn++; } return false; } } else if (this.tokenType === 6 /* LineCommentInit */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; this.finalizeToken(3 /* LineComment */); return true; } else if (this.tokenType === 7 /* BlockCommentInit */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; this.finalizeToken(4 /* BlockComment */); return true; } else if (this.tokenType === 8 /* BlockCommentTerm */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; this.finalizeToken(0); return true; } else if (this.tokenType === 9 /* DoubleQuotes */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; if (this.stringOpen) { this.stringOpen = false; this.finalizeToken(0); } else { this.stringOpen = true; this.finalizeToken(5 /* String */); } return true; } else if (this.tokenType === 10 /* Slash */) { if (this.firstSlash) { this.firstSlash = false; if (this.charType === 10 /* Slash */) { this.tokenType = 6 /* LineCommentInit */; return false; } else if (this.charType === 42 /* Asterisk */) { this.tokenType = 7 /* BlockCommentInit */; return false; } else { this.finalizeToken(this.charType); return true; } } else { this.firstSlash = true; this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; return false; } } else if (this.tokenType === 13 /* NewLine */) { this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentLine++; this.documentColumn = 0; this.finalizeToken(0); return true; } else if (this.tokenType > 10) { // all other token types this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; this.finalizeToken(0); return true; } else if (this.tokenType === -1) { // undefined this.tokenValue = this.tokenValue + char; this.parsed = true; this.documentColumn++; this.finalizeToken(0); return true; } return false; } finalizeToken(newType) { this.token = new Token(this.tokenType, this.tokenValue, this.tokenPosition); this.tokenType = newType; this.tokenValue = ''; this.tokenPosition = { line: this.documentLine, character: this.documentColumn }; } } function getType(c) { const charCode = c.charCodeAt(0); // Find a better way to incorporate the % if (charCode >= 65 && charCode <= 90 || charCode >= 97 && charCode <= 122 || charCode === 37) { return 1 /* Alphanumeric */; } else if (charCode >= 48 && charCode <= 57) { return 2 /* Numeric */; } else if (charCode === 34) { return 9 /* DoubleQuotes */; } else if (charCode === 47) { return 10 /* Slash */; } else if (charCode === 9) { return 11 /* Tab */; } else if (charCode === 10) { return 13 /* NewLine */; } else if (charCode === 32) { return 32 /* Space */; } else if (charCode === 33) { return 33 /* ExclamationMark */; } else if (charCode === 35) { return 35 /* NumberSign */; } else if (charCode === 36) { return 36 /* DollarSign */; // } else if (charCode === 37) { // return Type.PercentSign; } else if (charCode === 38) { return 38 /* Ampersand */; } else if (charCode === 39) { return 39 /* SingleQuote */; } else if (charCode === 40) { return 40 /* OpenParen */; } else if (charCode === 41) { return 41 /* CloseParen */; } else if (charCode === 42) { return 42 /* Asterisk */; } else if (charCode === 43) { return 43 /* PlusSign */; } else if (charCode === 44) { return 44 /* Comma */; } else if (charCode === 45) { return 45 /* MinusSign */; } else if (charCode === 46) { return 46 /* Period */; } else if (charCode === 58) { return 58 /* Colon */; } else if (charCode === 59) { return 59 /* SemiColon */; } else if (charCode === 60) { return 60 /* LessThan */; } else if (charCode === 61) { return 61 /* EqualSign */; } else if (charCode === 62) { return 62 /* GreaterThan */; } else if (charCode === 63) { return 63 /* QuestionMark */; } else if (charCode === 64) { return 64 /* AtSymbol */; } else if (charCode === 91) { return 91 /* OpenBracket */; } else if (charCode === 92) { return 92 /* Backslash */; } else if (charCode === 93) { return 93 /* CloseBracket */; } else if (charCode === 94) { return 94 /* Caret */; } else if (charCode === 95) { return 95 /* Underscore */; } else if (charCode === 96) { return 96 /* BackQuote */; } else if (charCode === 123) { return 123 /* OpenBrace */; } else if (charCode === 124) { return 124 /* Pipe */; } else if (charCode === 125) { return 125 /* CloseBrace */; } else if (charCode === 126) { return 126 /* Tilde */; } else { return -1 /* Undefined */; } } //# sourceMappingURL=tokenizer.js.map