Annotate the mode of Beleriand
[tengwarjs.git] / numbers.js
1
2 var Parser = require("./parser");
3
4 var array_ = Array.prototype;
5
6 module.exports = parseNumber;
7 function parseNumber(callback, options) {
8 return parseDigits(function (digits) {
9 if (digits) {
10 return parseConvert(callback, digits.join(""), options);
11 } else {
12 return callback();
13 }
14 });
15 }
16
17 var digits = "0123456789";
18 var parseDigit = function (callback) {
19 return function (character) {
20 if (character !== "" && digits.indexOf(character) !== -1) {
21 return callback(character);
22 } else {
23 return callback()(character);
24 }
25 };
26 };
27
28 function parseConvert(callback, number, options) {
29 return Parser.countPrimes(function (primes) {
30 return callback(convert(number, primes, options));
31 });
32 }
33
34 function convert(string, alt, options) {
35 var error;
36 var radix;
37 var duodecimal = options.duodecimal;
38 var font = options.font;
39 var makeColumn = font.makeColumn;
40 if (alt == 0) {
41 radix = duodecimal ? 12 : 10;
42 } else {
43 radix = duodecimal ? 10 : 12;
44 error = alt > 1;
45 }
46 var number = parseInt(string, 10);
47 var string = number.toString(radix).split("");
48 return string.map(function (character) {
49 var column = makeColumn(""+parseInt(character, 12));
50 if (error) {
51 column.addError("Numbers can only be parsed in either decimal or dudecimal.");
52 }
53 return column;
54 });
55 }
56
57 var parseDigits = Parser.makeParseSome(parseDigit);
58