fluent-syntax
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -7,2 +7,24 @@ # Changelog | ||
## fluent-syntax 0.4.0 (May 17th, 2017) | ||
- Introduce the FluentParser and the FluentSerializer classes. | ||
Instances can be used to store config for multiple parse/serialize | ||
operations. | ||
The fluent-syntax package still exports the `parse` and `serialize` | ||
helpers. For more fine-grained control, `FluentParser` and | ||
`FluentSerializer` may be used for instantiation. | ||
- Build compat.js using babel-plugin-transform-builtin-extend. | ||
This ensures the correct behavior of `err instanceof ParseError`. | ||
- The compat build is now transpiled using rollup-plugin-babel and | ||
babel-plugin-transform-builtin-extend. | ||
This ensures that the "use strict" pragma is scoped to the UMD wrapper. It | ||
also correctly parses the top-level "this" keyword (which the previous | ||
setup turned into "undefined"). | ||
## fluent-syntax 0.3.0 | ||
@@ -9,0 +31,0 @@ |
3051
compat.js
@@ -1,1416 +0,1577 @@ | ||
'use strict'; | ||
/* fluent-syntax@0.3.0 */ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define('fluent-syntax', ['exports'], factory) : | ||
(factory((global.FluentSyntax = global.FluentSyntax || {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/* fluent-syntax@0.3.0 */ | ||
(function (global, factory) { | ||
(typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define('fluent-syntax', ['exports'], factory) : factory(global.FluentSyntax = global.FluentSyntax || {}); | ||
})(undefined, function (exports) { | ||
'use strict'; | ||
var Node = function Node() { | ||
_classCallCheck(this, Node); | ||
}; | ||
var Resource = function (_Node) { | ||
_inherits(Resource, _Node); | ||
function Resource() { | ||
var body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var comment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
_classCallCheck(this, Resource); | ||
var _this = _possibleConstructorReturn(this, (Resource.__proto__ || Object.getPrototypeOf(Resource)).call(this)); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
_this.type = 'Resource'; | ||
_this.body = body; | ||
_this.comment = comment; | ||
return _this; | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
return Resource; | ||
}(Node); | ||
var Entry = function (_Node2) { | ||
_inherits(Entry, _Node2); | ||
function Entry() { | ||
var span = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var annotations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
_classCallCheck(this, Entry); | ||
var _this2 = _possibleConstructorReturn(this, (Entry.__proto__ || Object.getPrototypeOf(Entry)).call(this)); | ||
_this2.type = 'Entry'; | ||
_this2.span = span; | ||
_this2.annotations = annotations; | ||
return _this2; | ||
} | ||
_createClass(Entry, [{ | ||
key: 'addSpan', | ||
value: function addSpan(start, end) { | ||
this.span = new Span(start, end); | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
var slicedToArray = function () { | ||
function sliceIterator(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
}, { | ||
key: 'addAnnotation', | ||
value: function addAnnotation(annot) { | ||
this.annotations.push(annot); | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"]) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
}]); | ||
} | ||
return Entry; | ||
}(Node); | ||
return _arr; | ||
} | ||
var Message = function (_Entry) { | ||
_inherits(Message, _Entry); | ||
return function (arr, i) { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else if (Symbol.iterator in Object(arr)) { | ||
return sliceIterator(arr, i); | ||
} else { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
}; | ||
}(); | ||
function Message(id) { | ||
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var attributes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
var tags = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
var comment = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null; | ||
var span = arguments[5]; | ||
var annotations = arguments[6]; | ||
var Node = function Node() { | ||
classCallCheck(this, Node); | ||
}; | ||
_classCallCheck(this, Message); | ||
var Resource = function (_Node) { | ||
inherits(Resource, _Node); | ||
var _this3 = _possibleConstructorReturn(this, (Message.__proto__ || Object.getPrototypeOf(Message)).call(this, span, annotations)); | ||
function Resource() { | ||
var body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var comment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
classCallCheck(this, Resource); | ||
_this3.type = 'Message'; | ||
_this3.id = id; | ||
_this3.value = value; | ||
_this3.attributes = attributes; | ||
_this3.tags = tags; | ||
_this3.comment = comment; | ||
return _this3; | ||
} | ||
var _this = possibleConstructorReturn(this, (Resource.__proto__ || Object.getPrototypeOf(Resource)).call(this)); | ||
return Message; | ||
}(Entry); | ||
_this.type = 'Resource'; | ||
_this.body = body; | ||
_this.comment = comment; | ||
return _this; | ||
} | ||
var Pattern = function (_Node3) { | ||
_inherits(Pattern, _Node3); | ||
return Resource; | ||
}(Node); | ||
function Pattern(elements) { | ||
_classCallCheck(this, Pattern); | ||
var Entry = function (_Node2) { | ||
inherits(Entry, _Node2); | ||
var _this4 = _possibleConstructorReturn(this, (Pattern.__proto__ || Object.getPrototypeOf(Pattern)).call(this)); | ||
function Entry() { | ||
var span = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var annotations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
classCallCheck(this, Entry); | ||
_this4.type = 'Pattern'; | ||
_this4.elements = elements; | ||
return _this4; | ||
var _this2 = possibleConstructorReturn(this, (Entry.__proto__ || Object.getPrototypeOf(Entry)).call(this)); | ||
_this2.type = 'Entry'; | ||
_this2.span = span; | ||
_this2.annotations = annotations; | ||
return _this2; | ||
} | ||
createClass(Entry, [{ | ||
key: 'addSpan', | ||
value: function addSpan(start, end) { | ||
this.span = new Span(start, end); | ||
} | ||
}, { | ||
key: 'addAnnotation', | ||
value: function addAnnotation(annot) { | ||
this.annotations.push(annot); | ||
} | ||
}]); | ||
return Entry; | ||
}(Node); | ||
return Pattern; | ||
}(Node); | ||
var Message = function (_Entry) { | ||
inherits(Message, _Entry); | ||
var TextElement = function (_Node4) { | ||
_inherits(TextElement, _Node4); | ||
function Message(id) { | ||
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var attributes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
var tags = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
var comment = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null; | ||
var span = arguments[5]; | ||
var annotations = arguments[6]; | ||
classCallCheck(this, Message); | ||
function TextElement(value) { | ||
_classCallCheck(this, TextElement); | ||
var _this3 = possibleConstructorReturn(this, (Message.__proto__ || Object.getPrototypeOf(Message)).call(this, span, annotations)); | ||
var _this5 = _possibleConstructorReturn(this, (TextElement.__proto__ || Object.getPrototypeOf(TextElement)).call(this)); | ||
_this3.type = 'Message'; | ||
_this3.id = id; | ||
_this3.value = value; | ||
_this3.attributes = attributes; | ||
_this3.tags = tags; | ||
_this3.comment = comment; | ||
return _this3; | ||
} | ||
_this5.type = 'TextElement'; | ||
_this5.value = value; | ||
return _this5; | ||
} | ||
return Message; | ||
}(Entry); | ||
return TextElement; | ||
}(Node); | ||
var Pattern = function (_Node3) { | ||
inherits(Pattern, _Node3); | ||
var Expression = function (_Node5) { | ||
_inherits(Expression, _Node5); | ||
function Pattern(elements) { | ||
classCallCheck(this, Pattern); | ||
function Expression() { | ||
_classCallCheck(this, Expression); | ||
var _this4 = possibleConstructorReturn(this, (Pattern.__proto__ || Object.getPrototypeOf(Pattern)).call(this)); | ||
var _this6 = _possibleConstructorReturn(this, (Expression.__proto__ || Object.getPrototypeOf(Expression)).call(this)); | ||
_this4.type = 'Pattern'; | ||
_this4.elements = elements; | ||
return _this4; | ||
} | ||
_this6.type = 'Expression'; | ||
return _this6; | ||
} | ||
return Pattern; | ||
}(Node); | ||
return Expression; | ||
}(Node); | ||
var TextElement = function (_Node4) { | ||
inherits(TextElement, _Node4); | ||
var StringExpression = function (_Expression) { | ||
_inherits(StringExpression, _Expression); | ||
function TextElement(value) { | ||
classCallCheck(this, TextElement); | ||
function StringExpression(value) { | ||
_classCallCheck(this, StringExpression); | ||
var _this5 = possibleConstructorReturn(this, (TextElement.__proto__ || Object.getPrototypeOf(TextElement)).call(this)); | ||
var _this7 = _possibleConstructorReturn(this, (StringExpression.__proto__ || Object.getPrototypeOf(StringExpression)).call(this)); | ||
_this5.type = 'TextElement'; | ||
_this5.value = value; | ||
return _this5; | ||
} | ||
_this7.type = 'StringExpression'; | ||
_this7.value = value; | ||
return _this7; | ||
} | ||
return TextElement; | ||
}(Node); | ||
return StringExpression; | ||
}(Expression); | ||
var Expression = function (_Node5) { | ||
inherits(Expression, _Node5); | ||
var NumberExpression = function (_Expression2) { | ||
_inherits(NumberExpression, _Expression2); | ||
function Expression() { | ||
classCallCheck(this, Expression); | ||
function NumberExpression(value) { | ||
_classCallCheck(this, NumberExpression); | ||
var _this6 = possibleConstructorReturn(this, (Expression.__proto__ || Object.getPrototypeOf(Expression)).call(this)); | ||
var _this8 = _possibleConstructorReturn(this, (NumberExpression.__proto__ || Object.getPrototypeOf(NumberExpression)).call(this)); | ||
_this6.type = 'Expression'; | ||
return _this6; | ||
} | ||
_this8.type = 'NumberExpression'; | ||
_this8.value = value; | ||
return _this8; | ||
} | ||
return Expression; | ||
}(Node); | ||
return NumberExpression; | ||
}(Expression); | ||
var StringExpression = function (_Expression) { | ||
inherits(StringExpression, _Expression); | ||
var MessageReference = function (_Expression3) { | ||
_inherits(MessageReference, _Expression3); | ||
function StringExpression(value) { | ||
classCallCheck(this, StringExpression); | ||
function MessageReference(id) { | ||
_classCallCheck(this, MessageReference); | ||
var _this7 = possibleConstructorReturn(this, (StringExpression.__proto__ || Object.getPrototypeOf(StringExpression)).call(this)); | ||
var _this9 = _possibleConstructorReturn(this, (MessageReference.__proto__ || Object.getPrototypeOf(MessageReference)).call(this)); | ||
_this7.type = 'StringExpression'; | ||
_this7.value = value; | ||
return _this7; | ||
} | ||
_this9.type = 'MessageReference'; | ||
_this9.id = id; | ||
return _this9; | ||
} | ||
return StringExpression; | ||
}(Expression); | ||
return MessageReference; | ||
}(Expression); | ||
var NumberExpression = function (_Expression2) { | ||
inherits(NumberExpression, _Expression2); | ||
var ExternalArgument = function (_Expression4) { | ||
_inherits(ExternalArgument, _Expression4); | ||
function NumberExpression(value) { | ||
classCallCheck(this, NumberExpression); | ||
function ExternalArgument(id) { | ||
_classCallCheck(this, ExternalArgument); | ||
var _this8 = possibleConstructorReturn(this, (NumberExpression.__proto__ || Object.getPrototypeOf(NumberExpression)).call(this)); | ||
var _this10 = _possibleConstructorReturn(this, (ExternalArgument.__proto__ || Object.getPrototypeOf(ExternalArgument)).call(this)); | ||
_this8.type = 'NumberExpression'; | ||
_this8.value = value; | ||
return _this8; | ||
} | ||
_this10.type = 'ExternalArgument'; | ||
_this10.id = id; | ||
return _this10; | ||
} | ||
return NumberExpression; | ||
}(Expression); | ||
return ExternalArgument; | ||
}(Expression); | ||
var MessageReference = function (_Expression3) { | ||
inherits(MessageReference, _Expression3); | ||
var SelectExpression = function (_Expression5) { | ||
_inherits(SelectExpression, _Expression5); | ||
function MessageReference(id) { | ||
classCallCheck(this, MessageReference); | ||
function SelectExpression(expression, variants) { | ||
_classCallCheck(this, SelectExpression); | ||
var _this9 = possibleConstructorReturn(this, (MessageReference.__proto__ || Object.getPrototypeOf(MessageReference)).call(this)); | ||
var _this11 = _possibleConstructorReturn(this, (SelectExpression.__proto__ || Object.getPrototypeOf(SelectExpression)).call(this)); | ||
_this9.type = 'MessageReference'; | ||
_this9.id = id; | ||
return _this9; | ||
} | ||
_this11.type = 'SelectExpression'; | ||
_this11.expression = expression; | ||
_this11.variants = variants; | ||
return _this11; | ||
} | ||
return MessageReference; | ||
}(Expression); | ||
return SelectExpression; | ||
}(Expression); | ||
var ExternalArgument = function (_Expression4) { | ||
inherits(ExternalArgument, _Expression4); | ||
var AttributeExpression = function (_Expression6) { | ||
_inherits(AttributeExpression, _Expression6); | ||
function ExternalArgument(id) { | ||
classCallCheck(this, ExternalArgument); | ||
function AttributeExpression(id, name) { | ||
_classCallCheck(this, AttributeExpression); | ||
var _this10 = possibleConstructorReturn(this, (ExternalArgument.__proto__ || Object.getPrototypeOf(ExternalArgument)).call(this)); | ||
var _this12 = _possibleConstructorReturn(this, (AttributeExpression.__proto__ || Object.getPrototypeOf(AttributeExpression)).call(this)); | ||
_this10.type = 'ExternalArgument'; | ||
_this10.id = id; | ||
return _this10; | ||
} | ||
_this12.type = 'AttributeExpression'; | ||
_this12.id = id; | ||
_this12.name = name; | ||
return _this12; | ||
} | ||
return ExternalArgument; | ||
}(Expression); | ||
return AttributeExpression; | ||
}(Expression); | ||
var SelectExpression = function (_Expression5) { | ||
inherits(SelectExpression, _Expression5); | ||
var VariantExpression = function (_Expression7) { | ||
_inherits(VariantExpression, _Expression7); | ||
function SelectExpression(expression, variants) { | ||
classCallCheck(this, SelectExpression); | ||
function VariantExpression(id, key) { | ||
_classCallCheck(this, VariantExpression); | ||
var _this11 = possibleConstructorReturn(this, (SelectExpression.__proto__ || Object.getPrototypeOf(SelectExpression)).call(this)); | ||
var _this13 = _possibleConstructorReturn(this, (VariantExpression.__proto__ || Object.getPrototypeOf(VariantExpression)).call(this)); | ||
_this11.type = 'SelectExpression'; | ||
_this11.expression = expression; | ||
_this11.variants = variants; | ||
return _this11; | ||
} | ||
_this13.type = 'VariantExpression'; | ||
_this13.id = id; | ||
_this13.key = key; | ||
return _this13; | ||
} | ||
return SelectExpression; | ||
}(Expression); | ||
return VariantExpression; | ||
}(Expression); | ||
var AttributeExpression = function (_Expression6) { | ||
inherits(AttributeExpression, _Expression6); | ||
var CallExpression = function (_Expression8) { | ||
_inherits(CallExpression, _Expression8); | ||
function AttributeExpression(id, name) { | ||
classCallCheck(this, AttributeExpression); | ||
function CallExpression(callee, args) { | ||
_classCallCheck(this, CallExpression); | ||
var _this12 = possibleConstructorReturn(this, (AttributeExpression.__proto__ || Object.getPrototypeOf(AttributeExpression)).call(this)); | ||
var _this14 = _possibleConstructorReturn(this, (CallExpression.__proto__ || Object.getPrototypeOf(CallExpression)).call(this)); | ||
_this12.type = 'AttributeExpression'; | ||
_this12.id = id; | ||
_this12.name = name; | ||
return _this12; | ||
} | ||
_this14.type = 'CallExpression'; | ||
_this14.callee = callee; | ||
_this14.args = args; | ||
return _this14; | ||
} | ||
return AttributeExpression; | ||
}(Expression); | ||
return CallExpression; | ||
}(Expression); | ||
var VariantExpression = function (_Expression7) { | ||
inherits(VariantExpression, _Expression7); | ||
var Attribute = function (_Node6) { | ||
_inherits(Attribute, _Node6); | ||
function VariantExpression(id, key) { | ||
classCallCheck(this, VariantExpression); | ||
function Attribute(id, value) { | ||
_classCallCheck(this, Attribute); | ||
var _this13 = possibleConstructorReturn(this, (VariantExpression.__proto__ || Object.getPrototypeOf(VariantExpression)).call(this)); | ||
var _this15 = _possibleConstructorReturn(this, (Attribute.__proto__ || Object.getPrototypeOf(Attribute)).call(this)); | ||
_this13.type = 'VariantExpression'; | ||
_this13.id = id; | ||
_this13.key = key; | ||
return _this13; | ||
} | ||
_this15.type = 'Attribute'; | ||
_this15.id = id; | ||
_this15.value = value; | ||
return _this15; | ||
} | ||
return VariantExpression; | ||
}(Expression); | ||
return Attribute; | ||
}(Node); | ||
var CallExpression = function (_Expression8) { | ||
inherits(CallExpression, _Expression8); | ||
var Tag = function (_Node7) { | ||
_inherits(Tag, _Node7); | ||
function CallExpression(callee, args) { | ||
classCallCheck(this, CallExpression); | ||
function Tag(name) { | ||
_classCallCheck(this, Tag); | ||
var _this14 = possibleConstructorReturn(this, (CallExpression.__proto__ || Object.getPrototypeOf(CallExpression)).call(this)); | ||
var _this16 = _possibleConstructorReturn(this, (Tag.__proto__ || Object.getPrototypeOf(Tag)).call(this)); | ||
_this14.type = 'CallExpression'; | ||
_this14.callee = callee; | ||
_this14.args = args; | ||
return _this14; | ||
} | ||
_this16.type = 'Tag'; | ||
_this16.name = name; | ||
return _this16; | ||
} | ||
return CallExpression; | ||
}(Expression); | ||
return Tag; | ||
}(Node); | ||
var Attribute = function (_Node6) { | ||
inherits(Attribute, _Node6); | ||
var Variant = function (_Node8) { | ||
_inherits(Variant, _Node8); | ||
function Attribute(id, value) { | ||
classCallCheck(this, Attribute); | ||
function Variant(key, value) { | ||
var def = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var _this15 = possibleConstructorReturn(this, (Attribute.__proto__ || Object.getPrototypeOf(Attribute)).call(this)); | ||
_classCallCheck(this, Variant); | ||
_this15.type = 'Attribute'; | ||
_this15.id = id; | ||
_this15.value = value; | ||
return _this15; | ||
} | ||
var _this17 = _possibleConstructorReturn(this, (Variant.__proto__ || Object.getPrototypeOf(Variant)).call(this)); | ||
return Attribute; | ||
}(Node); | ||
_this17.type = 'Variant'; | ||
_this17.key = key; | ||
_this17.value = value; | ||
_this17.default = def; | ||
return _this17; | ||
} | ||
var Tag = function (_Node7) { | ||
inherits(Tag, _Node7); | ||
return Variant; | ||
}(Node); | ||
function Tag(name) { | ||
classCallCheck(this, Tag); | ||
var NamedArgument = function (_Node9) { | ||
_inherits(NamedArgument, _Node9); | ||
var _this16 = possibleConstructorReturn(this, (Tag.__proto__ || Object.getPrototypeOf(Tag)).call(this)); | ||
function NamedArgument(name, val) { | ||
_classCallCheck(this, NamedArgument); | ||
_this16.type = 'Tag'; | ||
_this16.name = name; | ||
return _this16; | ||
} | ||
var _this18 = _possibleConstructorReturn(this, (NamedArgument.__proto__ || Object.getPrototypeOf(NamedArgument)).call(this)); | ||
return Tag; | ||
}(Node); | ||
_this18.type = 'NamedArgument'; | ||
_this18.name = name; | ||
_this18.val = val; | ||
return _this18; | ||
} | ||
var Variant = function (_Node8) { | ||
inherits(Variant, _Node8); | ||
return NamedArgument; | ||
}(Node); | ||
function Variant(key, value) { | ||
var def = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
classCallCheck(this, Variant); | ||
var Identifier = function (_Node10) { | ||
_inherits(Identifier, _Node10); | ||
var _this17 = possibleConstructorReturn(this, (Variant.__proto__ || Object.getPrototypeOf(Variant)).call(this)); | ||
function Identifier(name) { | ||
_classCallCheck(this, Identifier); | ||
_this17.type = 'Variant'; | ||
_this17.key = key; | ||
_this17.value = value; | ||
_this17.default = def; | ||
return _this17; | ||
} | ||
var _this19 = _possibleConstructorReturn(this, (Identifier.__proto__ || Object.getPrototypeOf(Identifier)).call(this)); | ||
return Variant; | ||
}(Node); | ||
_this19.type = 'Identifier'; | ||
_this19.name = name; | ||
return _this19; | ||
} | ||
var NamedArgument = function (_Node9) { | ||
inherits(NamedArgument, _Node9); | ||
return Identifier; | ||
}(Node); | ||
function NamedArgument(name, val) { | ||
classCallCheck(this, NamedArgument); | ||
var Symbol$1 = function (_Identifier) { | ||
_inherits(Symbol$1, _Identifier); | ||
var _this18 = possibleConstructorReturn(this, (NamedArgument.__proto__ || Object.getPrototypeOf(NamedArgument)).call(this)); | ||
function Symbol$1(name) { | ||
_classCallCheck(this, Symbol$1); | ||
_this18.type = 'NamedArgument'; | ||
_this18.name = name; | ||
_this18.val = val; | ||
return _this18; | ||
} | ||
var _this20 = _possibleConstructorReturn(this, (Symbol$1.__proto__ || Object.getPrototypeOf(Symbol$1)).call(this, name)); | ||
return NamedArgument; | ||
}(Node); | ||
_this20.type = 'Symbol'; | ||
return _this20; | ||
} | ||
var Identifier = function (_Node10) { | ||
inherits(Identifier, _Node10); | ||
return Symbol$1; | ||
}(Identifier); | ||
function Identifier(name) { | ||
classCallCheck(this, Identifier); | ||
var Comment = function (_Entry2) { | ||
_inherits(Comment, _Entry2); | ||
var _this19 = possibleConstructorReturn(this, (Identifier.__proto__ || Object.getPrototypeOf(Identifier)).call(this)); | ||
function Comment(content, span, annotations) { | ||
_classCallCheck(this, Comment); | ||
_this19.type = 'Identifier'; | ||
_this19.name = name; | ||
return _this19; | ||
} | ||
var _this21 = _possibleConstructorReturn(this, (Comment.__proto__ || Object.getPrototypeOf(Comment)).call(this, span, annotations)); | ||
return Identifier; | ||
}(Node); | ||
_this21.type = 'Comment'; | ||
_this21.content = content; | ||
return _this21; | ||
} | ||
var _Symbol = function (_Identifier) { | ||
inherits(_Symbol, _Identifier); | ||
return Comment; | ||
}(Entry); | ||
function _Symbol(name) { | ||
classCallCheck(this, _Symbol); | ||
var Section = function (_Entry3) { | ||
_inherits(Section, _Entry3); | ||
var _this20 = possibleConstructorReturn(this, (_Symbol.__proto__ || Object.getPrototypeOf(_Symbol)).call(this, name)); | ||
function Section(name) { | ||
var comment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var span = arguments[2]; | ||
var annotations = arguments[3]; | ||
_this20.type = 'Symbol'; | ||
return _this20; | ||
} | ||
_classCallCheck(this, Section); | ||
return _Symbol; | ||
}(Identifier); | ||
var _this22 = _possibleConstructorReturn(this, (Section.__proto__ || Object.getPrototypeOf(Section)).call(this, span, annotations)); | ||
var Comment = function (_Entry2) { | ||
inherits(Comment, _Entry2); | ||
_this22.type = 'Section'; | ||
_this22.name = name; | ||
_this22.comment = comment; | ||
return _this22; | ||
} | ||
function Comment(content, span, annotations) { | ||
classCallCheck(this, Comment); | ||
return Section; | ||
}(Entry); | ||
var _this21 = possibleConstructorReturn(this, (Comment.__proto__ || Object.getPrototypeOf(Comment)).call(this, span, annotations)); | ||
var Function = function (_Identifier2) { | ||
_inherits(Function, _Identifier2); | ||
_this21.type = 'Comment'; | ||
_this21.content = content; | ||
return _this21; | ||
} | ||
function Function(name) { | ||
_classCallCheck(this, Function); | ||
return Comment; | ||
}(Entry); | ||
var _this23 = _possibleConstructorReturn(this, (Function.__proto__ || Object.getPrototypeOf(Function)).call(this, name)); | ||
var Section = function (_Entry3) { | ||
inherits(Section, _Entry3); | ||
_this23.type = 'Function'; | ||
return _this23; | ||
} | ||
function Section(name) { | ||
var comment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var span = arguments[2]; | ||
var annotations = arguments[3]; | ||
classCallCheck(this, Section); | ||
return Function; | ||
}(Identifier); | ||
var _this22 = possibleConstructorReturn(this, (Section.__proto__ || Object.getPrototypeOf(Section)).call(this, span, annotations)); | ||
var Junk = function (_Entry4) { | ||
_inherits(Junk, _Entry4); | ||
_this22.type = 'Section'; | ||
_this22.name = name; | ||
_this22.comment = comment; | ||
return _this22; | ||
} | ||
function Junk(content, span, annotations) { | ||
_classCallCheck(this, Junk); | ||
return Section; | ||
}(Entry); | ||
var _this24 = _possibleConstructorReturn(this, (Junk.__proto__ || Object.getPrototypeOf(Junk)).call(this, span, annotations)); | ||
var Function$1 = function (_Identifier2) { | ||
inherits(Function, _Identifier2); | ||
_this24.type = 'Junk'; | ||
_this24.content = content; | ||
return _this24; | ||
} | ||
function Function(name) { | ||
classCallCheck(this, Function); | ||
return Junk; | ||
}(Entry); | ||
var _this23 = possibleConstructorReturn(this, (Function.__proto__ || Object.getPrototypeOf(Function)).call(this, name)); | ||
var Span = function (_Node11) { | ||
_inherits(Span, _Node11); | ||
_this23.type = 'Function'; | ||
return _this23; | ||
} | ||
function Span(start, end) { | ||
_classCallCheck(this, Span); | ||
return Function; | ||
}(Identifier); | ||
var _this25 = _possibleConstructorReturn(this, (Span.__proto__ || Object.getPrototypeOf(Span)).call(this)); | ||
var Junk = function (_Entry4) { | ||
inherits(Junk, _Entry4); | ||
_this25.type = 'Span'; | ||
_this25.start = start; | ||
_this25.end = end; | ||
return _this25; | ||
} | ||
function Junk(content, span, annotations) { | ||
classCallCheck(this, Junk); | ||
return Span; | ||
}(Node); | ||
var _this24 = possibleConstructorReturn(this, (Junk.__proto__ || Object.getPrototypeOf(Junk)).call(this, span, annotations)); | ||
var Annotation = function (_Node12) { | ||
_inherits(Annotation, _Node12); | ||
_this24.type = 'Junk'; | ||
_this24.content = content; | ||
return _this24; | ||
} | ||
function Annotation(code) { | ||
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
var message = arguments[2]; | ||
return Junk; | ||
}(Entry); | ||
_classCallCheck(this, Annotation); | ||
var Span = function (_Node11) { | ||
inherits(Span, _Node11); | ||
var _this26 = _possibleConstructorReturn(this, (Annotation.__proto__ || Object.getPrototypeOf(Annotation)).call(this)); | ||
function Span(start, end) { | ||
classCallCheck(this, Span); | ||
_this26.type = 'Annotation'; | ||
_this26.code = code; | ||
_this26.args = args; | ||
_this26.message = message; | ||
return _this26; | ||
} | ||
var _this25 = possibleConstructorReturn(this, (Span.__proto__ || Object.getPrototypeOf(Span)).call(this)); | ||
_createClass(Annotation, [{ | ||
key: 'addSpan', | ||
value: function addSpan(start, end) { | ||
this.span = new Span(start, end); | ||
} | ||
}]); | ||
_this25.type = 'Span'; | ||
_this25.start = start; | ||
_this25.end = end; | ||
return _this25; | ||
} | ||
return Annotation; | ||
}(Node); | ||
return Span; | ||
}(Node); | ||
var ParserStream = function () { | ||
function ParserStream(string) { | ||
_classCallCheck(this, ParserStream); | ||
var Annotation = function (_Node12) { | ||
inherits(Annotation, _Node12); | ||
this.string = string; | ||
this.iter = string[Symbol.iterator](); | ||
this.buf = []; | ||
this.peekIndex = 0; | ||
this.index = 0; | ||
function Annotation(code) { | ||
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
var message = arguments[2]; | ||
classCallCheck(this, Annotation); | ||
this.iterEnd = false; | ||
this.peekEnd = false; | ||
var _this26 = possibleConstructorReturn(this, (Annotation.__proto__ || Object.getPrototypeOf(Annotation)).call(this)); | ||
this.ch = this.iter.next().value; | ||
_this26.type = 'Annotation'; | ||
_this26.code = code; | ||
_this26.args = args; | ||
_this26.message = message; | ||
return _this26; | ||
} | ||
createClass(Annotation, [{ | ||
key: 'addSpan', | ||
value: function addSpan(start, end) { | ||
this.span = new Span(start, end); | ||
} | ||
}]); | ||
return Annotation; | ||
}(Node); | ||
_createClass(ParserStream, [{ | ||
key: 'next', | ||
value: function next() { | ||
if (this.iterEnd) { | ||
return undefined; | ||
} | ||
var ParserStream = function () { | ||
function ParserStream(string) { | ||
classCallCheck(this, ParserStream); | ||
if (this.buf.length === 0) { | ||
this.ch = this.iter.next().value; | ||
} else { | ||
this.ch = this.buf.shift(); | ||
} | ||
this.string = string; | ||
this.iter = string[Symbol.iterator](); | ||
this.buf = []; | ||
this.peekIndex = 0; | ||
this.index = 0; | ||
this.index++; | ||
this.iterEnd = false; | ||
this.peekEnd = false; | ||
if (this.ch === undefined) { | ||
this.iterEnd = true; | ||
this.peekEnd = true; | ||
} | ||
this.ch = this.iter.next().value; | ||
} | ||
this.peekIndex = this.index; | ||
createClass(ParserStream, [{ | ||
key: "next", | ||
value: function next() { | ||
if (this.iterEnd) { | ||
return undefined; | ||
} | ||
return this.ch; | ||
if (this.buf.length === 0) { | ||
this.ch = this.iter.next().value; | ||
} else { | ||
this.ch = this.buf.shift(); | ||
} | ||
}, { | ||
key: 'current', | ||
value: function current() { | ||
return this.ch; | ||
this.index++; | ||
if (this.ch === undefined) { | ||
this.iterEnd = true; | ||
this.peekEnd = true; | ||
} | ||
}, { | ||
key: 'currentIs', | ||
value: function currentIs(ch) { | ||
return this.ch === ch; | ||
this.peekIndex = this.index; | ||
return this.ch; | ||
} | ||
}, { | ||
key: "current", | ||
value: function current() { | ||
return this.ch; | ||
} | ||
}, { | ||
key: "currentIs", | ||
value: function currentIs(ch) { | ||
return this.ch === ch; | ||
} | ||
}, { | ||
key: "currentPeek", | ||
value: function currentPeek() { | ||
if (this.peekEnd) { | ||
return undefined; | ||
} | ||
}, { | ||
key: 'currentPeek', | ||
value: function currentPeek() { | ||
if (this.peekEnd) { | ||
return undefined; | ||
} | ||
var diff = this.peekIndex - this.index; | ||
var diff = this.peekIndex - this.index; | ||
if (diff === 0) { | ||
return this.ch; | ||
} | ||
return this.buf[diff - 1]; | ||
if (diff === 0) { | ||
return this.ch; | ||
} | ||
}, { | ||
key: 'currentPeekIs', | ||
value: function currentPeekIs(ch) { | ||
return this.currentPeek() === ch; | ||
return this.buf[diff - 1]; | ||
} | ||
}, { | ||
key: "currentPeekIs", | ||
value: function currentPeekIs(ch) { | ||
return this.currentPeek() === ch; | ||
} | ||
}, { | ||
key: "peek", | ||
value: function peek() { | ||
if (this.peekEnd) { | ||
return undefined; | ||
} | ||
}, { | ||
key: 'peek', | ||
value: function peek() { | ||
if (this.peekEnd) { | ||
return undefined; | ||
} | ||
this.peekIndex += 1; | ||
this.peekIndex += 1; | ||
var diff = this.peekIndex - this.index; | ||
var diff = this.peekIndex - this.index; | ||
if (diff > this.buf.length) { | ||
var ch = this.iter.next().value; | ||
if (ch !== undefined) { | ||
this.buf.push(ch); | ||
} else { | ||
this.peekEnd = true; | ||
return undefined; | ||
} | ||
if (diff > this.buf.length) { | ||
var ch = this.iter.next().value; | ||
if (ch !== undefined) { | ||
this.buf.push(ch); | ||
} else { | ||
this.peekEnd = true; | ||
return undefined; | ||
} | ||
} | ||
return this.buf[diff - 1]; | ||
return this.buf[diff - 1]; | ||
} | ||
}, { | ||
key: "getIndex", | ||
value: function getIndex() { | ||
return this.index; | ||
} | ||
}, { | ||
key: "getPeekIndex", | ||
value: function getPeekIndex() { | ||
return this.peekIndex; | ||
} | ||
}, { | ||
key: "peekCharIs", | ||
value: function peekCharIs(ch) { | ||
if (this.peekEnd) { | ||
return false; | ||
} | ||
}, { | ||
key: 'getIndex', | ||
value: function getIndex() { | ||
return this.index; | ||
} | ||
}, { | ||
key: 'getPeekIndex', | ||
value: function getPeekIndex() { | ||
return this.peekIndex; | ||
} | ||
}, { | ||
key: 'peekCharIs', | ||
value: function peekCharIs(ch) { | ||
if (this.peekEnd) { | ||
return false; | ||
} | ||
var ret = this.peek(); | ||
var ret = this.peek(); | ||
this.peekIndex -= 1; | ||
this.peekIndex -= 1; | ||
return ret === ch; | ||
return ret === ch; | ||
} | ||
}, { | ||
key: "resetPeek", | ||
value: function resetPeek() { | ||
this.peekIndex = this.index; | ||
this.peekEnd = this.iterEnd; | ||
} | ||
}, { | ||
key: "skipToPeek", | ||
value: function skipToPeek() { | ||
var diff = this.peekIndex - this.index; | ||
for (var i = 0; i < diff; i++) { | ||
this.ch = this.buf.shift(); | ||
} | ||
}, { | ||
key: 'resetPeek', | ||
value: function resetPeek() { | ||
this.peekIndex = this.index; | ||
this.peekEnd = this.iterEnd; | ||
} | ||
}, { | ||
key: 'skipToPeek', | ||
value: function skipToPeek() { | ||
var diff = this.peekIndex - this.index; | ||
for (var i = 0; i < diff; i++) { | ||
this.ch = this.buf.shift(); | ||
} | ||
this.index = this.peekIndex; | ||
} | ||
}, { | ||
key: "getSlice", | ||
value: function getSlice(start, end) { | ||
return this.string.substring(start, end); | ||
} | ||
}]); | ||
return ParserStream; | ||
}(); | ||
this.index = this.peekIndex; | ||
} | ||
}, { | ||
key: 'getSlice', | ||
value: function getSlice(start, end) { | ||
return this.string.substring(start, end); | ||
} | ||
}]); | ||
function _extendableBuiltin(cls) { | ||
function ExtendableBuiltin() { | ||
var instance = Reflect.construct(cls, Array.from(arguments)); | ||
Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); | ||
return instance; | ||
} | ||
return ParserStream; | ||
}(); | ||
ExtendableBuiltin.prototype = Object.create(cls.prototype, { | ||
constructor: { | ||
value: cls, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
var ParseError = function (_Error) { | ||
_inherits(ParseError, _Error); | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(ExtendableBuiltin, cls); | ||
} else { | ||
ExtendableBuiltin.__proto__ = cls; | ||
} | ||
function ParseError(code) { | ||
_classCallCheck(this, ParseError); | ||
return ExtendableBuiltin; | ||
} | ||
var _this27 = _possibleConstructorReturn(this, (ParseError.__proto__ || Object.getPrototypeOf(ParseError)).call(this)); | ||
var ParseError = function (_extendableBuiltin2) { | ||
inherits(ParseError, _extendableBuiltin2); | ||
_this27.code = code; | ||
function ParseError(code) { | ||
classCallCheck(this, ParseError); | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
var _this = possibleConstructorReturn(this, (ParseError.__proto__ || Object.getPrototypeOf(ParseError)).call(this)); | ||
_this27.args = args; | ||
_this27.message = getErrorMessage(code, args); | ||
return _this27; | ||
_this.code = code; | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
return ParseError; | ||
}(Error); | ||
_this.args = args; | ||
_this.message = getErrorMessage(code, args); | ||
return _this; | ||
} | ||
function getErrorMessage(code, args) { | ||
switch (code) { | ||
case 'E0001': | ||
return 'Generic error'; | ||
case 'E0002': | ||
return 'Expected an entry start'; | ||
case 'E0003': | ||
{ | ||
var _args = _slicedToArray(args, 1), | ||
token = _args[0]; | ||
return ParseError; | ||
}(_extendableBuiltin(Error)); | ||
return 'Expected token: "' + token + '"'; | ||
} | ||
case 'E0004': | ||
{ | ||
var _args2 = _slicedToArray(args, 1), | ||
range = _args2[0]; | ||
function getErrorMessage(code, args) { | ||
switch (code) { | ||
case 'E0001': | ||
return 'Generic error'; | ||
case 'E0002': | ||
return 'Expected an entry start'; | ||
case 'E0003': | ||
{ | ||
var _args = slicedToArray(args, 1), | ||
token = _args[0]; | ||
return 'Expected a character from range: "' + range + '"'; | ||
} | ||
case 'E0005': | ||
{ | ||
var _args3 = _slicedToArray(args, 1), | ||
id = _args3[0]; | ||
return 'Expected token: "' + token + '"'; | ||
} | ||
case 'E0004': | ||
{ | ||
var _args2 = slicedToArray(args, 1), | ||
range = _args2[0]; | ||
return 'Expected entry "' + id + '" to have a value, attributes or tags'; | ||
} | ||
case 'E0006': | ||
{ | ||
var _args4 = _slicedToArray(args, 1), | ||
field = _args4[0]; | ||
return 'Expected a character from range: "' + range + '"'; | ||
} | ||
case 'E0005': | ||
{ | ||
var _args3 = slicedToArray(args, 1), | ||
id = _args3[0]; | ||
return 'Expected field: "' + field + '"'; | ||
} | ||
case 'E0007': | ||
return 'Keyword cannot end with a whitespace'; | ||
case 'E0008': | ||
return 'Callee has to be a simple identifier'; | ||
case 'E0009': | ||
return 'Key has to be a simple identifier'; | ||
case 'E0010': | ||
return 'Expected one of the variants to be marked as default (*)'; | ||
case 'E0011': | ||
return 'Expected at least one variant after "->"'; | ||
case 'E0012': | ||
return 'Tags cannot be added to messages with attributes'; | ||
case 'E0013': | ||
return 'Expected variant key'; | ||
case 'E0014': | ||
return 'Expected literal'; | ||
case 'E0015': | ||
return 'Only one variant can be marked as default (*)'; | ||
default: | ||
return code; | ||
} | ||
return 'Expected entry "' + id + '" to have a value, attributes or tags'; | ||
} | ||
case 'E0006': | ||
{ | ||
var _args4 = slicedToArray(args, 1), | ||
field = _args4[0]; | ||
return 'Expected field: "' + field + '"'; | ||
} | ||
case 'E0007': | ||
return 'Keyword cannot end with a whitespace'; | ||
case 'E0008': | ||
return 'Callee has to be a simple identifier'; | ||
case 'E0009': | ||
return 'Key has to be a simple identifier'; | ||
case 'E0010': | ||
return 'Expected one of the variants to be marked as default (*)'; | ||
case 'E0011': | ||
return 'Expected at least one variant after "->"'; | ||
case 'E0012': | ||
return 'Tags cannot be added to messages with attributes'; | ||
case 'E0013': | ||
return 'Expected variant key'; | ||
case 'E0014': | ||
return 'Expected literal'; | ||
case 'E0015': | ||
return 'Only one variant can be marked as default (*)'; | ||
default: | ||
return code; | ||
} | ||
} | ||
/* eslint no-magic-numbers: "off" */ | ||
/* eslint no-magic-numbers: "off" */ | ||
var FTLParserStream = function (_ParserStream) { | ||
_inherits(FTLParserStream, _ParserStream); | ||
var FTLParserStream = function (_ParserStream) { | ||
inherits(FTLParserStream, _ParserStream); | ||
function FTLParserStream() { | ||
_classCallCheck(this, FTLParserStream); | ||
function FTLParserStream() { | ||
classCallCheck(this, FTLParserStream); | ||
return possibleConstructorReturn(this, (FTLParserStream.__proto__ || Object.getPrototypeOf(FTLParserStream)).apply(this, arguments)); | ||
} | ||
return _possibleConstructorReturn(this, (FTLParserStream.__proto__ || Object.getPrototypeOf(FTLParserStream)).apply(this, arguments)); | ||
} | ||
_createClass(FTLParserStream, [{ | ||
key: 'peekLineWS', | ||
value: function peekLineWS() { | ||
var ch = this.currentPeek(); | ||
while (ch) { | ||
if (ch !== ' ' && ch !== '\t') { | ||
break; | ||
} | ||
ch = this.peek(); | ||
createClass(FTLParserStream, [{ | ||
key: 'peekLineWS', | ||
value: function peekLineWS() { | ||
var ch = this.currentPeek(); | ||
while (ch) { | ||
if (ch !== ' ' && ch !== '\t') { | ||
break; | ||
} | ||
ch = this.peek(); | ||
} | ||
}, { | ||
key: 'skipWSLines', | ||
value: function skipWSLines() { | ||
while (true) { | ||
this.peekLineWS(); | ||
} | ||
}, { | ||
key: 'skipWSLines', | ||
value: function skipWSLines() { | ||
while (true) { | ||
this.peekLineWS(); | ||
if (this.currentPeek() === '\n') { | ||
this.skipToPeek(); | ||
this.next(); | ||
} else { | ||
this.resetPeek(); | ||
break; | ||
} | ||
if (this.currentPeek() === '\n') { | ||
this.skipToPeek(); | ||
this.next(); | ||
} else { | ||
this.resetPeek(); | ||
break; | ||
} | ||
} | ||
}, { | ||
key: 'skipLineWS', | ||
value: function skipLineWS() { | ||
while (this.ch) { | ||
if (this.ch !== ' ' && this.ch !== '\t') { | ||
break; | ||
} | ||
this.next(); | ||
} | ||
}, { | ||
key: 'skipLineWS', | ||
value: function skipLineWS() { | ||
while (this.ch) { | ||
if (this.ch !== ' ' && this.ch !== '\t') { | ||
break; | ||
} | ||
this.next(); | ||
} | ||
}, { | ||
key: 'expectChar', | ||
value: function expectChar(ch) { | ||
if (this.ch === ch) { | ||
this.next(); | ||
return true; | ||
} | ||
} | ||
}, { | ||
key: 'expectChar', | ||
value: function expectChar(ch) { | ||
if (this.ch === ch) { | ||
this.next(); | ||
return true; | ||
} | ||
if (ch === '\n') { | ||
// Unicode Character 'SYMBOL FOR NEWLINE' (U+2424) | ||
throw new ParseError('E0003', '\u2424'); | ||
} | ||
if (ch === '\n') { | ||
// Unicode Character 'SYMBOL FOR NEWLINE' (U+2424) | ||
throw new ParseError('E0003', '\u2424'); | ||
} | ||
throw new ParseError('E0003', ch); | ||
throw new ParseError('E0003', ch); | ||
} | ||
}, { | ||
key: 'takeCharIf', | ||
value: function takeCharIf(ch) { | ||
if (this.ch === ch) { | ||
this.next(); | ||
return true; | ||
} | ||
}, { | ||
key: 'takeCharIf', | ||
value: function takeCharIf(ch) { | ||
if (this.ch === ch) { | ||
this.next(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: 'takeChar', | ||
value: function takeChar(f) { | ||
var ch = this.ch; | ||
if (ch !== undefined && f(ch)) { | ||
this.next(); | ||
return ch; | ||
} | ||
return undefined; | ||
} | ||
}, { | ||
key: 'isIDStart', | ||
value: function isIDStart() { | ||
if (this.ch === undefined) { | ||
return false; | ||
} | ||
}, { | ||
key: 'takeChar', | ||
value: function takeChar(f) { | ||
var ch = this.ch; | ||
if (ch !== undefined && f(ch)) { | ||
this.next(); | ||
return ch; | ||
} | ||
return undefined; | ||
} | ||
}, { | ||
key: 'isIDStart', | ||
value: function isIDStart() { | ||
if (this.ch === undefined) { | ||
return false; | ||
} | ||
var cc = this.ch.charCodeAt(0); | ||
return cc >= 97 && cc <= 122 || // a-z | ||
cc >= 65 && cc <= 90 || // A-Z | ||
cc === 95; // _ | ||
var cc = this.ch.charCodeAt(0); | ||
return cc >= 97 && cc <= 122 || // a-z | ||
cc >= 65 && cc <= 90 || // A-Z | ||
cc === 95; // _ | ||
} | ||
}, { | ||
key: 'isNumberStart', | ||
value: function isNumberStart() { | ||
var cc = this.ch.charCodeAt(0); | ||
return cc >= 48 && cc <= 57 || cc === 45; // 0-9 | ||
} | ||
}, { | ||
key: 'isPeekNextLineIndented', | ||
value: function isPeekNextLineIndented() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
}, { | ||
key: 'isNumberStart', | ||
value: function isNumberStart() { | ||
var cc = this.ch.charCodeAt(0); | ||
return cc >= 48 && cc <= 57 || cc === 45; // 0-9 | ||
} | ||
}, { | ||
key: 'isPeekNextLineIndented', | ||
value: function isPeekNextLineIndented() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peek(); | ||
this.peek(); | ||
if (this.currentPeekIs(' ')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
if (this.currentPeekIs(' ')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
this.resetPeek(); | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLineVariantStart', | ||
value: function isPeekNextLineVariantStart() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLineVariantStart', | ||
value: function isPeekNextLineVariantStart() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peek(); | ||
this.peek(); | ||
var ptr = this.getPeekIndex(); | ||
var ptr = this.getPeekIndex(); | ||
this.peekLineWS(); | ||
this.peekLineWS(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
if (this.currentPeekIs('*')) { | ||
this.peek(); | ||
} | ||
if (this.currentPeekIs('[') && !this.peekCharIs('[')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLineAttributeStart', | ||
value: function isPeekNextLineAttributeStart() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
if (this.currentPeekIs('*')) { | ||
this.peek(); | ||
} | ||
var ptr = this.getPeekIndex(); | ||
if (this.currentPeekIs('[') && !this.peekCharIs('[')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLineAttributeStart', | ||
value: function isPeekNextLineAttributeStart() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peekLineWS(); | ||
this.peek(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
var ptr = this.getPeekIndex(); | ||
if (this.currentPeekIs('.')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
this.peekLineWS(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLinePattern', | ||
value: function isPeekNextLinePattern() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peek(); | ||
if (this.currentPeekIs('.')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
var ptr = this.getPeekIndex(); | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLinePattern', | ||
value: function isPeekNextLinePattern() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peekLineWS(); | ||
this.peek(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
var ptr = this.getPeekIndex(); | ||
if (this.currentPeekIs('}') || this.currentPeekIs('.') || this.currentPeekIs('#') || this.currentPeekIs('[') || this.currentPeekIs('*') || this.currentPeekIs('{')) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
this.peekLineWS(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return true; | ||
return false; | ||
} | ||
}, { | ||
key: 'isPeekNextLineTagStart', | ||
value: function isPeekNextLineTagStart() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peek(); | ||
if (this.currentPeekIs('}') || this.currentPeekIs('.') || this.currentPeekIs('#') || this.currentPeekIs('[') || this.currentPeekIs('*')) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
var ptr = this.getPeekIndex(); | ||
this.resetPeek(); | ||
return true; | ||
} | ||
}, { | ||
key: 'isPeekNextLineTagStart', | ||
value: function isPeekNextLineTagStart() { | ||
if (!this.currentPeekIs('\n')) { | ||
return false; | ||
} | ||
this.peekLineWS(); | ||
this.peek(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
var ptr = this.getPeekIndex(); | ||
if (this.currentPeekIs('#')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
this.peekLineWS(); | ||
if (this.getPeekIndex() - ptr === 0) { | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'skipToNextEntryStart', | ||
value: function skipToNextEntryStart() { | ||
while (this.next()) { | ||
if (this.currentIs('\n') && !this.peekCharIs('\n')) { | ||
this.next(); | ||
if (this.ch === undefined || this.isIDStart() || this.currentIs('/') && this.peekCharIs('/') || this.currentIs('[') && this.peekCharIs('[')) { | ||
break; | ||
} | ||
} | ||
} | ||
if (this.currentPeekIs('#')) { | ||
this.resetPeek(); | ||
return true; | ||
} | ||
}, { | ||
key: 'takeIDStart', | ||
value: function takeIDStart() { | ||
if (this.isIDStart()) { | ||
var ret = this.ch; | ||
this.resetPeek(); | ||
return false; | ||
} | ||
}, { | ||
key: 'skipToNextEntryStart', | ||
value: function skipToNextEntryStart() { | ||
while (this.next()) { | ||
if (this.currentIs('\n') && !this.peekCharIs('\n')) { | ||
this.next(); | ||
return ret; | ||
if (this.ch === undefined || this.isIDStart() || this.currentIs('/') && this.peekCharIs('/') || this.currentIs('[') && this.peekCharIs('[')) { | ||
break; | ||
} | ||
} | ||
throw new ParseError('E0004', 'a-zA-Z'); | ||
} | ||
}, { | ||
key: 'takeIDChar', | ||
value: function takeIDChar() { | ||
var closure = function closure(ch) { | ||
var cc = ch.charCodeAt(0); | ||
return cc >= 97 && cc <= 122 || // a-z | ||
cc >= 65 && cc <= 90 || // A-Z | ||
cc >= 48 && cc <= 57 || // 0-9 | ||
cc === 95 || cc === 45; // _- | ||
}; | ||
return this.takeChar(closure); | ||
} | ||
}, { | ||
key: 'takeIDStart', | ||
value: function takeIDStart() { | ||
if (this.isIDStart()) { | ||
var ret = this.ch; | ||
this.next(); | ||
return ret; | ||
} | ||
}, { | ||
key: 'takeSymbChar', | ||
value: function takeSymbChar() { | ||
var closure = function closure(ch) { | ||
if (ch === undefined) { | ||
return false; | ||
} | ||
throw new ParseError('E0004', 'a-zA-Z'); | ||
} | ||
}, { | ||
key: 'takeIDChar', | ||
value: function takeIDChar() { | ||
var closure = function closure(ch) { | ||
var cc = ch.charCodeAt(0); | ||
return cc >= 97 && cc <= 122 || // a-z | ||
cc >= 65 && cc <= 90 || // A-Z | ||
cc >= 48 && cc <= 57 || // 0-9 | ||
cc === 95 || cc === 45; // _- | ||
}; | ||
var cc = ch.charCodeAt(0); | ||
return cc >= 97 && cc <= 122 || // a-z | ||
cc >= 65 && cc <= 90 || // A-Z | ||
cc >= 48 && cc <= 57 || // 0-9 | ||
cc === 95 || cc === 45 || cc === 32; // _- | ||
}; | ||
return this.takeChar(closure); | ||
} | ||
}, { | ||
key: 'takeSymbChar', | ||
value: function takeSymbChar() { | ||
var closure = function closure(ch) { | ||
if (ch === undefined) { | ||
return false; | ||
} | ||
return this.takeChar(closure); | ||
} | ||
}, { | ||
key: 'takeDigit', | ||
value: function takeDigit() { | ||
var closure = function closure(ch) { | ||
var cc = ch.charCodeAt(0); | ||
return cc >= 48 && cc <= 57; // 0-9 | ||
}; | ||
var cc = ch.charCodeAt(0); | ||
return cc >= 97 && cc <= 122 || // a-z | ||
cc >= 65 && cc <= 90 || // A-Z | ||
cc >= 48 && cc <= 57 || // 0-9 | ||
cc === 95 || cc === 45 || cc === 32; // _- | ||
}; | ||
return this.takeChar(closure); | ||
} | ||
}]); | ||
return this.takeChar(closure); | ||
} | ||
}, { | ||
key: 'takeDigit', | ||
value: function takeDigit() { | ||
var closure = function closure(ch) { | ||
var cc = ch.charCodeAt(0); | ||
return cc >= 48 && cc <= 57; // 0-9 | ||
}; | ||
return FTLParserStream; | ||
}(ParserStream); | ||
return this.takeChar(closure); | ||
} | ||
}]); | ||
return FTLParserStream; | ||
}(ParserStream); | ||
/* eslint no-magic-numbers: [0] */ | ||
/* eslint no-magic-numbers: [0] */ | ||
function parse(source) { | ||
var comment = null; | ||
var FluentParser = function () { | ||
function FluentParser() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref$withSpans = _ref.withSpans, | ||
withSpans = _ref$withSpans === undefined ? true : _ref$withSpans, | ||
_ref$withAnnotations = _ref.withAnnotations, | ||
withAnnotations = _ref$withAnnotations === undefined ? true : _ref$withAnnotations; | ||
var ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
classCallCheck(this, FluentParser); | ||
var entries = []; | ||
this.withSpans = withSpans; | ||
this.withAnnotations = withAnnotations; | ||
} | ||
while (ps.current()) { | ||
var entry = getEntryOrJunk(ps); | ||
createClass(FluentParser, [{ | ||
key: 'parse', | ||
value: function parse(source) { | ||
var comment = null; | ||
if (entry.type === 'Comment' && entries.length === 0) { | ||
comment = entry; | ||
} else { | ||
entries.push(entry); | ||
var ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
var entries = []; | ||
while (ps.current()) { | ||
var entry = getEntryOrJunk(this, ps); | ||
if (entry.type === 'Comment' && entries.length === 0) { | ||
comment = entry; | ||
} else { | ||
entries.push(entry); | ||
} | ||
ps.skipWSLines(); | ||
} | ||
return new Resource(entries, comment); | ||
} | ||
}, { | ||
key: 'parseEntry', | ||
value: function parseEntry(source) { | ||
var ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
return getEntryOrJunk(this, ps); | ||
} | ||
}]); | ||
return FluentParser; | ||
}(); | ||
return new Resource(entries, comment); | ||
} | ||
function getEntryOrJunk(parser, ps) { | ||
var entryStartPos = ps.getIndex(); | ||
function parseEntry(source) { | ||
var ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
return getEntryOrJunk(ps); | ||
} | ||
function getEntryOrJunk(ps) { | ||
var entryStartPos = ps.getIndex(); | ||
try { | ||
var entry = getEntry(ps); | ||
try { | ||
var entry = getEntry(ps); | ||
if (parser.withSpans) { | ||
entry.addSpan(entryStartPos, ps.getIndex()); | ||
return entry; | ||
} catch (err) { | ||
if (!(err instanceof ParseError)) { | ||
throw err; | ||
} | ||
} | ||
return entry; | ||
} catch (err) { | ||
if (!(err instanceof ParseError)) { | ||
throw err; | ||
} | ||
var annot = new Annotation(err.code, err.args, err.message); | ||
annot.addSpan(ps.getIndex(), ps.getIndex()); | ||
var errorIndex = ps.getIndex(); | ||
ps.skipToNextEntryStart(); | ||
var nextEntryStart = ps.getIndex(); | ||
ps.skipToNextEntryStart(); | ||
var nextEntryStart = ps.getIndex(); | ||
// Create a Junk instance | ||
var slice = ps.getSlice(entryStartPos, nextEntryStart); | ||
var junk = new Junk(slice); | ||
// Create a Junk instance | ||
var slice = ps.getSlice(entryStartPos, nextEntryStart); | ||
var junk = new Junk(slice); | ||
if (parser.withSpans) { | ||
junk.addSpan(entryStartPos, nextEntryStart); | ||
} | ||
if (parser.withAnnotations) { | ||
var annot = new Annotation(err.code, err.args, err.message); | ||
annot.addSpan(errorIndex, errorIndex); | ||
junk.addAnnotation(annot); | ||
return junk; | ||
} | ||
return junk; | ||
} | ||
} | ||
function getEntry(ps) { | ||
var comment = void 0; | ||
function getEntry(ps) { | ||
var comment = void 0; | ||
if (ps.currentIs('/')) { | ||
comment = getComment(ps); | ||
} | ||
if (ps.currentIs('/')) { | ||
comment = getComment(ps); | ||
} | ||
if (ps.currentIs('[')) { | ||
return getSection(ps, comment); | ||
} | ||
if (ps.currentIs('[')) { | ||
return getSection(ps, comment); | ||
} | ||
if (ps.isIDStart()) { | ||
return getMessage(ps, comment); | ||
} | ||
if (ps.isIDStart()) { | ||
return getMessage(ps, comment); | ||
} | ||
if (comment) { | ||
return comment; | ||
} | ||
throw new ParseError('E0002'); | ||
if (comment) { | ||
return comment; | ||
} | ||
throw new ParseError('E0002'); | ||
} | ||
function getComment(ps) { | ||
ps.expectChar('/'); | ||
ps.expectChar('/'); | ||
ps.takeCharIf(' '); | ||
function getComment(ps) { | ||
ps.expectChar('/'); | ||
ps.expectChar('/'); | ||
ps.takeCharIf(' '); | ||
var content = ''; | ||
var content = ''; | ||
while (true) { | ||
var ch = void 0; | ||
while (ch = ps.takeChar(function (x) { | ||
return x !== '\n'; | ||
})) { | ||
content += ch; | ||
} | ||
while (true) { | ||
var ch = void 0; | ||
while (ch = ps.takeChar(function (x) { | ||
return x !== '\n'; | ||
})) { | ||
content += ch; | ||
} | ||
ps.next(); | ||
if (ps.current() === '/') { | ||
content += '\n'; | ||
ps.next(); | ||
if (ps.current() === '/') { | ||
content += '\n'; | ||
ps.next(); | ||
ps.expectChar('/'); | ||
ps.takeCharIf(' '); | ||
} else { | ||
break; | ||
} | ||
ps.expectChar('/'); | ||
ps.takeCharIf(' '); | ||
} else { | ||
break; | ||
} | ||
return new Comment(content); | ||
} | ||
return new Comment(content); | ||
} | ||
function getSection(ps, comment) { | ||
ps.expectChar('['); | ||
ps.expectChar('['); | ||
function getSection(ps, comment) { | ||
ps.expectChar('['); | ||
ps.expectChar('['); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
var symb = getSymbol(ps); | ||
var symb = getSymbol(ps); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
ps.expectChar(']'); | ||
ps.expectChar(']'); | ||
ps.expectChar(']'); | ||
ps.expectChar(']'); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
ps.expectChar('\n'); | ||
ps.expectChar('\n'); | ||
return new Section(symb, comment); | ||
} | ||
return new Section(symb, comment); | ||
} | ||
function getMessage(ps, comment) { | ||
var id = getIdentifier(ps); | ||
function getMessage(ps, comment) { | ||
var id = getIdentifier(ps); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
var pattern = void 0; | ||
var attrs = void 0; | ||
var tags = void 0; | ||
var pattern = void 0; | ||
var attrs = void 0; | ||
var tags = void 0; | ||
if (ps.currentIs('=')) { | ||
ps.next(); | ||
ps.skipLineWS(); | ||
if (ps.currentIs('=')) { | ||
ps.next(); | ||
ps.skipLineWS(); | ||
pattern = getPattern(ps); | ||
} | ||
pattern = getPattern(ps); | ||
} | ||
if (ps.isPeekNextLineAttributeStart()) { | ||
attrs = getAttributes(ps); | ||
} | ||
if (ps.isPeekNextLineAttributeStart()) { | ||
attrs = getAttributes(ps); | ||
} | ||
if (ps.isPeekNextLineTagStart()) { | ||
if (attrs !== undefined) { | ||
throw new ParseError('E0012'); | ||
} | ||
tags = getTags(ps); | ||
if (ps.isPeekNextLineTagStart()) { | ||
if (attrs !== undefined) { | ||
throw new ParseError('E0012'); | ||
} | ||
tags = getTags(ps); | ||
} | ||
if (pattern === undefined && attrs === undefined && tags === undefined) { | ||
throw new ParseError('E0005', id.name); | ||
} | ||
return new Message(id, pattern, attrs, tags, comment); | ||
if (pattern === undefined && attrs === undefined && tags === undefined) { | ||
throw new ParseError('E0005', id.name); | ||
} | ||
function getAttributes(ps) { | ||
var attrs = []; | ||
return new Message(id, pattern, attrs, tags, comment); | ||
} | ||
while (true) { | ||
ps.expectChar('\n'); | ||
ps.skipLineWS(); | ||
function getAttributes(ps) { | ||
var attrs = []; | ||
ps.expectChar('.'); | ||
while (true) { | ||
ps.expectChar('\n'); | ||
ps.skipLineWS(); | ||
var key = getIdentifier(ps); | ||
ps.expectChar('.'); | ||
ps.skipLineWS(); | ||
var key = getIdentifier(ps); | ||
ps.expectChar('='); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
ps.expectChar('='); | ||
var value = getPattern(ps); | ||
ps.skipLineWS(); | ||
if (value === undefined) { | ||
throw new ParseError('E0006', 'value'); | ||
} | ||
var value = getPattern(ps); | ||
attrs.push(new Attribute(key, value)); | ||
if (value === undefined) { | ||
throw new ParseError('E0006', 'value'); | ||
} | ||
if (!ps.isPeekNextLineAttributeStart()) { | ||
break; | ||
} | ||
attrs.push(new Attribute(key, value)); | ||
if (!ps.isPeekNextLineAttributeStart()) { | ||
break; | ||
} | ||
return attrs; | ||
} | ||
return attrs; | ||
} | ||
function getTags(ps) { | ||
var tags = []; | ||
function getTags(ps) { | ||
var tags = []; | ||
while (true) { | ||
ps.expectChar('\n'); | ||
ps.skipLineWS(); | ||
while (true) { | ||
ps.expectChar('\n'); | ||
ps.skipLineWS(); | ||
ps.expectChar('#'); | ||
ps.expectChar('#'); | ||
var symbol = getSymbol(ps); | ||
var symbol = getSymbol(ps); | ||
tags.push(new Tag(symbol)); | ||
tags.push(new Tag(symbol)); | ||
if (!ps.isPeekNextLineTagStart()) { | ||
break; | ||
} | ||
if (!ps.isPeekNextLineTagStart()) { | ||
break; | ||
} | ||
return tags; | ||
} | ||
return tags; | ||
} | ||
function getIdentifier(ps) { | ||
var name = ''; | ||
function getIdentifier(ps) { | ||
var name = ''; | ||
name += ps.takeIDStart(); | ||
name += ps.takeIDStart(); | ||
var ch = void 0; | ||
while (ch = ps.takeIDChar()) { | ||
name += ch; | ||
} | ||
return new Identifier(name); | ||
var ch = void 0; | ||
while (ch = ps.takeIDChar()) { | ||
name += ch; | ||
} | ||
function getVariantKey(ps) { | ||
var ch = ps.current(); | ||
return new Identifier(name); | ||
} | ||
if (!ch) { | ||
throw new ParseError('E0013'); | ||
} | ||
function getVariantKey(ps) { | ||
var ch = ps.current(); | ||
var cc = ch.charCodeAt(0); | ||
if (!ch) { | ||
throw new ParseError('E0013'); | ||
} | ||
if (cc >= 48 && cc <= 57 || cc === 45) { | ||
// 0-9, - | ||
return getNumber(ps); | ||
} | ||
var cc = ch.charCodeAt(0); | ||
return getSymbol(ps); | ||
if (cc >= 48 && cc <= 57 || cc === 45) { | ||
// 0-9, - | ||
return getNumber(ps); | ||
} | ||
function getVariants(ps) { | ||
var variants = []; | ||
var hasDefault = false; | ||
return getSymbol(ps); | ||
} | ||
while (true) { | ||
var defaultIndex = false; | ||
function getVariants(ps) { | ||
var variants = []; | ||
var hasDefault = false; | ||
ps.expectChar('\n'); | ||
ps.skipLineWS(); | ||
while (true) { | ||
var defaultIndex = false; | ||
if (ps.currentIs('*')) { | ||
if (hasDefault) { | ||
throw new ParseError('E0015'); | ||
} | ||
ps.next(); | ||
defaultIndex = true; | ||
hasDefault = true; | ||
ps.expectChar('\n'); | ||
ps.skipLineWS(); | ||
if (ps.currentIs('*')) { | ||
if (hasDefault) { | ||
throw new ParseError('E0015'); | ||
} | ||
ps.next(); | ||
defaultIndex = true; | ||
hasDefault = true; | ||
} | ||
ps.expectChar('['); | ||
ps.expectChar('['); | ||
var key = getVariantKey(ps); | ||
var key = getVariantKey(ps); | ||
ps.expectChar(']'); | ||
ps.expectChar(']'); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
var value = getPattern(ps); | ||
var value = getPattern(ps); | ||
if (!value) { | ||
throw new ParseError('E0006', 'value'); | ||
} | ||
if (!value) { | ||
throw new ParseError('E0006', 'value'); | ||
} | ||
variants.push(new Variant(key, value, defaultIndex)); | ||
variants.push(new Variant(key, value, defaultIndex)); | ||
if (!ps.isPeekNextLineVariantStart()) { | ||
break; | ||
} | ||
if (!ps.isPeekNextLineVariantStart()) { | ||
break; | ||
} | ||
} | ||
if (!hasDefault) { | ||
throw new ParseError('E0010'); | ||
} | ||
return variants; | ||
if (!hasDefault) { | ||
throw new ParseError('E0010'); | ||
} | ||
function getSymbol(ps) { | ||
var name = ''; | ||
return variants; | ||
} | ||
name += ps.takeIDStart(); | ||
function getSymbol(ps) { | ||
var name = ''; | ||
while (true) { | ||
var ch = ps.takeSymbChar(); | ||
if (ch) { | ||
name += ch; | ||
} else { | ||
break; | ||
} | ||
name += ps.takeIDStart(); | ||
while (true) { | ||
var ch = ps.takeSymbChar(); | ||
if (ch) { | ||
name += ch; | ||
} else { | ||
break; | ||
} | ||
return new Symbol$1(name.trimRight()); | ||
} | ||
function getDigits(ps) { | ||
var num = ''; | ||
return new _Symbol(name.trimRight()); | ||
} | ||
var ch = void 0; | ||
while (ch = ps.takeDigit()) { | ||
num += ch; | ||
} | ||
function getDigits(ps) { | ||
var num = ''; | ||
if (num.length === 0) { | ||
throw new ParseError('E0004', '0-9'); | ||
} | ||
var ch = void 0; | ||
while (ch = ps.takeDigit()) { | ||
num += ch; | ||
} | ||
return num; | ||
if (num.length === 0) { | ||
throw new ParseError('E0004', '0-9'); | ||
} | ||
function getNumber(ps) { | ||
var num = ''; | ||
return num; | ||
} | ||
if (ps.currentIs('-')) { | ||
num += '-'; | ||
ps.next(); | ||
} | ||
function getNumber(ps) { | ||
var num = ''; | ||
num = '' + num + getDigits(ps); | ||
if (ps.currentIs('-')) { | ||
num += '-'; | ||
ps.next(); | ||
} | ||
if (ps.currentIs('.')) { | ||
num += '.'; | ||
ps.next(); | ||
num = '' + num + getDigits(ps); | ||
} | ||
num = '' + num + getDigits(ps); | ||
return new NumberExpression(num); | ||
if (ps.currentIs('.')) { | ||
num += '.'; | ||
ps.next(); | ||
num = '' + num + getDigits(ps); | ||
} | ||
function getPattern(ps) { | ||
var buffer = ''; | ||
var elements = []; | ||
var firstLine = true; | ||
return new NumberExpression(num); | ||
} | ||
var ch = void 0; | ||
while (ch = ps.current()) { | ||
if (ch === '\n') { | ||
if (firstLine && buffer.length !== 0) { | ||
break; | ||
} | ||
function getPattern(ps) { | ||
var buffer = ''; | ||
var elements = []; | ||
var firstLine = true; | ||
if (!ps.isPeekNextLinePattern()) { | ||
break; | ||
} | ||
var ch = void 0; | ||
while (ch = ps.current()) { | ||
if (ch === '\n') { | ||
if (firstLine && buffer.length !== 0) { | ||
break; | ||
} | ||
ps.next(); | ||
ps.skipLineWS(); | ||
if (!ps.isPeekNextLinePattern()) { | ||
break; | ||
} | ||
firstLine = false; | ||
ps.next(); | ||
ps.skipLineWS(); | ||
if (buffer.length !== 0) { | ||
buffer += ch; | ||
} | ||
continue; | ||
} else if (ch === '\\') { | ||
var ch2 = ps.peek(); | ||
if (ch2 === '{' || ch2 === '"') { | ||
buffer += ch2; | ||
} else { | ||
buffer += ch + ch2; | ||
} | ||
ps.next(); | ||
} else if (ch === '{') { | ||
ps.next(); | ||
if (!firstLine) { | ||
buffer += ch; | ||
} | ||
firstLine = false; | ||
continue; | ||
} else if (ch === '\\') { | ||
var ch2 = ps.peek(); | ||
if (ch2 === '{' || ch2 === '"') { | ||
buffer += ch2; | ||
} else { | ||
buffer += ch + ch2; | ||
} | ||
ps.next(); | ||
} else if (ch === '{') { | ||
ps.next(); | ||
ps.skipLineWS(); | ||
ps.skipLineWS(); | ||
if (buffer.length !== 0) { | ||
elements.push(new TextElement(buffer)); | ||
} | ||
if (buffer.length !== 0) { | ||
elements.push(new TextElement(buffer)); | ||
} | ||
buffer = ''; | ||
buffer = ''; | ||
elements.push(getExpression(ps)); | ||
elements.push(getExpression(ps)); | ||
ps.expectChar('}'); | ||
ps.expectChar('}'); | ||
continue; | ||
} else { | ||
buffer += ps.ch; | ||
} | ||
ps.next(); | ||
continue; | ||
} else { | ||
buffer += ps.ch; | ||
} | ||
ps.next(); | ||
} | ||
if (buffer.length !== 0) { | ||
elements.push(new TextElement(buffer)); | ||
} | ||
if (buffer.length !== 0) { | ||
elements.push(new TextElement(buffer)); | ||
} | ||
return new Pattern(elements); | ||
return new Pattern(elements); | ||
} | ||
function getExpression(ps) { | ||
if (ps.isPeekNextLineVariantStart()) { | ||
var variants = getVariants(ps); | ||
ps.expectChar('\n'); | ||
ps.expectChar(' '); | ||
ps.skipLineWS(); | ||
return new SelectExpression(null, variants); | ||
} | ||
function getExpression(ps) { | ||
if (ps.isPeekNextLineVariantStart()) { | ||
var variants = getVariants(ps); | ||
var selector = getSelectorExpression(ps); | ||
ps.skipLineWS(); | ||
if (ps.currentIs('-')) { | ||
ps.peek(); | ||
if (!ps.currentPeekIs('>')) { | ||
ps.resetPeek(); | ||
} else { | ||
ps.next(); | ||
ps.next(); | ||
ps.skipLineWS(); | ||
var _variants = getVariants(ps); | ||
if (_variants.length === 0) { | ||
throw new ParseError('E0011'); | ||
} | ||
ps.expectChar('\n'); | ||
@@ -1420,563 +1581,561 @@ ps.expectChar(' '); | ||
return new SelectExpression(null, variants); | ||
return new SelectExpression(selector, _variants); | ||
} | ||
} | ||
var selector = getSelectorExpression(ps); | ||
return selector; | ||
} | ||
ps.skipLineWS(); | ||
function getSelectorExpression(ps) { | ||
var literal = getLiteral(ps); | ||
if (ps.currentIs('-')) { | ||
ps.peek(); | ||
if (!ps.currentPeekIs('>')) { | ||
ps.resetPeek(); | ||
} else { | ||
ps.next(); | ||
ps.next(); | ||
if (literal.type !== 'MessageReference') { | ||
return literal; | ||
} | ||
ps.skipLineWS(); | ||
var ch = ps.current(); | ||
var _variants = getVariants(ps); | ||
if (ch === '.') { | ||
ps.next(); | ||
if (_variants.length === 0) { | ||
throw new ParseError('E0011'); | ||
} | ||
ps.expectChar('\n'); | ||
ps.expectChar(' '); | ||
ps.skipLineWS(); | ||
return new SelectExpression(selector, _variants); | ||
} | ||
} | ||
return selector; | ||
var attr = getIdentifier(ps); | ||
return new AttributeExpression(literal.id, attr); | ||
} | ||
function getSelectorExpression(ps) { | ||
var literal = getLiteral(ps); | ||
if (ch === '[') { | ||
ps.next(); | ||
if (literal.type !== 'MessageReference') { | ||
return literal; | ||
} | ||
var key = getVariantKey(ps); | ||
var ch = ps.current(); | ||
ps.expectChar(']'); | ||
if (ch === '.') { | ||
ps.next(); | ||
return new VariantExpression(literal.id, key); | ||
} | ||
var attr = getIdentifier(ps); | ||
return new AttributeExpression(literal.id, attr); | ||
} | ||
if (ch === '(') { | ||
ps.next(); | ||
if (ch === '[') { | ||
ps.next(); | ||
var args = getCallArgs(ps); | ||
var key = getVariantKey(ps); | ||
ps.expectChar(')'); | ||
ps.expectChar(']'); | ||
return new CallExpression(literal.id, args); | ||
} | ||
return new VariantExpression(literal.id, key); | ||
} | ||
return literal; | ||
} | ||
if (ch === '(') { | ||
ps.next(); | ||
function getCallArgs(ps) { | ||
var args = []; | ||
var args = getCallArgs(ps); | ||
ps.skipLineWS(); | ||
ps.expectChar(')'); | ||
return new CallExpression(literal.id, args); | ||
while (true) { | ||
if (ps.current() === ')') { | ||
break; | ||
} | ||
return literal; | ||
} | ||
var exp = getSelectorExpression(ps); | ||
function getCallArgs(ps) { | ||
var args = []; | ||
ps.skipLineWS(); | ||
while (true) { | ||
if (ps.current() === ')') { | ||
break; | ||
if (ps.current() === ':') { | ||
if (exp.type !== 'MessageReference') { | ||
throw new ParseError('E0009'); | ||
} | ||
var exp = getSelectorExpression(ps); | ||
ps.next(); | ||
ps.skipLineWS(); | ||
if (ps.current() === ':') { | ||
if (exp.type !== 'MessageReference') { | ||
throw new ParseError('E0009'); | ||
} | ||
var val = getArgVal(ps); | ||
ps.next(); | ||
ps.skipLineWS(); | ||
args.push(new NamedArgument(exp.id, val)); | ||
} else { | ||
args.push(exp); | ||
} | ||
var val = getArgVal(ps); | ||
ps.skipLineWS(); | ||
args.push(new NamedArgument(exp.id, val)); | ||
} else { | ||
args.push(exp); | ||
} | ||
if (ps.current() === ',') { | ||
ps.next(); | ||
ps.skipLineWS(); | ||
if (ps.current() === ',') { | ||
ps.next(); | ||
ps.skipLineWS(); | ||
continue; | ||
} else { | ||
break; | ||
} | ||
continue; | ||
} else { | ||
break; | ||
} | ||
return args; | ||
} | ||
return args; | ||
} | ||
function getArgVal(ps) { | ||
if (ps.isNumberStart()) { | ||
return getNumber(ps); | ||
} else if (ps.currentIs('"')) { | ||
return getString(ps); | ||
} | ||
throw new ParseError('E0006', 'value'); | ||
function getArgVal(ps) { | ||
if (ps.isNumberStart()) { | ||
return getNumber(ps); | ||
} else if (ps.currentIs('"')) { | ||
return getString(ps); | ||
} | ||
throw new ParseError('E0006', 'value'); | ||
} | ||
function getString(ps) { | ||
var val = ''; | ||
function getString(ps) { | ||
var val = ''; | ||
ps.expectChar('"'); | ||
ps.expectChar('"'); | ||
var ch = void 0; | ||
while (ch = ps.takeChar(function (x) { | ||
return x !== '"'; | ||
})) { | ||
val += ch; | ||
} | ||
ps.next(); | ||
return new StringExpression(val); | ||
var ch = void 0; | ||
while (ch = ps.takeChar(function (x) { | ||
return x !== '"'; | ||
})) { | ||
val += ch; | ||
} | ||
function getLiteral(ps) { | ||
var ch = ps.current(); | ||
ps.next(); | ||
if (!ch) { | ||
throw new ParseError('E0014'); | ||
} | ||
return new StringExpression(val); | ||
} | ||
if (ps.isNumberStart()) { | ||
return getNumber(ps); | ||
} else if (ch === '"') { | ||
return getString(ps); | ||
} else if (ch === '$') { | ||
ps.next(); | ||
var _name = getIdentifier(ps); | ||
return new ExternalArgument(_name); | ||
} | ||
function getLiteral(ps) { | ||
var ch = ps.current(); | ||
var name = getIdentifier(ps); | ||
return new MessageReference(name); | ||
if (!ch) { | ||
throw new ParseError('E0014'); | ||
} | ||
function indent(content) { | ||
return content.split('\n').join('\n '); | ||
if (ps.isNumberStart()) { | ||
return getNumber(ps); | ||
} else if (ch === '"') { | ||
return getString(ps); | ||
} else if (ch === '$') { | ||
ps.next(); | ||
var _name = getIdentifier(ps); | ||
return new ExternalArgument(_name); | ||
} | ||
function containNewLine(elems) { | ||
var withNewLine = elems.filter(function (elem) { | ||
return elem.type === 'TextElement' && elem.value.includes('\n'); | ||
}); | ||
return !!withNewLine.length; | ||
} | ||
var name = getIdentifier(ps); | ||
return new MessageReference(name); | ||
} | ||
function serialize(resource) { | ||
var withJunk = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
function indent(content) { | ||
return content.split('\n').join('\n '); | ||
} | ||
var parts = []; | ||
function containNewLine(elems) { | ||
var withNewLine = elems.filter(function (elem) { | ||
return elem.type === 'TextElement' && elem.value.includes('\n'); | ||
}); | ||
return !!withNewLine.length; | ||
} | ||
if (resource.comment) { | ||
parts.push(serializeComment(resource.comment) + '\n\n'); | ||
} | ||
var FluentSerializer = function () { | ||
function FluentSerializer() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref$withJunk = _ref.withJunk, | ||
withJunk = _ref$withJunk === undefined ? false : _ref$withJunk; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
classCallCheck(this, FluentSerializer); | ||
try { | ||
for (var _iterator = resource.body[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var entry = _step.value; | ||
this.withJunk = withJunk; | ||
} | ||
if (entry.types !== 'Junk' || withJunk) { | ||
parts.push(serializeEntry(entry)); | ||
} | ||
createClass(FluentSerializer, [{ | ||
key: 'serialize', | ||
value: function serialize(resource) { | ||
var parts = []; | ||
if (resource.comment) { | ||
parts.push(serializeComment(resource.comment) + '\n\n'); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
for (var _iterator = resource.body[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var entry = _step.value; | ||
if (entry.types !== 'Junk' || this.withJunk) { | ||
parts.push(this.serializeEntry(entry)); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return parts.join(''); | ||
} | ||
}, { | ||
key: 'serializeEntry', | ||
value: function serializeEntry(entry) { | ||
switch (entry.type) { | ||
case 'Message': | ||
return serializeMessage(entry); | ||
case 'Section': | ||
return serializeSection(entry); | ||
case 'Comment': | ||
return serializeComment(entry); | ||
case 'Junk': | ||
return serializeJunk(entry); | ||
default: | ||
throw new Error('Unknown entry type: ' + entry.type); | ||
} | ||
} | ||
}]); | ||
return FluentSerializer; | ||
}(); | ||
return parts.join(''); | ||
} | ||
function serializeComment(comment) { | ||
return comment.content.split('\n').map(function (line) { | ||
return '// ' + line; | ||
}).join('\n'); | ||
} | ||
function serializeEntry(entry) { | ||
switch (entry.type) { | ||
case 'Message': | ||
return serializeMessage(entry); | ||
case 'Section': | ||
return serializeSection(entry); | ||
case 'Comment': | ||
return serializeComment(entry); | ||
case 'Junk': | ||
return serializeJunk(entry); | ||
default: | ||
throw new Error('Unknown entry type: ' + entry.type); | ||
} | ||
} | ||
function serializeSection(section) { | ||
var name = serializeSymbol(section.name); | ||
function serializeComment(comment) { | ||
return comment.content.split('\n').map(function (line) { | ||
return '// ' + line; | ||
}).join('\n'); | ||
if (section.comment) { | ||
var comment = serializeComment(section.comment); | ||
return '\n\n' + comment + '\n[[ ' + name + ' ]]\n\n'; | ||
} | ||
function serializeSection(section) { | ||
var name = serializeSymbol(section.name); | ||
return '\n\n[[ ' + name + ' ]]\n\n'; | ||
} | ||
if (section.comment) { | ||
var comment = serializeComment(section.comment); | ||
return '\n\n' + comment + '\n[[ ' + name + ' ]]\n\n'; | ||
} | ||
function serializeJunk(junk) { | ||
return junk.content; | ||
} | ||
return '\n\n[[ ' + name + ' ]]\n\n'; | ||
} | ||
function serializeMessage(message) { | ||
var parts = []; | ||
function serializeJunk(junk) { | ||
return junk.content; | ||
if (message.comment) { | ||
parts.push(serializeComment(message.comment)); | ||
parts.push('\n'); | ||
} | ||
function serializeMessage(message) { | ||
var parts = []; | ||
parts.push(serializeIdentifier(message.id)); | ||
if (message.comment) { | ||
parts.push(serializeComment(message.comment)); | ||
parts.push('\n'); | ||
} | ||
if (message.value) { | ||
parts.push(' ='); | ||
parts.push(serializeValue(message.value)); | ||
} | ||
parts.push(serializeIdentifier(message.id)); | ||
if (message.tags) { | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
if (message.value) { | ||
parts.push(' ='); | ||
parts.push(serializeValue(message.value)); | ||
} | ||
try { | ||
for (var _iterator2 = message.tags[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var tag = _step2.value; | ||
if (message.tags) { | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
parts.push(serializeTag(tag)); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
for (var _iterator2 = message.tags[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var tag = _step2.value; | ||
parts.push(serializeTag(tag)); | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
} | ||
if (message.attributes) { | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
if (message.attributes) { | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = message.attributes[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var attribute = _step3.value; | ||
parts.push(serializeAttribute(attribute)); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
for (var _iterator3 = message.attributes[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var attribute = _step3.value; | ||
parts.push(serializeAttribute(attribute)); | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
parts.push('\n'); | ||
return parts.join(''); | ||
} | ||
function serializeTag(tag) { | ||
var name = serializeSymbol(tag.name); | ||
return '\n #' + name; | ||
} | ||
parts.push('\n'); | ||
return parts.join(''); | ||
} | ||
function serializeAttribute(attribute) { | ||
var id = serializeIdentifier(attribute.id); | ||
var value = indent(serializeValue(attribute.value)); | ||
return '\n .' + id + ' =' + value; | ||
} | ||
function serializeTag(tag) { | ||
var name = serializeSymbol(tag.name); | ||
return '\n #' + name; | ||
} | ||
function serializeValue(pattern) { | ||
var content = indent(serializePattern(pattern)); | ||
var multi = containNewLine(pattern.elements); | ||
function serializeAttribute(attribute) { | ||
var id = serializeIdentifier(attribute.id); | ||
var value = indent(serializeValue(attribute.value)); | ||
return '\n .' + id + ' =' + value; | ||
} | ||
if (multi) { | ||
return '\n ' + content; | ||
} | ||
function serializeValue(pattern) { | ||
var content = indent(serializePattern(pattern)); | ||
var multi = containNewLine(pattern.elements); | ||
return ' ' + content; | ||
if (multi) { | ||
return '\n ' + content; | ||
} | ||
function serializePattern(pattern) { | ||
return pattern.elements.map(serializeElement).join(''); | ||
} | ||
return ' ' + content; | ||
} | ||
function serializeElement(element) { | ||
switch (element.type) { | ||
case 'TextElement': | ||
return serializeTextElement(element); | ||
case 'SelectExpression': | ||
return '{' + serializeSelectExpression(element) + '}'; | ||
default: | ||
return '{ ' + serializeExpression(element) + ' }'; | ||
} | ||
} | ||
function serializePattern(pattern) { | ||
return pattern.elements.map(serializeElement).join(''); | ||
} | ||
function serializeTextElement(text) { | ||
return text.value; | ||
function serializeElement(element) { | ||
switch (element.type) { | ||
case 'TextElement': | ||
return serializeTextElement(element); | ||
case 'SelectExpression': | ||
return '{' + serializeSelectExpression(element) + '}'; | ||
default: | ||
return '{ ' + serializeExpression(element) + ' }'; | ||
} | ||
} | ||
function serializeExpression(expr) { | ||
switch (expr.type) { | ||
case 'StringExpression': | ||
return serializeStringExpression(expr); | ||
case 'NumberExpression': | ||
return serializeNumberExpression(expr); | ||
case 'MessageReference': | ||
return serializeMessageReference(expr); | ||
case 'ExternalArgument': | ||
return serializeExternalArgument(expr); | ||
case 'AttributeExpression': | ||
return serializeAttributeExpression(expr); | ||
case 'VariantExpression': | ||
return serializeVariantExpression(expr); | ||
case 'CallExpression': | ||
return serializeCallExpression(expr); | ||
default: | ||
throw new Error('Unknown expression type: ' + expr.type); | ||
} | ||
} | ||
function serializeTextElement(text) { | ||
return text.value; | ||
} | ||
function serializeStringExpression(expr) { | ||
return '"' + expr.value + '"'; | ||
function serializeExpression(expr) { | ||
switch (expr.type) { | ||
case 'StringExpression': | ||
return serializeStringExpression(expr); | ||
case 'NumberExpression': | ||
return serializeNumberExpression(expr); | ||
case 'MessageReference': | ||
return serializeMessageReference(expr); | ||
case 'ExternalArgument': | ||
return serializeExternalArgument(expr); | ||
case 'AttributeExpression': | ||
return serializeAttributeExpression(expr); | ||
case 'VariantExpression': | ||
return serializeVariantExpression(expr); | ||
case 'CallExpression': | ||
return serializeCallExpression(expr); | ||
default: | ||
throw new Error('Unknown expression type: ' + expr.type); | ||
} | ||
} | ||
function serializeNumberExpression(expr) { | ||
return expr.value; | ||
} | ||
function serializeStringExpression(expr) { | ||
return '"' + expr.value + '"'; | ||
} | ||
function serializeMessageReference(expr) { | ||
return serializeIdentifier(expr.id); | ||
} | ||
function serializeNumberExpression(expr) { | ||
return expr.value; | ||
} | ||
function serializeExternalArgument(expr) { | ||
return '$' + serializeIdentifier(expr.id); | ||
function serializeMessageReference(expr) { | ||
return serializeIdentifier(expr.id); | ||
} | ||
function serializeExternalArgument(expr) { | ||
return '$' + serializeIdentifier(expr.id); | ||
} | ||
function serializeSelectExpression(expr) { | ||
var parts = []; | ||
if (expr.expression) { | ||
var selector = ' ' + serializeExpression(expr.expression) + ' ->'; | ||
parts.push(selector); | ||
} | ||
function serializeSelectExpression(expr) { | ||
var parts = []; | ||
var _iteratorNormalCompletion4 = true; | ||
var _didIteratorError4 = false; | ||
var _iteratorError4 = undefined; | ||
if (expr.expression) { | ||
var selector = ' ' + serializeExpression(expr.expression) + ' ->'; | ||
parts.push(selector); | ||
try { | ||
for (var _iterator4 = expr.variants[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
var variant = _step4.value; | ||
parts.push(serializeVariant(variant)); | ||
} | ||
var _iteratorNormalCompletion4 = true; | ||
var _didIteratorError4 = false; | ||
var _iteratorError4 = undefined; | ||
} catch (err) { | ||
_didIteratorError4 = true; | ||
_iteratorError4 = err; | ||
} finally { | ||
try { | ||
for (var _iterator4 = expr.variants[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
var variant = _step4.value; | ||
parts.push(serializeVariant(variant)); | ||
if (!_iteratorNormalCompletion4 && _iterator4.return) { | ||
_iterator4.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError4 = true; | ||
_iteratorError4 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion4 && _iterator4.return) { | ||
_iterator4.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError4) { | ||
throw _iteratorError4; | ||
} | ||
if (_didIteratorError4) { | ||
throw _iteratorError4; | ||
} | ||
} | ||
parts.push('\n'); | ||
return parts.join(''); | ||
} | ||
function serializeVariant(variant) { | ||
var key = serializeVariantKey(variant.key); | ||
var value = indent(serializeValue(variant.value)); | ||
parts.push('\n'); | ||
return parts.join(''); | ||
} | ||
if (variant.default) { | ||
return '\n *[' + key + ']' + value; | ||
} | ||
function serializeVariant(variant) { | ||
var key = serializeVariantKey(variant.key); | ||
var value = indent(serializeValue(variant.value)); | ||
return '\n [' + key + ']' + value; | ||
if (variant.default) { | ||
return '\n *[' + key + ']' + value; | ||
} | ||
function serializeAttributeExpression(expr) { | ||
var id = serializeIdentifier(expr.id); | ||
var name = serializeIdentifier(expr.name); | ||
return id + '.' + name; | ||
} | ||
return '\n [' + key + ']' + value; | ||
} | ||
function serializeVariantExpression(expr) { | ||
var id = serializeIdentifier(expr.id); | ||
var key = serializeVariantKey(expr.key); | ||
return id + '[' + key + ']'; | ||
} | ||
function serializeAttributeExpression(expr) { | ||
var id = serializeIdentifier(expr.id); | ||
var name = serializeIdentifier(expr.name); | ||
return id + '.' + name; | ||
} | ||
function serializeCallExpression(expr) { | ||
var fun = serializeFunction(expr.callee); | ||
var args = expr.args.map(serializeCallArgument).join(', '); | ||
return fun + '(' + args + ')'; | ||
} | ||
function serializeVariantExpression(expr) { | ||
var id = serializeIdentifier(expr.id); | ||
var key = serializeVariantKey(expr.key); | ||
return id + '[' + key + ']'; | ||
} | ||
function serializeCallArgument(arg) { | ||
switch (arg.type) { | ||
case 'NamedArgument': | ||
return serializeNamedArgument(arg); | ||
default: | ||
return serializeExpression(arg); | ||
} | ||
} | ||
function serializeCallExpression(expr) { | ||
var fun = serializeFunction(expr.callee); | ||
var args = expr.args.map(serializeCallArgument).join(', '); | ||
return fun + '(' + args + ')'; | ||
} | ||
function serializeNamedArgument(arg) { | ||
var name = serializeIdentifier(arg.name); | ||
var value = serializeArgumentValue(arg.val); | ||
return name + ': ' + value; | ||
function serializeCallArgument(arg) { | ||
switch (arg.type) { | ||
case 'NamedArgument': | ||
return serializeNamedArgument(arg); | ||
default: | ||
return serializeExpression(arg); | ||
} | ||
} | ||
function serializeArgumentValue(argval) { | ||
switch (argval.type) { | ||
case 'StringExpression': | ||
return serializeStringExpression(argval); | ||
case 'NumberExpression': | ||
return serializeNumberExpression(argval); | ||
default: | ||
throw new Error('Unknown argument type: ' + argval.type); | ||
} | ||
} | ||
function serializeNamedArgument(arg) { | ||
var name = serializeIdentifier(arg.name); | ||
var value = serializeArgumentValue(arg.val); | ||
return name + ': ' + value; | ||
} | ||
function serializeIdentifier(identifier) { | ||
return identifier.name; | ||
function serializeArgumentValue(argval) { | ||
switch (argval.type) { | ||
case 'StringExpression': | ||
return serializeStringExpression(argval); | ||
case 'NumberExpression': | ||
return serializeNumberExpression(argval); | ||
default: | ||
throw new Error('Unknown argument type: ' + argval.type); | ||
} | ||
} | ||
function serializeSymbol(symbol) { | ||
return symbol.name; | ||
} | ||
function serializeIdentifier(identifier) { | ||
return identifier.name; | ||
} | ||
function serializeVariantKey(key) { | ||
switch (key.type) { | ||
case 'Symbol': | ||
return serializeSymbol(key); | ||
case 'NumberExpression': | ||
return serializeNumberExpression(key); | ||
default: | ||
throw new Error('Unknown variant key type: ' + key.type); | ||
} | ||
} | ||
function serializeSymbol(symbol) { | ||
return symbol.name; | ||
} | ||
function serializeFunction(fun) { | ||
return fun.name; | ||
function serializeVariantKey(key) { | ||
switch (key.type) { | ||
case 'Symbol': | ||
return serializeSymbol(key); | ||
case 'NumberExpression': | ||
return serializeNumberExpression(key); | ||
default: | ||
throw new Error('Unknown variant key type: ' + key.type); | ||
} | ||
} | ||
function lineOffset(source, pos) { | ||
// Substract 1 to get the offset. | ||
return source.substring(0, pos).split('\n').length - 1; | ||
} | ||
function serializeFunction(fun) { | ||
return fun.name; | ||
} | ||
function columnOffset(source, pos) { | ||
var lastLineBreak = source.lastIndexOf('\n', pos); | ||
return lastLineBreak === -1 ? pos | ||
// Substracting two offsets gives length; substract 1 to get the offset. | ||
: pos - lastLineBreak - 1; | ||
} | ||
function parse(source, opts) { | ||
var parser = new FluentParser(opts); | ||
return parser.parse(source); | ||
} | ||
exports.lineOffset = lineOffset; | ||
exports.columnOffset = columnOffset; | ||
exports.Resource = Resource; | ||
exports.Entry = Entry; | ||
exports.Message = Message; | ||
exports.Pattern = Pattern; | ||
exports.TextElement = TextElement; | ||
exports.Expression = Expression; | ||
exports.StringExpression = StringExpression; | ||
exports.NumberExpression = NumberExpression; | ||
exports.MessageReference = MessageReference; | ||
exports.ExternalArgument = ExternalArgument; | ||
exports.SelectExpression = SelectExpression; | ||
exports.AttributeExpression = AttributeExpression; | ||
exports.VariantExpression = VariantExpression; | ||
exports.CallExpression = CallExpression; | ||
exports.Attribute = Attribute; | ||
exports.Tag = Tag; | ||
exports.Variant = Variant; | ||
exports.NamedArgument = NamedArgument; | ||
exports.Identifier = Identifier; | ||
exports.Symbol = Symbol$1; | ||
exports.Comment = Comment; | ||
exports.Section = Section; | ||
exports.Function = Function; | ||
exports.Junk = Junk; | ||
exports.Span = Span; | ||
exports.Annotation = Annotation; | ||
exports.parse = parse; | ||
exports.parseEntry = parseEntry; | ||
exports.serialize = serialize; | ||
exports.serializeEntry = serializeEntry; | ||
function serialize(resource, opts) { | ||
var serializer = new FluentSerializer(opts); | ||
return serializer.serialize(resource); | ||
} | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}); | ||
function lineOffset(source, pos) { | ||
// Substract 1 to get the offset. | ||
return source.substring(0, pos).split('\n').length - 1; | ||
} | ||
function columnOffset(source, pos) { | ||
var lastLineBreak = source.lastIndexOf('\n', pos); | ||
return lastLineBreak === -1 ? pos | ||
// Substracting two offsets gives length; substract 1 to get the offset. | ||
: pos - lastLineBreak - 1; | ||
} | ||
exports.FluentParser = FluentParser; | ||
exports.FluentSerializer = FluentSerializer; | ||
exports.parse = parse; | ||
exports.serialize = serialize; | ||
exports.lineOffset = lineOffset; | ||
exports.columnOffset = columnOffset; | ||
exports.Resource = Resource; | ||
exports.Entry = Entry; | ||
exports.Message = Message; | ||
exports.Pattern = Pattern; | ||
exports.TextElement = TextElement; | ||
exports.Expression = Expression; | ||
exports.StringExpression = StringExpression; | ||
exports.NumberExpression = NumberExpression; | ||
exports.MessageReference = MessageReference; | ||
exports.ExternalArgument = ExternalArgument; | ||
exports.SelectExpression = SelectExpression; | ||
exports.AttributeExpression = AttributeExpression; | ||
exports.VariantExpression = VariantExpression; | ||
exports.CallExpression = CallExpression; | ||
exports.Attribute = Attribute; | ||
exports.Tag = Tag; | ||
exports.Variant = Variant; | ||
exports.NamedArgument = NamedArgument; | ||
exports.Identifier = Identifier; | ||
exports.Symbol = _Symbol; | ||
exports.Comment = Comment; | ||
exports.Section = Section; | ||
exports.Function = Function$1; | ||
exports.Junk = Junk; | ||
exports.Span = Span; | ||
exports.Annotation = Annotation; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
@@ -600,4 +600,3 @@ /* fluent-syntax@0.3.0 */ | ||
this.currentPeekIs('[') || | ||
this.currentPeekIs('*') || | ||
this.currentPeekIs('{')) { | ||
this.currentPeekIs('*')) { | ||
this.resetPeek(); | ||
@@ -698,32 +697,42 @@ return false; | ||
function parse(source) { | ||
let comment = null; | ||
class FluentParser { | ||
constructor({ | ||
withSpans = true, | ||
withAnnotations = true, | ||
} = {}) { | ||
this.withSpans = withSpans; | ||
this.withAnnotations = withAnnotations; | ||
} | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
parse(source) { | ||
let comment = null; | ||
const entries = []; | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
while (ps.current()) { | ||
const entry = getEntryOrJunk(ps); | ||
const entries = []; | ||
if (entry.type === 'Comment' && entries.length === 0) { | ||
comment = entry; | ||
} else { | ||
entries.push(entry); | ||
while (ps.current()) { | ||
const entry = getEntryOrJunk(this, ps); | ||
if (entry.type === 'Comment' && entries.length === 0) { | ||
comment = entry; | ||
} else { | ||
entries.push(entry); | ||
} | ||
ps.skipWSLines(); | ||
} | ||
return new Resource(entries, comment); | ||
} | ||
parseEntry(source) { | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
return getEntryOrJunk(this, ps); | ||
} | ||
return new Resource(entries, comment); | ||
} | ||
function parseEntry(source) { | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
return getEntryOrJunk(ps); | ||
} | ||
function getEntryOrJunk(ps) { | ||
function getEntryOrJunk(parser, ps) { | ||
const entryStartPos = ps.getIndex(); | ||
@@ -733,3 +742,5 @@ | ||
const entry = getEntry(ps); | ||
entry.addSpan(entryStartPos, ps.getIndex()); | ||
if (parser.withSpans) { | ||
entry.addSpan(entryStartPos, ps.getIndex()); | ||
} | ||
return entry; | ||
@@ -741,5 +752,3 @@ } catch (err) { | ||
const annot = new Annotation(err.code, err.args, err.message); | ||
annot.addSpan(ps.getIndex(), ps.getIndex()); | ||
const errorIndex = ps.getIndex(); | ||
ps.skipToNextEntryStart(); | ||
@@ -751,4 +760,10 @@ const nextEntryStart = ps.getIndex(); | ||
const junk = new Junk(slice); | ||
junk.addSpan(entryStartPos, nextEntryStart); | ||
junk.addAnnotation(annot); | ||
if (parser.withSpans) { | ||
junk.addSpan(entryStartPos, nextEntryStart); | ||
} | ||
if (parser.withAnnotations) { | ||
const annot = new Annotation(err.code, err.args, err.message); | ||
annot.addSpan(errorIndex, errorIndex); | ||
junk.addAnnotation(annot); | ||
} | ||
return junk; | ||
@@ -1058,7 +1073,6 @@ } | ||
firstLine = false; | ||
if (buffer.length !== 0) { | ||
if (!firstLine) { | ||
buffer += ch; | ||
} | ||
firstLine = false; | ||
continue; | ||
@@ -1275,3 +1289,2 @@ } else if (ch === '\\') { | ||
function containNewLine(elems) { | ||
@@ -1284,34 +1297,38 @@ const withNewLine = elems.filter( | ||
class FluentSerializer { | ||
constructor({ withJunk = false } = {}) { | ||
this.withJunk = withJunk; | ||
} | ||
function serialize(resource, withJunk = false) { | ||
const parts = []; | ||
serialize(resource) { | ||
const parts = []; | ||
if (resource.comment) { | ||
parts.push( | ||
`${serializeComment(resource.comment)}\n\n` | ||
); | ||
} | ||
if (resource.comment) { | ||
parts.push( | ||
`${serializeComment(resource.comment)}\n\n` | ||
); | ||
} | ||
for (const entry of resource.body) { | ||
if (entry.types !== 'Junk' || withJunk) { | ||
parts.push(serializeEntry(entry)); | ||
for (const entry of resource.body) { | ||
if (entry.types !== 'Junk' || this.withJunk) { | ||
parts.push(this.serializeEntry(entry)); | ||
} | ||
} | ||
return parts.join(''); | ||
} | ||
return parts.join(''); | ||
} | ||
function serializeEntry(entry) { | ||
switch (entry.type) { | ||
case 'Message': | ||
return serializeMessage(entry); | ||
case 'Section': | ||
return serializeSection(entry); | ||
case 'Comment': | ||
return serializeComment(entry); | ||
case 'Junk': | ||
return serializeJunk(entry); | ||
default : | ||
throw new Error(`Unknown entry type: ${entry.type}`); | ||
serializeEntry(entry) { | ||
switch (entry.type) { | ||
case 'Message': | ||
return serializeMessage(entry); | ||
case 'Section': | ||
return serializeSection(entry); | ||
case 'Comment': | ||
return serializeComment(entry); | ||
case 'Junk': | ||
return serializeJunk(entry); | ||
default : | ||
throw new Error(`Unknown entry type: ${entry.type}`); | ||
} | ||
} | ||
@@ -1571,2 +1588,12 @@ } | ||
function parse(source, opts) { | ||
const parser = new FluentParser(opts); | ||
return parser.parse(source); | ||
} | ||
function serialize(resource, opts) { | ||
const serializer = new FluentSerializer(opts); | ||
return serializer.serialize(resource); | ||
} | ||
function lineOffset(source, pos) { | ||
@@ -1585,2 +1612,6 @@ // Substract 1 to get the offset. | ||
exports.FluentParser = FluentParser; | ||
exports.FluentSerializer = FluentSerializer; | ||
exports.parse = parse; | ||
exports.serialize = serialize; | ||
exports.lineOffset = lineOffset; | ||
@@ -1614,6 +1645,2 @@ exports.columnOffset = columnOffset; | ||
exports.Annotation = Annotation; | ||
exports.parse = parse; | ||
exports.parseEntry = parseEntry; | ||
exports.serialize = serialize; | ||
exports.serializeEntry = serializeEntry; | ||
@@ -1620,0 +1647,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "fluent-syntax", | ||
"description": "AST and parser for Fluent", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"homepage": "http://projectfluent.io", | ||
@@ -6,0 +6,0 @@ "author": "Mozilla <l10n-drivers@mozilla.org>", |
@@ -176,4 +176,3 @@ /* eslint no-magic-numbers: "off" */ | ||
this.currentPeekIs('[') || | ||
this.currentPeekIs('*') || | ||
this.currentPeekIs('{')) { | ||
this.currentPeekIs('*')) { | ||
this.resetPeek(); | ||
@@ -180,0 +179,0 @@ return false; |
@@ -0,5 +1,17 @@ | ||
import FluentParser from './parser'; | ||
import FluentSerializer from './serializer'; | ||
export * from './ast'; | ||
export * from './parser'; | ||
export * from './serializer'; | ||
export { FluentParser, FluentSerializer }; | ||
export function parse(source, opts) { | ||
const parser = new FluentParser(opts); | ||
return parser.parse(source); | ||
} | ||
export function serialize(resource, opts) { | ||
const serializer = new FluentSerializer(opts); | ||
return serializer.serialize(resource); | ||
} | ||
export function lineOffset(source, pos) { | ||
@@ -6,0 +18,0 @@ // Substract 1 to get the offset. |
@@ -7,32 +7,42 @@ /* eslint no-magic-numbers: [0] */ | ||
export function parse(source) { | ||
let comment = null; | ||
export default class FluentParser { | ||
constructor({ | ||
withSpans = true, | ||
withAnnotations = true, | ||
} = {}) { | ||
this.withSpans = withSpans; | ||
this.withAnnotations = withAnnotations; | ||
} | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
parse(source) { | ||
let comment = null; | ||
const entries = []; | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
while (ps.current()) { | ||
const entry = getEntryOrJunk(ps); | ||
const entries = []; | ||
if (entry.type === 'Comment' && entries.length === 0) { | ||
comment = entry; | ||
} else { | ||
entries.push(entry); | ||
while (ps.current()) { | ||
const entry = getEntryOrJunk(this, ps); | ||
if (entry.type === 'Comment' && entries.length === 0) { | ||
comment = entry; | ||
} else { | ||
entries.push(entry); | ||
} | ||
ps.skipWSLines(); | ||
} | ||
return new AST.Resource(entries, comment); | ||
} | ||
parseEntry(source) { | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
return getEntryOrJunk(this, ps); | ||
} | ||
return new AST.Resource(entries, comment); | ||
} | ||
export function parseEntry(source) { | ||
const ps = new FTLParserStream(source); | ||
ps.skipWSLines(); | ||
return getEntryOrJunk(ps); | ||
} | ||
function getEntryOrJunk(ps) { | ||
function getEntryOrJunk(parser, ps) { | ||
const entryStartPos = ps.getIndex(); | ||
@@ -42,3 +52,5 @@ | ||
const entry = getEntry(ps); | ||
entry.addSpan(entryStartPos, ps.getIndex()); | ||
if (parser.withSpans) { | ||
entry.addSpan(entryStartPos, ps.getIndex()); | ||
} | ||
return entry; | ||
@@ -50,5 +62,3 @@ } catch (err) { | ||
const annot = new AST.Annotation(err.code, err.args, err.message); | ||
annot.addSpan(ps.getIndex(), ps.getIndex()); | ||
const errorIndex = ps.getIndex(); | ||
ps.skipToNextEntryStart(); | ||
@@ -60,4 +70,10 @@ const nextEntryStart = ps.getIndex(); | ||
const junk = new AST.Junk(slice); | ||
junk.addSpan(entryStartPos, nextEntryStart); | ||
junk.addAnnotation(annot); | ||
if (parser.withSpans) { | ||
junk.addSpan(entryStartPos, nextEntryStart); | ||
} | ||
if (parser.withAnnotations) { | ||
const annot = new AST.Annotation(err.code, err.args, err.message); | ||
annot.addSpan(errorIndex, errorIndex); | ||
junk.addAnnotation(annot); | ||
} | ||
return junk; | ||
@@ -367,7 +383,6 @@ } | ||
firstLine = false; | ||
if (buffer.length !== 0) { | ||
if (!firstLine) { | ||
buffer += ch; | ||
} | ||
firstLine = false; | ||
continue; | ||
@@ -374,0 +389,0 @@ } else if (ch === '\\') { |
@@ -5,3 +5,2 @@ function indent(content) { | ||
function containNewLine(elems) { | ||
@@ -14,34 +13,38 @@ const withNewLine = elems.filter( | ||
export default class FluentSerializer { | ||
constructor({ withJunk = false } = {}) { | ||
this.withJunk = withJunk; | ||
} | ||
export function serialize(resource, withJunk = false) { | ||
const parts = []; | ||
serialize(resource) { | ||
const parts = []; | ||
if (resource.comment) { | ||
parts.push( | ||
`${serializeComment(resource.comment)}\n\n` | ||
); | ||
} | ||
if (resource.comment) { | ||
parts.push( | ||
`${serializeComment(resource.comment)}\n\n` | ||
); | ||
} | ||
for (const entry of resource.body) { | ||
if (entry.types !== 'Junk' || withJunk) { | ||
parts.push(serializeEntry(entry)); | ||
for (const entry of resource.body) { | ||
if (entry.types !== 'Junk' || this.withJunk) { | ||
parts.push(this.serializeEntry(entry)); | ||
} | ||
} | ||
return parts.join(''); | ||
} | ||
return parts.join(''); | ||
} | ||
export function serializeEntry(entry) { | ||
switch (entry.type) { | ||
case 'Message': | ||
return serializeMessage(entry); | ||
case 'Section': | ||
return serializeSection(entry); | ||
case 'Comment': | ||
return serializeComment(entry); | ||
case 'Junk': | ||
return serializeJunk(entry); | ||
default : | ||
throw new Error(`Unknown entry type: ${entry.type}`); | ||
serializeEntry(entry) { | ||
switch (entry.type) { | ||
case 'Message': | ||
return serializeMessage(entry); | ||
case 'Section': | ||
return serializeSection(entry); | ||
case 'Comment': | ||
return serializeComment(entry); | ||
case 'Junk': | ||
return serializeJunk(entry); | ||
default : | ||
throw new Error(`Unknown entry type: ${entry.type}`); | ||
} | ||
} | ||
@@ -48,0 +51,0 @@ } |
114213
14
4211