| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- "use strict";
- var __extends = (this && this.__extends) || (function () {
- var extendStatics = function (d, b) {
- extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return extendStatics(d, b);
- };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- var __values = (this && this.__values) || function(o) {
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
- if (m) return m.call(o);
- if (o && typeof o.length === "number") return {
- next: function () {
- if (o && i >= o.length) o = void 0;
- return { value: o && o[i++], done: !o };
- }
- };
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
- };
- var __read = (this && this.__read) || function (o, n) {
- var m = typeof Symbol === "function" && o[Symbol.iterator];
- if (!m) return o;
- var i = m.call(o), r, ar = [], e;
- try {
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
- }
- catch (error) { e = { error: error }; }
- finally {
- try {
- if (r && !r.done && (m = i["return"])) m.call(i);
- }
- finally { if (e) throw e.error; }
- }
- return ar;
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Text = exports.Element = exports.AttributeValue = exports.Node = void 0;
- var types_1 = require("./types");
- var parser_1 = __importDefault(require("./parser"));
- var utils_1 = require("./utils");
- var Node = /** @class */ (function () {
- function Node(tagName) {
- this.tagName = tagName;
- }
- Node.prototype.dump = function () {
- return { type: this.tagName };
- };
- return Node;
- }());
- exports.Node = Node;
- var AttributeValue = /** @class */ (function () {
- function AttributeValue(value, start, end) {
- this.value = value;
- this.start = start;
- this.end = end;
- }
- return AttributeValue;
- }());
- exports.AttributeValue = AttributeValue;
- var Element = /** @class */ (function (_super) {
- __extends(Element, _super);
- function Element(tagName, attributes, children) {
- var _this = _super.call(this, tagName) || this;
- _this.attributes = attributes;
- _this.children = children;
- return _this;
- }
- Element.prototype.dump = function () {
- var e_1, _a;
- var attributes = {};
- if (this.attributes) {
- try {
- for (var _b = __values(this.attributes), _c = _b.next(); !_c.done; _c = _b.next()) {
- var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
- if (value && value.value) {
- attributes[key] = value.value;
- }
- else {
- attributes[key] = null;
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- }
- var children = this.children.map(function (child) { return child.dump(); });
- return { type: this.tagName, attributes: attributes, children: children };
- };
- return Element;
- }(Node));
- exports.Element = Element;
- var Text = /** @class */ (function (_super) {
- __extends(Text, _super);
- function Text(content, start, end) {
- var _this = _super.call(this, Text.tagName) || this;
- _this.content = content;
- _this.start = start;
- _this.end = end;
- return _this;
- }
- Text.prototype.dump = function () {
- return { type: this.tagName, content: this.content };
- };
- Text.tagName = 'text';
- return Text;
- }(Node));
- exports.Text = Text;
- /**
- * Simple wxml parser
- */
- var WxmlParser = /** @class */ (function (_super) {
- __extends(WxmlParser, _super);
- function WxmlParser(source, fileName) {
- if (fileName === void 0) { fileName = ''; }
- var _this = _super.call(this, source, fileName) || this;
- _this.source = source;
- _this.fileName = fileName;
- _this.state = 1 /* NORMAL */;
- return _this;
- }
- WxmlParser.prototype.parse = function () {
- try {
- var nodes = this._parse();
- return nodes;
- }
- catch (e) {
- if (this.fileName) {
- e.message += "\nfile: " + this.fileName;
- }
- e.message += "\nline: " + this.line + ", column: " + this.column + "\n\n" + this.currentContext();
- throw e;
- }
- };
- WxmlParser.prototype._parse = function () {
- var nodes = [];
- while (!this.eof()) {
- this.consumeWhitespace();
- if (this.match(60 /* LESS_THAN */) && this.match(47 /* SLASH */, this.pos + 1)) {
- break;
- }
- // Ignore comments
- if (this.match(60 /* LESS_THAN */, this.pos) &&
- this.match(33 /* EXCLAMATION */, this.pos + 1) &&
- this.match(45 /* MINUS */, this.pos + 2) &&
- this.match(45 /* MINUS */, this.pos + 3)) {
- this.advance(4);
- this.parseComments();
- continue;
- }
- if (this.match(60 /* LESS_THAN */)) {
- var node = this.parseWxmlTag();
- nodes.push(node);
- continue;
- }
- var textNode = this.parseText();
- if (textNode.content.length > 0)
- nodes.push(textNode);
- }
- return nodes;
- };
- WxmlParser.prototype.parseWxmlTag = function () {
- if (this.consumeChar() !== 60 /* LESS_THAN */) {
- throw new Error('unexpected character for wxml start tag');
- }
- this.consumeWhitespace();
- var tagName = this.parseTagName();
- if (!tagName || tagName.length === 0) {
- throw new Error("unexpected tag name " + this.currentChar());
- }
- var attributes = this.parseAttributes();
- // self-closing tag
- if (this.match(47 /* SLASH */)) {
- this.advance();
- if (this.consumeChar() !== 62 /* GREATER_THAN */) {
- throw new Error('unexpected character ' + this.currentChar());
- }
- return new Element(tagName, attributes, []);
- }
- if (this.consumeChar() !== 62 /* GREATER_THAN */) {
- throw new Error('expected character > to close a tag');
- }
- if (tagName.toLowerCase() === types_1.WXS_LITERAL) {
- this.state = 2 /* WXS */;
- }
- var childNodes = this.parse();
- if (this.consumeChar() !== 60 /* LESS_THAN */) {
- throw new Error('expected char ' + String.fromCharCode(60 /* LESS_THAN */) + ' but got ' + this.currentChar());
- }
- if (this.consumeChar() !== 47 /* SLASH */) {
- throw new Error('expected char ' + String.fromCharCode(47 /* SLASH */) + ' but got ' + this.currentChar());
- }
- this.consumeWhitespace();
- var endTagName = this.parseTagName();
- if (endTagName !== tagName) {
- throw new Error("expected tag name " + tagName + " but got " + endTagName);
- }
- this.consumeWhitespace();
- if (!this.match(62 /* GREATER_THAN */)) {
- throw new Error('expected char ' + String.fromCharCode(62 /* GREATER_THAN */) + ' but got ' + this.currentChar());
- }
- this.advance();
- if (tagName.toLowerCase() === types_1.WXS_LITERAL) {
- this.state = 1 /* NORMAL */;
- }
- return new Element(tagName, attributes, childNodes);
- };
- WxmlParser.prototype.parseText = function () {
- var start = this.pos;
- if (this.state === 2 /* WXS */) {
- while (!this.eof() && !(this.match(60 /* LESS_THAN */) && this.match(47 /* SLASH */, this.pos + 1))) {
- if (!this.consumeQuoteString() && !this.consumeWXSComments()) {
- this.advance();
- }
- }
- return new Text(this.source.substring(start, this.pos), start, this.pos);
- }
- return new Text(this.parseTextContents(), start, this.pos);
- };
- WxmlParser.prototype.parseTextContents = function () {
- var result = [];
- while (!this.eof() && (!this.match(60 /* LESS_THAN */) || this.state === 4 /* INT */)) {
- if (this.match(123 /* LEFT_CURLY_BRACE */) && this.match(123 /* LEFT_CURLY_BRACE */, this.pos + 1)) {
- this.state = 4 /* INT */;
- }
- if (this.match(125 /* RIGHT_CURLY_BRACE */) && this.match(125 /* RIGHT_CURLY_BRACE */, this.pos + 1)) {
- this.state = 1 /* NORMAL */;
- }
- var quoteStringRef = { result: '' };
- if (this.consumeQuoteString(quoteStringRef)) {
- result.push(quoteStringRef.result);
- continue;
- }
- var ch = this.source[this.pos];
- result.push(ch);
- this.advance();
- }
- return result.join('');
- };
- /**
- * Ignore comments
- */
- WxmlParser.prototype.parseComments = function () {
- while (!this.eof()) {
- if (this.match(45 /* MINUS */, this.pos) &&
- this.match(45 /* MINUS */, this.pos + 1) &&
- this.match(62 /* GREATER_THAN */, this.pos + 2)) {
- this.advance(3);
- return;
- }
- this.advance();
- }
- };
- WxmlParser.prototype.parseTagName = function () {
- // loosy check
- // TODO: prevent number as first letter
- return this.consumeWhile(function (c) { return utils_1.isLetter(c) || utils_1.isNumber(c); });
- };
- WxmlParser.prototype.parseAttributeName = function () {
- // loosy check
- // TODO: prevent number as first letter
- // Note: can have colon (:) in between
- return this.consumeWhile(function (c) { return utils_1.isLetter(c) || utils_1.isNumber(c) || c === 58 /* COLON */; });
- };
- WxmlParser.prototype.parseAttributes = function () {
- var attrs = new Map();
- while (!this.eof()) {
- this.consumeWhitespace();
- if (this.match(47 /* SLASH */) || this.match(62 /* GREATER_THAN */))
- break;
- if (!utils_1.isLetter(this.peekCharCode()) && !this.match(58 /* COLON */))
- break;
- var _a = this.parseAttribute(), name = _a.name, value = _a.value;
- attrs.set(name, value);
- }
- return attrs;
- };
- WxmlParser.prototype.parseAttribute = function () {
- var name = this.parseAttributeName();
- this.consumeWhitespace();
- if (!this.match(61 /* EQUALS */)) {
- return { name: name, value: null };
- }
- this.advance();
- this.consumeWhitespace();
- var value = this.parseAttrValue();
- return { name: name, value: value };
- };
- WxmlParser.prototype.parseAttrValue = function () {
- var leftQuote = this.consumeChar();
- var start = this.pos;
- if (leftQuote !== 39 /* SINGLE_QUOTE */ && leftQuote !== 34 /* DOUBLE_QUOTE */) {
- throw new Error("expected char " + String.fromCharCode(39 /* SINGLE_QUOTE */) + " or " + String.fromCharCode(34 /* DOUBLE_QUOTE */) + " " +
- ("but got " + String.fromCharCode(leftQuote)));
- }
- var value = this.consumeWhile(function (ch) { return ch !== leftQuote; });
- var end = this.pos;
- if (this.consumeChar() !== leftQuote) {
- throw new Error('expected char ' + String.fromCharCode(leftQuote) + ' to close an attribute');
- }
- var attribute = new AttributeValue(value, start, end);
- return attribute;
- };
- WxmlParser.prototype.consumeWXSComments = function () {
- if (this.match(47 /* SLASH */) && this.match(47 /* SLASH */, this.pos + 1)) {
- while (!this.eof()) {
- if (this.match(10 /* LINE_FEED */)) {
- this.advance();
- break;
- }
- if (this.match(13 /* CARRIAGE_RETURN */) && this.match(10 /* LINE_FEED */)) {
- this.advance(2);
- break;
- }
- // If no line end is met we should end at this point
- if (this.match(60 /* LESS_THAN */) && this.match(47 /* SLASH */, this.pos + 1)) {
- return false;
- }
- this.advance();
- }
- return true;
- }
- else if (this.match(47 /* SLASH */) && this.match(42 /* ASTERISK */, this.pos + 1)) {
- while (!this.eof()) {
- if (this.match(42 /* ASTERISK */) && this.match(47 /* SLASH */, this.pos + 1)) {
- this.advance(2);
- break;
- }
- this.advance();
- }
- return true;
- }
- return false;
- };
- return WxmlParser;
- }(parser_1.default));
- exports.default = WxmlParser;
|